View source
<?php
namespace Drupal\Tests\image\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\image\Entity\ImageStyle;
use Drupal\Tests\TestFileCreationTrait;
class ImageStyleFlushTest extends ImageFieldTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
compareFiles as drupalCompareFiles;
}
protected $defaultTheme = 'stark';
public function createSampleImage($style, $wrapper) {
static $file;
if (!isset($file)) {
$files = $this
->drupalGetTestFiles('image');
$file = reset($files);
}
$source_uri = \Drupal::service('file_system')
->copy($file->uri, $wrapper . '://');
$derivative_uri = $style
->buildUri($source_uri);
$derivative = $style
->createDerivative($source_uri, $derivative_uri);
return $derivative ? $derivative_uri : FALSE;
}
public function getImageCount($style, $wrapper) {
$count = 0;
if (is_dir($wrapper . '://styles/' . $style
->id())) {
$count = count(\Drupal::service('file_system')
->scanDirectory($wrapper . '://styles/' . $style
->id(), '/.*/'));
}
return $count;
}
public function testFlush() {
$style_name = strtolower($this
->randomMachineName(10));
$style_label = $this
->randomString();
$style_path = 'admin/config/media/image-styles/manage/' . $style_name;
$effect_edits = [
'image_resize' => [
'data[width]' => 100,
'data[height]' => 101,
],
'image_scale' => [
'data[width]' => 110,
'data[height]' => 111,
'data[upscale]' => 1,
],
];
$edit = [
'name' => $style_name,
'label' => $style_label,
];
$this
->drupalGet('admin/config/media/image-styles/add');
$this
->submitForm($edit, 'Create new style');
foreach ($effect_edits as $effect => $edit) {
$this
->drupalGet($style_path);
$this
->submitForm([
'new' => $effect,
], 'Add');
if (!empty($edit)) {
$this
->submitForm($edit, 'Add effect');
}
}
$style = ImageStyle::load($style_name);
$image_path = $this
->createSampleImage($style, 'public');
$this
->assertEquals(2, $this
->getImageCount($style, 'public'), new FormattableMarkup('Image style %style image %file successfully generated.', [
'%style' => $style
->label(),
'%file' => $image_path,
]));
$image_path = $this
->createSampleImage($style, 'private');
$this
->assertEquals(1, $this
->getImageCount($style, 'private'), new FormattableMarkup('Image style %style image %file successfully generated.', [
'%style' => $style
->label(),
'%file' => $image_path,
]));
$style_path = 'admin/config/media/image-styles/manage/' . $style
->id();
$uuids = [];
foreach ($style
->getEffects() as $uuid => $effect) {
$uuids[$effect
->getPluginId()] = $uuid;
}
$this
->drupalGet($style_path . '/effects/' . $uuids['image_scale'] . '/delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet($style_path);
$this
->submitForm([], 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertEquals(1, $this
->getImageCount($style, 'public'), new FormattableMarkup('Image style %style flushed correctly for %wrapper wrapper.', [
'%style' => $style
->label(),
'%wrapper' => 'public',
]));
$this
->assertEquals(0, $this
->getImageCount($style, 'private'), new FormattableMarkup('Image style %style flushed correctly for %wrapper wrapper.', [
'%style' => $style
->label(),
'%wrapper' => 'private',
]));
}
}