Chris@0: entityTypeId = 'comment'; Chris@0: $this->nodeBundle = 'article'; Chris@0: $this->bundle = 'comment_article'; Chris@0: $this->testLanguageSelector = FALSE; Chris@0: $this->subject = $this->randomMachineName(); Chris@0: parent::setUp(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setupBundle() { Chris@0: parent::setupBundle(); Chris@0: $this->drupalCreateContentType(['type' => $this->nodeBundle, 'name' => $this->nodeBundle]); Chris@0: // Add a comment field to the article content type. Chris@0: $this->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article'); Chris@0: // Create a page content type. Chris@0: $this->drupalCreateContentType(['type' => 'page', 'name' => 'page']); Chris@0: // Add a comment field to the page content type - this one won't be Chris@0: // translatable. Chris@0: $this->addDefaultCommentField('node', 'page', 'comment'); Chris@0: // Mark this bundle as translatable. Chris@0: $this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function getTranslatorPermissions() { Chris@0: return array_merge(parent::getTranslatorPermissions(), ['post comments', 'administer comments', 'access comments']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function createEntity($values, $langcode, $comment_type = 'comment_article') { Chris@0: if ($comment_type == 'comment_article') { Chris@0: // This is the article node type, with the 'comment_article' field. Chris@0: $node_type = 'article'; Chris@0: $field_name = 'comment_article'; Chris@0: } Chris@0: else { Chris@0: // This is the page node type with the non-translatable 'comment' field. Chris@0: $node_type = 'page'; Chris@0: $field_name = 'comment'; Chris@0: } Chris@0: $node = $this->drupalCreateNode([ Chris@0: 'type' => $node_type, Chris@0: $field_name => [ Chris@17: ['status' => CommentItemInterface::OPEN], Chris@0: ], Chris@0: ]); Chris@0: $values['entity_id'] = $node->id(); Chris@0: $values['entity_type'] = 'node'; Chris@0: $values['field_name'] = $field_name; Chris@0: $values['uid'] = $node->getOwnerId(); Chris@0: return parent::createEntity($values, $langcode, $comment_type); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function getNewEntityValues($langcode) { Chris@0: // Comment subject is not translatable hence we use a fixed value. Chris@0: return [ Chris@0: 'subject' => [['value' => $this->subject]], Chris@0: 'comment_body' => [['value' => $this->randomMachineName(16)]], Chris@0: ] + parent::getNewEntityValues($langcode); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doTestPublishedStatus() { Chris@0: $entity_manager = \Drupal::entityManager(); Chris@0: $storage = $entity_manager->getStorage($this->entityTypeId); Chris@0: Chris@0: $storage->resetCache(); Chris@0: $entity = $storage->load($this->entityId); Chris@0: Chris@0: // Unpublish translations. Chris@0: foreach ($this->langcodes as $index => $langcode) { Chris@0: if ($index > 0) { Chris@0: $edit = ['status' => 0]; Chris@18: $url = $entity->toUrl('edit-form', ['language' => ConfigurableLanguage::load($langcode)]); Chris@0: $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode)); Chris@0: $storage->resetCache(); Chris@0: $entity = $storage->load($this->entityId); Chris@0: $this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished(), 'The translation has been correctly unpublished.'); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doTestAuthoringInfo() { Chris@0: $storage = $this->container->get('entity_type.manager') Chris@0: ->getStorage($this->entityTypeId); Chris@0: $storage->resetCache([$this->entityId]); Chris@0: $entity = $storage->load($this->entityId); Chris@0: $languages = $this->container->get('language_manager')->getLanguages(); Chris@0: $values = []; Chris@0: Chris@0: // Post different authoring information for each translation. Chris@0: foreach ($this->langcodes as $langcode) { Chris@18: $url = $entity->toUrl('edit-form', ['language' => $languages[$langcode]]); Chris@0: $user = $this->drupalCreateUser(); Chris@0: $values[$langcode] = [ Chris@0: 'uid' => $user->id(), Chris@0: 'created' => REQUEST_TIME - mt_rand(0, 1000), Chris@0: ]; Chris@18: /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */ Chris@18: $date_formatter = $this->container->get('date.formatter'); Chris@0: $edit = [ Chris@18: 'uid' => $user->getAccountName() . ' (' . $user->id() . ')', Chris@18: 'date[date]' => $date_formatter->format($values[$langcode]['created'], 'custom', 'Y-m-d'), Chris@18: 'date[time]' => $date_formatter->format($values[$langcode]['created'], 'custom', 'H:i:s'), Chris@0: ]; Chris@0: $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode)); Chris@0: } Chris@0: Chris@0: $storage->resetCache([$this->entityId]); Chris@0: $entity = $storage->load($this->entityId); Chris@0: foreach ($this->langcodes as $langcode) { Chris@0: $metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode)); Chris@0: $this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly stored.'); Chris@0: $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests translate link on comment content admin page. Chris@0: */ Chris@0: public function testTranslateLinkCommentAdminPage() { Chris@0: $this->adminUser = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer comments', 'skip comment approval'])); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: $cid_translatable = $this->createEntity([], $this->langcodes[0]); Chris@0: $cid_untranslatable = $this->createEntity([], $this->langcodes[0], 'comment'); Chris@0: Chris@0: // Verify translation links. Chris@0: $this->drupalGet('admin/content/comment'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertLinkByHref('comment/' . $cid_translatable . '/translations'); Chris@0: $this->assertNoLinkByHref('comment/' . $cid_untranslatable . '/translations'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doTestTranslationEdit() { Chris@0: $storage = $this->container->get('entity_type.manager') Chris@0: ->getStorage($this->entityTypeId); Chris@0: $storage->resetCache([$this->entityId]); Chris@0: $entity = $storage->load($this->entityId); Chris@0: $languages = $this->container->get('language_manager')->getLanguages(); Chris@0: Chris@0: foreach ($this->langcodes as $langcode) { Chris@0: // We only want to test the title for non-english translations. Chris@0: if ($langcode != 'en') { Chris@0: $options = ['language' => $languages[$langcode]]; Chris@18: $url = $entity->toUrl('edit-form', $options); Chris@0: $this->drupalGet($url); Chris@0: Chris@0: $title = t('Edit @type @title [%language translation]', [ Chris@0: '@type' => $this->entityTypeId, Chris@0: '@title' => $entity->getTranslation($langcode)->label(), Chris@0: '%language' => $languages[$langcode]->getName(), Chris@0: ]); Chris@0: $this->assertRaw($title); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: }