3 namespace Drupal\Tests\content_translation\Functional;
6 use Drupal\language\Entity\ConfigurableLanguage;
9 * Tests the untranslatable fields behaviors.
11 * @group content_translation
13 class ContentTranslationUntranslatableFieldsTest extends ContentTranslationPendingRevisionTestBase {
18 protected function setUp() {
21 // Configure one field as untranslatable.
22 $this->drupalLogin($this->administrator);
24 'settings[' . $this->entityTypeId . '][' . $this->bundle . '][fields][' . $this->fieldName . ']' => 0,
26 $this->drupalPostForm('admin/config/regional/content-language', $edit, 'Save configuration');
28 /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
29 $entity_field_manager = $this->container->get('entity_field.manager');
30 $entity_field_manager->clearCachedFieldDefinitions();
31 $definitions = $entity_field_manager->getFieldDefinitions($this->entityTypeId, $this->bundle);
32 $this->assertFalse($definitions[$this->fieldName]->isTranslatable());
36 * Tests that hiding untranslatable field widgets works correctly.
38 public function testHiddenWidgets() {
39 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
40 $entity_type_manager = $this->container->get('entity_type.manager');
41 $id = $this->createEntity(['title' => $this->randomString()], 'en');
42 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
43 $entity = $entity_type_manager
44 ->getStorage($this->entityTypeId)
47 // Check that the untranslatable field widget is displayed on the edit form
48 // and no translatability clue is displayed yet.
49 $en_edit_url = $entity->toUrl('edit-form');
50 $this->drupalGet($en_edit_url);
51 $field_xpath = '//input[@name="' . $this->fieldName . '[0][value]"]';
52 $this->assertNotEmpty($this->xpath($field_xpath));
53 $clue_xpath = '//label[@for="edit-' . strtr($this->fieldName, '_', '-') . '-0-value"]/span[text()="(all languages)"]';
54 $this->assertEmpty($this->xpath($clue_xpath));
56 // Add a translation and check that the untranslatable field widget is
57 // displayed on the translation and edit forms along with translatability
59 $add_url = Url::fromRoute("entity.{$this->entityTypeId}.content_translation_add", [
60 $entity->getEntityTypeId() => $entity->id(),
64 $this->drupalGet($add_url);
65 $this->assertNotEmpty($this->xpath($field_xpath));
66 $this->assertNotEmpty($this->xpath($clue_xpath));
67 $this->drupalPostForm(NULL, [], 'Save');
69 // Check that the widget is displayed along with its clue in the edit form
70 // for both languages.
71 $this->drupalGet($en_edit_url);
72 $this->assertNotEmpty($this->xpath($field_xpath));
73 $this->assertNotEmpty($this->xpath($clue_xpath));
74 $it_edit_url = $entity->toUrl('edit-form', ['language' => ConfigurableLanguage::load('it')]);
75 $this->drupalGet($it_edit_url);
76 $this->assertNotEmpty($this->xpath($field_xpath));
77 $this->assertNotEmpty($this->xpath($clue_xpath));
79 // Configure untranslatable field widgets to be hidden on non-default
80 // language edit forms.
81 $settings_key = 'settings[' . $this->entityTypeId . '][' . $this->bundle . '][settings][content_translation][untranslatable_fields_hide]';
82 $settings_url = 'admin/config/regional/content-language';
83 $this->drupalPostForm($settings_url, [$settings_key => 1], 'Save configuration');
85 // Verify that the widget is displayed in the default language edit form,
86 // but no clue is displayed.
87 $this->drupalGet($en_edit_url);
88 $field_xpath = '//input[@name="' . $this->fieldName . '[0][value]"]';
89 $this->assertNotEmpty($this->xpath($field_xpath));
90 $this->assertEmpty($this->xpath($clue_xpath));
92 // Verify no widget is displayed on the non-default language edit form.
93 $this->drupalGet($it_edit_url);
94 $this->assertEmpty($this->xpath($field_xpath));
95 $this->assertEmpty($this->xpath($clue_xpath));
97 // Verify a warning is displayed.
98 $this->assertSession()->pageTextContains('Fields that apply to all languages are hidden to avoid conflicting changes.');
99 $edit_path = $entity->toUrl('edit-form')->toString();
100 $link_xpath = '//a[@href=:edit_path and text()="Edit them on the original language form"]';
101 $elements = $this->xpath($link_xpath, [':edit_path' => $edit_path]);
102 $this->assertNotEmpty($elements);
104 // Configure untranslatable field widgets to be displayed on non-default
105 // language edit forms.
106 $this->drupalPostForm($settings_url, [$settings_key => 0], 'Save configuration');
108 // Check that the widget is displayed along with its clue in the edit form
109 // for both languages.
110 $this->drupalGet($en_edit_url);
111 $this->assertNotEmpty($this->xpath($field_xpath));
112 $this->assertNotEmpty($this->xpath($clue_xpath));
113 $this->drupalGet($it_edit_url);
114 $this->assertNotEmpty($this->xpath($field_xpath));
115 $this->assertNotEmpty($this->xpath($clue_xpath));
117 // Enable content moderation and verify that widgets are hidden despite them
118 // being configured to be displayed.
119 $this->enableContentModeration();
120 $this->drupalGet($it_edit_url);
121 $this->assertEmpty($this->xpath($field_xpath));
122 $this->assertEmpty($this->xpath($clue_xpath));
124 // Verify a warning is displayed.
125 $this->assertSession()->pageTextContains('Fields that apply to all languages are hidden to avoid conflicting changes.');
126 $elements = $this->xpath($link_xpath, [':edit_path' => $edit_path]);
127 $this->assertNotEmpty($elements);
129 // Verify that checkboxes on the language content settings page are checked
130 // and disabled for moderated bundles.
131 $this->drupalGet($settings_url);
132 $input_xpath = '//input[@name="settings[' . $this->entityTypeId . '][' . $this->bundle . '][settings][content_translation][untranslatable_fields_hide]" and @value=1 and @disabled="disabled"]';
133 $elements = $this->xpath($input_xpath);
134 $this->assertNotEmpty($elements);
135 $this->drupalPostForm(NULL, [$settings_key => 0], 'Save configuration');
136 $elements = $this->xpath($input_xpath);
137 $this->assertNotEmpty($elements);