View source
<?php
namespace Drupal\comment\Tests;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\content_translation\Tests\ContentTranslationUITestBase;
use Drupal\language\Entity\ConfigurableLanguage;
class CommentTranslationUITest extends ContentTranslationUITestBase {
use CommentTestTrait;
protected $subject;
protected $adminUser;
protected $defaultCacheContexts = [
'languages:language_interface',
'session',
'theme',
'timezone',
'url.query_args:_wrapper_format',
'url.query_args.pagers:0',
'user.permissions',
'user.roles',
];
public static $modules = array(
'language',
'content_translation',
'node',
'comment',
);
protected function setUp() {
$this->entityTypeId = 'comment';
$this->nodeBundle = 'article';
$this->bundle = 'comment_article';
$this->testLanguageSelector = FALSE;
$this->subject = $this
->randomMachineName();
parent::setUp();
}
function setupBundle() {
parent::setupBundle();
$this
->drupalCreateContentType(array(
'type' => $this->nodeBundle,
'name' => $this->nodeBundle,
));
$this
->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'page',
));
$this
->addDefaultCommentField('node', 'page', 'comment');
$this->container
->get('content_translation.manager')
->setEnabled('comment', 'comment_article', TRUE);
}
protected function getTranslatorPermissions() {
return array_merge(parent::getTranslatorPermissions(), array(
'post comments',
'administer comments',
'access comments',
));
}
protected function createEntity($values, $langcode, $comment_type = 'comment_article') {
if ($comment_type == 'comment_article') {
$node_type = 'article';
$field_name = 'comment_article';
}
else {
$node_type = 'page';
$field_name = 'comment';
}
$node = $this
->drupalCreateNode(array(
'type' => $node_type,
$field_name => array(
array(
'status' => CommentItemInterface::OPEN,
),
),
));
$values['entity_id'] = $node
->id();
$values['entity_type'] = 'node';
$values['field_name'] = $field_name;
$values['uid'] = $node
->getOwnerId();
return parent::createEntity($values, $langcode, $comment_type);
}
protected function getNewEntityValues($langcode) {
return array(
'subject' => array(
array(
'value' => $this->subject,
),
),
'comment_body' => array(
array(
'value' => $this
->randomMachineName(16),
),
),
) + parent::getNewEntityValues($langcode);
}
protected function doTestPublishedStatus() {
$entity_manager = \Drupal::entityManager();
$storage = $entity_manager
->getStorage($this->entityTypeId);
$storage
->resetCache();
$entity = $storage
->load($this->entityId);
foreach ($this->langcodes as $index => $langcode) {
if ($index > 0) {
$edit = array(
'status' => 0,
);
$url = $entity
->urlInfo('edit-form', array(
'language' => ConfigurableLanguage::load($langcode),
));
$this
->drupalPostForm($url, $edit, $this
->getFormSubmitAction($entity, $langcode));
$storage
->resetCache();
$entity = $storage
->load($this->entityId);
$this
->assertFalse($this->manager
->getTranslationMetadata($entity
->getTranslation($langcode))
->isPublished(), 'The translation has been correctly unpublished.');
}
}
}
protected function doTestAuthoringInfo() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$languages = $this->container
->get('language_manager')
->getLanguages();
$values = array();
foreach ($this->langcodes as $langcode) {
$url = $entity
->urlInfo('edit-form', [
'language' => $languages[$langcode],
]);
$user = $this
->drupalCreateUser();
$values[$langcode] = array(
'uid' => $user
->id(),
'created' => REQUEST_TIME - mt_rand(0, 1000),
);
$edit = array(
'uid' => $user
->getUsername() . '(' . $user
->id() . ')',
'date[date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
'date[time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
);
$this
->drupalPostForm($url, $edit, $this
->getFormSubmitAction($entity, $langcode));
}
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
foreach ($this->langcodes as $langcode) {
$metadata = $this->manager
->getTranslationMetadata($entity
->getTranslation($langcode));
$this
->assertEqual($metadata
->getAuthor()
->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
$this
->assertEqual($metadata
->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
}
}
function testTranslateLinkCommentAdminPage() {
$this->adminUser = $this
->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), array(
'access administration pages',
'administer comments',
'skip comment approval',
)));
$this
->drupalLogin($this->adminUser);
$cid_translatable = $this
->createEntity(array(), $this->langcodes[0]);
$cid_untranslatable = $this
->createEntity(array(), $this->langcodes[0], 'comment');
$this
->drupalGet('admin/content/comment');
$this
->assertResponse(200);
$this
->assertLinkByHref('comment/' . $cid_translatable . '/translations');
$this
->assertNoLinkByHref('comment/' . $cid_untranslatable . '/translations');
}
protected function doTestTranslationEdit() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$languages = $this->container
->get('language_manager')
->getLanguages();
foreach ($this->langcodes as $langcode) {
if ($langcode != 'en') {
$options = array(
'language' => $languages[$langcode],
);
$url = $entity
->urlInfo('edit-form', $options);
$this
->drupalGet($url);
$title = t('Edit @type @title [%language translation]', array(
'@type' => $this->entityTypeId,
'@title' => $entity
->getTranslation($langcode)
->label(),
'%language' => $languages[$langcode]
->getName(),
));
$this
->assertRaw($title);
}
}
}
}