CommentTranslationUITest.php 6.03 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
13
14
15

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

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

  /**
   * Modules to enable.
   *
   * @var array
   */
28
  public static $modules = array('language', 'content_translation', 'node', 'comment');
29
30
31
32
33
34
35
36
37
38

  public static function getInfo() {
    return array(
      'name' => 'Comment translation UI',
      'description' => 'Tests the basic comment translation UI.',
      'group' => 'Comment',
    );
  }

  function setUp() {
39
    $this->entityTypeId = 'comment';
40
    $this->nodeBundle = 'article';
41
    $this->bundle = 'node__comment_article';
42
43
44
45
46
47
    $this->testLanguageSelector = FALSE;
    $this->subject = $this->randomName();
    parent::setUp();
  }

  /**
48
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::setupBundle().
49
50
51
52
   */
  function setupBundle() {
    parent::setupBundle();
    $this->drupalCreateContentType(array('type' => $this->nodeBundle, 'name' => $this->nodeBundle));
53
54
55
56
57
58
59
60
61
62
63
64
65
    // Add a comment field to the article content type.
    $this->container->get('comment.manager')->addDefaultField('node', 'article', 'comment_article');
    // 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.
    content_translation_set_config('comment', 'node__comment_article', 'enabled', TRUE);
    // Refresh entity info.
    entity_info_cache_clear();
    // Flush the permissions after adding the translatable comment bundle.
    $this->checkPermissions(array(), TRUE);
66
67
68
  }

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

  /**
76
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::setupTestFields().
77
78
79
   */
  function setupTestFields() {
    parent::setupTestFields();
80
    $field = field_info_field('comment', 'comment_body');
81
    $field->translatable = TRUE;
82
    $field->save();
83
84
85
  }

  /**
86
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::createEntity().
87
   */
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  protected function createEntity($values, $langcode, $node_bundle = 'node__comment_article') {
    // The argument is called 'node_bundle' but its actually just the entity
    // bundle. Comment entity's bundle is of the form
    // {entity_type}__{field_name}. Based on the passed bundle we need to
    // determine the type of node and the name of the comment field.
    if ($node_bundle == 'node__comment_article') {
      // 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';
102
    }
103
104
105
    $node = $this->drupalCreateNode(array(
      'type' => $node_type,
      $field_name => array(
106
        array('status' => CommentItemInterface::OPEN)
107
108
109
110
111
      ),
    ));
    $values['entity_id'] = $node->id();
    $values['entity_type'] = 'node';
    $values['field_id'] = $node_bundle;
112
    $values['uid'] = $node->getOwnerId();
113
    return parent::createEntity($values, $langcode, $node_bundle);
114
115
116
  }

  /**
117
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getNewEntityValues().
118
119
120
121
122
   */
  protected function getNewEntityValues($langcode) {
    // Comment subject is not translatable hence we use a fixed value.
    return array(
      'subject' => $this->subject,
123
      'comment_body' => array(array('value' => $this->randomName(16))),
124
125
126
    ) + parent::getNewEntityValues($langcode);
  }

127
  /**
128
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::assertPublishedStatus().
129
130
131
   */
  protected function assertPublishedStatus() {
    parent::assertPublishedStatus();
132
    $entity = entity_load($this->entityTypeId, $this->entityId);
133
134
135
136
137
    $user = $this->drupalCreateUser(array('access comments'));
    $this->drupalLogin($user);
    $languages = language_list();

    // Check that simple users cannot see unpublished field translations.
138
    $path = $entity->getSystemPath();
139
140
141
    foreach ($this->langcodes as $index => $langcode) {
      $translation = $this->getTranslation($entity, $langcode);
      $value = $this->getValue($translation, 'comment_body', $langcode);
142
      $this->drupalGet($path, array('language' => $languages[$langcode]));
143
144
145
146
147
148
149
150
151
152
153
154
      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);
  }

155
156
157
158
  /**
   * Tests translate link on comment content admin page.
   */
  function testTranslateLinkCommentAdminPage() {
159
    $this->admin_user = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), array('access administration pages', 'administer comments', 'skip comment approval')));
160
161
    $this->drupalLogin($this->admin_user);

162
163
    $cid_translatable = $this->createEntity(array(), $this->langcodes[0]);
    $cid_untranslatable = $this->createEntity(array(), $this->langcodes[0], 'node__comment');
164
165
166
167
168
169
170
171

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

172
}