View source
<?php
namespace Drupal\Tests\image\Functional;
use Drupal\Core\Url;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\ImageStyleInterface;
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
use Drupal\Tests\TestFileCreationTrait;
class ImageAdminStylesTest extends ImageFieldTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
compareFiles as drupalCompareFiles;
}
protected $defaultTheme = 'stark';
public function createSampleImage(ImageStyleInterface $style) {
static $file_path;
if (!isset($file_path)) {
$files = $this
->drupalGetTestFiles('image');
$file = reset($files);
$file_path = \Drupal::service('file_system')
->copy($file->uri, 'public://');
}
return $style
->buildUrl($file_path) ? $file_path : FALSE;
}
public function getImageCount(ImageStyleInterface $style) {
$count = 0;
if (is_dir('public://styles/' . $style
->id())) {
$count = count(\Drupal::service('file_system')
->scanDirectory('public://styles/' . $style
->id(), '/.*/'));
}
return $count;
}
public function testNumericStyleName() {
$style_name = rand();
$style_label = $this
->randomString();
$edit = [
'name' => $style_name,
'label' => $style_label,
];
$this
->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style'));
$this
->assertRaw(t('Style %name was created.', [
'%name' => $style_label,
]));
$options = image_style_options();
$this
->assertArrayHasKey($style_name, $options);
}
public function testStyle() {
$admin_path = 'admin/config/media/image-styles';
$style_name = strtolower($this
->randomMachineName(10));
$style_label = $this
->randomString();
$style_path = $admin_path . '/manage/' . $style_name;
$effect_edits = [
'image_resize' => [
'width' => 100,
'height' => 101,
],
'image_scale' => [
'width' => 110,
'height' => 111,
'upscale' => 1,
],
'image_scale_and_crop' => [
'width' => 120,
'height' => 121,
],
'image_crop' => [
'width' => 130,
'height' => 131,
'anchor' => 'left-top',
],
'image_desaturate' => [],
'image_rotate' => [
'degrees' => 5,
'random' => 1,
'bgcolor' => '#FFFF00',
],
];
$edit = [
'name' => $style_name,
'label' => $style_label,
];
$this
->drupalPostForm($admin_path . '/add', $edit, t('Create new style'));
$this
->assertRaw(t('Style %name was created.', [
'%name' => $style_label,
]));
$this
->drupalGet($admin_path);
$this
->assertLinkByHref($style_path);
$this
->assertLinkByHref($style_path . '/flush');
$this
->assertLinkByHref($style_path . '/delete');
foreach ($effect_edits as $effect => $edit) {
$edit_data = [];
foreach ($edit as $field => $value) {
$edit_data['data[' . $field . ']'] = $value;
}
$this
->drupalPostForm($style_path, [
'new' => $effect,
], t('Add'));
if (!empty($edit)) {
$this
->drupalPostForm(NULL, $edit_data, t('Add effect'));
}
}
$style = ImageStyle::load($style_name);
$this
->assertEqual('bar', $style
->getThirdPartySetting('image_module_test', 'foo'), 'Third party settings were added to the image style.');
$style_uri_path = $style
->toUrl()
->toString();
$this
->assertStringContainsString($style_path, $style_uri_path, 'The image style URI is correct.');
$uuids = [];
foreach ($style
->getEffects() as $uuid => $effect) {
$uuids[$effect
->getPluginId()] = $uuid;
$effect_configuration = $effect
->getConfiguration();
foreach ($effect_edits[$effect
->getPluginId()] as $field => $value) {
$this
->assertEqual($value, $effect_configuration['data'][$field], new FormattableMarkup('The %field field in the %effect effect has the correct value of %value.', [
'%field' => $field,
'%effect' => $effect
->getPluginId(),
'%value' => $value,
]));
}
}
foreach (array_keys($effect_edits) as $effect_name) {
$this
->assertTrue(isset($uuids[$effect_name]), new FormattableMarkup('A %effect_name effect was saved with ID %uuid', [
'%effect_name' => $effect_name,
'%uuid' => $uuids[$effect_name],
]));
}
$effect_edits_order = array_keys($effect_edits);
$order_correct = TRUE;
$index = 0;
foreach ($style
->getEffects() as $effect) {
if ($effect_edits_order[$index] != $effect
->getPluginId()) {
$order_correct = FALSE;
}
$index++;
}
$this
->assertTrue($order_correct, 'The order of the effects is correctly set by default.');
$style_name = strtolower($this
->randomMachineName(10));
$style_label = $this
->randomMachineName();
$weight = count($effect_edits);
$edit = [
'name' => $style_name,
'label' => $style_label,
];
foreach ($style
->getEffects() as $uuid => $effect) {
$edit['effects[' . $uuid . '][weight]'] = $weight;
$weight--;
}
$image_path = $this
->createSampleImage($style);
$this
->assertEqual($this
->getImageCount($style), 1, new FormattableMarkup('Image style %style image %file successfully generated.', [
'%style' => $style
->label(),
'%file' => $image_path,
]));
$this
->drupalPostForm($style_path, $edit, t('Save'));
$style_path = 'admin/config/media/image-styles/manage/' . $style_name;
$this
->drupalGet($style_path);
$this
->assertTitle("Edit style {$style_label} | Drupal");
$option = $this
->xpath('//select[@id=:id]//option', [
':id' => 'edit-new--2',
]);
$this
->assertEquals('Ajax test', $option[1]
->getText(), '"Ajax test" is the first selectable effect.');
$this
->assertEqual($this
->getImageCount($style), 0, new FormattableMarkup('Image style %style was flushed after renaming the style and updating the order of effects.', [
'%style' => $style
->label(),
]));
$style = ImageStyle::load($style_name);
$effect_edits_order = array_reverse($effect_edits_order);
$order_correct = TRUE;
$index = 0;
foreach ($style
->getEffects() as $effect) {
if ($effect_edits_order[$index] != $effect
->getPluginId()) {
$order_correct = FALSE;
}
$index++;
}
$this
->assertTrue($order_correct, 'The order of the effects is correctly set by default.');
$image_path = $this
->createSampleImage($style);
$this
->assertEqual($this
->getImageCount($style), 1, new FormattableMarkup('Image style %style image %file successfully generated.', [
'%style' => $style
->label(),
'%file' => $image_path,
]));
$this
->drupalPostForm($style_path . '/effects/' . $uuids['image_crop'] . '/delete', [], t('Delete'));
$this
->assertSession()
->statusCodeEquals(200);
$image_crop_effect = $style
->getEffect($uuids['image_crop']);
$this
->assertRaw(t('The image effect %name has been deleted.', [
'%name' => $image_crop_effect
->label(),
]));
$this
->assertNoLinkByHref($style_path . '/effects/' . $uuids['image_crop'] . '/delete');
$entity_type_manager = $this->container
->get('entity_type.manager');
$style = $entity_type_manager
->getStorage('image_style')
->loadUnchanged($style
->id());
$this
->assertFalse($style
->getEffects()
->has($uuids['image_crop']), new FormattableMarkup('Effect with ID %uuid no longer found on image style %style', [
'%uuid' => $uuids['image_crop'],
'%style' => $style
->label(),
]));
$edit = [
'data[degrees]' => 5,
'data[random]' => 0,
'data[bgcolor]' => '',
];
$this
->drupalPostForm($style_path, [
'new' => 'image_rotate',
], t('Add'));
$this
->drupalPostForm(NULL, $edit, t('Add effect'));
$entity_type_manager = $this->container
->get('entity_type.manager');
$style = $entity_type_manager
->getStorage('image_style')
->loadUnchanged($style_name);
$this
->assertCount(6, $style
->getEffects(), 'Rotate effect with transparent background was added.');
$this
->drupalPostForm($style_path . '/delete', [], t('Delete'));
$directory = 'public://styles/' . $style_name;
$this
->assertDirectoryNotExists($directory);
$this
->assertNull(ImageStyle::load($style_name), new FormattableMarkup('Image style %style successfully deleted.', [
'%style' => $style
->label(),
]));
foreach (ImageStyle::loadMultiple() as $image_style) {
$image_style
->delete();
}
$this
->drupalGet($admin_path);
$this
->assertRaw(t('There are currently no styles. <a href=":url">Add a new one</a>.', [
':url' => Url::fromRoute('image.style_add')
->toString(),
]));
}
public function testStyleReplacement() {
$style_name = strtolower($this
->randomMachineName(10));
$style_label = $this
->randomString();
$style = ImageStyle::create([
'name' => $style_name,
'label' => $style_label,
]);
$style
->save();
$style_path = 'admin/config/media/image-styles/manage/';
$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();
$test_image = current($this
->drupalGetTestFiles('image'));
$nid = $this
->uploadNodeImage($test_image, $field_name, 'article', $this
->randomMachineName());
$node = Node::load($nid);
$fid = $node
->get($field_name)->target_id;
$original_uri = File::load($fid)
->getFileUri();
$this
->drupalGet('node/' . $nid);
$this
->assertRaw(file_url_transform_relative($style
->buildUrl($original_uri)), new FormattableMarkup('Image displayed using style @style.', [
'@style' => $style_name,
]));
$new_style_name = strtolower($this
->randomMachineName(10));
$new_style_label = $this
->randomString();
$edit = [
'name' => $new_style_name,
'label' => $new_style_label,
];
$this
->drupalPostForm($style_path . $style_name, $edit, t('Save'));
$this
->assertText(t('Changes to the style have been saved.'), new FormattableMarkup('Style %name was renamed to %new_name.', [
'%name' => $style_name,
'%new_name' => $new_style_name,
]));
$this
->drupalGet('node/' . $nid);
$style = ImageStyle::load($new_style_name);
$this
->assertRaw(file_url_transform_relative($style
->buildUrl($original_uri)), 'Image displayed using style replacement style.');
$edit = [
'replacement' => 'thumbnail',
];
$this
->drupalPostForm($style_path . $new_style_name . '/delete', $edit, t('Delete'));
$message = t('The image style %name has been deleted.', [
'%name' => $new_style_label,
]);
$this
->assertRaw($message);
$replacement_style = ImageStyle::load('thumbnail');
$this
->drupalGet('node/' . $nid);
$this
->assertRaw(file_url_transform_relative($replacement_style
->buildUrl($original_uri)), 'Image displayed using style replacement style.');
}
public function testEditEffect() {
$style_name = 'test_style_effect_edit';
$this
->drupalGet('admin/config/media/image-styles/add');
$this
->drupalPostForm(NULL, [
'label' => 'Test style effect edit',
'name' => $style_name,
], t('Create new style'));
$this
->drupalPostForm(NULL, [
'new' => 'image_scale_and_crop',
], t('Add'));
$this
->drupalPostForm(NULL, [
'data[width]' => '300',
'data[height]' => '200',
], t('Add effect'));
$this
->assertText(t('Scale and crop 300×200'));
$this
->clickLink(t('Edit'));
$this
->drupalPostForm(NULL, [
'data[width]' => '360',
'data[height]' => '240',
], t('Update effect'));
$this
->assertText(t('Scale and crop 360×240'));
$this
->assertNoText(t('Scale and crop 300×200'));
$this
->drupalGet('admin/config/media/image-styles/add');
$this
->drupalPostForm(NULL, [
'label' => 'Test style scale edit scale',
'name' => 'test_style_scale_edit_scale',
], t('Create new style'));
$this
->drupalPostForm(NULL, [
'new' => 'image_scale',
], t('Add'));
$this
->drupalPostForm(NULL, [
'data[width]' => '12',
'data[height]' => '19',
], t('Add effect'));
$this
->clickLink(t('Edit'));
$this
->drupalPostForm(NULL, [
'data[width]' => '24',
'data[height]' => '19',
], t('Update effect'));
$this
->drupalGet('admin/config/media/image-styles');
$rows = $this
->xpath('//table/tbody/tr');
$i = 0;
foreach ($rows as $row) {
if ($row
->find('css', 'td')
->getText() === 'Test style scale edit scale') {
$this
->clickLink('Edit', $i);
break;
}
$i++;
}
$this
->drupalPostForm(NULL, [
'new' => 'image_scale',
], t('Add'));
$this
->drupalPostForm(NULL, [
'data[width]' => '12',
'data[height]' => '19',
], t('Add effect'));
$this
->assertText(t('Scale 24×19'));
$this
->assertText(t('Scale 12×19'));
$uuid = $this->container
->get('uuid');
$this
->drupalGet('admin/config/media/image-styles/manage/' . $style_name . '/effects/' . $uuid
->generate());
$this
->assertSession()
->statusCodeEquals(404);
}
public function testFlushUserInterface() {
$admin_path = 'admin/config/media/image-styles';
$style_name = strtolower($this
->randomMachineName(10));
$style = ImageStyle::create([
'name' => $style_name,
'label' => $this
->randomString(),
]);
$style
->save();
$files = $this
->drupalGetTestFiles('image');
$image_uri = $files[0]->uri;
$derivative_uri = $style
->buildUri($image_uri);
$this
->assertTrue($style
->createDerivative($image_uri, $derivative_uri));
$this
->assertEqual($this
->getImageCount($style), 1);
$this
->drupalGet($admin_path);
$flush_path = $admin_path . '/manage/' . $style_name . '/flush';
$this
->assertLinkByHref($flush_path);
$this
->drupalPostForm($flush_path, [], t('Flush'));
$this
->assertEqual($this
->getImageCount($style), 0);
}
public function testConfigImport() {
$style_name = strtolower($this
->randomMachineName(10));
$style_label = $this
->randomString();
$style = ImageStyle::create([
'name' => $style_name,
'label' => $style_label,
]);
$style
->save();
$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();
$test_image = current($this
->drupalGetTestFiles('image'));
$nid = $this
->uploadNodeImage($test_image, $field_name, 'article', $this
->randomMachineName());
$node = Node::load($nid);
$fid = $node
->get($field_name)->target_id;
$original_uri = File::load($fid)
->getFileUri();
$this
->drupalGet('node/' . $nid);
$this
->assertRaw(file_url_transform_relative($style
->buildUrl($original_uri)), new FormattableMarkup('Image displayed using style @style.', [
'@style' => $style_name,
]));
$sync = $this->container
->get('config.storage.sync');
$active = $this->container
->get('config.storage');
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.');
}
public function testImageStyleAccess() {
$style = ImageStyle::create([
'name' => 'style_foo',
'label' => $this
->randomString(),
]);
$style
->save();
$this
->drupalGet('admin/config/media/image-styles');
$this
->clickLink(t('Edit'));
$this
->assertRaw(t('Select a new effect'));
}
}