Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
|
@ -0,0 +1,7 @@
|
|||
image.style.*.third_party.image_module_test:
|
||||
type: mapping
|
||||
label: 'Schema for image_module_test module additions to image_style entity'
|
||||
mapping:
|
||||
foo:
|
||||
type: string
|
||||
label: 'Label for foo'
|
|
@ -0,0 +1,6 @@
|
|||
name: 'Image test'
|
||||
type: module
|
||||
description: 'Provides hook implementations for testing Image module functionality.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
core: 8.x
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Provides Image module hook implementations for testing purposes.
|
||||
*/
|
||||
|
||||
use Drupal\image\ImageStyleInterface;
|
||||
|
||||
function image_module_test_file_download($uri) {
|
||||
$default_uri = \Drupal::state()->get('image.test_file_download') ?: FALSE;
|
||||
if ($default_uri == $uri) {
|
||||
return array('X-Image-Owned-By' => 'image_module_test');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_image_effect_info_alter().
|
||||
*
|
||||
* Used to keep a count of cache misses in \Drupal\image\ImageEffectManager.
|
||||
*/
|
||||
function image_module_test_image_effect_info_alter(&$effects) {
|
||||
$image_effects_definition_called = &drupal_static(__FUNCTION__, 0);
|
||||
$image_effects_definition_called++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_image_style_presave().
|
||||
*
|
||||
* Used to save test third party settings in the image style entity.
|
||||
*/
|
||||
function image_module_test_image_style_presave(ImageStyleInterface $style) {
|
||||
$style->setThirdPartySetting('image_module_test', 'foo', 'bar');
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\image_module_test\Plugin\ImageEffect\NullTestImageEffect.
|
||||
*/
|
||||
|
||||
namespace Drupal\image_module_test\Plugin\ImageEffect;
|
||||
|
||||
use Drupal\Core\Image\ImageInterface;
|
||||
use Drupal\image\ImageEffectBase;
|
||||
|
||||
/**
|
||||
* Performs no operation on an image resource.
|
||||
*
|
||||
* @ImageEffect(
|
||||
* id = "image_module_test_null",
|
||||
* label = @Translation("Image module test")
|
||||
* )
|
||||
*/
|
||||
class NullTestImageEffect extends ImageEffectBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transformDimensions(array &$dimensions) {
|
||||
// Unset image dimensions.
|
||||
$dimensions['width'] = $dimensions['height'] = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function applyEffect(ImageInterface $image) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
name: 'Image test views'
|
||||
type: module
|
||||
description: 'Provides default views for views image tests.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- image
|
||||
- views
|
|
@ -0,0 +1,77 @@
|
|||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
module:
|
||||
- file
|
||||
- user
|
||||
id: test_image_user_image_data
|
||||
label: test_image_user_image_data
|
||||
module: views
|
||||
description: ''
|
||||
tag: ''
|
||||
base_table: users_field_data
|
||||
base_field: uid
|
||||
core: 8.x
|
||||
display:
|
||||
default:
|
||||
display_plugin: default
|
||||
id: default
|
||||
display_title: Master
|
||||
position: 0
|
||||
display_options:
|
||||
access:
|
||||
type: perm
|
||||
options:
|
||||
perm: 'access user profiles'
|
||||
cache:
|
||||
type: tag
|
||||
style:
|
||||
type: table
|
||||
options:
|
||||
grouping: { }
|
||||
row_class: ''
|
||||
default_row_class: true
|
||||
override: true
|
||||
sticky: false
|
||||
caption: ''
|
||||
summary: ''
|
||||
description: ''
|
||||
columns:
|
||||
name: name
|
||||
fid: fid
|
||||
info:
|
||||
name:
|
||||
sortable: false
|
||||
default_sort_order: asc
|
||||
align: ''
|
||||
separator: ''
|
||||
empty_column: false
|
||||
responsive: ''
|
||||
fid:
|
||||
sortable: false
|
||||
default_sort_order: asc
|
||||
align: ''
|
||||
separator: ''
|
||||
empty_column: false
|
||||
responsive: ''
|
||||
default: '-1'
|
||||
empty_table: false
|
||||
row:
|
||||
type: fields
|
||||
options:
|
||||
inline: { }
|
||||
separator: ''
|
||||
hide_empty: false
|
||||
default_field_elements: true
|
||||
relationships:
|
||||
user_picture_target_id:
|
||||
id: user_picture_target_id
|
||||
table: user__user_picture
|
||||
field: user_picture_target_id
|
||||
relationship: none
|
||||
group_type: group
|
||||
admin_label: 'image from user_picture'
|
||||
required: true
|
||||
plugin_id: standard
|
||||
arguments: { }
|
||||
display_extenders: { }
|
242
core/modules/image/tests/src/Unit/ImageStyleTest.php
Normal file
242
core/modules/image/tests/src/Unit/ImageStyleTest.php
Normal file
|
@ -0,0 +1,242 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\image\Unit\ImageStyleTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\image\Unit;
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Drupal\Component\Utility\Crypt;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\image\Entity\ImageStyle
|
||||
*
|
||||
* @group Image
|
||||
*/
|
||||
class ImageStyleTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The entity type used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityType;
|
||||
|
||||
/**
|
||||
* The entity manager used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* The ID of the type of the entity under test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entityTypeId;
|
||||
|
||||
/**
|
||||
* Gets a mocked image style for testing.
|
||||
*
|
||||
* @param string $image_effect_id
|
||||
* The image effect ID.
|
||||
* @param \Drupal\image\ImageEffectInterface|\PHPUnit_Framework_MockObject_MockObject $image_effect
|
||||
* The image effect used for testing.
|
||||
*
|
||||
* @return \Drupal\image\ImageStyleInterface|\Drupal\image\ImageStyleInterface
|
||||
* The mocked image style.
|
||||
*/
|
||||
protected function getImageStyleMock($image_effect_id, $image_effect, $stubs = array()) {
|
||||
$effectManager = $this->getMockBuilder('\Drupal\image\ImageEffectManager')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$effectManager->expects($this->any())
|
||||
->method('createInstance')
|
||||
->with($image_effect_id)
|
||||
->will($this->returnValue($image_effect));
|
||||
$default_stubs = array(
|
||||
'getImageEffectPluginManager',
|
||||
'fileUriScheme',
|
||||
'fileUriTarget',
|
||||
'fileDefaultScheme',
|
||||
);
|
||||
$image_style = $this->getMockBuilder('\Drupal\image\Entity\ImageStyle')
|
||||
->setConstructorArgs(array(
|
||||
array('effects' => array($image_effect_id => array('id' => $image_effect_id))),
|
||||
$this->entityTypeId,
|
||||
))
|
||||
->setMethods(array_merge($default_stubs, $stubs))
|
||||
->getMock();
|
||||
|
||||
$image_style->expects($this->any())
|
||||
->method('getImageEffectPluginManager')
|
||||
->will($this->returnValue($effectManager));
|
||||
$image_style->expects($this->any())
|
||||
->method('fileUriScheme')
|
||||
->will($this->returnCallback(array($this, 'fileUriScheme')));
|
||||
$image_style->expects($this->any())
|
||||
->method('fileUriTarget')
|
||||
->will($this->returnCallback(array($this, 'fileUriTarget')));
|
||||
$image_style->expects($this->any())
|
||||
->method('fileDefaultScheme')
|
||||
->will($this->returnCallback(array($this, 'fileDefaultScheme')));
|
||||
|
||||
return $image_style;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->entityTypeId = $this->randomMachineName();
|
||||
$this->provider = $this->randomMachineName();
|
||||
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||
$this->entityType->expects($this->any())
|
||||
->method('getProvider')
|
||||
->will($this->returnValue($this->provider));
|
||||
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
|
||||
$this->entityManager->expects($this->any())
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->will($this->returnValue($this->entityType));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getDerivativeExtension
|
||||
*/
|
||||
public function testGetDerivativeExtension() {
|
||||
$image_effect_id = $this->randomMachineName();
|
||||
$logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock();
|
||||
$image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase')
|
||||
->setConstructorArgs(array(array(), $image_effect_id, array(), $logger))
|
||||
->getMock();
|
||||
$image_effect->expects($this->any())
|
||||
->method('getDerivativeExtension')
|
||||
->will($this->returnValue('png'));
|
||||
|
||||
$image_style = $this->getImageStyleMock($image_effect_id, $image_effect);
|
||||
|
||||
$extensions = array('jpeg', 'gif', 'png');
|
||||
foreach ($extensions as $extension) {
|
||||
$extensionReturned = $image_style->getDerivativeExtension($extension);
|
||||
$this->assertEquals($extensionReturned, 'png');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::buildUri
|
||||
*/
|
||||
public function testBuildUri() {
|
||||
// Image style that changes the extension.
|
||||
$image_effect_id = $this->randomMachineName();
|
||||
$logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock();
|
||||
$image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase')
|
||||
->setConstructorArgs(array(array(), $image_effect_id, array(), $logger))
|
||||
->getMock();
|
||||
$image_effect->expects($this->any())
|
||||
->method('getDerivativeExtension')
|
||||
->will($this->returnValue('png'));
|
||||
|
||||
$image_style = $this->getImageStyleMock($image_effect_id, $image_effect);
|
||||
$this->assertEquals($image_style->buildUri('public://test.jpeg'), 'public://styles/' . $image_style->id() . '/public/test.jpeg.png');
|
||||
|
||||
// Image style that doesn't change the extension.
|
||||
$image_effect_id = $this->randomMachineName();
|
||||
$image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase')
|
||||
->setConstructorArgs(array(array(), $image_effect_id, array(), $logger))
|
||||
->getMock();
|
||||
$image_effect->expects($this->any())
|
||||
->method('getDerivativeExtension')
|
||||
->will($this->returnArgument(0));
|
||||
|
||||
$image_style = $this->getImageStyleMock($image_effect_id, $image_effect);
|
||||
$this->assertEquals($image_style->buildUri('public://test.jpeg'), 'public://styles/' . $image_style->id() . '/public/test.jpeg');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getPathToken
|
||||
*/
|
||||
public function testGetPathToken() {
|
||||
$logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock();
|
||||
$private_key = $this->randomMachineName();
|
||||
$hash_salt = $this->randomMachineName();
|
||||
|
||||
// Image style that changes the extension.
|
||||
$image_effect_id = $this->randomMachineName();
|
||||
$image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase')
|
||||
->setConstructorArgs(array(array(), $image_effect_id, array(), $logger))
|
||||
->getMock();
|
||||
$image_effect->expects($this->any())
|
||||
->method('getDerivativeExtension')
|
||||
->will($this->returnValue('png'));
|
||||
|
||||
$image_style = $this->getImageStyleMock($image_effect_id, $image_effect, array('getPrivateKey', 'getHashSalt'));
|
||||
$image_style->expects($this->any())
|
||||
->method('getPrivateKey')
|
||||
->will($this->returnValue($private_key));
|
||||
$image_style->expects($this->any())
|
||||
->method('getHashSalt')
|
||||
->will($this->returnValue($hash_salt));
|
||||
|
||||
// Assert the extension has been added to the URI before creating the token.
|
||||
$this->assertEquals($image_style->getPathToken('public://test.jpeg.png'), $image_style->getPathToken('public://test.jpeg'));
|
||||
$this->assertEquals(substr(Crypt::hmacBase64($image_style->id() . ':' . 'public://test.jpeg.png', $private_key . $hash_salt), 0, 8), $image_style->getPathToken('public://test.jpeg'));
|
||||
$this->assertNotEquals(substr(Crypt::hmacBase64($image_style->id() . ':' . 'public://test.jpeg', $private_key . $hash_salt), 0, 8), $image_style->getPathToken('public://test.jpeg'));
|
||||
|
||||
// Image style that doesn't change the extension.
|
||||
$image_effect_id = $this->randomMachineName();
|
||||
$image_effect = $this->getMockBuilder('\Drupal\image\ImageEffectBase')
|
||||
->setConstructorArgs(array(array(), $image_effect_id, array(), $logger))
|
||||
->getMock();
|
||||
$image_effect->expects($this->any())
|
||||
->method('getDerivativeExtension')
|
||||
->will($this->returnArgument(0));
|
||||
|
||||
$image_style = $this->getImageStyleMock($image_effect_id, $image_effect, array('getPrivateKey', 'getHashSalt'));
|
||||
$image_style->expects($this->any())
|
||||
->method('getPrivateKey')
|
||||
->will($this->returnValue($private_key));
|
||||
$image_style->expects($this->any())
|
||||
->method('getHashSalt')
|
||||
->will($this->returnValue($hash_salt));
|
||||
// Assert no extension has been added to the uri before creating the token.
|
||||
$this->assertNotEquals($image_style->getPathToken('public://test.jpeg.png'), $image_style->getPathToken('public://test.jpeg'));
|
||||
$this->assertNotEquals(substr(Crypt::hmacBase64($image_style->id() . ':' . 'public://test.jpeg.png', $private_key . $hash_salt), 0, 8), $image_style->getPathToken('public://test.jpeg'));
|
||||
$this->assertEquals(substr(Crypt::hmacBase64($image_style->id() . ':' . 'public://test.jpeg', $private_key . $hash_salt), 0, 8), $image_style->getPathToken('public://test.jpeg'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock function for ImageStyle::fileUriScheme().
|
||||
*/
|
||||
public function fileUriScheme($uri) {
|
||||
if (preg_match('/^([\w\-]+):\/\/|^(data):/', $uri, $matches)) {
|
||||
// The scheme will always be the last element in the matches array.
|
||||
return array_pop($matches);
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock function for ImageStyle::fileUriTarget().
|
||||
*/
|
||||
public function fileUriTarget($uri) {
|
||||
// Remove the scheme from the URI and remove erroneous leading or trailing,
|
||||
// forward-slashes and backslashes.
|
||||
$target = trim(preg_replace('/^[\w\-]+:\/\/|^data:/', '', $uri), '\/');
|
||||
|
||||
// If nothing was replaced, the URI doesn't have a valid scheme.
|
||||
return $target !== $uri ? $target : FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock function for ImageStyle::fileDefaultScheme().
|
||||
*/
|
||||
public function fileDefaultScheme() {
|
||||
return 'public';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\image\Unit\PageCache\DenyPrivateImageStyleDownloadTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\image\Unit\PageCache;
|
||||
|
||||
use Drupal\Core\PageCache\ResponsePolicyInterface;
|
||||
use Drupal\image\PageCache\DenyPrivateImageStyleDownload;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\image\PageCache\DenyPrivateImageStyleDownload
|
||||
* @group image
|
||||
*/
|
||||
class DenyPrivateImageStyleDownloadTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The response policy under test.
|
||||
*
|
||||
* @var \Drupal\image\PageCache\DenyPrivateImageStyleDownload
|
||||
*/
|
||||
protected $policy;
|
||||
|
||||
/**
|
||||
* A request object.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* A response object.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
protected $response;
|
||||
|
||||
/**
|
||||
* The current route match.
|
||||
*
|
||||
* @var \Drupal\Core\Routing\RouteMatch|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $routeMatch;
|
||||
|
||||
public function setUp() {
|
||||
$this->routeMatch = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
|
||||
$this->policy = new DenyPrivateImageStyleDownload($this->routeMatch);
|
||||
$this->response = new Response();
|
||||
$this->request = new Request();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that caching is denied on the private image style download route.
|
||||
*
|
||||
* @dataProvider providerPrivateImageStyleDownloadPolicy
|
||||
* @covers ::check
|
||||
*/
|
||||
public function testPrivateImageStyleDownloadPolicy($expected_result, $route_name) {
|
||||
$this->routeMatch->expects($this->once())
|
||||
->method('getRouteName')
|
||||
->will($this->returnValue($route_name));
|
||||
|
||||
$actual_result = $this->policy->check($this->response, $this->request);
|
||||
$this->assertSame($expected_result, $actual_result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides data and expected results for the test method.
|
||||
*
|
||||
* @return array
|
||||
* Data and expected results.
|
||||
*/
|
||||
public function providerPrivateImageStyleDownloadPolicy() {
|
||||
return [
|
||||
[ResponsePolicyInterface::DENY, 'image.style_private'],
|
||||
[NULL, 'some.other.route'],
|
||||
[NULL, NULL],
|
||||
[NULL, FALSE],
|
||||
[NULL, TRUE],
|
||||
[NULL, new \StdClass()],
|
||||
[NULL, [1, 2, 3]],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue