ContentTranslationSettingsTest.php 15.1 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\content_translation\Functional;
4

5
use Drupal\Component\Render\FormattableMarkup;
6
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
7
use Drupal\comment\Tests\CommentTestTrait;
8
use Drupal\Core\Field\Entity\BaseFieldOverride;
9
use Drupal\Core\Language\Language;
10
use Drupal\field\Entity\FieldConfig;
11
use Drupal\language\Entity\ContentLanguageSettings;
12
use Drupal\Tests\BrowserTestBase;
13 14

/**
15 16 17
 * Tests the content translation settings UI.
 *
 * @group content_translation
18
 */
19
class ContentTranslationSettingsTest extends BrowserTestBase {
20

21 22
  use CommentTestTrait;

23 24 25 26 27
  /**
   * Modules to enable.
   *
   * @var array
   */
28 29 30 31 32 33 34 35
  protected static $modules = [
    'language',
    'content_translation',
    'node',
    'comment',
    'field_ui',
    'entity_test',
  ];
36

37 38 39 40 41
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

42
  protected function setUp(): void {
43 44
    parent::setUp();

45
    // Set up two content types to test fields shared between different
46
    // bundles.
47 48
    $this->drupalCreateContentType(['type' => 'article']);
    $this->drupalCreateContentType(['type' => 'page']);
49 50
    $this->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
    $this->addDefaultCommentField('node', 'page', 'comment_page');
51

52 53 54 55 56 57 58 59 60 61 62
    $admin_user = $this->drupalCreateUser([
      'access administration pages',
      'administer languages',
      'administer content translation',
      'administer content types',
      'administer node fields',
      'administer comment fields',
      'administer comments',
      'administer comment types',
      'administer account settings',
    ]);
63 64 65 66 67 68
    $this->drupalLogin($admin_user);
  }

