annotate core/modules/comment/tests/src/Functional/CommentTranslationUITest.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\comment\Functional;
Chris@0 4
Chris@0 5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
Chris@0 6 use Drupal\comment\Tests\CommentTestTrait;
Chris@0 7 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 8 use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Tests the Comment Translation UI.
Chris@0 12 *
Chris@0 13 * @group comment
Chris@0 14 */
Chris@0 15 class CommentTranslationUITest extends ContentTranslationUITestBase {
Chris@0 16
Chris@0 17 use CommentTestTrait;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The subject of the test comment.
Chris@0 21 */
Chris@0 22 protected $subject;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * An administrative user with permission to administer comments.
Chris@0 26 *
Chris@0 27 * @var \Drupal\user\UserInterface
Chris@0 28 */
Chris@0 29 protected $adminUser;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * {inheritdoc}
Chris@0 33 */
Chris@0 34 protected $defaultCacheContexts = [
Chris@0 35 'languages:language_interface',
Chris@0 36 'session',
Chris@0 37 'theme',
Chris@0 38 'timezone',
Chris@0 39 'url.query_args:_wrapper_format',
Chris@0 40 'url.query_args.pagers:0',
Chris@0 41 'user.permissions',
Chris@0 42 'user.roles',
Chris@0 43 ];
Chris@0 44
Chris@0 45 /**
Chris@0 46 * Modules to install.
Chris@0 47 *
Chris@0 48 * @var array
Chris@0 49 */
Chris@0 50 public static $modules = ['language', 'content_translation', 'node', 'comment'];
Chris@0 51
Chris@0 52 protected function setUp() {
Chris@0 53 $this->entityTypeId = 'comment';
Chris@0 54 $this->nodeBundle = 'article';
Chris@0 55 $this->bundle = 'comment_article';
Chris@0 56 $this->testLanguageSelector = FALSE;
Chris@0 57 $this->subject = $this->randomMachineName();
Chris@0 58 parent::setUp();
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * {@inheritdoc}
Chris@0 63 */
Chris@0 64 public function setupBundle() {
Chris@0 65 parent::setupBundle();
Chris@0 66 $this->drupalCreateContentType(['type' => $this->nodeBundle, 'name' => $this->nodeBundle]);
Chris@0 67 // Add a comment field to the article content type.
Chris@0 68 $this->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
Chris@0 69 // Create a page content type.
Chris@0 70 $this->drupalCreateContentType(['type' => 'page', 'name' => 'page']);
Chris@0 71 // Add a comment field to the page content type - this one won't be
Chris@0 72 // translatable.
Chris@0 73 $this->addDefaultCommentField('node', 'page', 'comment');
Chris@0 74 // Mark this bundle as translatable.
Chris@0 75 $this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE);
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * {@inheritdoc}
Chris@0 80 */
Chris@0 81 protected function getTranslatorPermissions() {
Chris@0 82 return array_merge(parent::getTranslatorPermissions(), ['post comments', 'administer comments', 'access comments']);
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * {@inheritdoc}
Chris@0 87 */
Chris@0 88 protected function createEntity($values, $langcode, $comment_type = 'comment_article') {
Chris@0 89 if ($comment_type == 'comment_article') {
Chris@0 90 // This is the article node type, with the 'comment_article' field.
Chris@0 91 $node_type = 'article';
Chris@0 92 $field_name = 'comment_article';
Chris@0 93 }
Chris@0 94 else {
Chris@0 95 // This is the page node type with the non-translatable 'comment' field.
Chris@0 96 $node_type = 'page';
Chris@0 97 $field_name = 'comment';
Chris@0 98 }
Chris@0 99 $node = $this->drupalCreateNode([
Chris@0 100 'type' => $node_type,
Chris@0 101 $field_name => [
Chris@4 102 ['status' => CommentItemInterface::OPEN],
Chris@0 103 ],
Chris@0 104 ]);
Chris@0 105 $values['entity_id'] = $node->id();
Chris@0 106 $values['entity_type'] = 'node';
Chris@0 107 $values['field_name'] = $field_name;
Chris@0 108 $values['uid'] = $node->getOwnerId();
Chris@0 109 return parent::createEntity($values, $langcode, $comment_type);
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * {@inheritdoc}
Chris@0 114 */
Chris@0 115 protected function getNewEntityValues($langcode) {
Chris@0 116 // Comment subject is not translatable hence we use a fixed value.
Chris@0 117 return [
Chris@0 118 'subject' => [['value' => $this->subject]],
Chris@0 119 'comment_body' => [['value' => $this->randomMachineName(16)]],
Chris@0 120 ] + parent::getNewEntityValues($langcode);
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * {@inheritdoc}
Chris@0 125 */
Chris@0 126 protected function doTestPublishedStatus() {
Chris@0 127 $entity_manager = \Drupal::entityManager();
Chris@0 128 $storage = $entity_manager->getStorage($this->entityTypeId);
Chris@0 129
Chris@0 130 $storage->resetCache();
Chris@0 131 $entity = $storage->load($this->entityId);
Chris@0 132
Chris@0 133 // Unpublish translations.
Chris@0 134 foreach ($this->langcodes as $index => $langcode) {
Chris@0 135 if ($index > 0) {
Chris@0 136 $edit = ['status' => 0];
Chris@0 137 $url = $entity->urlInfo('edit-form', ['language' => ConfigurableLanguage::load($langcode)]);
Chris@0 138 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
Chris@0 139 $storage->resetCache();
Chris@0 140 $entity = $storage->load($this->entityId);
Chris@0 141 $this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished(), 'The translation has been correctly unpublished.');
Chris@0 142 }
Chris@0 143 }
Chris@0 144 }
Chris@0 145
Chris@0 146 /**
Chris@0 147 * {@inheritdoc}
Chris@0 148 */
Chris@0 149 protected function doTestAuthoringInfo() {
Chris@0 150 $storage = $this->container->get('entity_type.manager')
Chris@0 151 ->getStorage($this->entityTypeId);
Chris@0 152 $storage->resetCache([$this->entityId]);
Chris@0 153 $entity = $storage->load($this->entityId);
Chris@0 154 $languages = $this->container->get('language_manager')->getLanguages();
Chris@0 155 $values = [];
Chris@0 156
Chris@0 157 // Post different authoring information for each translation.
Chris@0 158 foreach ($this->langcodes as $langcode) {
Chris@0 159 $url = $entity->urlInfo('edit-form', ['language' => $languages[$langcode]]);
Chris@0 160 $user = $this->drupalCreateUser();
Chris@0 161 $values[$langcode] = [
Chris@0 162 'uid' => $user->id(),
Chris@0 163 'created' => REQUEST_TIME - mt_rand(0, 1000),
Chris@0 164 ];
Chris@0 165 $edit = [
Chris@0 166 'uid' => $user->getUsername() . ' (' . $user->id() . ')',
Chris@0 167 'date[date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
Chris@0 168 'date[time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
Chris@0 169 ];
Chris@0 170 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
Chris@0 171 }
Chris@0 172
Chris@0 173 $storage->resetCache([$this->entityId]);
Chris@0 174 $entity = $storage->load($this->entityId);
Chris@0 175 foreach ($this->langcodes as $langcode) {
Chris@0 176 $metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
Chris@0 177 $this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
Chris@0 178 $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
Chris@0 179 }
Chris@0 180 }
Chris@0 181
Chris@0 182 /**
Chris@0 183 * Tests translate link on comment content admin page.
Chris@0 184 */
Chris@0 185 public function testTranslateLinkCommentAdminPage() {
Chris@0 186 $this->adminUser = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer comments', 'skip comment approval']));
Chris@0 187 $this->drupalLogin($this->adminUser);
Chris@0 188
Chris@0 189 $cid_translatable = $this->createEntity([], $this->langcodes[0]);
Chris@0 190 $cid_untranslatable = $this->createEntity([], $this->langcodes[0], 'comment');
Chris@0 191
Chris@0 192 // Verify translation links.
Chris@0 193 $this->drupalGet('admin/content/comment');
Chris@0 194 $this->assertResponse(200);
Chris@0 195 $this->assertLinkByHref('comment/' . $cid_translatable . '/translations');
Chris@0 196 $this->assertNoLinkByHref('comment/' . $cid_untranslatable . '/translations');
Chris@0 197 }
Chris@0 198
Chris@0 199 /**
Chris@0 200 * {@inheritdoc}
Chris@0 201 */
Chris@0 202 protected function doTestTranslationEdit() {
Chris@0 203 $storage = $this->container->get('entity_type.manager')
Chris@0 204 ->getStorage($this->entityTypeId);
Chris@0 205 $storage->resetCache([$this->entityId]);
Chris@0 206 $entity = $storage->load($this->entityId);
Chris@0 207 $languages = $this->container->get('language_manager')->getLanguages();
Chris@0 208
Chris@0 209 foreach ($this->langcodes as $langcode) {
Chris@0 210 // We only want to test the title for non-english translations.
Chris@0 211 if ($langcode != 'en') {
Chris@0 212 $options = ['language' => $languages[$langcode]];
Chris@0 213 $url = $entity->urlInfo('edit-form', $options);
Chris@0 214 $this->drupalGet($url);
Chris@0 215
Chris@0 216 $title = t('Edit @type @title [%language translation]', [
Chris@0 217 '@type' => $this->entityTypeId,
Chris@0 218 '@title' => $entity->getTranslation($langcode)->label(),
Chris@0 219 '%language' => $languages[$langcode]->getName(),
Chris@0 220 ]);
Chris@0 221 $this->assertRaw($title);
Chris@0 222 }
Chris@0 223 }
Chris@0 224 }
Chris@0 225
Chris@0 226 }