You are here

public function ImageAdminStylesTest::testConfigImport in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/image/tests/src/Functional/ImageAdminStylesTest.php \Drupal\Tests\image\Functional\ImageAdminStylesTest::testConfigImport()
  2. 10 core/modules/image/tests/src/Functional/ImageAdminStylesTest.php \Drupal\Tests\image\Functional\ImageAdminStylesTest::testConfigImport()

Tests image style configuration import that does a delete.

File

core/modules/image/tests/src/Functional/ImageAdminStylesTest.php, line 458

Class

ImageAdminStylesTest
Tests creation, deletion, and editing of image styles and effects.

Namespace

Drupal\Tests\image\Functional

Code

public function testConfigImport() {

  // Create a new style.
  $style_name = strtolower($this
    ->randomMachineName(10));
  $style_label = $this
    ->randomString();
  $style = ImageStyle::create([
    'name' => $style_name,
    'label' => $style_label,
  ]);
  $style
    ->save();

  // Create an image field that uses the new style.
  $field_name = strtolower($this
    ->randomMachineName(10));
  $this
    ->createImageField($field_name, 'article');
  \Drupal::service('entity_display.repository')
    ->getViewDisplay('node', 'article')
    ->setComponent($field_name, [
    'type' => 'image',
    'settings' => [
      'image_style' => $style_name,
    ],
  ])
    ->save();

  // Create a new node with an image attached.
  $test_image = current($this
    ->drupalGetTestFiles('image'));
  $nid = $this
    ->uploadNodeImage($test_image, $field_name, 'article', $this
    ->randomMachineName());
  $node = Node::load($nid);

  // Get node field original image URI.
  $fid = $node
    ->get($field_name)->target_id;
  $original_uri = File::load($fid)
    ->getFileUri();

  // Test that image is displayed using newly created style.
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertRaw(file_url_transform_relative($style
    ->buildUrl($original_uri)), new FormattableMarkup('Image displayed using style @style.', [
    '@style' => $style_name,
  ]));

  // Copy config to sync, and delete the image style.
  $sync = $this->container
    ->get('config.storage.sync');
  $active = $this->container
    ->get('config.storage');

  // Remove the image field from the display, to avoid a dependency error
  // during import.
  EntityViewDisplay::load('node.article.default')
    ->removeComponent($field_name)
    ->save();
  $this
    ->copyConfig($active, $sync);
  $sync
    ->delete('image.style.' . $style_name);
  $this
    ->configImporter()
    ->import();
  $this
    ->assertNull(ImageStyle::load($style_name), 'Style deleted after config import.');
  $this
    ->assertEqual($this
    ->getImageCount($style), 0, 'Image style was flushed after being deleted by config import.');
}