  /**
   * Tests that the settings UI works as expected.
   */
69
  public function testSettingsUI() {
70
    // Check for the content_translation_menu_links_discovered_alter() changes.
71
    $this->drupalGet('admin/config');
72
    $this->assertSession()->linkExists('Content language and translation');
73
    $this->assertText('Configure language and translation support for content.');
74 75
    // Test that the translation settings are ignored if the bundle is marked
    // translatable but the entity type is not.
76
    $edit = ['settings[comment][comment_article][translatable]' => TRUE];
77 78 79 80
    $this->assertSettings('comment', NULL, FALSE, $edit);

    // Test that the translation settings are ignored if only a field is marked
    // as translatable and not the related entity type and bundle.
81
    $edit = ['settings[comment][comment_article][fields][comment_body]' => TRUE];
82 83 84 85
    $this->assertSettings('comment', NULL, FALSE, $edit);

    // Test that the translation settings are not stored if an entity type and
    // bundle are marked as translatable but no field is.
86
    $edit = [
87
      'entity_types[comment]' => TRUE,
88
      'settings[comment][comment_article][translatable]' => TRUE,
89 90 91 92 93 94 95 96 97 98
      // Base fields are translatable by default.
      'settings[comment][comment_article][fields][changed]' => FALSE,
      'settings[comment][comment_article][fields][created]' => FALSE,
      'settings[comment][comment_article][fields][homepage]' => FALSE,
      'settings[comment][comment_article][fields][hostname]' => FALSE,
      'settings[comment][comment_article][fields][mail]' => FALSE,
      'settings[comment][comment_article][fields][name]' => FALSE,
      'settings[comment][comment_article][fields][status]' => FALSE,
      'settings[comment][comment_article][fields][subject]' => FALSE,
      'settings[comment][comment_article][fields][uid]' => FALSE,
99
    ];
100
    $this->assertSettings('comment', 'comment_article', FALSE, $edit);
101
    $xpath_err = '//div[contains(@class, "error")]';
102
    $this->assertNotEmpty($this->xpath($xpath_err), 'Enabling translation only for entity bundles generates a form error.');
103 104 105

    // Test that the translation settings are not stored if a non-configurable
    // language is set as default and the language selector is hidden.
106
    $edit = [
107
      'entity_types[comment]' => TRUE,
108
      'settings[comment][comment_article][settings][language][langcode]' => Language::LANGCODE_NOT_SPECIFIED,
109
      'settings[comment][comment_article][settings][language][language_alterable]' => FALSE,
110 111
      'settings[comment][comment_article][translatable]' => TRUE,
      'settings[comment][comment_article][fields][comment_body]' => TRUE,
112
    ];
113
    $this->assertSettings('comment', 'comment_article', FALSE, $edit);
114
    $this->assertNotEmpty($this->xpath($xpath_err), 'Enabling translation with a fixed non-configurable language generates a form error.');
115 116 117

    // Test that a field shared among different bundles can be enabled without
    // needing to make all the related bundles translatable.
118
    $edit = [
119
      'entity_types[comment]' => TRUE,
120
      'settings[comment][comment_article][settings][language][langcode]' => 'current_interface',
121
      'settings[comment][comment_article][settings][language][language_alterable]' => TRUE,
122 123
      'settings[comment][comment_article][translatable]' => TRUE,
      'settings[comment][comment_article][fields][comment_body]' => TRUE,
124 125 126
      // Override both comment subject fields to untranslatable.
      'settings[comment][comment_article][fields][subject]' => FALSE,
      'settings[comment][comment][fields][subject]' => FALSE,
127
    ];
128
    $this->assertSettings('comment', 'comment_article', TRUE, $edit);
129 130
    $entity_field_manager = \Drupal::service('entity_field.manager');
    $definition = $entity_field_manager->getFieldDefinitions('comment', 'comment_article')['comment_body'];
131
    $this->assertTrue($definition->isTranslatable(), 'Article comment body is translatable.');
132
    $definition = $entity_field_manager->getFieldDefinitions('comment', 'comment_article')['subject'];
133 134
    $this->assertFalse($definition->isTranslatable(), 'Article comment subject is not translatable.');

135
    $definition = $entity_field_manager->getFieldDefinitions('comment', 'comment')['comment_body'];
136
    $this->assertFalse($definition->isTranslatable(), 'Page comment body is not translatable.');
137
    $definition = $entity_field_manager->getFieldDefinitions('comment', 'comment')['subject'];
138 139
    $this->assertFalse($definition->isTranslatable(), 'Page comment subject is not translatable.');

140
    // Test that translation can be enabled for base fields.
141
    $edit = [
142 143 144 145
      'entity_types[entity_test_mul]' => TRUE,
      'settings[entity_test_mul][entity_test_mul][translatable]' => TRUE,
      'settings[entity_test_mul][entity_test_mul][fields][name]' => TRUE,
      'settings[entity_test_mul][entity_test_mul][fields][user_id]' => FALSE,
146
    ];
147
    $this->assertSettings('entity_test_mul', 'entity_test_mul', TRUE, $edit);
148 149
    $field_override = BaseFieldOverride::loadByName('entity_test_mul', 'entity_test_mul', 'name');
    $this->assertTrue($field_override->isTranslatable(), 'Base fields can be overridden with a base field bundle override entity.');
150
    $definitions = $entity_field_manager->getFieldDefinitions('entity_test_mul', 'entity_test_mul');
151 152
    $this->assertTrue($definitions['name']->isTranslatable());
    $this->assertFalse($definitions['user_id']->isTranslatable());
153 154

    // Test that language settings are correctly stored.
155
    $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_article');
156
    $this->assertEqual($language_configuration->getDefaultLangcode(), 'current_interface', 'The default language for article comments is set to the interface text language selected for page.');
157
    $this->assertTrue($language_configuration->isLanguageAlterable(), 'The language selector for article comments is shown.');
158

159 160
    // Verify language widget appears on comment type form.
    $this->drupalGet('admin/structure/comment/manage/comment_article');
161
    $this->assertSession()->fieldExists('language_configuration[content_translation]');
162
    $this->assertSession()->checkboxChecked('edit-language-configuration-content-translation');
163 164

    // Verify that translation may be enabled for the article content type.
165
    $edit = [
166
      'language_configuration[content_translation]' => TRUE,
167
    ];
168 169
    // Make sure the checkbox is available and not checked by default.
    $this->drupalGet('admin/structure/types/manage/article');
170
    $this->assertSession()->fieldExists('language_configuration[content_translation]');
171
    $this->assertSession()->checkboxNotChecked('edit-language-configuration-content-translation');
172
    $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
173
    $this->drupalGet('admin/structure/types/manage/article');
174
    $this->assertSession()->checkboxChecked('edit-language-configuration-content-translation');
175 176

    // Test that the title field of nodes is available in the settings form.
177
    $edit = [
178 179
      'entity_types[node]' => TRUE,
      'settings[node][article][settings][language][langcode]' => 'current_interface',
180
      'settings[node][article][settings][language][language_alterable]' => TRUE,
181
      'settings[node][article][translatable]' => TRUE,
182
      'settings[node][article][fields][title]' => TRUE,
183
    ];
184 185
    $this->assertSettings('node', NULL, TRUE, $edit);

186
    foreach ([TRUE, FALSE] as $translatable) {
187
      // Test that configurable field translatability is correctly switched.
188
      $edit = ['settings[node][article][fields][body]' => $translatable];
189
      $this->assertSettings('node', 'article', TRUE, $edit);
190
      $field = FieldConfig::loadByName('node', 'article', 'body');
191
      $definitions = $entity_field_manager->getFieldDefinitions('node', 'article');
192
      $this->assertEqual($definitions['body']->isTranslatable(), $translatable, 'Field translatability correctly switched.');
193
      $this->assertEqual($field->isTranslatable(), $definitions['body']->isTranslatable(), 'Configurable field translatability correctly switched.');
194 195 196

      // Test that also the Field UI form behaves correctly.
      $translatable = !$translatable;
197
      $edit = ['translatable' => $translatable];
198
      $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
199
      $entity_field_manager->clearCachedFieldDefinitions();
200
      $field = FieldConfig::loadByName('node', 'article', 'body');
201
      $definitions = $entity_field_manager->getFieldDefinitions('node', 'article');
202
      $this->assertEqual($definitions['body']->isTranslatable(), $translatable, 'Field translatability correctly switched.');
203
      $this->assertEqual($field->isTranslatable(), $definitions['body']->isTranslatable(), 'Configurable field translatability correctly switched.');
204
    }
205 206 207 208 209

    // Test that the order of the language list is similar to other language
    // lists, such as in Views UI.
    $this->drupalGet('admin/config/regional/content-language');

210
    $expected_elements = [
211 212 213 214 215 216
      'site_default',
      'current_interface',
      'authors_default',
      'en',
      'und',
      'zxx',
217
    ];
218 219 220
    $elements = $this->xpath('//select[@id="edit-settings-node-article-settings-language-langcode"]/option');
    // Compare values inside the option elements with expected values.
    for ($i = 0; $i < count($elements); $i++) {
221
      $this->assertEqual($elements[$i]->getValue(), $expected_elements[$i]);
222
    }
223 224
  }

225 226 227
  /**
   * Tests the language settings checkbox on account settings page.
   */
228
  public function testAccountLanguageSettingsUI() {
229 230
    // Make sure the checkbox is available and not checked by default.
    $this->drupalGet('admin/config/people/accounts');
231
    $this->assertSession()->fieldExists('language[content_translation]');
232
    $this->assertSession()->checkboxNotChecked('edit-language-content-translation');
233

234
    $edit = [
235
      'language[content_translation]' => TRUE,
236
    ];
237 238
    $this->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
    $this->drupalGet('admin/config/people/accounts');
239
    $this->assertSession()->checkboxChecked('edit-language-content-translation');
240 241

    // Make sure account settings can be saved.
242
    $this->drupalPostForm('admin/config/people/accounts', ['anonymous' => 'Save me please!'], 'Save configuration');
243
    $this->assertSession()->fieldValueEquals('anonymous', 'Save me please!');
244
    $this->assertText('The configuration options have been saved.');
245 246
  }

247 248 249 250
  /**
   * Asserts that translatability has the expected value for the given bundle.
   *
   * @param string $entity_type
251
   *   The entity type for which to check translatability.
252
   * @param string $bundle
253
   *   The bundle for which to check translatability.
254
   * @param bool $enabled
255
   *   TRUE if translatability should be enabled, FALSE otherwise.
256
   * @param array $edit
257
   *   An array of values to submit to the content translation settings page.
258
   *
259
   * @return bool
260 261 262
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertSettings($entity_type, $bundle, $enabled, $edit) {
263
    $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
264
    $args = ['@entity_type' => $entity_type, '@bundle' => $bundle, '@enabled' => $enabled ? 'enabled' : 'disabled'];
265
    $message = new FormattableMarkup('Translation for entity @entity_type (@bundle) is @enabled.', $args);
266
    return $this->assertEqual(\Drupal::service('content_translation.manager')->isEnabled($entity_type, $bundle), $enabled, $message);
267 268
  }

269
  /**
270
   * Tests that field setting depends on bundle translatability.
271
   */
272
  public function testFieldTranslatableSettingsUI() {
273
    // At least one field needs to be translatable to enable article for
274 275
    // translation. Create an extra field to be used for this purpose. We use
    // the UI to test our form alterations.
276
    $edit = [
277 278
      'new_storage_type' => 'text',
      'label' => 'Test',
279
      'field_name' => 'article_text',
280
    ];
281
    $this->drupalPostForm('admin/structure/types/manage/article/fields/add-field', $edit, 'Save and continue');
282

283 284
    // Tests that field doesn't have translatable setting if bundle is not
    // translatable.
285
    $path = 'admin/structure/types/manage/article/fields/node.article.field_article_text';
286
    $this->drupalGet($path);
287
    $this->assertSession()->fieldDisabled('edit-translatable');
288
    $this->assertText('To configure translation for this field, enable language support for this type.', 'No translatable setting for field.');
289

290 291
    // Tests that field has translatable setting if bundle is translatable.
    // Note: this field is not translatable when enable bundle translatability.
292
    $edit = [
293
      'entity_types[node]' => TRUE,
294
      'settings[node][article][settings][language][language_alterable]' => TRUE,
295
      'settings[node][article][translatable]' => TRUE,
296
      'settings[node][article][fields][field_article_text]' => TRUE,
297
    ];
298 299
    $this->assertSettings('node', 'article', TRUE, $edit);
    $this->drupalGet($path);
300 301
    $this->assertSession()->fieldEnabled('edit-translatable');
    $this->assertSession()->checkboxChecked('edit-translatable');
302
    $this->assertNoText('To enable translation of this field, enable language support for this type.', 'Translatable setting for field available.');
303 304
  }

305 306 307
  /**
   * Tests the translatable settings checkbox for untranslatable entities.
   */
308
  public function testNonTranslatableTranslationSettingsUI() {
309
    $this->drupalGet('admin/config/regional/content-language');
310
    $this->assertSession()->fieldNotExists('settings[entity_test][entity_test][translatable]');
311 312
  }

313
}