Update core 8.3.0

This commit is contained in:
Rob Davies 2017-04-13 15:53:35 +01:00
parent da7a7918f8
commit cd7a898e66
6144 changed files with 132297 additions and 87747 deletions

View file

@ -18,12 +18,12 @@ class FileMoveTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('image');
public static $modules = ['image'];
/**
* Tests moving a randomly generated image.
*/
function testNormal() {
public function testNormal() {
// Pick a file for testing.
$file = File::create((array) current($this->drupalGetTestFiles('image')));

View file

@ -19,7 +19,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
/**
* Given an image style, generate an image.
*/
function createSampleImage(ImageStyleInterface $style) {
public function createSampleImage(ImageStyleInterface $style) {
static $file_path;
// First, we need to make sure we have an image in our testing
@ -36,7 +36,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
/**
* Count the number of images currently create for a style.
*/
function getImageCount(ImageStyleInterface $style) {
public function getImageCount(ImageStyleInterface $style) {
return count(file_scan_directory('public://styles/' . $style->id(), '/.*/'));
}
@ -44,66 +44,66 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
* Test creating an image style with a numeric name and ensuring it can be
* applied to an image.
*/
function testNumericStyleName() {
public function testNumericStyleName() {
$style_name = rand();
$style_label = $this->randomString();
$edit = array(
$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.', array('%name' => $style_label)));
$this->assertRaw(t('Style %name was created.', ['%name' => $style_label]));
$options = image_style_options();
$this->assertTrue(array_key_exists($style_name, $options), format_string('Array key %key exists.', array('%key' => $style_name)));
$this->assertTrue(array_key_exists($style_name, $options), format_string('Array key %key exists.', ['%key' => $style_name]));
}
/**
* General test to add a style, add/remove/edit effects to it, then delete it.
*/
function testStyle() {
public function testStyle() {
$admin_path = 'admin/config/media/image-styles';
// Setup a style to be created and effects to add to it.
$style_name = strtolower($this->randomMachineName(10));
$style_label = $this->randomString();
$style_path = $admin_path . '/manage/' . $style_name;
$effect_edits = array(
'image_resize' => array(
$effect_edits = [
'image_resize' => [
'width' => 100,
'height' => 101,
),
'image_scale' => array(
],
'image_scale' => [
'width' => 110,
'height' => 111,
'upscale' => 1,
),
'image_scale_and_crop' => array(
],
'image_scale_and_crop' => [
'width' => 120,
'height' => 121,
),
'image_crop' => array(
],
'image_crop' => [
'width' => 130,
'height' => 131,
'anchor' => 'left-top',
),
'image_desaturate' => array(
],
'image_desaturate' => [
// No options for desaturate.
),
'image_rotate' => array(
],
'image_rotate' => [
'degrees' => 5,
'random' => 1,
'bgcolor' => '#FFFF00',
),
);
],
];
// Add style form.
$edit = array(
$edit = [
'name' => $style_name,
'label' => $style_label,
);
];
$this->drupalPostForm($admin_path . '/add', $edit, t('Create new style'));
$this->assertRaw(t('Style %name was created.', array('%name' => $style_label)));
$this->assertRaw(t('Style %name was created.', ['%name' => $style_label]));
// Ensure that the expected entity operations are there.
$this->drupalGet($admin_path);
@ -115,12 +115,12 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Add each sample effect to the style.
foreach ($effect_edits as $effect => $edit) {
$edit_data = array();
$edit_data = [];
foreach ($edit as $field => $value) {
$edit_data['data[' . $field . ']'] = $value;
}
// Add the effect.
$this->drupalPostForm($style_path, array('new' => $effect), t('Add'));
$this->drupalPostForm($style_path, ['new' => $effect], t('Add'));
if (!empty($edit)) {
$this->drupalPostForm(NULL, $edit_data, t('Add effect'));
}
@ -140,13 +140,13 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Confirm that all effects on the image style have settings that match
// what was saved.
$uuids = array();
$uuids = [];
foreach ($style->getEffects() as $uuid => $effect) {
// Store the uuid for later use.
$uuids[$effect->getPluginId()] = $uuid;
$effect_configuration = $effect->getConfiguration();
foreach ($effect_edits[$effect->getPluginId()] as $field => $value) {
$this->assertEqual($value, $effect_configuration['data'][$field], SafeMarkup::format('The %field field in the %effect effect has the correct value of %value.', array('%field' => $field, '%effect' => $effect->getPluginId(), '%value' => $value)));
$this->assertEqual($value, $effect_configuration['data'][$field], SafeMarkup::format('The %field field in the %effect effect has the correct value of %value.', ['%field' => $field, '%effect' => $effect->getPluginId(), '%value' => $value]));
}
}
@ -154,10 +154,10 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
foreach (array_keys($effect_edits) as $effect_name) {
$this->assertTrue(isset($uuids[$effect_name]), format_string(
'A %effect_name effect was saved with ID %uuid',
array(
[
'%effect_name' => $effect_name,
'%uuid' => $uuids[$effect_name],
)));
]));
}
// Image style overview form (ordering and renaming).
@ -180,10 +180,10 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$style_name = strtolower($this->randomMachineName(10));
$style_label = $this->randomMachineName();
$weight = count($effect_edits);
$edit = array(
$edit = [
'name' => $style_name,
'label' => $style_label,
);
];
foreach ($style->getEffects() as $uuid => $effect) {
$edit['effects[' . $uuid . '][weight]'] = $weight;
$weight--;
@ -191,7 +191,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Create an image to make sure it gets flushed after saving.
$image_path = $this->createSampleImage($style);
$this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style->label(), '%file' => $image_path)));
$this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
$this->drupalPostForm($style_path, $edit, t('Update style'));
@ -200,13 +200,13 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Check that the URL was updated.
$this->drupalGet($style_path);
$this->assertTitle(t('Edit style @name | Drupal', array('@name' => $style_label)));
$this->assertResponse(200, format_string('Image style %original renamed to %new', array('%original' => $style->id(), '%new' => $style_name)));
$this->assertTitle(t('Edit style @name | Drupal', ['@name' => $style_label]));
$this->assertResponse(200, format_string('Image style %original renamed to %new', ['%original' => $style->id(), '%new' => $style_name]));
// Check that the image was flushed after updating the style.
// This is especially important when renaming the style. Make sure that
// the old image directory has been deleted.
$this->assertEqual($this->getImageCount($style), 0, format_string('Image style %style was flushed after renaming the style and updating the order of effects.', array('%style' => $style->label())));
$this->assertEqual($this->getImageCount($style), 0, format_string('Image style %style was flushed after renaming the style and updating the order of effects.', ['%style' => $style->label()]));
// Load the style by the new name with the new weights.
$style = ImageStyle::load($style_name);
@ -227,14 +227,14 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Create an image to make sure it gets flushed after deleting an effect.
$image_path = $this->createSampleImage($style);
$this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style->label(), '%file' => $image_path)));
$this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
// Delete the 'image_crop' effect from the style.
$this->drupalPostForm($style_path . '/effects/' . $uuids['image_crop'] . '/delete', array(), t('Delete'));
$this->drupalPostForm($style_path . '/effects/' . $uuids['image_crop'] . '/delete', [], t('Delete'));
// Confirm that the form submission was successful.
$this->assertResponse(200);
$image_crop_effect = $style->getEffect($uuids['image_crop']);
$this->assertRaw(t('The image effect %name has been deleted.', array('%name' => $image_crop_effect->label())));
$this->assertRaw(t('The image effect %name has been deleted.', ['%name' => $image_crop_effect->label()]));
// Confirm that there is no longer a link to the effect.
$this->assertNoLinkByHref($style_path . '/effects/' . $uuids['image_crop'] . '/delete');
// Refresh the image style information and verify that the effect was
@ -243,18 +243,18 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$style = $entity_type_manager->getStorage('image_style')->loadUnchanged($style->id());
$this->assertFalse($style->getEffects()->has($uuids['image_crop']), format_string(
'Effect with ID %uuid no longer found on image style %style',
array(
[
'%uuid' => $uuids['image_crop'],
'%style' => $style->label(),
)));
]));
// Additional test on Rotate effect, for transparent background.
$edit = array(
$edit = [
'data[degrees]' => 5,
'data[random]' => 0,
'data[bgcolor]' => '',
);
$this->drupalPostForm($style_path, array('new' => 'image_rotate'), t('Add'));
];
$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);
@ -263,13 +263,13 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Style deletion form.
// Delete the style.
$this->drupalPostForm($style_path . '/delete', array(), t('Delete'));
$this->drupalPostForm($style_path . '/delete', [], t('Delete'));
// Confirm the style directory has been removed.
$directory = file_default_scheme() . '://styles/' . $style_name;
$this->assertFalse(is_dir($directory), format_string('Image style %style directory removed on style deletion.', array('%style' => $style->label())));
$this->assertFalse(is_dir($directory), format_string('Image style %style directory removed on style deletion.', ['%style' => $style->label()]));
$this->assertFalse(ImageStyle::load($style_name), format_string('Image style %style successfully deleted.', array('%style' => $style->label())));
$this->assertFalse(ImageStyle::load($style_name), format_string('Image style %style successfully deleted.', ['%style' => $style->label()]));
// Test empty text when there are no image styles.
@ -330,11 +330,11 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
/**
* Test deleting a style and choosing a replacement style.
*/
function testStyleReplacement() {
public function testStyleReplacement() {
// Create a new style.
$style_name = strtolower($this->randomMachineName(10));
$style_label = $this->randomString();
$style = ImageStyle::create(array('name' => $style_name, 'label' => $style_label));
$style = ImageStyle::create(['name' => $style_name, 'label' => $style_label]);
$style->save();
$style_path = 'admin/config/media/image-styles/manage/';
@ -342,10 +342,10 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$field_name = strtolower($this->randomMachineName(10));
$this->createImageField($field_name, 'article');
entity_get_display('node', 'article', 'default')
->setComponent($field_name, array(
->setComponent($field_name, [
'type' => 'image',
'settings' => array('image_style' => $style_name),
))
'settings' => ['image_style' => $style_name],
])
->save();
// Create a new node with an image attached.
@ -359,17 +359,17 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Test that image is displayed using newly created style.
$this->drupalGet('node/' . $nid);
$this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), format_string('Image displayed using style @style.', array('@style' => $style_name)));
$this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), format_string('Image displayed using style @style.', ['@style' => $style_name]));
// Rename the style and make sure the image field is updated.
$new_style_name = strtolower($this->randomMachineName(10));
$new_style_label = $this->randomString();
$edit = array(
$edit = [
'name' => $new_style_name,
'label' => $new_style_label,
);
];
$this->drupalPostForm($style_path . $style_name, $edit, t('Update style'));
$this->assertText(t('Changes to the style have been saved.'), format_string('Style %name was renamed to %new_name.', array('%name' => $style_name, '%new_name' => $new_style_name)));
$this->assertText(t('Changes to the style have been saved.'), format_string('Style %name was renamed to %new_name.', ['%name' => $style_name, '%new_name' => $new_style_name]));
$this->drupalGet('node/' . $nid);
// Reload the image style using the new name.
@ -377,11 +377,11 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), 'Image displayed using style replacement style.');
// Delete the style and choose a replacement style.
$edit = array(
$edit = [
'replacement' => 'thumbnail',
);
];
$this->drupalPostForm($style_path . $new_style_name . '/delete', $edit, t('Delete'));
$message = t('The image style %name has been deleted.', array('%name' => $new_style_label));
$message = t('The image style %name has been deleted.', ['%name' => $new_style_label]);
$this->assertRaw($message);
$replacement_style = ImageStyle::load('thumbnail');
@ -392,18 +392,18 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
/**
* Verifies that editing an image effect does not cause it to be duplicated.
*/
function testEditEffect() {
public function testEditEffect() {
// Add a scale effect.
$style_name = 'test_style_effect_edit';
$this->drupalGet('admin/config/media/image-styles/add');
$this->drupalPostForm(NULL, array('label' => 'Test style effect edit', 'name' => $style_name), t('Create new style'));
$this->drupalPostForm(NULL, array('new' => 'image_scale_and_crop'), t('Add'));
$this->drupalPostForm(NULL, array('data[width]' => '300', 'data[height]' => '200'), t('Add effect'));
$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'));
// There should normally be only one edit link on this page initially.
$this->clickLink(t('Edit'));
$this->drupalPostForm(NULL, array('data[width]' => '360', 'data[height]' => '240'), t('Update effect'));
$this->drupalPostForm(NULL, ['data[width]' => '360', 'data[height]' => '240'], t('Update effect'));
$this->assertText(t('Scale and crop 360×240'));
// Check that the previous effect is replaced.
@ -411,17 +411,17 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Add another scale effect.
$this->drupalGet('admin/config/media/image-styles/add');
$this->drupalPostForm(NULL, array('label' => 'Test style scale edit scale', 'name' => 'test_style_scale_edit_scale'), t('Create new style'));
$this->drupalPostForm(NULL, array('new' => 'image_scale'), t('Add'));
$this->drupalPostForm(NULL, array('data[width]' => '12', 'data[height]' => '19'), t('Add effect'));
$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'));
// Edit the scale effect that was just added.
$this->clickLink(t('Edit'));
$this->drupalPostForm(NULL, array('data[width]' => '24', 'data[height]' => '19'), t('Update effect'));
$this->drupalPostForm(NULL, array('new' => 'image_scale'), t('Add'));
$this->drupalPostForm(NULL, ['data[width]' => '24', 'data[height]' => '19'], t('Update effect'));
$this->drupalPostForm(NULL, ['new' => 'image_scale'], t('Add'));
// Add another scale effect and make sure both exist.
$this->drupalPostForm(NULL, array('data[width]' => '12', 'data[height]' => '19'), t('Add effect'));
$this->drupalPostForm(NULL, ['data[width]' => '12', 'data[height]' => '19'], t('Add effect'));
$this->assertText(t('Scale 24×19'));
$this->assertText(t('Scale 12×19'));
@ -439,7 +439,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Create a new style.
$style_name = strtolower($this->randomMachineName(10));
$style = ImageStyle::create(array('name' => $style_name, 'label' => $this->randomString()));
$style = ImageStyle::create(['name' => $style_name, 'label' => $this->randomString()]);
$style->save();
// Create an image to make sure it gets flushed.
@ -456,7 +456,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$this->assertLinkByHref($flush_path);
// Flush the image style derivatives using the user interface.
$this->drupalPostForm($flush_path, array(), t('Flush'));
$this->drupalPostForm($flush_path, [], t('Flush'));
// The derivative image file should have been deleted.
$this->assertEqual($this->getImageCount($style), 0);
@ -465,21 +465,21 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
/**
* Tests image style configuration import that does a delete.
*/
function testConfigImport() {
public function testConfigImport() {
// Create a new style.
$style_name = strtolower($this->randomMachineName(10));
$style_label = $this->randomString();
$style = ImageStyle::create(array('name' => $style_name, 'label' => $style_label));
$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');
entity_get_display('node', 'article', 'default')
->setComponent($field_name, array(
->setComponent($field_name, [
'type' => 'image',
'settings' => array('image_style' => $style_name),
))
'settings' => ['image_style' => $style_name],
])
->save();
// Create a new node with an image attached.
@ -493,7 +493,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Test that image is displayed using newly created style.
$this->drupalGet('node/' . $nid);
$this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), format_string('Image displayed using style @style.', array('@style' => $style_name)));
$this->assertRaw(file_url_transform_relative($style->buildUrl($original_uri)), format_string('Image displayed using style @style.', ['@style' => $style_name]));
// Copy config to sync, and delete the image style.
$sync = $this->container->get('config.storage.sync');
@ -515,7 +515,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
* Tests access for the image style listing.
*/
public function testImageStyleAccess() {
$style = ImageStyle::create(array('name' => 'style_foo', 'label' => $this->randomString()));
$style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]);
$style->save();
$this->drupalGet('admin/config/media/image-styles');

View file

@ -17,14 +17,14 @@ class ImageDimensionsTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('image', 'image_module_test');
public static $modules = ['image', 'image_module_test'];
protected $profile = 'testing';
/**
* Test styled image dimensions cumulatively.
*/
function testImageDimensions() {
public function testImageDimensions() {
$image_factory = $this->container->get('image.factory');
// Create a working copy of the file.
$files = $this->drupalGetTestFiles('image');
@ -33,33 +33,33 @@ class ImageDimensionsTest extends WebTestBase {
// Create a style.
/** @var $style \Drupal\image\ImageStyleInterface */
$style = ImageStyle::create(array('name' => 'test', 'label' => 'Test'));
$style = ImageStyle::create(['name' => 'test', 'label' => 'Test']);
$style->save();
$generated_uri = 'public://styles/test/public/' . \Drupal::service('file_system')->basename($original_uri);
$url = file_url_transform_relative($style->buildUrl($original_uri));
$variables = array(
$variables = [
'#theme' => 'image_style',
'#style_name' => 'test',
'#uri' => $original_uri,
'#width' => 40,
'#height' => 20,
);
];
// Verify that the original image matches the hard-coded values.
$image_file = $image_factory->get($original_uri);
$this->assertEqual($image_file->getWidth(), $variables['#width']);
$this->assertEqual($image_file->getHeight(), $variables['#height']);
// Scale an image that is wider than it is high.
$effect = array(
$effect = [
'id' => 'image_scale',
'data' => array(
'data' => [
'width' => 120,
'height' => 90,
'upscale' => TRUE,
),
],
'weight' => 0,
);
];
$style->addImageEffect($effect);
$style->save();
@ -73,14 +73,14 @@ class ImageDimensionsTest extends WebTestBase {
$this->assertEqual($image_file->getHeight(), 60);
// Rotate 90 degrees anticlockwise.
$effect = array(
$effect = [
'id' => 'image_rotate',
'data' => array(
'data' => [
'degrees' => -90,
'random' => FALSE,
),
],
'weight' => 1,
);
];
$style->addImageEffect($effect);
$style->save();
@ -94,15 +94,15 @@ class ImageDimensionsTest extends WebTestBase {
$this->assertEqual($image_file->getHeight(), 120);
// Scale an image that is higher than it is wide (rotated by previous effect).
$effect = array(
$effect = [
'id' => 'image_scale',
'data' => array(
'data' => [
'width' => 120,
'height' => 90,
'upscale' => TRUE,
),
],
'weight' => 2,
);
];
$style->addImageEffect($effect);
$style->save();
@ -116,15 +116,15 @@ class ImageDimensionsTest extends WebTestBase {
$this->assertEqual($image_file->getHeight(), 90);
// Test upscale disabled.
$effect = array(
$effect = [
'id' => 'image_scale',
'data' => array(
'data' => [
'width' => 400,
'height' => 200,
'upscale' => FALSE,
),
],
'weight' => 3,
);
];
$style->addImageEffect($effect);
$style->save();
@ -138,11 +138,11 @@ class ImageDimensionsTest extends WebTestBase {
$this->assertEqual($image_file->getHeight(), 90);
// Add a desaturate effect.
$effect = array(
$effect = [
'id' => 'image_desaturate',
'data' => array(),
'data' => [],
'weight' => 4,
);
];
$style->addImageEffect($effect);
$style->save();
@ -156,14 +156,14 @@ class ImageDimensionsTest extends WebTestBase {
$this->assertEqual($image_file->getHeight(), 90);
// Add a random rotate effect.
$effect = array(
$effect = [
'id' => 'image_rotate',
'data' => array(
'data' => [
'degrees' => 180,
'random' => TRUE,
),
],
'weight' => 5,
);
];
$style->addImageEffect($effect);
$style->save();
@ -175,15 +175,15 @@ class ImageDimensionsTest extends WebTestBase {
// Add a crop effect.
$effect = array(
$effect = [
'id' => 'image_crop',
'data' => array(
'data' => [
'width' => 30,
'height' => 30,
'anchor' => 'center-center',
),
],
'weight' => 6,
);
];
$style->addImageEffect($effect);
$style->save();
@ -197,14 +197,14 @@ class ImageDimensionsTest extends WebTestBase {
$this->assertEqual($image_file->getHeight(), 30);
// Rotate to a non-multiple of 90 degrees.
$effect = array(
$effect = [
'id' => 'image_rotate',
'data' => array(
'data' => [
'degrees' => 57,
'random' => FALSE,
),
],
'weight' => 7,
);
];
$effect_id = $style->addImageEffect($effect);
$style->save();
@ -221,11 +221,11 @@ class ImageDimensionsTest extends WebTestBase {
$style->deleteImageEffect($effect_plugin);
// Ensure that an effect can unset dimensions.
$effect = array(
$effect = [
'id' => 'image_module_test_null',
'data' => array(),
'data' => [],
'weight' => 8,
);
];
$style->addImageEffect($effect);
$style->save();

View file

@ -1,202 +0,0 @@
<?php
namespace Drupal\image\Tests;
use Drupal\image\Entity\ImageStyle;
use Drupal\system\Tests\Image\ToolkitTestBase;
/**
* Tests that the image effects pass parameters to the toolkit correctly.
*
* @group image
*/
class ImageEffectsTest extends ToolkitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('image', 'image_test', 'image_module_test');
/**
* The image effect manager.
*
* @var \Drupal\image\ImageEffectManager
*/
protected $manager;
protected function setUp() {
parent::setUp();
$this->manager = $this->container->get('plugin.manager.image.effect');
}
/**
* Test the image_resize_effect() function.
*/
function testResizeEffect() {
$this->assertImageEffect('image_resize', array(
'width' => 1,
'height' => 2,
));
$this->assertToolkitOperationsCalled(array('resize'));
// Check the parameters.
$calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly');
$this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly');
}
/**
* Test the image_scale_effect() function.
*/
function testScaleEffect() {
// @todo: need to test upscaling.
$this->assertImageEffect('image_scale', array(
'width' => 10,
'height' => 10,
));
$this->assertToolkitOperationsCalled(array('scale'));
// Check the parameters.
$calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly');
$this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly');
}
/**
* Test the image_crop_effect() function.
*/
function testCropEffect() {
// @todo should test the keyword offsets.
$this->assertImageEffect('image_crop', array(
'anchor' => 'top-1',
'width' => 3,
'height' => 4,
));
$this->assertToolkitOperationsCalled(array('crop'));
// Check the parameters.
$calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['crop'][0][0], 0, 'X was passed correctly');
$this->assertEqual($calls['crop'][0][1], 1, 'Y was passed correctly');
$this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly');
$this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly');
}
/**
* Tests the ConvertImageEffect plugin.
*/
function testConvertEffect() {
// Test jpeg.
$this->assertImageEffect('image_convert', array(
'extension' => 'jpeg',
));
$this->assertToolkitOperationsCalled(array('convert'));
// Check the parameters.
$calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['convert'][0][0], 'jpeg', 'Extension was passed correctly');
}
/**
* Test the image_scale_and_crop_effect() function.
*/
function testScaleAndCropEffect() {
$this->assertImageEffect('image_scale_and_crop', array(
'width' => 5,
'height' => 10,
));
$this->assertToolkitOperationsCalled(array('scale_and_crop'));
// Check the parameters.
$calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['scale_and_crop'][0][0], 5, 'Width was computed and passed correctly');
$this->assertEqual($calls['scale_and_crop'][0][1], 10, 'Height was computed and passed correctly');
}
/**
* Test the image_desaturate_effect() function.
*/
function testDesaturateEffect() {
$this->assertImageEffect('image_desaturate', array());
$this->assertToolkitOperationsCalled(array('desaturate'));
// Check the parameters.
$calls = $this->imageTestGetAllCalls();
$this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.');
}
/**
* Test the image_rotate_effect() function.
*/
function testRotateEffect() {
// @todo: need to test with 'random' => TRUE
$this->assertImageEffect('image_rotate', array(
'degrees' => 90,
'bgcolor' => '#fff',
));
$this->assertToolkitOperationsCalled(array('rotate'));
// Check the parameters.
$calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly');
$this->assertEqual($calls['rotate'][0][1], '#fff', 'Background color was passed correctly');
}
/**
* Test image effect caching.
*/
function testImageEffectsCaching() {
$image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter');
// First call should grab a fresh copy of the data.
$manager = $this->container->get('plugin.manager.image.effect');
$effects = $manager->getDefinitions();
$this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.');
// Second call should come from cache.
drupal_static_reset('image_module_test_image_effect_info_alter');
$cached_effects = $manager->getDefinitions();
$this->assertTrue($image_effect_definitions_called === 0, 'image_effect_definitions() returned data from cache.');
$this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
}
/**
* Tests if validation errors are passed plugin form to the parent form.
*/
public function testEffectFormValidationErrors() {
$account = $this->drupalCreateUser(['administer image styles']);
$this->drupalLogin($account);
/** @var \Drupal\image\ImageStyleInterface $style */
$style = ImageStyle::load('thumbnail');
// Image Scale is the only effect shipped with 'thumbnail', by default.
$uuids = $style->getEffects()->getInstanceIds();
$uuid = key($uuids);
// We are posting the form with both, width and height, empty.
$edit = ['data[width]' => '', 'data[height]' => ''];
$path = 'admin/config/media/image-styles/manage/thumbnail/effects/' . $uuid;
$this->drupalPostForm($path, $edit, t('Update effect'));
// Check that the error message has been displayed.
$this->assertText(t('Width and height can not both be blank.'));
}
/**
* Asserts the effect processing of an image effect plugin.
*
* @param string $effect_name
* The name of the image effect to test.
* @param array $data
* The data to pass to the image effect.
*
* @return bool
* TRUE if the assertion succeeded, FALSE otherwise.
*/
protected function assertImageEffect($effect_name, array $data) {
$effect = $this->manager->createInstance($effect_name, array('data' => $data));
return $this->assertTrue($effect->applyEffect($this->image), 'Function returned the expected value.');
}
}

View file

@ -19,7 +19,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
*
* @var array
*/
public static $modules = array('field_ui');
public static $modules = ['field_ui'];
/**
* Tests CRUD for fields and fields fields with default images.
@ -36,8 +36,8 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$file = File::create(['uri' => $desired_filepath, 'filename' => $filename, 'name' => $filename]);
$file->save();
}
$default_images = array();
foreach (array('field', 'field', 'field2', 'field_new', 'field_new') as $image_target) {
$default_images = [];
foreach (['field', 'field', 'field2', 'field_new', 'field_new'] as $image_target) {
$file = File::create((array) array_pop($files));
$file->save();
$default_images[$image_target] = $file;
@ -45,23 +45,23 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
// Create an image field and add an field to the article content type.
$field_name = strtolower($this->randomMachineName());
$storage_settings['default_image'] = array(
$storage_settings['default_image'] = [
'uuid' => $default_images['field']->uuid(),
'alt' => '',
'title' => '',
'width' => 0,
'height' => 0,
);
$field_settings['default_image'] = array(
];
$field_settings['default_image'] = [
'uuid' => $default_images['field']->uuid(),
'alt' => '',
'title' => '',
'width' => 0,
'height' => 0,
);
$widget_settings = array(
];
$widget_settings = [
'preview_image_style' => 'medium',
);
];
$field = $this->createImageField($field_name, 'article', $storage_settings, $field_settings, $widget_settings);
// The field default image id should be 2.
@ -84,15 +84,15 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
'bundle' => 'page',
'label' => $field->label(),
'required' => $field->isRequired(),
'settings' => array(
'default_image' => array(
'settings' => [
'default_image' => [
'uuid' => $default_images['field2']->uuid(),
'alt' => '',
'title' => '',
'width' => 0,
'height' => 0,
),
),
],
],
]);
$field2->save();
@ -112,7 +112,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field']->id(),
format_string(
'Article image field default equals expected file ID of @fid.',
array('@fid' => $default_images['field']->id())
['@fid' => $default_images['field']->id()]
)
);
// Confirm the defaults are present on the article field edit form.
@ -122,7 +122,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field']->id(),
format_string(
'Article image field field default equals expected file ID of @fid.',
array('@fid' => $default_images['field']->id())
['@fid' => $default_images['field']->id()]
)
);
@ -133,7 +133,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field']->id(),
format_string(
'Page image field default equals expected file ID of @fid.',
array('@fid' => $default_images['field']->id())
['@fid' => $default_images['field']->id()]
)
);
// Confirm the defaults are present on the page field edit form.
@ -144,38 +144,38 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field2']->id(),
format_string(
'Page image field field default equals expected file ID of @fid.',
array('@fid' => $default_images['field2']->id())
['@fid' => $default_images['field2']->id()]
)
);
// Confirm that the image default is shown for a new article node.
$article = $this->drupalCreateNode(array('type' => 'article'));
$article = $this->drupalCreateNode(['type' => 'article']);
$article_built = $this->drupalBuildEntityView($article);
$this->assertEqual(
$article_built[$field_name][0]['#item']->target_id,
$default_images['field']->id(),
format_string(
'A new article node without an image has the expected default image file ID of @fid.',
array('@fid' => $default_images['field']->id())
['@fid' => $default_images['field']->id()]
)
);
// Also check that the field renders without warnings when the label is
// hidden.
EntityViewDisplay::load('node.article.default')
->setComponent($field_name, array('label' => 'hidden', 'type' => 'image'))
->setComponent($field_name, ['label' => 'hidden', 'type' => 'image'])
->save();
$this->drupalGet('node/' . $article->id());
// Confirm that the image default is shown for a new page node.
$page = $this->drupalCreateNode(array('type' => 'page'));
$page = $this->drupalCreateNode(['type' => 'page']);
$page_built = $this->drupalBuildEntityView($page);
$this->assertEqual(
$page_built[$field_name][0]['#item']->target_id,
$default_images['field2']->id(),
format_string(
'A new page node without an image has the expected default image file ID of @fid.',
array('@fid' => $default_images['field2']->id())
['@fid' => $default_images['field2']->id()]
)
);
@ -192,12 +192,12 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field_new']->id(),
format_string(
'Updated image field default equals expected file ID of @fid.',
array('@fid' => $default_images['field_new']->id())
['@fid' => $default_images['field_new']->id()]
)
);
// Reload the nodes and confirm the field field defaults are used.
$node_storage->resetCache(array($article->id(), $page->id()));
$node_storage->resetCache([$article->id(), $page->id()]);
$article_built = $this->drupalBuildEntityView($article = $node_storage->load($article->id()));
$page_built = $this->drupalBuildEntityView($page = $node_storage->load($page->id()));
$this->assertEqual(
@ -205,7 +205,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field']->id(),
format_string(
'An existing article node without an image has the expected default image file ID of @fid.',
array('@fid' => $default_images['field']->id())
['@fid' => $default_images['field']->id()]
)
);
$this->assertEqual(
@ -213,7 +213,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field2']->id(),
format_string(
'An existing page node without an image has the expected default image file ID of @fid.',
array('@fid' => $default_images['field2']->id())
['@fid' => $default_images['field2']->id()]
)
);
@ -231,12 +231,12 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field_new']->id(),
format_string(
'Updated article image field field default equals expected file ID of @fid.',
array('@fid' => $default_images['field_new']->id())
['@fid' => $default_images['field_new']->id()]
)
);
// Reload the nodes.
$node_storage->resetCache(array($article->id(), $page->id()));
$node_storage->resetCache([$article->id(), $page->id()]);
$article_built = $this->drupalBuildEntityView($article = $node_storage->load($article->id()));
$page_built = $this->drupalBuildEntityView($page = $node_storage->load($page->id()));
@ -246,7 +246,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field_new']->id(),
format_string(
'An existing article node without an image has the expected default image file ID of @fid.',
array('@fid' => $default_images['field_new']->id())
['@fid' => $default_images['field_new']->id()]
)
);
// Confirm the page remains unchanged.
@ -255,7 +255,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field2']->id(),
format_string(
'An existing page node without an image has the expected default image file ID of @fid.',
array('@fid' => $default_images['field2']->id())
['@fid' => $default_images['field2']->id()]
)
);
@ -279,7 +279,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
);
// Reload the nodes.
$node_storage->resetCache(array($article->id(), $page->id()));
$node_storage->resetCache([$article->id(), $page->id()]);
$article_built = $this->drupalBuildEntityView($article = $node_storage->load($article->id()));
$page_built = $this->drupalBuildEntityView($page = $node_storage->load($page->id()));
// Confirm the article uses the new field (not field) default.
@ -288,7 +288,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field_new']->id(),
format_string(
'An existing article node without an image has the expected default image file ID of @fid.',
array('@fid' => $default_images['field_new']->id())
['@fid' => $default_images['field_new']->id()]
)
);
// Confirm the page remains unchanged.
@ -297,12 +297,12 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$default_images['field2']->id(),
format_string(
'An existing page node without an image has the expected default image file ID of @fid.',
array('@fid' => $default_images['field2']->id())
['@fid' => $default_images['field2']->id()]
)
);
$non_image = $this->drupalGetTestFiles('text');
$this->drupalPostForm(NULL, array('files[settings_default_image_uuid]' => drupal_realpath($non_image[0]->uri)), t("Upload"));
$this->drupalPostForm(NULL, ['files[settings_default_image_uuid]' => drupal_realpath($non_image[0]->uri)], t("Upload"));
$this->assertText('The specified file text-0.txt could not be uploaded.');
$this->assertText('Only files with the following extensions are allowed: png gif jpg jpeg.');
@ -316,16 +316,16 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
* Tests image field and field having an invalid default image.
*/
public function testInvalidDefaultImage() {
$field_storage = FieldStorageConfig::create(array(
$field_storage = FieldStorageConfig::create([
'field_name' => Unicode::strtolower($this->randomMachineName()),
'entity_type' => 'node',
'type' => 'image',
'settings' => array(
'default_image' => array(
'settings' => [
'default_image' => [
'uuid' => 100000,
)
),
));
]
],
]);
$field_storage->save();
$settings = $field_storage->getSettings();
// The non-existent default image should not be saved.
@ -335,11 +335,11 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
'field_storage' => $field_storage,
'bundle' => 'page',
'label' => $this->randomMachineName(),
'settings' => array(
'default_image' => array(
'settings' => [
'default_image' => [
'uuid' => 100000,
)
),
]
],
]);
$field->save();
$settings = $field->getSettings();

View file

@ -21,55 +21,55 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
*
* @var array
*/
public static $modules = array('field_ui');
public static $modules = ['field_ui'];
/**
* Test image formatters on node display for public files.
*/
function testImageFieldFormattersPublic() {
public function testImageFieldFormattersPublic() {
$this->_testImageFieldFormatters('public');
}
/**
* Test image formatters on node display for private files.
*/
function testImageFieldFormattersPrivate() {
public function testImageFieldFormattersPrivate() {
// Remove access content permission from anonymous users.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access content' => FALSE));
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, ['access content' => FALSE]);
$this->_testImageFieldFormatters('private');
}
/**
* Test image formatters on node display.
*/
function _testImageFieldFormatters($scheme) {
public function _testImageFieldFormatters($scheme) {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$field_name = strtolower($this->randomMachineName());
$field_settings = array('alt_field_required' => 0);
$instance = $this->createImageField($field_name, 'article', array('uri_scheme' => $scheme), $field_settings);
$field_settings = ['alt_field_required' => 0];
$instance = $this->createImageField($field_name, 'article', ['uri_scheme' => $scheme], $field_settings);
// Go to manage display page.
$this->drupalGet("admin/structure/types/manage/article/display");
// Test for existence of link to image styles configuration.
$this->drupalPostAjaxForm(NULL, array(), "{$field_name}_settings_edit");
$this->drupalPostAjaxForm(NULL, [], "{$field_name}_settings_edit");
$this->assertLinkByHref(\Drupal::url('entity.image_style.collection'), 0, 'Link to image styles configuration is found');
// Remove 'administer image styles' permission from testing admin user.
$admin_user_roles = $this->adminUser->getRoles(TRUE);
user_role_change_permissions(reset($admin_user_roles), array('administer image styles' => FALSE));
user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => FALSE]);
// Go to manage display page again.
$this->drupalGet("admin/structure/types/manage/article/display");
// Test for absence of link to image styles configuration.
$this->drupalPostAjaxForm(NULL, array(), "{$field_name}_settings_edit");
$this->drupalPostAjaxForm(NULL, [], "{$field_name}_settings_edit");
$this->assertNoLinkByHref(\Drupal::url('entity.image_style.collection'), 'Link to image styles configuration is absent when permissions are insufficient');
// Restore 'administer image styles' permission to testing admin user
user_role_change_permissions(reset($admin_user_roles), array('administer image styles' => TRUE));
user_role_change_permissions(reset($admin_user_roles), ['administer image styles' => TRUE]);
// Create a new node with an image attached.
$test_image = current($this->drupalGetTestFiles('image'));
@ -87,38 +87,38 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Save node.
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
$node_storage->resetCache(array($nid));
$node_storage->resetCache([$nid]);
$node = $node_storage->load($nid);
// Test that the default formatter is being used.
$file = $node->{$field_name}->entity;
$image_uri = $file->getFileUri();
$image = array(
$image = [
'#theme' => 'image',
'#uri' => $image_uri,
'#width' => 40,
'#height' => 20,
'#alt' => $alt,
);
];
$default_output = str_replace("\n", NULL, $renderer->renderRoot($image));
$this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
// Test the image linked to file formatter.
$display_options = array(
$display_options = [
'type' => 'image',
'settings' => array('image_link' => 'file'),
);
'settings' => ['image_link' => 'file'],
];
$display = entity_get_display('node', $node->getType(), 'default');
$display->setComponent($field_name, $display_options)
->save();
$image = array(
$image = [
'#theme' => 'image',
'#uri' => $image_uri,
'#width' => 40,
'#height' => 20,
'#alt' => $alt,
);
];
$default_output = '<a href="' . file_create_url($image_uri) . '">' . $renderer->renderRoot($image) . '</a>';
$this->drupalGet('node/' . $nid);
$this->assertCacheTag($file->getCacheTags()[0]);
@ -148,25 +148,25 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$display_options['settings']['image_link'] = 'content';
$display->setComponent($field_name, $display_options)
->save();
$image = array(
$image = [
'#theme' => 'image',
'#uri' => $image_uri,
'#width' => 40,
'#height' => 20,
);
];
$this->drupalGet('node/' . $nid);
$this->assertCacheTag($file->getCacheTags()[0]);
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$elements = $this->xpath(
'//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]',
array(
[
':path' => $node->url(),
':url' => file_url_transform_relative(file_create_url($image['#uri'])),
':width' => $image['#width'],
':height' => $image['#height'],
':alt' => $alt,
)
]
);
$this->assertEqual(count($elements), 1, 'Image linked to content formatter displaying correctly on full node view.');
@ -179,14 +179,14 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Ensure the derivative image is generated so we do not have to deal with
// image style callback paths.
$this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri));
$image_style = array(
$image_style = [
'#theme' => 'image_style',
'#uri' => $image_uri,
'#width' => 40,
'#height' => 20,
'#style_name' => 'thumbnail',
'#alt' => $alt,
);
];
$default_output = $renderer->renderRoot($image_style);
$this->drupalGet('node/' . $nid);
$image_style = ImageStyle::load('thumbnail');
@ -199,30 +199,43 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri));
$this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
}
// Test the image URL formatter without an image style.
$display_options = [
'type' => 'image_url',
'settings' => ['image_style' => ''],
];
$expected_url = file_url_transform_relative(file_create_url($image_uri));
$this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']);
// Test the image URL formatter with an image style.
$display_options['settings']['image_style'] = 'thumbnail';
$expected_url = file_url_transform_relative(ImageStyle::load('thumbnail')->buildUrl($image_uri));
$this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']);
}
/**
* Tests for image field settings.
*/
function testImageFieldSettings() {
public function testImageFieldSettings() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$test_image = current($this->drupalGetTestFiles('image'));
list(, $test_image_extension) = explode('.', $test_image->filename);
$field_name = strtolower($this->randomMachineName());
$field_settings = array(
$field_settings = [
'alt_field' => 1,
'file_extensions' => $test_image_extension,
'max_filesize' => '50 KB',
'max_resolution' => '100x100',
'min_resolution' => '10x10',
'title_field' => 1,
);
$widget_settings = array(
];
$widget_settings = [
'preview_image_style' => 'medium',
);
$field = $this->createImageField($field_name, 'article', array(), $field_settings, $widget_settings);
];
$field = $this->createImageField($field_name, 'article', [], $field_settings, $widget_settings);
// Verify that the min/max resolution set on the field are properly
// extracted, and displayed, on the image field's configuration form.
@ -234,7 +247,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$this->drupalGet('node/add/article');
$this->assertText(t('50 KB limit.'), 'Image widget max file size is displayed on article form.');
$this->assertText(t('Allowed types: @extensions.', array('@extensions' => $test_image_extension)), 'Image widget allowed file types displayed on article form.');
$this->assertText(t('Allowed types: @extensions.', ['@extensions' => $test_image_extension]), 'Image widget allowed file types displayed on article form.');
$this->assertText(t('Images must be larger than 10x10 pixels. Images larger than 100x100 pixels will be resized.'), 'Image widget allowed resolution displayed on article form.');
// We have to create the article first and then edit it because the alt
@ -249,7 +262,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$this->assertFieldByName($field_name . '[0][title]', '', 'Title field displayed on article form.');
// Verify that the attached image is being previewed using the 'medium'
// style.
$node_storage->resetCache(array($nid));
$node_storage->resetCache([$nid]);
$node = $node_storage->load($nid);
$file = $node->{$field_name}->entity;
@ -257,38 +270,38 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$this->assertTrue($this->cssSelect('img[width=40][height=20][class=image-style-medium][src="' . $url . '"]'));
// Add alt/title fields to the image and verify that they are displayed.
$image = array(
$image = [
'#theme' => 'image',
'#uri' => $file->getFileUri(),
'#alt' => $alt,
'#title' => $this->randomMachineName(),
'#width' => 40,
'#height' => 20,
);
$edit = array(
];
$edit = [
$field_name . '[0][alt]' => $image['#alt'],
$field_name . '[0][title]' => $image['#title'],
);
];
$this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published'));
$default_output = str_replace("\n", NULL, $renderer->renderRoot($image));
$this->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.');
// Verify that alt/title longer than allowed results in a validation error.
$test_size = 2000;
$edit = array(
$edit = [
$field_name . '[0][alt]' => $this->randomMachineName($test_size),
$field_name . '[0][title]' => $this->randomMachineName($test_size),
);
];
$this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published'));
$schema = $field->getFieldStorageDefinition()->getSchema();
$this->assertRaw(t('Alternative text cannot be longer than %max characters but is currently %length characters long.', array(
$this->assertRaw(t('Alternative text cannot be longer than %max characters but is currently %length characters long.', [
'%max' => $schema['columns']['alt']['length'],
'%length' => $test_size,
)));
$this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', array(
]));
$this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', [
'%max' => $schema['columns']['title']['length'],
'%length' => $test_size,
)));
]));
// Set cardinality to unlimited and add upload a second image.
// The image widget is extending on the file widget, but the image field
@ -297,20 +310,20 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// 1, so we need to make sure the file widget prevents these notices by
// providing all settings, even if they are not used.
// @see FileWidget::formMultipleElements().
$this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name . '/storage', array('cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED), t('Save field settings'));
$edit = array(
$this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name . '/storage', ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED], t('Save field settings'));
$edit = [
'files[' . $field_name . '_1][]' => drupal_realpath($test_image->uri),
);
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
// Add the required alt text.
$this->drupalPostForm(NULL, [$field_name . '[1][alt]' => $alt], t('Save and keep published'));
$this->assertText(format_string('Article @title has been updated.', array('@title' => $node->getTitle())));
$this->assertText(format_string('Article @title has been updated.', ['@title' => $node->getTitle()]));
// Assert ImageWidget::process() calls FieldWidget::process().
$this->drupalGet('node/' . $node->id() . '/edit');
$edit = array(
$edit = [
'files[' . $field_name . '_2][]' => drupal_realpath($test_image->uri),
);
];
$this->drupalPostAjaxForm(NULL, $edit, $field_name . '_2_upload_button');
$this->assertNoRaw('<input multiple type="file" id="edit-' . strtr($field_name, '_', '-') . '-2-upload" name="files[' . $field_name . '_2][]" size="22" class="js-form-file form-file">');
$this->assertRaw('<input multiple type="file" id="edit-' . strtr($field_name, '_', '-') . '-3-upload" name="files[' . $field_name . '_3][]" size="22" class="js-form-file form-file">');
@ -319,7 +332,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
/**
* Test use of a default image with an image field.
*/
function testImageFieldDefaultImage() {
public function testImageFieldDefaultImage() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
@ -330,7 +343,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Create a new node, with no images and verify that no images are
// displayed.
$node = $this->drupalCreateNode(array('type' => 'article'));
$node = $this->drupalCreateNode(['type' => 'article']);
$this->drupalGet('node/' . $node->id());
// Verify that no image is displayed on the page by checking for the class
// that would be used on the image field.
@ -342,12 +355,12 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$images = $this->drupalGetTestFiles('image');
$alt = $this->randomString(512);
$title = $this->randomString(1024);
$edit = array(
$edit = [
// Get the path of the 'image-test.png' file.
'files[settings_default_image_uuid]' => drupal_realpath($images[0]->uri),
'settings[default_image][alt]' => $alt,
'settings[default_image][title]' => $title,
);
];
$this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/storage", $edit, t('Save field settings'));
// Clear field definition cache so the new default image is detected.
\Drupal::entityManager()->clearCachedFieldDefinitions();
@ -355,14 +368,14 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$default_image = $field_storage->getSetting('default_image');
$file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']);
$this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
$image = array(
$image = [
'#theme' => 'image',
'#uri' => $file->getFileUri(),
'#alt' => $alt,
'#title' => $title,
'#width' => 40,
'#height' => 20,
);
];
$default_output = str_replace("\n", NULL, $renderer->renderRoot($image));
$this->drupalGet('node/' . $node->id());
$this->assertCacheTag($file->getCacheTags()[0]);
@ -378,16 +391,16 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Upload the 'image-test.gif' file.
$nid = $this->uploadNodeImage($images[2], $field_name, 'article', $alt);
$node_storage->resetCache(array($nid));
$node_storage->resetCache([$nid]);
$node = $node_storage->load($nid);
$file = $node->{$field_name}->entity;
$image = array(
$image = [
'#theme' => 'image',
'#uri' => $file->getFileUri(),
'#width' => 40,
'#height' => 20,
'#alt' => $alt,
);
];
$image_output = str_replace("\n", NULL, $renderer->renderRoot($image));
$this->drupalGet('node/' . $nid);
$this->assertCacheTag($file->getCacheTags()[0]);
@ -397,9 +410,9 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$this->assertRaw($image_output, 'User supplied image is displayed.');
// Remove default image from the field and make sure it is no longer used.
$edit = array(
$edit = [
'settings[default_image][uuid][fids]' => 0,
);
];
$this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name/storage", $edit, t('Save field settings'));
// Clear field definition cache so the new default image is detected.
\Drupal::entityManager()->clearCachedFieldDefinitions();
@ -409,14 +422,14 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Create an image field that uses the private:// scheme and test that the
// default image works as expected.
$private_field_name = strtolower($this->randomMachineName());
$this->createImageField($private_field_name, 'article', array('uri_scheme' => 'private'));
$this->createImageField($private_field_name, 'article', ['uri_scheme' => 'private']);
// Add a default image to the new field.
$edit = array(
$edit = [
// Get the path of the 'image-test.gif' file.
'files[settings_default_image_uuid]' => drupal_realpath($images[2]->uri),
'settings[default_image][alt]' => $alt,
'settings[default_image][title]' => $title,
);
];
$this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $private_field_name . '/storage', $edit, t('Save field settings'));
// Clear field definition cache so the new default image is detected.
\Drupal::entityManager()->clearCachedFieldDefinitions();
@ -428,15 +441,15 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$this->assertTrue($file->isPermanent(), 'The default image status is permanent.');
// Create a new node with no image attached and ensure that default private
// image is displayed.
$node = $this->drupalCreateNode(array('type' => 'article'));
$image = array(
$node = $this->drupalCreateNode(['type' => 'article']);
$image = [
'#theme' => 'image',
'#uri' => $file->getFileUri(),
'#alt' => $alt,
'#title' => $title,
'#width' => 40,
'#height' => 20,
);
];
$default_output = str_replace("\n", NULL, $renderer->renderRoot($image));
$this->drupalGet('node/' . $node->id());
$this->assertCacheTag($file->getCacheTags()[0]);

