3 namespace Drupal\diff\Tests;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\field_ui\Tests\FieldUiTestTrait;
8 use Drupal\Tests\diff\Functional\CoreVersionUiTestTrait;
11 * Tests the Diff module entity plugins.
15 class DiffPluginFileTest extends DiffPluginTestBase {
18 use CoreVersionUiTestTrait;
25 public static $modules = [
32 * The file system service.
34 * @var \Drupal\Core\File\FileSystemInterface
36 protected $fileSystem;
41 protected function setUp() {
44 $this->fileSystem = \Drupal::service('file_system');
46 // FieldUiTestTrait checks the breadcrumb when adding a field, so we need
47 // to show the breadcrumb block.
48 $this->drupalPlaceBlock('system_breadcrumb_block');
52 * Tests the File plugin.
54 * @see \Drupal\diff\Plugin\diff\Field\FileFieldBuilder
56 public function testFilePlugin() {
57 // Add file field to the article content type.
58 $file_field_name = 'field_file';
59 $field_storage = FieldStorageConfig::create(array(
60 'field_name' => $file_field_name,
61 'entity_type' => 'node',
64 $field_storage->save();
66 'entity_type' => 'node',
67 'field_storage' => $field_storage,
68 'bundle' => 'article',
72 // Make the field visible in the form and desfault display.
73 $this->viewDisplay->load('node.article.default')
74 ->setComponent('test_field')
75 ->setComponent($file_field_name)
77 $this->formDisplay->load('node.article.default')
78 ->setComponent('test_field', ['type' => 'entity_reference_autocomplete'])
79 ->setComponent($file_field_name, ['type' => 'file_generic'])
83 $node = $this->drupalCreateNode([
85 'title' => 'Test article',
87 $revision1 = $node->getRevisionId();
89 // Upload a file to the article.
90 $test_files = $this->drupalGetTestFiles('text');
91 $edit['files[field_file_0]'] = $this->fileSystem->realpath($test_files['0']->uri);
92 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Upload');
93 $edit['revision'] = TRUE;
94 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
95 $node = $this->drupalGetNodeByTitle('Test article', TRUE);
96 $revision2 = $node->getRevisionId();
98 // Replace the file by a different one.
99 $this->drupalPostForm('node/' . $node->id() . '/edit', [], 'Remove');
100 $this->drupalPostNodeForm(NULL, ['revision' => FALSE], t('Save and keep published'));
101 $edit['files[field_file_0]'] = $this->fileSystem->realpath($test_files['1']->uri);
102 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Upload');
103 $edit['revision'] = TRUE;
104 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
105 $node = $this->drupalGetNodeByTitle('Test article', TRUE);
106 $revision3 = $node->getRevisionId();
108 // Check differences between revisions.
109 $this->clickLink(t('Revisions'));
111 'radios_left' => $revision1,
112 'radios_right' => $revision3,
114 $this->drupalPostForm(NULL, $edit, t('Compare selected revisions'));
115 $this->assertText('File');
116 $this->assertText('File: text-1.txt');
117 $this->assertText('File ID: 4');
119 // Use the unified fields layout.
120 $this->clickLink('Unified fields');
121 $this->assertResponse(200);
122 $this->assertText('File');
123 $this->assertText('File: text-1.txt');
124 $this->assertText('File ID: 4');
128 * Tests the Image plugin.
130 * @see \Drupal\diff\Plugin\diff\Field\ImageFieldBuilder
132 public function testImagePlugin() {
133 // Add image field to the article content type.
134 $image_field_name = 'field_image';
135 FieldStorageConfig::create([
136 'field_name' => $image_field_name,
137 'entity_type' => 'node',
143 $field_config = FieldConfig::create([
144 'field_name' => $image_field_name,
146 'entity_type' => 'node',
147 'bundle' => 'article',
149 'settings' => ['alt_field' => 1],
151 $field_config->save();
153 $this->formDisplay->load('node.article.default')
154 ->setComponent($image_field_name, [
155 'type' => 'image_image',
160 $this->viewDisplay->load('node.article.default')
161 ->setComponent($image_field_name, [
167 // Create an article.
168 $node = $this->drupalCreateNode([
170 'title' => 'Test article',
172 $revision1 = $node->getRevisionId();
174 // Upload an image to the article.
175 $test_files = $this->drupalGetTestFiles('image');
176 $edit = ['files[field_image_0]' => $this->fileSystem->realpath($test_files['1']->uri)];
177 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
179 'field_image[0][alt]' => 'Image alt',
182 $this->drupalPostNodeForm(NULL, $edit, t('Save and keep published'));
183 $node = $this->drupalGetNodeByTitle('Test article', TRUE);
184 $revision2 = $node->getRevisionId();
186 // Replace the image by a different one.
187 $this->drupalPostForm('node/' . $node->id() . '/edit', [], 'Remove');
188 $this->drupalPostNodeForm(NULL, ['revision' => FALSE], t('Save and keep published'));
189 $edit = ['files[field_image_0]' => $this->fileSystem->realpath($test_files['1']->uri)];
190 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
192 'field_image[0][alt]' => 'Image alt updated',
195 $this->drupalPostNodeForm(NULL, $edit, t('Save and keep published'));
196 $node = $this->drupalGetNodeByTitle('Test article', TRUE);
197 $revision3 = $node->getRevisionId();
199 // Check differences between revisions.
200 $this->clickLink(t('Revisions'));
202 'radios_left' => $revision1,
203 'radios_right' => $revision3,
205 $this->drupalPostForm(NULL, $edit, t('Compare selected revisions'));
206 $this->assertText('Image');
207 $this->assertText('Image: image-test-transparent-indexed.gif');
208 // Image title must be absent since it is not set in previous revisions.
209 $this->assertNoText('Title');
211 // Enable Title field in instance settings.
212 $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.field_image', [
213 'settings[title_field]' => 1,
216 // Add image title and alt text.
218 'field_image[0][alt]' => 'Image alt updated new',
220 'field_image[0][title]' => 'Image title updated',
222 $this->drupalPostNodeForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
223 $this->drupalPostForm('node/' . $node->id() . '/revisions', [], t('Compare selected revisions'));
224 $rows = $this->xpath('//tbody/tr');
225 // Image title and alternative text must be shown.
226 $this->assertEqual(htmlspecialchars_decode(strip_tags($rows[2]->td[2]->asXML())), 'Alt: Image alt updated');
227 $this->assertEqual(htmlspecialchars_decode(strip_tags($rows[2]->td[5]->asXML())), 'Alt: Image alt updated new');
228 $this->assertEqual(htmlspecialchars_decode(strip_tags($rows[3]->td[2]->asXML())), '');
229 $this->assertEqual(htmlspecialchars_decode(strip_tags($rows[3]->td[5]->asXML())), 'Title: Image title updated');
232 $this->drupalGet('admin/config/content/diff/fields');
233 $this->drupalPostAjaxForm(NULL, [], 'node.field_image_settings_edit');
235 'fields[node.field_image][settings_edit_form][settings][show_id]' => TRUE,
237 $this->drupalPostAjaxForm(NULL, $edit, 'node.field_image_plugin_settings_update');
238 $this->drupalPostForm(NULL, [], t('Save'));
239 $this->drupalPostForm('node/' . $node->id() . '/revisions', [], t('Compare selected revisions'));
240 // Alt and title must be hidden.
241 $this->assertText('File ID: 2');
243 // Disable alt image fields.
244 $this->drupalGet('admin/config/content/diff/fields');
245 $this->drupalPostAjaxForm(NULL, [], 'node.field_image_settings_edit');
247 'fields[node.field_image][settings_edit_form][settings][compare_alt_field]' => FALSE,
249 $this->drupalPostAjaxForm(NULL, $edit, 'node.field_image_plugin_settings_update');
250 $this->drupalPostForm(NULL, [], t('Save'));
251 $this->drupalPostForm('node/' . $node->id() . '/revisions', [], t('Compare selected revisions'));
252 // Alt and title must be hidden.
253 $this->assertNoText('Alt: Image alt updated');
254 $this->assertNoText('Alt: Image alt updated new');
255 $this->assertText('Title: Image title updated');
257 // Disable title image fields, reenable alt.
258 $this->drupalGet('admin/config/content/diff/fields');
259 $this->drupalPostAjaxForm(NULL, [], 'node.field_image_settings_edit');
261 'fields[node.field_image][settings_edit_form][settings][compare_alt_field]' => TRUE,
262 'fields[node.field_image][settings_edit_form][settings][compare_title_field]' => FALSE,
264 $this->drupalPostAjaxForm(NULL, $edit, 'node.field_image_plugin_settings_update');
265 $this->drupalPostForm(NULL, [], t('Save'));
266 $this->drupalPostForm('node/' . $node->id() . '/revisions', [], t('Compare selected revisions'));
267 $this->assertText('Alt: Image alt updated');
268 $this->assertText('Alt: Image alt updated new');
269 $this->assertNoText('Title: Image title updated');
270 // Assert the thumbnail is displayed.
271 $img1_url = file_create_url(\Drupal::token()->replace("public://styles/thumbnail/public/[date:custom:Y]-[date:custom:m]/" . $test_files['1']->name));
272 $image_url = file_url_transform_relative($img1_url);
273 $this->assertRaw($image_url);
275 // Disable thumbnail image field.
276 $this->drupalPostAjaxForm('admin/config/content/diff/fields', [], 'node.field_image_settings_edit');
278 'fields[node.field_image][settings_edit_form][settings][show_thumbnail]' => FALSE,
280 $this->drupalPostAjaxForm(NULL, $edit, 'node.field_image_plugin_settings_update');
281 $this->drupalPostForm(NULL, [], t('Save'));
282 $this->drupalPostForm('node/' . $node->id() . '/revisions', [], t('Compare selected revisions'));
284 // Assert the thumbnail is not displayed.
285 $img1_url = file_create_url(\Drupal::token()->replace("public://styles/thumbnail/public/[date:custom:Y]-[date:custom:m]/" . $test_files['1']->name));
286 $image_url = file_url_transform_relative($img1_url);
287 $this->assertNoRaw($image_url);