Mercurial > hg > isophonics-drupal-site
comparison core/modules/image/tests/src/Kernel/ImageStyleCustomStreamWrappersTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\image\Kernel; | |
4 | |
5 use Drupal\Core\DependencyInjection\ContainerBuilder; | |
6 use Drupal\Core\StreamWrapper\PrivateStream; | |
7 use Drupal\Core\StreamWrapper\PublicStream; | |
8 use Drupal\file_test\StreamWrapper\DummyReadOnlyStreamWrapper; | |
9 use Drupal\file_test\StreamWrapper\DummyRemoteReadOnlyStreamWrapper; | |
10 use Drupal\file_test\StreamWrapper\DummyStreamWrapper; | |
11 use Drupal\image\Entity\ImageStyle; | |
12 use Drupal\KernelTests\KernelTestBase; | |
13 | |
14 /** | |
15 * Tests derivative generation with source images using stream wrappers. | |
16 * | |
17 * @group image | |
18 */ | |
19 class ImageStyleCustomStreamWrappersTest extends KernelTestBase { | |
20 | |
21 /** | |
22 * Modules to enable. | |
23 * | |
24 * @var string[] | |
25 */ | |
26 public static $modules = ['system', 'image']; | |
27 | |
28 /** | |
29 * A testing image style entity. | |
30 * | |
31 * @var \Drupal\image\ImageStyleInterface | |
32 */ | |
33 protected $imageStyle; | |
34 | |
35 /** | |
36 * The file system service. | |
37 * | |
38 * @var \Drupal\Core\File\FileSystemInterface | |
39 */ | |
40 protected $fileSystem; | |
41 | |
42 /** | |
43 * {@inheritdoc} | |
44 */ | |
45 protected function setUp() { | |
46 parent::setUp(); | |
47 $this->fileSystem = $this->container->get('file_system'); | |
48 $this->config('system.file')->set('default_scheme', 'public')->save(); | |
49 $this->imageStyle = ImageStyle::create(['name' => 'test']); | |
50 $this->imageStyle->save(); | |
51 } | |
52 | |
53 /** | |
54 * {@inheritdoc} | |
55 */ | |
56 public function register(ContainerBuilder $container) { | |
57 parent::register($container); | |
58 foreach ($this->providerTestCustomStreamWrappers() as $stream_wrapper) { | |
59 $scheme = $stream_wrapper[0]; | |
60 $class = $stream_wrapper[2]; | |
61 $container->register("stream_wrapper.$scheme", $class) | |
62 ->addTag('stream_wrapper', ['scheme' => $scheme]); | |
63 } | |
64 } | |
65 | |
66 /** | |
67 * Tests derivative creation with several source on a local writable stream. | |
68 * | |
69 * @dataProvider providerTestCustomStreamWrappers | |
70 * | |
71 * @param string $source_scheme | |
72 * The source stream wrapper scheme. | |
73 * @param string $expected_scheme | |
74 * The derivative expected stream wrapper scheme. | |
75 */ | |
76 public function testCustomStreamWrappers($source_scheme, $expected_scheme) { | |
77 $derivative_uri = $this->imageStyle->buildUri("$source_scheme://some/path/image.png"); | |
78 $derivative_scheme = $this->fileSystem->uriScheme($derivative_uri); | |
79 | |
80 // Check that the derivative scheme is the expected scheme. | |
81 $this->assertSame($expected_scheme, $derivative_scheme); | |
82 | |
83 // Check that the derivative URI is the expected one. | |
84 $expected_uri = "$expected_scheme://styles/{$this->imageStyle->id()}/$source_scheme/some/path/image.png"; | |
85 $this->assertSame($expected_uri, $derivative_uri); | |
86 } | |
87 | |
88 /** | |
89 * Provide test cases for testCustomStreamWrappers(). | |
90 * | |
91 * Derivatives created from writable source stream wrappers will inherit the | |
92 * scheme from source. Derivatives created from read-only stream wrappers will | |
93 * fall-back to the default scheme. | |
94 * | |
95 * @return array[] | |
96 * An array having each element an array with three items: | |
97 * - The source stream wrapper scheme. | |
98 * - The derivative expected stream wrapper scheme. | |
99 * - The stream wrapper service class. | |
100 */ | |
101 public function providerTestCustomStreamWrappers() { | |
102 return [ | |
103 ['public', 'public', PublicStream::class], | |
104 ['private', 'private', PrivateStream::class], | |
105 ['dummy', 'dummy', DummyStreamWrapper::class], | |
106 ['dummy-readonly', 'public', DummyReadOnlyStreamWrapper::class], | |
107 ['dummy-remote-readonly', 'public', DummyRemoteReadOnlyStreamWrapper::class], | |
108 ]; | |
109 } | |
110 | |
111 } |