updated core from 8.4 to 8.5 : bug with login_destination

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-13 14:01:21 +01:00
parent 0d78da249b
commit e668535a4e
3486 changed files with 89604 additions and 33283 deletions
@@ -10,7 +10,7 @@ use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Render\Element\StatusMessages;
use Drupal\Core\Render\RendererInterface;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\user\PrivateTempStoreFactory;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -23,7 +23,7 @@ class QuickEditImageController extends ControllerBase {
/**
* Stores The Quick Edit tempstore.
*
* @var \Drupal\user\PrivateTempStore
* @var \Drupal\Core\TempStore\PrivateTempStore
*/
protected $tempStore;
@@ -48,7 +48,7 @@ class QuickEditImageController extends ControllerBase {
* The renderer.
* @param \Drupal\Core\Image\ImageFactory $image_factory
* The image factory.
* @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
*/
public function __construct(RendererInterface $renderer, ImageFactory $image_factory, PrivateTempStoreFactory $temp_store_factory) {
@@ -64,7 +64,7 @@ class QuickEditImageController extends ControllerBase {
return new static(
$container->get('renderer'),
$container->get('image.factory'),
$container->get('user.private_tempstore')
$container->get('tempstore.private')
);
}
@@ -9,6 +9,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides an add form for image effects.
*
* @internal
*/
class ImageEffectAddForm extends ImageEffectFormBase {
@@ -8,6 +8,8 @@ use Drupal\image\ImageStyleInterface;
/**
* Form for deleting an image effect.
*
* @internal
*/
class ImageEffectDeleteForm extends ConfirmFormBase {
@@ -7,6 +7,8 @@ use Drupal\image\ImageStyleInterface;
/**
* Provides an edit form for image effects.
*
* @internal
*/
class ImageEffectEditForm extends ImageEffectFormBase {
@@ -6,6 +6,8 @@ use Drupal\Core\Form\FormStateInterface;
/**
* Controller for image style addition forms.
*
* @internal
*/
class ImageStyleAddForm extends ImageStyleFormBase {
@@ -7,6 +7,8 @@ use Drupal\Core\Form\FormStateInterface;
/**
* Creates a form to delete an image style.
*
* @internal
*/
class ImageStyleDeleteForm extends EntityDeleteForm {
@@ -12,6 +12,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Controller for image style edit form.
*
* @internal
*/
class ImageStyleEditForm extends ImageStyleFormBase {
@@ -7,6 +7,8 @@ use Drupal\Core\Form\FormStateInterface;
/**
* Form controller for image style flush.
*
* @internal
*/
class ImageStyleFlushForm extends EntityConfirmFormBase {
@@ -39,9 +39,17 @@ class ImageStyleListBuilder extends ConfigEntityListBuilder {
'url' => $entity->urlInfo('flush-form'),
];
return parent::getDefaultOperations($entity) + [
$operations = parent::getDefaultOperations($entity) + [
'flush' => $flush,
];
// Remove destination URL from the edit link to allow editing image
// effects.
if (isset($operations['edit'])) {
$operations['edit']['url'] = $entity->toUrl('edit-form');
}
return $operations;
}
/**
@@ -48,8 +48,11 @@ class PathProcessorImageStyles implements InboundPathProcessorInterface {
if (strpos($path, '/' . $directory_path . '/styles/') === 0) {
$path_prefix = '/' . $directory_path . '/styles/';
}
elseif (strpos($path, '/system/files/styles/') === 0) {
// Check if the string '/system/files/styles/' exists inside the path,
// that means we have a case of private file's image style.
elseif (strpos($path, '/system/files/styles/') !== FALSE) {
$path_prefix = '/system/files/styles/';
$path = substr($path, strpos($path, $path_prefix), strlen($path));
}
else {
return $path;
@@ -344,8 +344,8 @@ class ImageItem extends FileItem {
if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= 5) {
$tmp_file = drupal_tempnam('temporary://', 'generateImage_');
$destination = $tmp_file . '.' . $extension;
file_unmanaged_move($tmp_file, $destination, FILE_CREATE_DIRECTORY);
if ($path = $random->image(drupal_realpath($destination), $min_resolution, $max_resolution)) {
file_unmanaged_move($tmp_file, $destination);
if ($path = $random->image(\Drupal::service('file_system')->realpath($destination), $min_resolution, $max_resolution)) {
$image = File::create();
$image->setFileUri($path);
$image->setOwnerId(\Drupal::currentUser()->id());
@@ -354,7 +354,7 @@ class ImageItem extends FileItem {
$destination_dir = static::doGetUploadLocation($settings);
file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
$destination = $destination_dir . '/' . basename($path);
$file = file_move($image, $destination, FILE_CREATE_DIRECTORY);
$file = file_move($image, $destination);
$images[$extension][$min_resolution][$max_resolution][$file->id()] = $file;
}
else {
@@ -2,9 +2,12 @@
namespace Drupal\image\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\file\Entity\File;
use Drupal\file\Plugin\Field\FieldWidget\FileWidget;
use Drupal\image\Entity\ImageStyle;
@@ -22,6 +25,36 @@ use Drupal\image\Entity\ImageStyle;
*/
class ImageWidget extends FileWidget {
/**
* The image factory service.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* Constructs an ImageWidget object.
*
* @param string $plugin_id
* The plugin_id for the widget.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the widget is associated.
* @param array $settings
* The widget settings.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
* The element info manager service.
* @param \Drupal\Core\Image\ImageFactory $image_factory
* The image factory service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ElementInfoManagerInterface $element_info, ImageFactory $image_factory = NULL) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $element_info);
$this->imageFactory = $image_factory ?: \Drupal::service('image.factory');
}
/**
* {@inheritdoc}
*/
@@ -112,15 +145,21 @@ class ImageWidget extends FileWidget {
$field_settings = $this->getFieldSettings();
// Add image validation.
$element['#upload_validators']['file_validate_is_image'] = [];
// Add upload resolution validation.
if ($field_settings['max_resolution'] || $field_settings['min_resolution']) {
$element['#upload_validators']['file_validate_image_resolution'] = [$field_settings['max_resolution'], $field_settings['min_resolution']];
}
// If not using custom extension validation, ensure this is an image.
$supported_extensions = ['png', 'gif', 'jpg', 'jpeg'];
$extensions = isset($element['#upload_validators']['file_validate_extensions'][0]) ? $element['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions);
$extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
$extensions = $field_settings['file_extensions'];
$supported_extensions = $this->imageFactory->getSupportedExtensions();
// If using custom extension validation, ensure that the extensions are
// supported by the current image toolkit. Otherwise, validate against all
// toolkit supported extensions.
$extensions = !empty($extensions) ? array_intersect(explode(' ', $extensions), $supported_extensions) : $supported_extensions;
$element['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);
// Add mobile device image capture acceptance.
@@ -256,7 +295,8 @@ class ImageWidget extends FileWidget {
public static function validateRequiredFields($element, FormStateInterface $form_state) {
// Only do validation if the function is triggered from other places than
// the image process form.
if (!in_array('file_managed_file_submit', $form_state->getTriggeringElement()['#submit'])) {
$triggering_element = $form_state->getTriggeringElement();
if (empty($triggering_element['#submit']) || !in_array('file_managed_file_submit', $triggering_element['#submit'])) {
// If the image is not there, we do not check for empty values.
$parents = $element['#parents'];
$field = array_pop($parents);
@@ -0,0 +1,43 @@
<?php
namespace Drupal\image\Plugin\migrate\cckfield\d7;
@trigger_error('ImageField is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use \Drupal\image\Plugin\migrate\field\d7\ImageField instead.', E_USER_DEPRECATED);
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_drupal\Plugin\migrate\cckfield\CckFieldPluginBase;
/**
* @MigrateCckField(
* id = "image",
* core = {7},
* source_module = "image",
* destination_module = "file"
* )
*
* @deprecated in Drupal 8.5.x, to be removed before Drupal 9.0.x. Use
* \Drupal\image\Plugin\migrate\field\d7\ImageField instead.
*
* @see https://www.drupal.org/node/2751897
*/
class ImageField extends CckFieldPluginBase {
/**
* {@inheritdoc}
*/
public function processCckFieldValues(MigrationInterface $migration, $field_name, $data) {
$process = [
'plugin' => 'sub_process',
'source' => $field_name,
'process' => [
'target_id' => 'fid',
'alt' => 'alt',
'title' => 'title',
'width' => 'width',
'height' => 'height',
],
];
$migration->mergeProcessOfProperty($field_name, $process);
}
}
@@ -0,0 +1,15 @@
<?php
namespace Drupal\image\Plugin\migrate\field\d6;
use Drupal\file\Plugin\migrate\field\d6\FileField;
/**
* @MigrateField(
* id = "imagefield",
* core = {6},
* source_module = "imagefield",
* destination_module = "image"
* )
*/
class ImageField extends FileField {}
@@ -0,0 +1,36 @@
<?php
namespace Drupal\image\Plugin\migrate\field\d7;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
/**
* @MigrateField(
* id = "image",
* core = {7},
* source_module = "image",
* destination_module = "image"
* )
*/
class ImageField extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function processFieldValues(MigrationInterface $migration, $field_name, $data) {
$process = [
'plugin' => 'sub_process',
'source' => $field_name,
'process' => [
'target_id' => 'fid',
'alt' => 'alt',
'title' => 'title',
'width' => 'width',
'height' => 'height',
],
];
$migration->mergeProcessOfProperty($field_name, $process);
}
}
@@ -422,9 +422,20 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Edit the scale effect that was just added.
$this->clickLink(t('Edit'));
$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.
// Add another scale effect and make sure both exist. Click through from
// the overview to make sure that it is possible to add new effect then.
$this->drupalGet('admin/config/media/image-styles');
$rows = $this->xpath('//table/tbody/tr');
$i = 0;
foreach ($rows as $row) {
if (((string) $row->td[0]) === 'Test style scale edit scale') {
$this->clickLink('Edit', $i);
break;
}
$i++;
}
$this->drupalPostForm(NULL, ['new' => 'image_scale'], t('Add'));
$this->drupalPostForm(NULL, ['data[width]' => '12', 'data[height]' => '19'], t('Add effect'));
$this->assertText(t('Scale 24×19'));
$this->assertText(t('Scale 12×19'));
@@ -305,7 +305,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
);
$non_image = $this->drupalGetTestFiles('text');
$this->drupalPostForm(NULL, ['files[settings_default_image_uuid]' => drupal_realpath($non_image[0]->uri)], t("Upload"));
$this->drupalPostForm(NULL, ['files[settings_default_image_uuid]' => \Drupal::service('file_system')->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.');
@@ -312,7 +312,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// @see FileWidget::formMultipleElements().
$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),
'files[' . $field_name . '_1][]' => \Drupal::service('file_system')->realpath($test_image->uri),
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Add the required alt text.
@@ -322,7 +322,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Assert ImageWidget::process() calls FieldWidget::process().
$this->drupalGet('node/' . $node->id() . '/edit');
$edit = [
'files[' . $field_name . '_2][]' => drupal_realpath($test_image->uri),
'files[' . $field_name . '_2][]' => \Drupal::service('file_system')->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">');
@@ -357,7 +357,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$title = $this->randomString(1024);
$edit = [
// Get the path of the 'image-test.png' file.
'files[settings_default_image_uuid]' => drupal_realpath($images[0]->uri),
'files[settings_default_image_uuid]' => \Drupal::service('file_system')->realpath($images[0]->uri),
'settings[default_image][alt]' => $alt,
'settings[default_image][title]' => $title,
];
@@ -426,7 +426,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Add a default image to the new field.
$edit = [
// Get the path of the 'image-test.gif' file.
'files[settings_default_image_uuid]' => drupal_realpath($images[2]->uri),
'files[settings_default_image_uuid]' => \Drupal::service('file_system')->realpath($images[2]->uri),
'settings[default_image][alt]' => $alt,
'settings[default_image][title]' => $title,
];
@@ -69,7 +69,7 @@ abstract class ImageFieldTestBase extends WebTestBase {
$edit = [
'title[0][value]' => $this->randomMachineName(),
];
$edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
$edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
$this->drupalPostForm('node/add/' . $type, $edit, t('Preview'));
}
@@ -89,7 +89,7 @@ abstract class ImageFieldTestBase extends WebTestBase {
$edit = [
'title[0][value]' => $this->randomMachineName(),
];
$edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
$edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
$this->drupalPostForm('node/add/' . $type, $edit, t('Save'));
if ($alt) {
// Add alt text.
@@ -2,12 +2,70 @@
namespace Drupal\image\Tests;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
/**
* Tests validation functions such as min/max resolution.
*
* @group image
*/
class ImageFieldValidateTest extends ImageFieldTestBase {
/**
* Test image validity.
*/
public function testValid() {
$file_system = $this->container->get('file_system');
$image_files = $this->drupalGetTestFiles('image');
$field_name = strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', [], ['file_directory' => 'test-upload']);
$expected_path = 'public://test-upload';
// Create alt text for the image.
$alt = $this->randomMachineName();
// Create a node with a valid image.
$node = $this->uploadNodeImage($image_files[0], $field_name, 'article', $alt);
$this->assertTrue(file_exists($expected_path . '/' . $image_files[0]->filename));
// Remove the image.
$this->drupalPostForm('node/' . $node . '/edit', [], t('Remove'));
$this->drupalPostForm(NULL, [], t('Save'));
// Get invalid image test files from simpletest.
$files = file_scan_directory(drupal_get_path('module', 'simpletest') . '/files', '/invalid-img-.*/');
$invalid_image_files = [];
foreach ($files as $file) {
$invalid_image_files[$file->filename] = $file;
}
// Try uploading a zero-byte image.
$zero_size_image = $invalid_image_files['invalid-img-zero-size.png'];
$edit = [
'files[' . $field_name . '_0]' => $file_system->realpath($zero_size_image->uri),
];
$this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload'));
$this->assertFalse(file_exists($expected_path . '/' . $zero_size_image->filename));
// Try uploading an invalid image.
$invalid_image = $invalid_image_files['invalid-img-test.png'];
$edit = [
'files[' . $field_name . '_0]' => $file_system->realpath($invalid_image->uri),
];
$this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload'));
$this->assertFalse(file_exists($expected_path . '/' . $invalid_image->filename));
// Upload a valid image again.
$valid_image = $image_files[0];
$edit = [
'files[' . $field_name . '_0]' => $file_system->realpath($valid_image->uri),
];
$this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload'));
$this->assertTrue(file_exists($expected_path . '/' . $valid_image->filename));
}
/**
* Test min/max resolution settings.
*/
@@ -62,6 +120,7 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
}
if ($image_file->getWidth() < $min_resolution['width']) {
$image_that_is_too_small = $image;
$image_that_is_too_small_file = $image_file;
}
if ($image_that_is_too_small && $image_that_is_too_big) {
break;
@@ -69,7 +128,11 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
}
$this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article');
$this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename]));
$this->assertRaw(t('The image is too small; the minimum dimensions are %dimensions pixels.', ['%dimensions' => '50x50']));
$this->assertRaw(t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [
'%dimensions' => '50x50',
'%width' => $image_that_is_too_small_file->getWidth(),
'%height' => $image_that_is_too_small_file->getHeight(),
]));
$this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article');
$this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.'));
$this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article');
@@ -178,4 +241,48 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
$this->assertEqual(count($elements), 1, 'Ajax validation messages are displayed once.');
}
/**
* Tests that image field validation works with other form submit handlers.
*/
public function testFriendlyAjaxValidation() {
// Add a custom field to the Article content type that contains an AJAX
// handler on a select field.
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_dummy_select',
'type' => 'image_module_test_dummy_ajax',
'entity_type' => 'node',
'cardinality' => 1,
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'node',
'bundle' => 'article',
'field_name' => 'field_dummy_select',
'label' => t('Dummy select'),
])->save();
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.article.default')
->setComponent(
'field_dummy_select',
[
'type' => 'image_module_test_dummy_ajax_widget',
'weight' => 1,
])
->save();
// Then, add an image field.
$this->createImageField('field_dummy_image', 'article');
// Open an article and trigger the AJAX handler.
$this->drupalGet('node/add/article');
$edit = [
'field_dummy_select[select_widget]' => 'bam',
];
$this->drupalPostAjaxForm(NULL, $edit, 'field_dummy_select[select_widget]');
}
}
@@ -95,7 +95,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
// Edit the node to upload a file.
$edit = [];
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[0]->uri);
$edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('image')[0]->uri);
$this->drupalPostForm('node/' . $default_language_node->id() . '/edit', $edit, t('Save'));
$edit = [$this->fieldName . '[0][alt]' => 'Lost in translation image', $this->fieldName . '[0][title]' => 'Lost in translation image title'];
$this->drupalPostForm(NULL, $edit, t('Save'));
@@ -108,7 +108,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$edit = [];
$edit['title[0][value]'] = 'Scarlett Johansson';
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[1]->uri);
$edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('image')[1]->uri);
$this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
$edit = [$this->fieldName . '[0][alt]' => 'Scarlett Johansson image', $this->fieldName . '[0][title]' => 'Scarlett Johansson image title'];
$this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
@@ -140,7 +140,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$edit = [];
$edit['title[0][value]'] = 'Akiko Takeshita';
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[2]->uri);
$edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('image')[2]->uri);
$this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
$edit = [$this->fieldName . '[0][alt]' => 'Akiko Takeshita image', $this->fieldName . '[0][title]' => 'Akiko Takeshita image title'];
$this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
@@ -175,7 +175,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
$edit = [];
$edit['title[0][value]'] = 'Giovanni Ribisi';
$name = 'files[' . $this->fieldName . '_0]';
$edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[3]->uri);
$edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('image')[3]->uri);
$this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
$name = $this->fieldName . '[0][alt]';
@@ -3,6 +3,7 @@
namespace Drupal\image\Tests;
use Drupal\image\Entity\ImageStyle;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
@@ -17,18 +18,29 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
*
* @var array
*/
public static $modules = ['image', 'image_module_test'];
public static $modules = ['image', 'image_module_test', 'language'];
/**
* The image style.
*
* @var \Drupal\image\ImageStyleInterface
*/
protected $style;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]);
$this->style = ImageStyle::create([
'name' => 'style_foo',
'label' => $this->randomString(),
]);
$this->style->save();
// Create a new language.
ConfigurableLanguage::createFromLangcode('fr')->save();
}
/**
@@ -73,6 +85,20 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
$this->doImageStyleUrlAndPathTests('private', FALSE);
}
/**
* Tests an image style URL with the "public://" schema and language prefix.
*/
public function testImageStyleUrlAndPathPublicLanguage() {
$this->doImageStyleUrlAndPathTests('public', TRUE, TRUE, 'fr');
}
/**
* Tests an image style URL with the "private://" schema and language prefix.
*/
public function testImageStyleUrlAndPathPrivateLanguage() {
$this->doImageStyleUrlAndPathTests('private', TRUE, TRUE, 'fr');
}
/**
* Tests an image style URL with a file URL that has an extra slash in it.
*/
@@ -93,7 +119,7 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
/**
* Tests building an image style URL.
*/
public function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = FALSE) {
public function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = FALSE, $langcode = FALSE) {
$this->prepareRequestForGenerator($clean_url);
// Make the default scheme neither "public" nor "private" to verify the
@@ -105,6 +131,13 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
$status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
$this->assertNotIdentical(FALSE, $status, 'Created the directory for the generated images for the test style.');
// Override the language to build the URL for the correct language.
if ($langcode) {
$language_manager = \Drupal::service('language_manager');
$language = $language_manager->getLanguage($langcode);
$language_manager->setConfigOverrideLanguage($language);
}
// Create a working copy of the file.
$files = $this->drupalGetTestFiles('image');
$file = array_shift($files);
@@ -119,6 +152,11 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$generate_url = $this->style->buildUrl($original_uri, $clean_url);
// Make sure that language prefix is never added to the image style URL.
if ($langcode) {
$this->assertTrue(strpos($generate_url, "/$langcode/") === FALSE, 'Langcode was not found in the image style URL.');
}
// Ensure that the tests still pass when the file is generated by accessing
// a poorly constructed (but still valid) file URL that has an extra slash
// in it.
@@ -157,7 +195,8 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
$this->assertEqual($this->drupalGetHeader('Content-Length'), $image->getFileSize(), 'Expected Content-Length was reported.');
// Check that we did not download the original file.
$original_image = $this->container->get('image.factory')->get($original_uri);
$original_image = $this->container->get('image.factory')
->get($original_uri);
$this->assertNotEqual($this->drupalGetHeader('Content-Length'), $original_image->getFileSize());
if ($scheme == 'private') {
@@ -192,13 +231,15 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
$this->drupalGet($generate_url_noaccess);
$this->assertResponse(403, 'Confirmed that access is denied for the private image style.');
// Verify that images are not appended to the response. Currently this test only uses PNG images.
// Verify that images are not appended to the response.
// Currently this test only uses PNG images.
if (strpos($generate_url, '.png') === FALSE) {
$this->fail('Confirming that private image styles are not appended require PNG file.');
}
else {
// Check for PNG-Signature (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2) in the
// response body.
// Check for PNG-Signature
// (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2)
// in the response body.
$this->assertNoRaw(chr(137) . chr(80) . chr(78) . chr(71) . chr(13) . chr(10) . chr(26) . chr(10), 'No PNG signature found in the response body.');
}
}
@@ -215,7 +256,9 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
// Allow insecure image derivatives to be created for the remainder of this
// test.
$this->config('image.settings')->set('allow_insecure_derivatives', TRUE)->save();
$this->config('image.settings')
->set('allow_insecure_derivatives', TRUE)
->save();
// Create another working copy of the file.
$files = $this->drupalGetTestFiles('image');
@@ -239,7 +282,8 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
// Stop supressing the security token in the URL.
$this->config('image.settings')->set('suppress_itok_output', FALSE)->save();
// Ensure allow_insecure_derivatives is enabled.
$this->assertEqual($this->config('image.settings')->get('allow_insecure_derivatives'), TRUE);
$this->assertEqual($this->config('image.settings')
->get('allow_insecure_derivatives'), TRUE);
// Check that a security token is still required when generating a second
// image derivative using the first one as a source.
$nested_url = $this->style->buildUrl($generated_uri, $clean_url);
@@ -30,8 +30,8 @@ class RelationshipUserImageDataTest extends ViewTestBase {
*/
public static $testViews = ['test_image_user_image_data'];
protected function setUp() {
parent::setUp();
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
// Create the user profile field and instance.
FieldStorageConfig::create([