You are here

public function DiffPluginFileTest::testImagePlugin in Diff 8

Tests the Image plugin.

See also

\Drupal\diff\Plugin\diff\Field\ImageFieldBuilder

File

tests/src/Functional/DiffPluginFileTest.php, line 129

Class

DiffPluginFileTest
Tests the Diff module entity plugins.

Namespace

Drupal\Tests\diff\Functional

Code

public function testImagePlugin() {

  // Add image field to the article content type.
  $image_field_name = 'field_image';
  FieldStorageConfig::create([
    'field_name' => $image_field_name,
    'entity_type' => 'node',
    'type' => 'image',
    'settings' => [],
    'cardinality' => 1,
  ])
    ->save();
  $field_config = FieldConfig::create([
    'field_name' => $image_field_name,
    'label' => 'Image',
    'entity_type' => 'node',
    'bundle' => 'article',
    'required' => FALSE,
    'settings' => [
      'alt_field' => 1,
    ],
  ]);
  $field_config
    ->save();
  $this->formDisplay
    ->load('node.article.default')
    ->setComponent($image_field_name, [
    'type' => 'image_image',
    'settings' => [],
  ])
    ->save();
  $this->viewDisplay
    ->load('node.article.default')
    ->setComponent($image_field_name, [
    'type' => 'image',
    'settings' => [],
  ])
    ->save();

  // Create an article.
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'title' => 'Test article',
  ]);
  $revision1 = $node
    ->getRevisionId();

  // Upload an image to the article.
  $test_files = $this
    ->drupalGetTestFiles('image');
  $edit = [
    'files[field_image_0]' => $this->fileSystem
      ->realpath($test_files['1']->uri),
  ];
  $this
    ->drupalPostNodeForm('node/' . $node
    ->id() . '/edit', $edit, t('Save and keep published'));
  $edit = [
    'field_image[0][alt]' => 'Image alt',
    'revision' => TRUE,
  ];
  $this
    ->drupalPostNodeForm(NULL, $edit, t('Save and keep published'));
  $node = $this
    ->drupalGetNodeByTitle('Test article', TRUE);
  $revision2 = $node
    ->getRevisionId();

  // Replace the image by a different one.
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', [], 'Remove');
  $this
    ->drupalPostNodeForm(NULL, [
    'revision' => FALSE,
  ], t('Save and keep published'));
  $edit = [
    'files[field_image_0]' => $this->fileSystem
      ->realpath($test_files['1']->uri),
  ];
  $this
    ->drupalPostNodeForm('node/' . $node
    ->id() . '/edit', $edit, t('Save and keep published'));
  $edit = [
    'field_image[0][alt]' => 'Image alt updated',
    'revision' => TRUE,
  ];
  $this
    ->drupalPostNodeForm(NULL, $edit, t('Save and keep published'));
  $node = $this
    ->drupalGetNodeByTitle('Test article', TRUE);
  $revision3 = $node
    ->getRevisionId();

  // Check differences between revisions.
  $this
    ->clickLink(t('Revisions'));
  $edit = [
    'radios_left' => $revision1,
    'radios_right' => $revision3,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Compare selected revisions'));
  $this
    ->assertText('Image');
  $this
    ->assertText('Image: image-test-transparent-indexed_0.gif');

  // Image title must be absent since it is not set in previous revisions.
  $this
    ->assertNoText('Title');

  // Enable Title field in instance settings.
  $this
    ->drupalPostForm('admin/structure/types/manage/article/fields/node.article.field_image', [
    'settings[title_field]' => 1,
  ], 'Save settings');

  // Add image title and alt text.
  $edit = [
    'field_image[0][alt]' => 'Image alt updated new',
    'revision' => TRUE,
    'field_image[0][title]' => 'Image title updated',
  ];
  $this
    ->drupalPostNodeForm('node/' . $node
    ->id() . '/edit', $edit, t('Save and keep published'));
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/revisions', [], t('Compare selected revisions'));

  // Image title and alternative text must be shown.
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->elementContains('css', 'tr:nth-child(3) td:nth-child(3)', 'Alt: Image alt updated');
  $assert_session
    ->elementTextContains('css', 'tr:nth-child(3) td:nth-child(6)', 'Alt: Image alt updated new');
  $this
    ->assertEquals('', $assert_session
    ->elementExists('css', 'tr:nth-child(4) td:nth-child(3)')
    ->getText());
  $assert_session
    ->elementTextContains('css', 'tr:nth-child(4) td:nth-child(6)', 'Title: Image title updated');

  // Show File ID.
  $this
    ->drupalGet('admin/config/content/diff/fields');
  $this
    ->drupalPostForm(NULL, [], 'node__field_image_settings_edit');
  $edit = [
    'fields[node__field_image][settings_edit_form][settings][show_id]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'node__field_image_plugin_settings_update');
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/revisions', [], t('Compare selected revisions'));

  // Alt and title must be hidden.
  $this
    ->assertText('File ID: 2');

  // Disable alt image fields.
  $this
    ->drupalGet('admin/config/content/diff/fields');
  $this
    ->drupalPostForm(NULL, [], 'node__field_image_settings_edit');
  $edit = [
    'fields[node__field_image][settings_edit_form][settings][compare_alt_field]' => FALSE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'node__field_image_plugin_settings_update');
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/revisions', [], t('Compare selected revisions'));

  // Alt and title must be hidden.
  $this
    ->assertNoText('Alt: Image alt updated');
  $this
    ->assertNoText('Alt: Image alt updated new');
  $this
    ->assertText('Title: Image title updated');

  // Disable title image fields, reenable alt.
  $this
    ->drupalGet('admin/config/content/diff/fields');
  $this
    ->drupalPostForm(NULL, [], 'node__field_image_settings_edit');
  $edit = [
    'fields[node__field_image][settings_edit_form][settings][compare_alt_field]' => TRUE,
    'fields[node__field_image][settings_edit_form][settings][compare_title_field]' => FALSE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'node__field_image_plugin_settings_update');
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/revisions', [], t('Compare selected revisions'));
  $this
    ->assertText('Alt: Image alt updated');
  $this
    ->assertText('Alt: Image alt updated new');
  $this
    ->assertNoText('Title: Image title updated');

  // Assert the thumbnail is displayed.
  $img1_url = file_create_url(\Drupal::token()
    ->replace("public://styles/thumbnail/public/[date:custom:Y]-[date:custom:m]/" . $test_files['1']->name));
  $image_url = file_url_transform_relative($img1_url);
  $this
    ->assertRaw($image_url);

  // Disable thumbnail image field.
  $this
    ->drupalPostForm('admin/config/content/diff/fields', [], 'node__field_image_settings_edit');
  $edit = [
    'fields[node__field_image][settings_edit_form][settings][show_thumbnail]' => FALSE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'node__field_image_plugin_settings_update');
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/revisions', [], t('Compare selected revisions'));

  // Assert the thumbnail is not displayed.
  $img1_url = file_create_url(\Drupal::token()
    ->replace("public://styles/thumbnail/public/[date:custom:Y]-[date:custom:m]/" . $test_files['1']->name));
  $image_url = file_url_transform_relative($img1_url);
  $this
    ->assertNoRaw($image_url);
}