View file

@ -20,6 +20,9 @@ use Drupal\simpletest\WebTestBase;
/**
* This class provides methods specifically for testing Image's field handling.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use \Drupal\Tests\image\Functional\ImageFieldTestBase instead.
*/
abstract class ImageFieldTestBase extends WebTestBase {
@ -30,7 +33,7 @@ abstract class ImageFieldTestBase extends WebTestBase {
*
* @var array
*/
public static $modules = array('node', 'image', 'field_ui', 'image_module_test');
public static $modules = ['node', 'image', 'field_ui', 'image_module_test'];
/**
* An user with permissions to administer content types and image styles.
@ -44,11 +47,11 @@ abstract class ImageFieldTestBase extends WebTestBase {
// Create Basic page and Article node types.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
}
$this->adminUser = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer node fields', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles', 'administer node display'));
$this->adminUser = $this->drupalCreateUser(['access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer node fields', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles', 'administer node display']);
$this->drupalLogin($this->adminUser);
}
@ -62,10 +65,10 @@ abstract class ImageFieldTestBase extends WebTestBase {
* @param string $type
* The type of node to create.
*/
function previewNodeImage($image, $field_name, $type) {
$edit = array(
public function previewNodeImage($image, $field_name, $type) {
$edit = [
'title[0][value]' => $this->randomMachineName(),
);
];
$edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('node/add/' . $type, $edit, t('Preview'));
}
@ -82,10 +85,10 @@ abstract class ImageFieldTestBase extends WebTestBase {
* @param $alt
* The alt text for the image. Use if the field settings require alt text.
*/
function uploadNodeImage($image, $field_name, $type, $alt = '') {
$edit = array(
public function uploadNodeImage($image, $field_name, $type, $alt = '') {
$edit = [
'title[0][value]' => $this->randomMachineName(),
);
];
$edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('node/add/' . $type, $edit, t('Save and publish'));
if ($alt) {
@ -94,7 +97,7 @@ abstract class ImageFieldTestBase extends WebTestBase {
}
// Retrieve ID of the newly created node from the current URL.
$matches = array();
$matches = [];
preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
return isset($matches[1]) ? $matches[1] : FALSE;
}

View file

@ -11,7 +11,7 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
/**
* Test min/max resolution settings.
*/
function testResolution() {
public function testResolution() {
$field_names = [
0 => strtolower($this->randomMachineName()),
1 => strtolower($this->randomMachineName()),
@ -85,16 +85,16 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
/**
* Test that required alt/title fields gets validated right.
*/
function testRequiredAttributes() {
public function testRequiredAttributes() {
$field_name = strtolower($this->randomMachineName());
$field_settings = array(
$field_settings = [
'alt_field' => 1,
'alt_field_required' => 1,
'title_field' => 1,
'title_field_required' => 1,
'required' => 1,
);
$instance = $this->createImageField($field_name, 'article', array(), $field_settings);
];
$instance = $this->createImageField($field_name, 'article', [], $field_settings);
$images = $this->drupalGetTestFiles('image');
// Let's just use the first image.
$image = $images[0];
@ -116,9 +116,9 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
$instance->setSetting('title_field_required', 0);
$instance->save();
$edit = array(
$edit = [
'title[0][value]' => $this->randomMachineName(),
);
];
$this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
$this->assertNoText(t('Alternative text field is required.'));
@ -129,9 +129,9 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
$instance->setSetting('title_field_required', 1);
$instance->save();
$edit = array(
$edit = [
'title[0][value]' => $this->randomMachineName(),
);
];
$this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
$this->assertNoText(t('Alternative text field is required.'));

View file

@ -1,31 +0,0 @@
<?php
namespace Drupal\image\Tests;
/**
* Tests the image field widget.
*
* @group image
*/
class ImageFieldWidgetTest extends ImageFieldTestBase {
/**
* Tests file widget element.
*/
public function testWidgetElement() {
// Check for image widget in add/node/article page
$field_name = strtolower($this->randomMachineName());
$min_resolution = 50;
$max_resolution = 100;
$field_settings = array(
'max_resolution' => $max_resolution . 'x' . $max_resolution,
'min_resolution' => $min_resolution . 'x' . $min_resolution,
'alt_field' => 0,
);
$this->createImageField($field_name, 'article', array(), $field_settings, array(), array(), 'Image test on [site:name]');
$this->drupalGet('node/add/article');
$this->assertNotEqual(0, count($this->xpath('//div[contains(@class, "field--widget-image-image")]')), 'Image field widget found on add/node page', 'Browser');
$this->assertNoText('Image test on [site:name]');
}
}

View file

@ -14,7 +14,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
/**
* {@inheritdoc}
*/
public static $modules = array('language', 'content_translation', 'field_ui');
public static $modules = ['language', 'content_translation', 'field_ui'];
/**
* The name of the image field used in the test.
@ -39,7 +39,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$this->createImageField($this->fieldName, 'basicpage', [], ['title_field' => 1]);
// Create and log in user.
$permissions = array(
$permissions = [
'access administration pages',
'administer content translation',
'administer content types',
@ -50,16 +50,16 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
'edit any basicpage content',
'translate any entity',
'delete any basicpage content',
);
];
$admin_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($admin_user);
// Add a second and third language.
$edit = array();
$edit = [];
$edit['predefined_langcode'] = 'fr';
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
$edit = array();
$edit = [];
$edit['predefined_langcode'] = 'nl';
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
}
@ -69,7 +69,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
*/
public function testSyncedImages() {
// Enable translation for "Basic page" nodes.
$edit = array(
$edit = [
'entity_types[node]' => 1,
'settings[node][basicpage][translatable]' => 1,
"settings[node][basicpage][fields][$this->fieldName]" => 1,
@ -78,7 +78,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
// checkboxes on the form.
"settings[node][basicpage][columns][$this->fieldName][alt]" => FALSE,
"settings[node][basicpage][columns][$this->fieldName][title]" => FALSE,
);
];
$this->drupalPostForm('admin/config/regional/content-language', $edit, 'Save configuration');
// Verify that the image field on the "Basic basic" node type is
@ -87,10 +87,10 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node image field is translatable.');
// Create a default language node.
$default_language_node = $this->drupalCreateNode(array('type' => 'basicpage', 'title' => 'Lost in translation'));
$default_language_node = $this->drupalCreateNode(['type' => 'basicpage', 'title' => 'Lost in translation']);
// Edit the node to upload a file.
$edit = array();
$edit = [];
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[0]->uri);
$this->drupalPostForm('node/' . $default_language_node->id() . '/edit', $edit, t('Save'));
@ -99,10 +99,10 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$first_fid = $this->getLastFileId();
// Translate the node into French: remove the existing file.
$this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/fr', array(), t('Remove'));
$this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/fr', [], t('Remove'));
// Upload a different file.
$edit = array();
$edit = [];
$edit['title[0][value]'] = 'Scarlett Johansson';
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[1]->uri);
@ -131,10 +131,10 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$this->assertTrue($file->isPermanent());
// Translate the node into dutch: remove the existing file.
$this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/nl', array(), t('Remove'));
$this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/nl', [], t('Remove'));
// Upload a different file.
$edit = array();
$edit = [];
$edit['title[0][value]'] = 'Akiko Takeshita';
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[2]->uri);
@ -166,10 +166,10 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$this->assertTrue($file->isPermanent());
// Edit the second translation: remove the existing file.
$this->drupalPostForm('fr/node/' . $default_language_node->id() . '/edit', array(), t('Remove'));
$this->drupalPostForm('fr/node/' . $default_language_node->id() . '/edit', [], t('Remove'));
// Upload a different file.
$edit = array();
$edit = [];
$edit['title[0][value]'] = 'Giovanni Ribisi';
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[3]->uri);
@ -194,7 +194,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$this->assertTrue($file->isPermanent());
// Delete the third translation.
$this->drupalPostForm('nl/node/' . $default_language_node->id() . '/delete', array(), t('Delete Dutch translation'));
$this->drupalPostForm('nl/node/' . $default_language_node->id() . '/delete', [], t('Delete Dutch translation'));
\Drupal::entityTypeManager()->getStorage('file')->resetCache();
@ -210,7 +210,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$this->assertTrue($file->isTemporary());
// Delete the all translations.
$this->drupalPostForm('node/' . $default_language_node->id() . '/delete', array(), t('Delete all translations'));
$this->drupalPostForm('node/' . $default_language_node->id() . '/delete', [], t('Delete all translations'));
\Drupal::entityTypeManager()->getStorage('file')->resetCache();

View file

@ -1,81 +0,0 @@
<?php
namespace Drupal\image\Tests;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
/**
* Tests image style deletion using the UI.
*
* @group image
*/
class ImageStyleDeleteTest extends ImageFieldTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create an image field 'foo' having the image style 'medium' as widget
// preview and as formatter.
$this->createImageField('foo', 'page', [], [], ['preview_image_style' => 'medium'], ['image_style' => 'medium']);
}
/**
* Tests image style deletion.
*/
public function testDelete() {
$this->drupalGet('admin/config/media/image-styles/manage/medium/delete');
// Checks that the 'replacement' select element is displayed.
$this->assertFieldByName('replacement');
// Checks that UI messages are correct.
$this->assertRaw(t('If this style is in use on the site, you may select another style to replace it. All images that have been generated for this style will be permanently deleted. If no replacement style is selected, the dependent configurations might need manual reconfiguration.'));
$this->assertNoRaw(t('All images that have been generated for this style will be permanently deleted. The dependent configurations might need manual reconfiguration.'));
// Delete 'medium' image style but replace it with 'thumbnail'. This style
// is involved in 'node.page.default' display view and form.
$this->drupalPostForm(NULL, ['replacement' => 'thumbnail'], t('Delete'));
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
$view_display = EntityViewDisplay::load('node.page.default');
// Checks that the formatter setting is replaced.
if ($this->assertNotNull($component = $view_display->getComponent('foo'))) {
$this->assertIdentical($component['settings']['image_style'], 'thumbnail');
}
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
$form_display = EntityFormDisplay::load('node.page.default');
// Check that the widget setting is replaced.
if ($this->assertNotNull($component = $form_display->getComponent('foo'))) {
$this->assertIdentical($component['settings']['preview_image_style'], 'thumbnail');
}
$this->drupalGet('admin/config/media/image-styles/manage/thumbnail/delete');
// Checks that the 'replacement' select element is displayed.
$this->assertFieldByName('replacement');
// Checks that UI messages are correct.
$this->assertRaw(t('If this style is in use on the site, you may select another style to replace it. All images that have been generated for this style will be permanently deleted. If no replacement style is selected, the dependent configurations might need manual reconfiguration.'));
$this->assertNoRaw(t('All images that have been generated for this style will be permanently deleted. The dependent configurations might need manual reconfiguration.'));
// Delete 'thumbnail' image style. Provide no replacement.
$this->drupalPostForm(NULL, [], t('Delete'));
$view_display = EntityViewDisplay::load('node.page.default');
// Checks that the formatter setting is disabled.
$this->assertNull($view_display->getComponent('foo'));
$this->assertNotNull($view_display->get('hidden')['foo']);
// Checks that widget setting is preserved with the image preview disabled.
$form_display = EntityFormDisplay::load('node.page.default');
$this->assertNotNull($widget = $form_display->getComponent('foo'));
$this->assertIdentical($widget['settings']['preview_image_style'], '');
// Now, there's only one image style configured on the system: 'large'.
$this->drupalGet('admin/config/media/image-styles/manage/large/delete');
// Checks that the 'replacement' select element is not displayed.
$this->assertNoFieldByName('replacement');
// Checks that UI messages are correct.
$this->assertNoRaw(t('If this style is in use on the site, you may select another style to replace it. All images that have been generated for this style will be permanently deleted. If no replacement style is selected, the dependent configurations might need manual reconfiguration.'));
$this->assertRaw(t('All images that have been generated for this style will be permanently deleted. The dependent configurations might need manual reconfiguration.'));
}
}

View file

@ -14,7 +14,7 @@ class ImageStyleFlushTest extends ImageFieldTestBase {
/**
* Given an image style and a wrapper, generate an image.
*/
function createSampleImage($style, $wrapper) {
public function createSampleImage($style, $wrapper) {
static $file;
if (!isset($file)) {
@ -34,42 +34,42 @@ class ImageStyleFlushTest extends ImageFieldTestBase {
/**
* Count the number of images currently created for a style in a wrapper.
*/
function getImageCount($style, $wrapper) {
public function getImageCount($style, $wrapper) {
return count(file_scan_directory($wrapper . '://styles/' . $style->id(), '/.*/'));
}
/**
* General test to flush a style.
*/
function testFlush() {
public function testFlush() {
// Setup a style to be created and effects to add to it.
$style_name = strtolower($this->randomMachineName(10));
$style_label = $this->randomString();
$style_path = 'admin/config/media/image-styles/manage/' . $style_name;
$effect_edits = array(
'image_resize' => array(
$effect_edits = [
'image_resize' => [
'data[width]' => 100,
'data[height]' => 101,
),
'image_scale' => array(
],
'image_scale' => [
'data[width]' => 110,
'data[height]' => 111,
'data[upscale]' => 1,
),
);
],
];
// Add style form.
$edit = array(
$edit = [
'name' => $style_name,
'label' => $style_label,
);
];
$this->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style'));
// Add each sample effect to the style.
foreach ($effect_edits as $effect => $edit) {
// Add the effect.
$this->drupalPostForm($style_path, array('new' => $effect), t('Add'));
$this->drupalPostForm($style_path, ['new' => $effect], t('Add'));
if (!empty($edit)) {
$this->drupalPostForm(NULL, $edit, t('Add effect'));
}
@ -82,29 +82,29 @@ class ImageStyleFlushTest extends ImageFieldTestBase {
$image_path = $this->createSampleImage($style, 'public');
// Expecting to find 2 images, one is the sample.png image shown in
// image style preview.
$this->assertEqual($this->getImageCount($style, 'public'), 2, format_string('Image style %style image %file successfully generated.', array('%style' => $style->label(), '%file' => $image_path)));
$this->assertEqual($this->getImageCount($style, 'public'), 2, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
// Create an image for the 'private' wrapper.
$image_path = $this->createSampleImage($style, 'private');
$this->assertEqual($this->getImageCount($style, 'private'), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style->label(), '%file' => $image_path)));
$this->assertEqual($this->getImageCount($style, 'private'), 1, format_string('Image style %style image %file successfully generated.', ['%style' => $style->label(), '%file' => $image_path]));
// Remove the 'image_scale' effect and updates the style, which in turn
// forces an image style flush.
$style_path = 'admin/config/media/image-styles/manage/' . $style->id();
$uuids = array();
$uuids = [];
foreach ($style->getEffects() as $uuid => $effect) {
$uuids[$effect->getPluginId()] = $uuid;
}
$this->drupalPostForm($style_path . '/effects/' . $uuids['image_scale'] . '/delete', array(), t('Delete'));
$this->drupalPostForm($style_path . '/effects/' . $uuids['image_scale'] . '/delete', [], t('Delete'));
$this->assertResponse(200);
$this->drupalPostForm($style_path, array(), t('Update style'));
$this->drupalPostForm($style_path, [], t('Update style'));
$this->assertResponse(200);
// Post flush, expected 1 image in the 'public' wrapper (sample.png).
$this->assertEqual($this->getImageCount($style, 'public'), 1, format_string('Image style %style flushed correctly for %wrapper wrapper.', array('%style' => $style->label(), '%wrapper' => 'public')));
$this->assertEqual($this->getImageCount($style, 'public'), 1, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'public']));
// Post flush, expected no image in the 'private' wrapper.
$this->assertEqual($this->getImageCount($style, 'private'), 0, format_string('Image style %style flushed correctly for %wrapper wrapper.', array('%style' => $style->label(), '%wrapper' => 'private')));
$this->assertEqual($this->getImageCount($style, 'private'), 0, format_string('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'private']));
}
}

View file

@ -17,7 +17,7 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('image', 'image_module_test');
public static $modules = ['image', 'image_module_test'];
/**
* @var \Drupal\image\ImageStyleInterface
@ -27,14 +27,14 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
protected function setUp() {
parent::setUp();
$this->style = ImageStyle::create(array('name' => 'style_foo', 'label' => $this->randomString()));
$this->style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]);
$this->style->save();
}
/**
* Tests \Drupal\image\ImageStyleInterface::buildUri().
*/
function testImageStylePath() {
public function testImageStylePath() {
$scheme = 'public';
$actual = $this->style->buildUri("$scheme://foo/bar.gif");
$expected = "$scheme://styles/" . $this->style->id() . "/$scheme/foo/bar.gif";
@ -48,42 +48,42 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
/**
* Tests an image style URL using the "public://" scheme.
*/
function testImageStyleUrlAndPathPublic() {
public function testImageStyleUrlAndPathPublic() {
$this->doImageStyleUrlAndPathTests('public');
}
/**
* Tests an image style URL using the "private://" scheme.
*/
function testImageStyleUrlAndPathPrivate() {
public function testImageStyleUrlAndPathPrivate() {
$this->doImageStyleUrlAndPathTests('private');
}
/**
* Tests an image style URL with the "public://" scheme and unclean URLs.
*/
function testImageStyleUrlAndPathPublicUnclean() {
public function testImageStyleUrlAndPathPublicUnclean() {
$this->doImageStyleUrlAndPathTests('public', FALSE);
}
/**
* Tests an image style URL with the "private://" schema and unclean URLs.
*/
function testImageStyleUrlAndPathPrivateUnclean() {
public function testImageStyleUrlAndPathPrivateUnclean() {
$this->doImageStyleUrlAndPathTests('private', FALSE);
}
/**
* Tests an image style URL with a file URL that has an extra slash in it.
*/
function testImageStyleUrlExtraSlash() {
public function testImageStyleUrlExtraSlash() {
$this->doImageStyleUrlAndPathTests('public', TRUE, TRUE);
}
/**
* Tests that an invalid source image returns a 404.
*/
function testImageStyleUrlForMissingSourceImage() {
public function testImageStyleUrlForMissingSourceImage() {
$non_existent_uri = 'public://foo.png';
$generated_url = $this->style->buildUrl($non_existent_uri);
$this->drupalGet($generated_url);
@ -93,7 +93,7 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
/**
* Tests building an image style URL.
*/
function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = FALSE) {
public function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = FALSE) {
$this->prepareRequestForGenerator($clean_url);
// Make the default scheme neither "public" nor "private" to verify the

View file

@ -23,7 +23,7 @@ class ImageThemeFunctionTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('image', 'entity_test');
public static $modules = ['image', 'entity_test'];
/**
* Created file entity.
@ -40,12 +40,12 @@ class ImageThemeFunctionTest extends WebTestBase {
protected function setUp() {
parent::setUp();
FieldStorageConfig::create(array(
FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'image_test',
'type' => 'image',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
))->save();
])->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'image_test',
@ -62,7 +62,7 @@ class ImageThemeFunctionTest extends WebTestBase {
/**
* Tests usage of the image field formatters.
*/
function testImageFormatterTheme() {
public function testImageFormatterTheme() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
@ -72,7 +72,7 @@ class ImageThemeFunctionTest extends WebTestBase {
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
$style = ImageStyle::create(array('name' => 'test', 'label' => 'Test'));
$style = ImageStyle::create(['name' => 'test', 'label' => 'Test']);
$style->save();
$url = file_url_transform_relative($style->buildUrl($original_uri));
@ -86,17 +86,17 @@ class ImageThemeFunctionTest extends WebTestBase {
// Create the base element that we'll use in the tests below.
$path = $this->randomMachineName();
$base_element = array(
$base_element = [
'#theme' => 'image_formatter',
'#image_style' => 'test',
'#item' => $entity->image_test,
'#url' => Url::fromUri('base:' . $path),
);
];
// Test using theme_image_formatter() with a NULL value for the alt option.
$element = $base_element;
$this->setRawContent($renderer->renderRoot($element));
$elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height]', array(':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()));
$elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height]', [':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()]);
$this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders with a NULL value for the alt option.');
// Test using theme_image_formatter() without an image title, alt text, or
@ -104,7 +104,7 @@ class ImageThemeFunctionTest extends WebTestBase {
$element = $base_element;
$element['#item']->alt = '';
$this->setRawContent($renderer->renderRoot($element));
$elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', array(':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()));
$elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', [':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()]);
$this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders without title, alt, or path options.');
// Link the image to a fragment on the page, and not a full URL.
@ -112,19 +112,19 @@ class ImageThemeFunctionTest extends WebTestBase {
$element = $base_element;
$element['#url'] = Url::fromRoute('<none>', [], ['fragment' => $fragment]);
$this->setRawContent($renderer->renderRoot($element));
$elements = $this->xpath('//a[@href=:fragment]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', array(
$elements = $this->xpath('//a[@href=:fragment]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', [
':fragment' => '#' . $fragment,
':url' => $url,
':width' => $image->getWidth(),
':height' => $image->getHeight()
));
]);
$this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders a link fragment.');
}
/**
* Tests usage of the image style theme function.
*/
function testImageStyleTheme() {
public function testImageStyleTheme() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
@ -134,87 +134,87 @@ class ImageThemeFunctionTest extends WebTestBase {
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
$style = ImageStyle::create(array('name' => 'image_test', 'label' => 'Test'));
$style = ImageStyle::create(['name' => 'image_test', 'label' => 'Test']);
$style->save();
$url = file_url_transform_relative($style->buildUrl($original_uri));
// Create the base element that we'll use in the tests below.
$base_element = array(
$base_element = [
'#theme' => 'image_style',
'#style_name' => 'image_test',
'#uri' => $original_uri,
);
];
$element = $base_element;
$this->setRawContent($renderer->renderRoot($element));
$elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url and @alt=""]', array(':url' => $url));
$elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url and @alt=""]', [':url' => $url]);
$this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly.');
// Test using theme_image_style() with a NULL value for the alt option.
$element = $base_element;
$element['#alt'] = NULL;
$this->setRawContent($renderer->renderRoot($element));
$elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url]', array(':url' => $url));
$elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url]', [':url' => $url]);
$this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly with a NULL value for the alt option.');
}
/**
* Tests image alt attribute functionality.
*/
function testImageAltFunctionality() {
public function testImageAltFunctionality() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
// Test using alt directly with alt attribute.
$image_with_alt_property = array(
$image_with_alt_property = [
'#theme' => 'image',
'#uri' => '/core/themes/bartik/logo.svg',
'#alt' => 'Regular alt',
'#title' => 'Test title',
'#width' => '50%',
'#height' => '50%',
'#attributes' => array('class' => 'image-with-regular-alt', 'id' => 'my-img'),
);
'#attributes' => ['class' => 'image-with-regular-alt', 'id' => 'my-img'],
];
$this->setRawContent($renderer->renderRoot($image_with_alt_property));
$elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', array(":class" => "image-with-regular-alt", ":alt" => "Regular alt"));
$elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-regular-alt", ":alt" => "Regular alt"]);
$this->assertEqual(count($elements), 1, 'Regular alt displays correctly');
// Test using alt attribute inside attributes.
$image_with_alt_attribute_alt_attribute = array(
$image_with_alt_attribute_alt_attribute = [
'#theme' => 'image',
'#uri' => '/core/themes/bartik/logo.svg',
'#width' => '50%',
'#height' => '50%',
'#attributes' => array(
'#attributes' => [
'class' => 'image-with-attribute-alt',
'id' => 'my-img',
'title' => 'New test title',
'alt' => 'Attribute alt',
),
);
],
];
$this->setRawContent($renderer->renderRoot($image_with_alt_attribute_alt_attribute));
$elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', array(":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"));
$elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"]);
$this->assertEqual(count($elements), 1, 'Attribute alt displays correctly');
// Test using alt attribute as property and inside attributes.
$image_with_alt_attribute_both = array(
$image_with_alt_attribute_both = [
'#theme' => 'image',
'#uri' => '/core/themes/bartik/logo.svg',
'#width' => '50%',
'#height' => '50%',
'#alt' => 'Kitten sustainable',
'#attributes' => array(
'#attributes' => [
'class' => 'image-with-attribute-alt',
'id' => 'my-img',
'title' => 'New test title',
'alt' => 'Attribute alt',
),
);
],
];
$this->setRawContent($renderer->renderRoot($image_with_alt_attribute_both));
$elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', array(":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"));
$elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', [":class" => "image-with-attribute-alt", ":alt" => "Attribute alt"]);
$this->assertEqual(count($elements), 1, 'Attribute alt overrides alt property if both set.');
}

View file

@ -0,0 +1,186 @@
<?php
namespace Drupal\image\Tests;
use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
use Drupal\simpletest\WebTestBase;
/**
* Tests the endpoints used by the "image" in-place editor.
*
* @group image
*/
class QuickEditImageControllerTest extends WebTestBase {
use ImageFieldCreationTrait;
/**
* {@inheritdoc}
*/
public static $modules = ['node', 'image', 'quickedit'];
/**
* The machine name of our image field.
*
* @var string
*/
protected $fieldName;
/**
* A user with permissions to edit articles and use Quick Edit.
*
* @var \Drupal\user\UserInterface
*/
protected $contentAuthorUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create the Article node type.
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
// Log in as a content author who can use Quick Edit and edit Articles.
$this->contentAuthorUser = $this->drupalCreateUser([
'access contextual links',
'access in-place editing',
'access content',
'create article content',
'edit any article content',
'delete any article content',
]);
$this->drupalLogin($this->contentAuthorUser);
// Create a field with basic resolution validators.
$this->fieldName = strtolower($this->randomMachineName());
$field_settings = [
'max_resolution' => '100x',
'min_resolution' => '50x',
];
$this->createImageField($this->fieldName, 'article', [], $field_settings);
}
/**
* Tests that routes restrict access for un-privileged users.
*/
public function testAccess() {
// Create an anonymous user.
$user = $this->createUser();
$this->drupalLogin($user);
// Create a test Node.
$node = $this->drupalCreateNode([
'type' => 'article',
'title' => t('Test Node'),
]);
$this->drupalGet('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default');
$this->assertResponse('403');
$this->drupalPost('quickedit/image/upload/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default', 'application/json', []);
$this->assertResponse('403');
}
/**
* Tests that the field info route returns expected data.
*/
public function testFieldInfo() {
// Create a test Node.
$node = $this->drupalCreateNode([
'type' => 'article',
'title' => t('Test Node'),
]);
$info = $this->drupalGetJSON('quickedit/image/info/node/' . $node->id() . '/' . $this->fieldName . '/' . $node->language()->getId() . '/default');
// Assert that the default settings for our field are respected by our JSON
// endpoint.
$this->assertTrue($info['alt_field']);
$this->assertFalse($info['title_field']);
}
/**
* Tests that uploading a valid image works.
*/
public function testValidImageUpload() {
// Create a test Node.
$node = $this->drupalCreateNode([
'type' => 'article',
'title' => t('Test Node'),
]);
// We want a test image that is a valid size.
$valid_image = FALSE;
$image_factory = $this->container->get('image.factory');
foreach ($this->drupalGetTestFiles('image') as $image) {
$image_file = $image_factory->get($image->uri);
if ($image_file->getWidth() > 50 && $image_file->getWidth() < 100) {
$valid_image = $image;
break;
}
}
$this->assertTrue($valid_image);
$this->uploadImage($valid_image, $node->id(), $this->fieldName, $node->language()->getId());
$this->assertText('fid', t('Valid upload completed successfully.'));
}
/**
* Tests that uploading a invalid image does not work.
*/
public function testInvalidUpload() {
// Create a test Node.
$node = $this->drupalCreateNode([
'type' => 'article',
'title' => t('Test Node'),
]);
// We want a test image that will fail validation.
$invalid_image = FALSE;
/** @var \Drupal\Core\Image\ImageFactory $image_factory */
$image_factory = $this->container->get('image.factory');
foreach ($this->drupalGetTestFiles('image') as $image) {
/** @var \Drupal\Core\Image\ImageInterface $image_file */
$image_file = $image_factory->get($image->uri);
if ($image_file->getWidth() < 50 || $image_file->getWidth() > 100 ) {
$invalid_image = $image;
break;
}
}
$this->assertTrue($invalid_image);
$this->uploadImage($invalid_image, $node->id(), $this->fieldName, $node->language()->getId());
$this->assertText('main_error', t('Invalid upload returned errors.'));
}
/**
* Uploads an image using the image module's Quick Edit route.
*
* @param object $image
* The image to upload.
* @param int $nid
* The target node ID.
* @param string $field_name
* The target field machine name.
* @param string $langcode
* The langcode to use when setting the field's value.
*
* @return mixed
* The content returned from the call to $this->curlExec().
*/
public function uploadImage($image, $nid, $field_name, $langcode) {
$filepath = $this->container->get('file_system')->realpath($image->uri);
$data = [
'files[image]' => curl_file_create($filepath),
];
$path = 'quickedit/image/upload/node/' . $nid . '/' . $field_name . '/' . $langcode . '/default';
// We assemble the curl request ourselves as drupalPost cannot process file
// uploads, and drupalPostForm only works with typical Drupal forms.
return $this->curlExec([
CURLOPT_URL => $this->buildUrl($path, []),
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Content-Type: multipart/form-data',
],
]);
}
}

View file

@ -21,25 +21,25 @@ class RelationshipUserImageDataTest extends ViewTestBase {
*
* @var array
*/
public static $modules = array('image', 'image_test_views', 'user');
public static $modules = ['image', 'image_test_views', 'user'];
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_image_user_image_data');
public static $testViews = ['test_image_user_image_data'];
protected function setUp() {
parent::setUp();
// Create the user profile field and instance.
FieldStorageConfig::create(array(
FieldStorageConfig::create([
'entity_type' => 'user',
'field_name' => 'user_picture',
'type' => 'image',
'translatable' => '0',
))->save();
])->save();
FieldConfig::create([
'label' => 'User Picture',
'description' => '',
@ -49,7 +49,7 @@ class RelationshipUserImageDataTest extends ViewTestBase {
'required' => 0,
])->save();
ViewTestData::createTestViews(get_class($this), array('image_test_views'));
ViewTestData::createTestViews(get_class($this), ['image_test_views']);
}
/**
@ -84,12 +84,12 @@ class RelationshipUserImageDataTest extends ViewTestBase {
];
$this->assertIdentical($expected, $view->getDependencies());
$this->executeView($view);
$expected_result = array(
array(
$expected_result = [
[
'file_managed_user__user_picture_fid' => '2',
),
);
$column_map = array('file_managed_user__user_picture_fid' => 'file_managed_user__user_picture_fid');
],
];
$column_map = ['file_managed_user__user_picture_fid' => 'file_managed_user__user_picture_fid'];
$this->assertIdenticalResultset($view, $expected_result, $column_map);
}