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