Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / diff / src / Tests / DiffAdminFormsTest.php
1 <?php
2
3 namespace Drupal\diff\Tests;
4 use Drupal\Tests\diff\Functional\CoreVersionUiTestTrait;
5
6 /**
7  * Tests the Diff admin forms.
8  *
9  * @group diff
10  */
11 class DiffAdminFormsTest extends DiffTestBase {
12
13   use CoreVersionUiTestTrait;
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'field_ui',
22     'help',
23   ];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30
31     $this->drupalLogin($this->rootUser);
32   }
33
34   /**
35    * Run all independent tests.
36    */
37   public function testAll() {
38     $this->doTestSettingsUi();
39     $this->doTestSettingsTab();
40     $this->doTestRequirements();
41     $this->doTestConfigurableFieldsTab();
42     $this->doTestPluginWeight();
43   }
44
45   /**
46    * Tests the descriptions in the Settings UI.
47    */
48   public function doTestSettingsUi() {
49     // Enable the help block.
50     $this->drupalPlaceBlock('help_block', ['region' => 'help']);
51
52     $this->drupalGet('admin/config/content/diff/general');
53     // Check the settings introduction text.
54     $this->assertText('Configurations for the revision comparison functionality and diff layout plugins.');
55     // Check the layout plugins descriptions.
56     $this->assertText('Field based layout, displays revision comparison side by side.');
57     $this->assertText('Field based layout, displays revision comparison line by line.');
58   }
59
60   /**
61    * Tests the Settings tab.
62    */
63   public function doTestSettingsTab() {
64     $edit = [
65       'radio_behavior' => 'linear',
66       'context_lines_leading' => 10,
67       'context_lines_trailing' => 5,
68     ];
69     $this->drupalPostForm('admin/config/content/diff/general', $edit, t('Save configuration'));
70     $this->assertText('The configuration options have been saved.');
71   }
72
73   /**
74    * Tests the module requirements.
75    */
76   public function doTestRequirements() {
77     module_load_install('diff');
78     $requirements = diff_requirements('runtime');
79     $this->assertEqual($requirements['html_diff_advanced']['title'], 'Diff');
80
81     $has_htmlDiffAdvanced = class_exists('\HtmlDiffAdvanced');
82     if (!$has_htmlDiffAdvanced) {
83       // The plugin is disabled dependencies are missing.
84       $this->assertEqual($requirements['html_diff_advanced']['value'], 'Visual inline layout');
85     }
86     else {
87       // The plugin is enabled by default if dependencies are met.
88       $this->assertEqual($requirements['html_diff_advanced']['value'], 'Installed correctly');
89     }
90   }
91
92   /**
93    * Tests the Configurable Fields tab.
94    */
95   public function doTestConfigurableFieldsTab() {
96     $this->drupalGet('admin/config/content/diff/fields');
97
98     // Test changing type without changing settings.
99     $edit = [
100       'fields[node.body][plugin][type]' => 'text_summary_field_diff_builder',
101     ];
102     $this->drupalPostForm(NULL, $edit, t('Save'));
103     $this->assertFieldByName('fields[node.body][plugin][type]', 'text_summary_field_diff_builder');
104     $edit = [
105       'fields[node.body][plugin][type]' => 'text_field_diff_builder',
106     ];
107     $this->drupalPostForm(NULL, $edit, t('Save'));
108     $this->assertFieldByName('fields[node.body][plugin][type]', 'text_field_diff_builder');
109
110     $this->drupalPostAjaxForm(NULL, [], 'node.body_settings_edit');
111     $this->assertText('Plugin settings: Text');
112     $edit = [
113       'fields[node.body][settings_edit_form][settings][show_header]' => TRUE,
114       'fields[node.body][settings_edit_form][settings][compare_format]' => FALSE,
115       'fields[node.body][settings_edit_form][settings][markdown]' => 'filter_xss_all',
116     ];
117     $this->drupalPostAjaxForm(NULL, $edit, 'node.body_plugin_settings_update');
118     $this->drupalPostForm(NULL, [], t('Save'));
119     $this->assertText('Your settings have been saved.');
120
121     // Check the values were saved.
122     $this->drupalPostAjaxForm(NULL, [], 'node.body_settings_edit');
123     $this->assertFieldByName('fields[node.body][settings_edit_form][settings][markdown]', 'filter_xss_all');
124
125     // Edit another field.
126     $this->drupalPostAjaxForm(NULL, [], 'node.title_settings_edit');
127     $edit = [
128       'fields[node.title][settings_edit_form][settings][markdown]' => 'filter_xss_all',
129     ];
130     $this->drupalPostAjaxForm(NULL, $edit, 'node.title_plugin_settings_update');
131     $this->drupalPostForm(NULL, [], t('Save'));
132
133     // Check both fields and their config values.
134     $this->drupalPostAjaxForm(NULL, [], 'node.body_settings_edit');
135     $this->assertFieldByName('fields[node.body][settings_edit_form][settings][markdown]', 'filter_xss_all');
136     $this->drupalPostAjaxForm(NULL, [], 'node.title_settings_edit');
137     $this->assertFieldByName('fields[node.title][settings_edit_form][settings][markdown]', 'filter_xss_all');
138
139     // Save field settings without changing anything and assert the config.
140     $this->drupalPostForm(NULL, [], t('Save'));
141     $this->drupalPostAjaxForm(NULL, [], 'node.body_settings_edit');
142     $this->assertFieldByName('fields[node.body][settings_edit_form][settings][markdown]', 'filter_xss_all');
143     $this->drupalPostAjaxForm(NULL, [], 'node.title_settings_edit');
144     $this->assertFieldByName('fields[node.title][settings_edit_form][settings][markdown]', 'filter_xss_all');
145
146     $edit = [
147       'fields[node.sticky][plugin][type]' => 'hidden',
148     ];
149     $this->drupalPostForm(NULL, $edit, t('Save'));
150     $this->assertFieldByName('fields[node.sticky][plugin][type]', 'hidden');
151   }
152
153   /**
154    * Tests the Compare Revisions vertical tab.
155    */
156   public function doTestPluginWeight() {
157     // Create a node with a revision.
158     $edit = [
159       'title[0][value]' => 'great_title',
160       'body[0][value]' => '<p>great_body</p>',
161     ];
162     $this->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
163     $this->clickLink('Edit');
164     $edit = [
165       'title[0][value]' => 'greater_title',
166       'body[0][value]' => '<p>greater_body</p>',
167     ];
168     $this->drupalPostNodeForm(NULL, $edit, t('Save and keep published'));
169
170     // Assert the diff display uses the classic layout.
171     $node = $this->getNodeByTitle('greater_title');
172     $this->drupalGet('node/' . $node->id() . '/revisions');
173     $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
174     $this->assertLink('Unified fields');
175     $this->assertLink('Split fields');
176     $this->assertLink('Raw');
177     $this->assertLink('Strip tags');
178     $text = $this->xpath('//tbody/tr[4]/td[3]');
179     $this->assertEqual(htmlspecialchars_decode(strip_tags($text[0]->asXML())), '<p>great_body</p>');
180
181     // Change the settings of the layouts, disable the single column.
182     $edit = [
183       'layout_plugins[split_fields][weight]' => '11',
184       'layout_plugins[unified_fields][enabled]' => FALSE,
185     ];
186     $this->drupalPostForm('admin/config/content/diff/general', $edit, t('Save configuration'));
187
188     // Assert the diff display uses the markdown layout.
189     $this->drupalGet('node/' . $node->id() . '/revisions');
190     $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
191     $this->assertResponse(200);
192     $this->assertNoLink('Unified fields');
193     $this->assertLink('Split fields');
194     $this->clickLink('Split fields');
195     $this->assertLink('Raw');
196     $this->assertLink('Strip tags');
197     $this->clickLink('Strip tags');
198     $text = $this->xpath('//tbody/tr[4]/td[2]');
199     $this->assertEqual(htmlspecialchars_decode(strip_tags($text[0]->asXML())), 'great_body');
200
201     // Change the settings of the layouts, enable single column.
202     $edit = [
203       'layout_plugins[unified_fields][enabled]' => TRUE,
204       'layout_plugins[split_fields][enabled]' => FALSE,
205     ];
206     $this->drupalPostForm('admin/config/content/diff/general', $edit, t('Save configuration'));
207
208     // Test the validation of form.
209     $edit = [
210       'layout_plugins[unified_fields][enabled]' => FALSE,
211       'layout_plugins[split_fields][enabled]' => FALSE,
212     ];
213     $this->drupalPostForm('admin/config/content/diff/general', $edit, t('Save configuration'));
214     $this->assertText('At least one layout plugin needs to be enabled.');
215
216     // Assert the diff display uses the single column layout.
217     $this->drupalGet('node/' . $node->id() . '/revisions');
218     $this->drupalPostForm(NULL, [], t('Compare selected revisions'));
219     $this->assertResponse(200);
220     $this->assertLink('Unified fields');
221     $this->assertNoLink('Split fields');
222     $this->assertLink('Raw');
223     $this->assertLink('Strip tags');
224     $text = $this->xpath('//tbody/tr[5]/td[4]');
225     $this->assertEqual(htmlspecialchars_decode(strip_tags($text[0]->asXML())), '<p>great_body</p>');
226     $this->clickLink('Strip tags');
227     $text = $this->xpath('//tbody/tr[5]/td[2]');
228     $this->assertEqual(htmlspecialchars_decode(strip_tags($text[0]->asXML())), 'great_body');
229   }
230
231 }