function ImageAdminStylesTest::testConfigImport

Same name in other branches
  1. 9 core/modules/image/tests/src/Functional/ImageAdminStylesTest.php \Drupal\Tests\image\Functional\ImageAdminStylesTest::testConfigImport()
  2. 8.9.x core/modules/image/tests/src/Functional/ImageAdminStylesTest.php \Drupal\Tests\image\Functional\ImageAdminStylesTest::testConfigImport()
  3. 11.x 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 464

Class

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

Namespace

Drupal\Tests\image\Functional

Code

public function testConfigImport() : void {
    // Create a new style.
    $style_name = $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 = $this->randomMachineName(10);
    $this->createImageField($field_name, 'node', '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->assertSession()
        ->responseContains(\Drupal::service('file_url_generator')->transformRelative($style->buildUrl($original_uri)));
    // 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->assertEquals(0, $this->getImageCount($style), 'Image style was flushed after being deleted by config import.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.