6e994053f5c2f05e68e333049535dd6c473e8cdc
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Experimental / ParagraphsExperimentalConfigTest.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Experimental;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\node\Entity\NodeType;
7
8 /**
9  * Tests paragraphs configuration.
10  *
11  * @group paragraphs
12  */
13 class ParagraphsExperimentalConfigTest extends ParagraphsExperimentalTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = array(
21     'content_translation',
22   );
23
24   /**
25    * Tests adding paragraphs with no translation enabled.
26    */
27   public function testFieldTranslationDisabled() {
28     $this->loginAsAdmin([
29       'administer languages',
30       'administer content translation',
31       'create content translations',
32       'translate any entity',
33     ]);
34
35     // Add a paragraphed content type.
36     $this->addParagraphedContentType('paragraphed_test', 'paragraphs_field');
37
38     $this->addParagraphsType('paragraph_type_test');
39     $this->addParagraphsType('text');
40
41     // Add a second language.
42     ConfigurableLanguage::create(['id' => 'de'])->save();
43
44     // Enable translation for paragraphed content type. Do not enable
45     // translation for the ERR paragraphs field nor for fields on the
46     // paragraph type.
47     $edit = [
48       'entity_types[node]' => TRUE,
49       'settings[node][paragraphed_test][translatable]' => TRUE,
50       'settings[node][paragraphed_test][fields][paragraphs_field]' => FALSE,
51     ];
52     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
53
54     // Create a node with a paragraph.
55     $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'paragraphs_field_paragraph_type_test_add_more');
56     $this->drupalPostForm(NULL, ['title[0][value]' => 'paragraphed_title'], t('Save and publish'));
57
58     // Attempt to add a translation.
59     $node = $this->drupalGetNodeByTitle('paragraphed_title');
60     $this->drupalGet('node/' . $node->id() . '/translations');
61     $this->clickLink(t('Add'));
62     // Save the translation.
63     $this->drupalPostForm(NULL, [], t('Save and keep published (this translation)'));
64     $this->assertText('paragraphed_test paragraphed_title has been updated.');
65   }
66
67   /**
68    * Tests content translation form translatability constraints messages.
69    */
70   public function testContentTranslationForm() {
71     $this->loginAsAdmin([
72       'administer languages',
73       'administer content translation',
74       'create content translations',
75       'translate any entity',
76     ]);
77
78     // Check warning message is displayed.
79     $this->drupalGet('admin/config/regional/content-language');
80     $this->assertText('(* unsupported) Paragraphs fields do not support translation.');
81
82     $this->addParagraphedContentType('paragraphed_test', 'paragraphs_field');
83     // Check error message is not displayed.
84     $this->drupalGet('admin/config/regional/content-language');
85     $this->assertText('(* unsupported) Paragraphs fields do not support translation.');
86     $this->assertNoRaw('<div class="messages messages--error');
87
88     // Add a second language.
89     ConfigurableLanguage::create(['id' => 'de'])->save();
90
91     // Enable translation for paragraphed content type.
92     $edit = [
93       'entity_types[node]' => TRUE,
94       'settings[node][paragraphed_test][translatable]' => TRUE,
95       'settings[node][paragraphed_test][fields][paragraphs_field]' => FALSE,
96     ];
97     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
98
99     // Check error message is still not displayed.
100     $this->drupalGet('admin/config/regional/content-language');
101     $this->assertText('(* unsupported) Paragraphs fields do not support translation.');
102     $this->assertNoRaw('<div class="messages messages--error');
103
104     // Check content type field management warning.
105     $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs_field');
106     $this->assertText('Paragraphs fields do not support translation.');
107
108     // Make the paragraphs field translatable.
109     $edit = [
110       'entity_types[node]' => TRUE,
111       'settings[node][paragraphed_test][translatable]' => TRUE,
112       'settings[node][paragraphed_test][fields][paragraphs_field]' => TRUE,
113     ];
114     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
115
116     // Check content type field management error.
117     $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs_field');
118     $this->assertText('Paragraphs fields do not support translation.');
119     $this->assertRaw('<div class="messages messages--error');
120
121     // Check a not paragraphs translatable field does not display the message.
122     $this->drupalGet('admin/structure/types/manage/paragraphed_test/fields/add-field');
123     $edit = [
124       'new_storage_type' => 'field_ui:entity_reference:node',
125       'label' => 'new_no_paragraphs_field',
126       'field_name' => 'new_no_paragraphs_field',
127     ];
128     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
129     $this->drupalPostForm(NULL, [], t('Save field settings'));
130     $this->assertNoText('Paragraphs fields do not support translation.');
131     $this->assertNoRaw('<div class="messages messages--warning');
132   }
133
134   /**
135    * Tests that we can use paragraphs widget only for paragraphs.
136    */
137   public function testAvoidUsingParagraphsWithWrongEntity() {
138     $node_type = NodeType::create([
139       'type' => 'article',
140       'name' => 'article',
141     ]);
142     $node_type->save();
143     $this->loginAsAdmin([
144       'edit any article content',
145     ]);
146     $this->addParagraphsType('paragraphed_type');
147
148     // Create reference to node.
149     $this->fieldUIAddNewField('admin/structure/types/manage/article', 'node_reference', 'NodeReference', 'entity_reference_revisions', [
150       'cardinality' => 'number',
151       'cardinality_number' => 1,
152       'settings[target_type]' => 'node',
153     ], [
154       'settings[handler_settings][target_bundles][article]' => 'article',
155     ]);
156     $this->drupalGet('admin/structure/types/manage/article/form-display');
157     $this->assertNoOption('edit-fields-field-node-reference-type', 'entity_reference_paragraphs');
158     $this->assertNoOption('edit-fields-field-node-reference-type', 'paragraphs');
159   }
160
161 }