CommentTranslationUITest.php 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\comment\Tests\CommentTranslationUITest.
 */

namespace Drupal\comment\Tests;

10
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
11
use Drupal\content_translation\Tests\ContentTranslationUITest;
12
use Drupal\language\Entity\ConfigurableLanguage;
13 14 15

/**
 * Tests the Comment Translation UI.
16 17
 *
 * @group comment
18
 */
19
class CommentTranslationUITest extends ContentTranslationUITest {
20 21 22 23 24 25

  /**
   * The subject of the test comment.
   */
  protected $subject;

26 27 28 29 30 31 32
  /**
   * An administrative user with permission to administer comments.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

33
  /**
34
   * Modules to install.
35 36 37
   *
   * @var array
   */
38
  public static $modules = array('language', 'content_translation', 'node', 'comment');
39

40
  protected function setUp() {
41
    $this->entityTypeId = 'comment';
42
    $this->nodeBundle = 'article';
43
    $this->bundle = 'comment_article';
44
    $this->testLanguageSelector = FALSE;
45
    $this->subject = $this->randomMachineName();
46 47 48 49
    parent::setUp();
  }

  /**
50
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::setupBundle().
51 52 53 54
   */
  function setupBundle() {
    parent::setupBundle();
    $this->drupalCreateContentType(array('type' => $this->nodeBundle, 'name' => $this->nodeBundle));
55
    // Add a comment field to the article content type.
56
    $this->container->get('comment.manager')->addDefaultField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
57 58 59 60 61 62
    // Create a page content type.
    $this->drupalCreateContentType(array('type' => 'page', 'name' => 'page'));
    // Add a comment field to the page content type - this one won't be
    // translatable.
    $this->container->get('comment.manager')->addDefaultField('node', 'page', 'comment');
    // Mark this bundle as translatable.
63
    $this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE);
64 65 66
  }

  /**
67
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getTranslatorPermission().
68
   */
69
  protected function getTranslatorPermissions() {
70
    return array_merge(parent::getTranslatorPermissions(), array('post comments', 'administer comments', 'access comments'));
71 72 73
  }

  /**
74
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::createEntity().
75
   */
76 77
  protected function createEntity($values, $langcode, $comment_type = 'comment_article') {
    if ($comment_type == 'comment_article') {
78 79 80 81 82 83 84 85
      // This is the article node type, with the 'comment_article' field.
      $node_type = 'article';
      $field_name = 'comment_article';
    }
    else {
      // This is the page node type with the non-translatable 'comment' field.
      $node_type = 'page';
      $field_name = 'comment';
86
    }
87 88 89
    $node = $this->drupalCreateNode(array(
      'type' => $node_type,
      $field_name => array(
90
        array('status' => CommentItemInterface::OPEN)
91 92 93 94
      ),
    ));
    $values['entity_id'] = $node->id();
    $values['entity_type'] = 'node';
95
    $values['field_name'] = $field_name;
96
    $values['uid'] = $node->getOwnerId();
97
    return parent::createEntity($values, $langcode, $comment_type);
98 99 100
  }

  /**
101
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getNewEntityValues().
102 103 104 105
   */
  protected function getNewEntityValues($langcode) {
    // Comment subject is not translatable hence we use a fixed value.
    return array(
106
      'subject' => array(array('value' => $this->subject)),
107
      'comment_body' => array(array('value' => $this->randomMachineName(16))),
108 109 110
    ) + parent::getNewEntityValues($langcode);
  }

111
  /**
112
   * {@inheritdoc}
113
   */
114 115 116 117 118 119
  protected function doTestPublishedStatus() {
    $entity_manager = \Drupal::entityManager();
    $storage = $entity_manager->getStorage($this->entityTypeId);

    $storage->resetCache();
    $entity = $storage->load($this->entityId);
120

121
    // Unpublish translations.
122 123
    foreach ($this->langcodes as $index => $langcode) {
      if ($index > 0) {
124 125 126 127 128 129
        $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.');
130
      }
131
    }
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  }

  /**
   * {@inheritdoc}
   */
  protected function doTestAuthoringInfo() {
    $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
    $path = $entity->getSystemPath('edit-form');
    $languages = $this->container->get('language_manager')->getLanguages();
    $values = array();

    // Post different authoring information for each translation.
    foreach ($this->langcodes as $langcode) {
      $user = $this->drupalCreateUser();
      $values[$langcode] = array(
        'uid' => $user->id(),
        'created' => REQUEST_TIME - mt_rand(0, 1000),
      );
      $edit = array(
        'name' => $user->getUsername(),
        'date[date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
        'date[time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
      );
      $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity, $langcode), array('language' => $languages[$langcode]));
    }
157

158 159 160 161 162 163
    $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.');
    }
164 165
  }

166 167 168 169
  /**
   * Tests translate link on comment content admin page.
   */
  function testTranslateLinkCommentAdminPage() {
170 171
    $this->adminUser = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), array('access administration pages', 'administer comments', 'skip comment approval')));
    $this->drupalLogin($this->adminUser);
172

173
    $cid_translatable = $this->createEntity(array(), $this->langcodes[0]);
174
    $cid_untranslatable = $this->createEntity(array(), $this->langcodes[0], 'comment');
175 176 177 178 179 180 181 182

    // Verify translation links.
    $this->drupalGet('admin/content/comment');
    $this->assertResponse(200);
    $this->assertLinkByHref('comment/' . $cid_translatable . '/translations');
    $this->assertNoLinkByHref('comment/' . $cid_untranslatable . '/translations');
  }

183
}