3 namespace Drupal\Tests\content_translation\Functional;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\language\Entity\ConfigurableLanguage;
11 * Tests the untranslatable fields behaviors.
13 * @group content_translation
15 class ContentTranslationUntranslatableFieldsTest extends ContentTranslationPendingRevisionTestBase {
20 public static $modules = ['field_test'];
25 protected function setUp() {
28 // Configure one field as untranslatable.
29 $this->drupalLogin($this->administrator);
31 'settings[' . $this->entityTypeId . '][' . $this->bundle . '][fields][' . $this->fieldName . ']' => 0,
33 $this->drupalPostForm('admin/config/regional/content-language', $edit, 'Save configuration');
35 /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
36 $entity_field_manager = $this->container->get('entity_field.manager');
37 $entity_field_manager->clearCachedFieldDefinitions();
38 $definitions = $entity_field_manager->getFieldDefinitions($this->entityTypeId, $this->bundle);
39 $this->assertFalse($definitions[$this->fieldName]->isTranslatable());
45 protected function setupTestFields() {
46 parent::setupTestFields();
48 $field_storage = FieldStorageConfig::create([
49 'field_name' => 'field_multilingual',
50 'type' => 'test_field',
51 'entity_type' => $this->entityTypeId,
54 $field_storage->save();
56 'field_storage' => $field_storage,
57 'bundle' => $this->bundle,
58 'label' => 'Untranslatable-but-visible test field',
59 'translatable' => FALSE,
61 entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
62 ->setComponent('field_multilingual', [
63 'type' => 'test_field_widget_multilingual',
69 * Tests that hiding untranslatable field widgets works correctly.
71 public function testHiddenWidgets() {
72 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
73 $entity_type_manager = $this->container->get('entity_type.manager');
74 $id = $this->createEntity(['title' => $this->randomString()], 'en');
75 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
76 $entity = $entity_type_manager
77 ->getStorage($this->entityTypeId)
80 // Check that the untranslatable field widget is displayed on the edit form
81 // and no translatability clue is displayed yet.
82 $en_edit_url = $entity->toUrl('edit-form');
83 $this->drupalGet($en_edit_url);
84 $field_xpath = '//input[@name="' . $this->fieldName . '[0][value]"]';
85 $this->assertNotEmpty($this->xpath($field_xpath));
86 $clue_xpath = '//label[@for="edit-' . strtr($this->fieldName, '_', '-') . '-0-value"]/span[text()="(all languages)"]';
87 $this->assertEmpty($this->xpath($clue_xpath));
88 $this->assertSession()->pageTextContains('Untranslatable-but-visible test field');
90 // Add a translation and check that the untranslatable field widget is
91 // displayed on the translation and edit forms along with translatability
93 $add_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_add", [
94 $entity->getEntityTypeId() => $entity->id(),
98 $this->drupalGet($add_url);
99 $this->assertNotEmpty($this->xpath($field_xpath));
100 $this->assertNotEmpty($this->xpath($clue_xpath));
101 $this->assertSession()->pageTextContains('Untranslatable-but-visible test field');
102 $this->drupalPostForm(NULL, [], 'Save');
104 // Check that the widget is displayed along with its clue in the edit form
105 // for both languages.
106 $this->drupalGet($en_edit_url);
107 $this->assertNotEmpty($this->xpath($field_xpath));
108 $this->assertNotEmpty($this->xpath($clue_xpath));
109 $it_edit_url = $entity->toUrl('edit-form', ['language' => ConfigurableLanguage::load('it')]);
110 $this->drupalGet($it_edit_url);
111 $this->assertNotEmpty($this->xpath($field_xpath));
112 $this->assertNotEmpty($this->xpath($clue_xpath));
114 // Configure untranslatable field widgets to be hidden on non-default
115 // language edit forms.
116 $settings_key = 'settings[' . $this->entityTypeId . '][' . $this->bundle . '][settings][content_translation][untranslatable_fields_hide]';
117 $settings_url = 'admin/config/regional/content-language';
118 $this->drupalPostForm($settings_url, [$settings_key => 1], 'Save configuration');
120 // Verify that the widget is displayed in the default language edit form,
121 // but no clue is displayed.
122 $this->drupalGet($en_edit_url);
123 $field_xpath = '//input[@name="' . $this->fieldName . '[0][value]"]';
124 $this->assertNotEmpty($this->xpath($field_xpath));
125 $this->assertEmpty($this->xpath($clue_xpath));
126 $this->assertSession()->pageTextContains('Untranslatable-but-visible test field');
128 // Verify no widget is displayed on the non-default language edit form.
129 $this->drupalGet($it_edit_url);
130 $this->assertEmpty($this->xpath($field_xpath));
131 $this->assertEmpty($this->xpath($clue_xpath));
132 $this->assertSession()->pageTextContains('Untranslatable-but-visible test field');
134 // Verify a warning is displayed.
135 $this->assertSession()->pageTextContains('Fields that apply to all languages are hidden to avoid conflicting changes.');
136 $edit_path = $entity->toUrl('edit-form')->toString();
137 $link_xpath = '//a[@href=:edit_path and text()="Edit them on the original language form"]';
138 $elements = $this->xpath($link_xpath, [':edit_path' => $edit_path]);
139 $this->assertNotEmpty($elements);
141 // Configure untranslatable field widgets to be displayed on non-default
142 // language edit forms.
143 $this->drupalPostForm($settings_url, [$settings_key => 0], 'Save configuration');
145 // Check that the widget is displayed along with its clue in the edit form
146 // for both languages.
147 $this->drupalGet($en_edit_url);
148 $this->assertNotEmpty($this->xpath($field_xpath));
149 $this->assertNotEmpty($this->xpath($clue_xpath));
150 $this->drupalGet($it_edit_url);
151 $this->assertNotEmpty($this->xpath($field_xpath));
152 $this->assertNotEmpty($this->xpath($clue_xpath));
154 // Enable content moderation and verify that widgets are hidden despite them
155 // being configured to be displayed.
156 $this->enableContentModeration();
157 $this->drupalGet($it_edit_url);
158 $this->assertEmpty($this->xpath($field_xpath));
159 $this->assertEmpty($this->xpath($clue_xpath));
161 // Verify a warning is displayed.
162 $this->assertSession()->pageTextContains('Fields that apply to all languages are hidden to avoid conflicting changes.');
163 $elements = $this->xpath($link_xpath, [':edit_path' => $edit_path]);
164 $this->assertNotEmpty($elements);
166 // Verify that checkboxes on the language content settings page are checked
167 // and disabled for moderated bundles.
168 $this->drupalGet($settings_url);
169 $input_xpath = '//input[@name="settings[' . $this->entityTypeId . '][' . $this->bundle . '][settings][content_translation][untranslatable_fields_hide]" and @value=1 and @disabled="disabled"]';
170 $elements = $this->xpath($input_xpath);
171 $this->assertNotEmpty($elements);
172 $this->drupalPostForm(NULL, [$settings_key => 0], 'Save configuration');
173 $elements = $this->xpath($input_xpath);
174 $this->assertNotEmpty($elements);