CommentTranslationUITest.php 5.63 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\field\Entity\FieldStorageConfig;
13 14 15

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

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

  /**
27
   * Modules to install.
28 29 30
   *
   * @var array
   */
31
  public static $modules = array('language', 'content_translation', 'node', 'comment');
32

33
  protected function setUp() {
34
    $this->entityTypeId = 'comment';
35
    $this->nodeBundle = 'article';
36
    $this->bundle = 'comment_article';
37
    $this->testLanguageSelector = FALSE;
38
    $this->subject = $this->randomMachineName();
39 40 41 42
    parent::setUp();
  }

  /**
43
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::setupBundle().
44 45 46 47
   */
  function setupBundle() {
    parent::setupBundle();
    $this->drupalCreateContentType(array('type' => $this->nodeBundle, 'name' => $this->nodeBundle));
48
    // Add a comment field to the article content type.
49
    $this->container->get('comment.manager')->addDefaultField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
50 51 52 53 54 55
    // 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.
56
    $this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE);
57 58 59
  }

  /**
60
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getTranslatorPermission().
61
   */
62
  protected function getTranslatorPermissions() {
63
    return array_merge(parent::getTranslatorPermissions(), array('post comments', 'administer comments', 'access comments'));
64 65 66
  }

  /**
67
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::setupTestFields().
68 69 70
   */
  function setupTestFields() {
    parent::setupTestFields();
71 72 73
    $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
    $field_storage->translatable = TRUE;
    $field_storage->save();
74 75 76
  }

  /**
77
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::createEntity().
78
   */
79 80
  protected function createEntity($values, $langcode, $comment_type = 'comment_article') {
    if ($comment_type == 'comment_article') {
81 82 83 84 85 86 87 88
      // 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';
89
    }
90 91 92
    $node = $this->drupalCreateNode(array(
      'type' => $node_type,
      $field_name => array(
93
        array('status' => CommentItemInterface::OPEN)
94 95 96 97
      ),
    ));
    $values['entity_id'] = $node->id();
    $values['entity_type'] = 'node';
98
    $values['field_name'] = $field_name;
99
    $values['uid'] = $node->getOwnerId();
100
    return parent::createEntity($values, $langcode, $comment_type);
101 102 103
  }

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

114
  /**
115
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::assertPublishedStatus().
116 117 118
   */
  protected function assertPublishedStatus() {
    parent::assertPublishedStatus();
119
    $entity = entity_load($this->entityTypeId, $this->entityId);
120 121
    $user = $this->drupalCreateUser(array('access comments'));
    $this->drupalLogin($user);
122
    $languages = $this->container->get('language_manager')->getLanguages();
123 124

    // Check that simple users cannot see unpublished field translations.
125
    $path = $entity->getSystemPath();
126 127 128
    foreach ($this->langcodes as $index => $langcode) {
      $translation = $this->getTranslation($entity, $langcode);
      $value = $this->getValue($translation, 'comment_body', $langcode);
129
      $this->drupalGet($path, array('language' => $languages[$langcode]));
130 131 132 133 134 135 136 137 138 139 140 141
      if ($index > 0) {
        $this->assertNoRaw($value, 'Unpublished field translation is not shown.');
      }
      else {
        $this->assertRaw($value, 'Published field translation is shown.');
      }
    }

    // Login as translator again to ensure subsequent tests do not break.
    $this->drupalLogin($this->translator);
  }

142 143 144 145
  /**
   * Tests translate link on comment content admin page.
   */
  function testTranslateLinkCommentAdminPage() {
146
    $this->admin_user = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), array('access administration pages', 'administer comments', 'skip comment approval')));
147 148
    $this->drupalLogin($this->admin_user);

149
    $cid_translatable = $this->createEntity(array(), $this->langcodes[0]);
150
    $cid_untranslatable = $this->createEntity(array(), $this->langcodes[0], 'comment');
151 152 153 154 155 156 157 158

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

159
}