annotate core/modules/comment/tests/src/Functional/CommentTranslationUITest.php @ 19:fa3358dc1485 tip

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