Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / file_mdm / tests / src / Kernel / FileMetadataManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\file_mdm\Kernel;
4
5 use Drupal\file_mdm\FileMetadataInterface;
6
7 /**
8  * Tests that File Metadata Manager works properly.
9  *
10  * @group File Metadata
11  */
12 class FileMetadataManagerTest extends FileMetadataManagerTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['system', 'simpletest', 'file_mdm', 'file_test'];
20
21   /**
22    * Tests using the 'getimagesize' plugin.
23    */
24   public function testFileMetadata() {
25     // Prepare a copy of test files.
26     file_unmanaged_copy(drupal_get_path('module', 'simpletest') . '/files/image-test.png', 'public://', FILE_EXISTS_REPLACE);
27     file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'public://', FILE_EXISTS_REPLACE);
28     // The image files that will be tested.
29     $image_files = [
30       [
31         // Pass a path instead of the URI.
32         'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg',
33         'count_keys' => 7,
34         'test_keys' => [
35           [0, 100],
36           [1, 75],
37           [2, IMAGETYPE_JPEG],
38           ['bits', 8],
39           ['channels', 3],
40           ['mime', 'image/jpeg'],
41         ],
42       ],
43       [
44         // Pass a URI.
45         'uri' => 'public://test-exif.jpeg',
46         'count_keys' => 7,
47         'test_keys' => [
48           [0, 100],
49           [1, 75],
50           [2, IMAGETYPE_JPEG],
51           ['bits', 8],
52           ['channels', 3],
53           ['mime', 'image/jpeg'],
54         ],
55       ],
56       [
57         // PHP getimagesize works on remote stream wrappers.
58         'uri' => 'dummy-remote://test-exif.jpeg',
59         'count_keys' => 7,
60         'test_keys' => [
61           [0, 100],
62           [1, 75],
63           [2, IMAGETYPE_JPEG],
64           ['bits', 8],
65           ['channels', 3],
66           ['mime', 'image/jpeg'],
67         ],
68       ],
69       [
70         // JPEG Image with GPS data.
71         'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/1024-2006_1011_093752.jpg',
72         'count_keys' => 7,
73         'test_keys' => [
74           [0, 1024],
75           [1, 768],
76           [2, IMAGETYPE_JPEG],
77           ['bits', 8],
78           ['channels', 3],
79           ['mime', 'image/jpeg'],
80         ],
81       ],
82       [
83         // TIFF image.
84         'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/sample-1.tiff',
85         'count_keys' => 5,
86         'test_keys' => [
87           [0, 174],
88           [1, 38],
89           [2, IMAGETYPE_TIFF_MM],
90           ['mime', 'image/tiff'],
91         ],
92       ],
93       [
94         // PNG image.
95         'uri' => 'public://image-test.png',
96         'count_keys' => 6,
97         'test_keys' => [
98           [0, 40],
99           [1, 20],
100           [2, IMAGETYPE_PNG],
101           ['bits', 8],
102           ['mime', 'image/png'],
103         ],
104       ],
105     ];
106
107     // Get the file metadata manager service.
108     $fmdm = $this->container->get('file_metadata_manager');
109
110     // Walk through test files.
111     foreach ($image_files as $image_file) {
112       $file_metadata = $fmdm->uri($image_file['uri']);
113       $this->assertNotNull($file_metadata->getMetadata('getimagesize'));
114       // Read from file.
115       $this->assertEqual($image_file['count_keys'], $this->countMetadataKeys($file_metadata, 'getimagesize'));
116       foreach ($image_file['test_keys'] as $test) {
117         $entry = $file_metadata->getMetadata('getimagesize', $test[0]);
118         $this->assertEqual($test[1], $entry);
119       }
120       // Try getting an unsupported key.
121       $this->assertNull($file_metadata->getMetadata('getimagesize', 'baz'));
122       // Try getting an invalid key.
123       $this->assertNull($file_metadata->getMetadata('getimagesize', ['qux' => 'laa']));
124       // Change MIME type.
125       $this->assertTrue($file_metadata->setMetadata('getimagesize', 'mime', 'foo/bar'));
126       $this->assertEqual('foo/bar', $file_metadata->getMetadata('getimagesize', 'mime'));
127       // Try adding an unsupported key.
128       $this->assertFalse($file_metadata->setMetadata('getimagesize', 'baz', 'qux'));
129       $this->assertNull($file_metadata->getMetadata('getimagesize', 'baz'));
130       // Try adding an invalid key.
131       $this->assertFalse($file_metadata->setMetadata('getimagesize', ['qux' => 'laa'], 'hoz'));
132       // Remove MIME type.
133       $this->assertTrue($file_metadata->removeMetadata('getimagesize', 'mime'));
134       $this->assertEqual($image_file['count_keys'] - 1, $this->countMetadataKeys($file_metadata, 'getimagesize'));
135       $this->assertNull($file_metadata->getMetadata('getimagesize', 'mime'));
136       // Try removing an unsupported key.
137       $this->assertFalse($file_metadata->removeMetadata('getimagesize', 'baz'));
138       // Try removing an invalid key.
139       $this->assertFalse($file_metadata->removeMetadata('getimagesize', ['qux' => 'laa']));
140       // Try getting/setting/removing metadata for a non-existing plugin.
141       $this->assertNull($file_metadata->getMetadata('laila', 'han'));
142       $this->assertFalse($file_metadata->setMetadata('laila', 'han', 'solo'));
143       $this->assertFalse($file_metadata->removeMetadata('laila', 'han'));
144     }
145
146     // Test releasing URI.
147     $this->assertEqual(6, $fmdm->count());
148     $this->assertTrue($fmdm->has($image_files[0]['uri']));
149     $this->assertTrue($fmdm->release($image_files[0]['uri']));
150     $this->assertEqual(5, $fmdm->count());
151     $this->assertFalse($fmdm->has($image_files[0]['uri']));
152     $this->assertFalse($fmdm->release($image_files[0]['uri']));
153
154     // Test loading metadata from an in-memory object.
155     $file_metadata_from = $fmdm->uri($image_files[0]['uri']);
156     $this->assertEqual(6, $fmdm->count());
157     $metadata = $file_metadata_from->getMetadata('getimagesize');
158     $new_file_metadata = $fmdm->uri('public://test-output.jpeg');
159     $this->assertEqual(7, $fmdm->count());
160     $new_file_metadata->loadMetadata('getimagesize', $metadata);
161     $this->assertEqual($image_files[0]['count_keys'], $this->countMetadataKeys($new_file_metadata, 'getimagesize'));
162     foreach ($image_files[0]['test_keys'] as $test) {
163       $entry = $file_metadata->getMetadata('getimagesize', $test[0]);
164       $this->assertEqual($test[1], $new_file_metadata->getMetadata('getimagesize', $test[0]));
165     }
166   }
167
168   /**
169    * Test caching.
170    */
171   public function testFileMetadataCaching() {
172     // Prepare a copy of test files.
173     file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'public://', FILE_EXISTS_REPLACE);
174     file_unmanaged_copy(drupal_get_path('module', 'simpletest') . '/files/image-test.gif', 'public://', FILE_EXISTS_REPLACE);
175     file_unmanaged_copy(drupal_get_path('module', 'simpletest') . '/files/image-test.png', 'public://', FILE_EXISTS_REPLACE);
176
177     // The image files that will be tested.
178     $image_files = [
179       [
180         // Pass a URI.
181         'uri' => 'public://image-test.gif',
182         'cache' => TRUE,
183         'delete' => TRUE,
184         'count_keys' => 7,
185         'test_keys' => [
186           [0, 40],
187           [1, 20],
188           [2, IMAGETYPE_GIF],
189           ['mime', 'image/gif'],
190         ],
191       ],
192       [
193         // Pass a path instead of the URI.
194         'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg',
195         'cache' => FALSE,
196         'delete' => FALSE,
197         'count_keys' => 7,
198         'test_keys' => [
199           [0, 100],
200           [1, 75],
201           [2, IMAGETYPE_JPEG],
202           ['mime', 'image/jpeg'],
203         ],
204       ],
205       [
206         // PHP getimagesize works on remote stream wrappers.
207         'uri' => 'dummy-remote://image-test.png',
208         'cache' => TRUE,
209         'delete' => TRUE,
210         'count_keys' => 6,
211         'test_keys' => [
212           [0, 40],
213           [1, 20],
214           [2, IMAGETYPE_PNG],
215           ['mime', 'image/png'],
216         ],
217       ],
218     ];
219
220     // Get the file metadata manager service.
221     $fmdm = $this->container->get('file_metadata_manager');
222
223     // Walk through test files.
224     foreach ($image_files as $image_file) {
225       // Read from file.
226       $file_metadata = $fmdm->uri($image_file['uri']);
227       $this->assertNotNull($file_metadata->getMetadata('getimagesize'));
228       $this->assertIdentical(FileMetadataInterface::LOADED_FROM_FILE, $file_metadata->isMetadataLoaded('getimagesize'));
229
230       // Release URI.
231       $file_metadata = NULL;
232       $this->assertTrue($fmdm->release($image_file['uri']));
233       $this->assertEqual(0, $fmdm->count());
234
235       if ($image_file['delete']) {
236         // Delete file.
237         file_unmanaged_delete($image_file['uri']);
238         // No file to be found at URI.
239         $this->assertFalse(file_exists($image_file['uri']));
240       }
241
242       // Read from cache if possible.
243       $file_metadata = $fmdm->uri($image_file['uri']);
244       $this->assertNotNull($file_metadata->getMetadata('getimagesize'));
245       if ($image_file['cache']) {
246         $this->assertIdentical(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata->isMetadataLoaded('getimagesize'));
247       }
248       else {
249         $this->assertIdentical(FileMetadataInterface::LOADED_FROM_FILE, $file_metadata->isMetadataLoaded('getimagesize'));
250       }
251       $this->assertEqual($image_file['count_keys'], $this->countMetadataKeys($file_metadata, 'getimagesize'));
252       foreach ($image_file['test_keys'] as $test) {
253         $entry = $file_metadata->getMetadata('getimagesize', $test[0]);
254         $this->assertEqual($test[1], $entry);
255       }
256
257       // Change MIME type and remove 0, 1, 2, 3.
258       $this->assertTrue($file_metadata->setMetadata('getimagesize', 'mime', 'foo/bar'));
259       $this->assertTrue($file_metadata->removeMetadata('getimagesize', 0));
260       $this->assertTrue($file_metadata->removeMetadata('getimagesize', 1));
261       $this->assertTrue($file_metadata->removeMetadata('getimagesize', 2));
262       $this->assertTrue($file_metadata->removeMetadata('getimagesize', 3));
263
264       // Save again to cache.
265       if ($image_file['cache']) {
266         $this->assertTrue($file_metadata->saveMetadataToCache('getimagesize'));
267       }
268       else {
269         $this->assertFalse($file_metadata->saveMetadataToCache('getimagesize'));
270       }
271
272       if ($image_file['cache']) {
273         // Release URI.
274         $file_metadata = NULL;
275         $this->assertTrue($fmdm->release($image_file['uri']));
276         $this->assertIdentical(0, $fmdm->count());
277
278         // Read from cache.
279         $file_metadata = $fmdm->uri($image_file['uri']);
280         $this->assertIdentical($image_file['count_keys'] - 4, $this->countMetadataKeys($file_metadata, 'getimagesize'));
281         $this->assertIdentical('foo/bar', $file_metadata->getMetadata('getimagesize', 'mime'));
282         $this->assertIdentical(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata->isMetadataLoaded('getimagesize'));
283       }
284
285       $file_metadata = NULL;
286       $this->assertTrue($fmdm->release($image_file['uri']));
287       $this->assertEqual(0, $fmdm->count());
288     }
289   }
290
291   /**
292    * Tests remote files, setting local temp path explicitly.
293    */
294   public function testRemoteFileSetLocalPath() {
295     // The image files that will be tested.
296     $image_files = [
297       [
298         // Remote storage file. Pass the path to a local copy of the file.
299         'uri' => 'dummy-remote://test-exif.jpeg',
300         'local_path' => $this->container->get('file_system')->realpath('temporary://test-exif.jpeg'),
301         'count_keys' => 7,
302         'test_keys' => [
303           [0, 100],
304           [1, 75],
305           [2, IMAGETYPE_JPEG],
306           ['bits', 8],
307           ['channels', 3],
308           ['mime', 'image/jpeg'],
309         ],
310       ],
311     ];
312
313     // Get the file metadata manager service.
314     $fmdm = $this->container->get('file_metadata_manager');
315
316     // Copy the test file to a temp location.
317     file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'temporary://', FILE_EXISTS_REPLACE);
318
319     // Test setting local temp path explicitly. The files should be parsed
320     // even if not available on the URI.
321     foreach ($image_files as $image_file) {
322       $file_metadata = $fmdm->uri($image_file['uri']);
323       $file_metadata->setLocalTempPath($image_file['local_path']);
324       // No file to be found at URI.
325       $this->assertFalse(file_exists($image_file['uri']));
326       // File to be found at local temp path.
327       $this->assertTrue(file_exists($file_metadata->getLocalTempPath()));
328       $this->assertEqual($image_file['count_keys'], $this->countMetadataKeys($file_metadata, 'getimagesize'));
329       foreach ($image_file['test_keys'] as $test) {
330         $entry = $file_metadata->getMetadata('getimagesize', $test[0]);
331         $this->assertEqual($test[1], $entry);
332       }
333       // Copies temp to destination URI.
334       $this->assertTrue($file_metadata->copyTempToUri());
335       $this->assertTrue(file_exists($image_file['uri']));
336
337       // Release URI and check metadata was cached.
338       $file_metadata = NULL;
339       $this->assertTrue($fmdm->release($image_file['uri']));
340       $this->assertEqual(0, $fmdm->count());
341       $file_metadata = $fmdm->uri($image_file['uri']);
342       $this->assertNotNull($file_metadata->getMetadata('getimagesize'));
343       $this->assertIdentical(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata->isMetadataLoaded('getimagesize'));
344     }
345   }
346
347   /**
348    * Tests remote files, letting file_mdm manage setting local temp path.
349    */
350   public function testRemoteFileCopy() {
351     // The image files that will be tested.
352     $image_files = [
353       [
354         // Remote storage file. Pass the path to a local copy of the file.
355         'uri' => 'dummy-remote://test-exif.jpeg',
356         'count_keys' => 7,
357         'test_keys' => [
358           [0, 100],
359           [1, 75],
360           [2, IMAGETYPE_JPEG],
361           ['bits', 8],
362           ['channels', 3],
363           ['mime', 'image/jpeg'],
364         ],
365       ],
366     ];
367
368     // Get the file metadata manager service.
369     $fmdm = $this->container->get('file_metadata_manager');
370     $file_system = $this->container->get('file_system');
371
372     // Copy the test file to dummy-remote wrapper.
373     file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'dummy-remote://', FILE_EXISTS_REPLACE);
374
375     foreach ($image_files as $image_file) {
376       $file_metadata = $fmdm->uri($image_file['uri']);
377       $file_metadata->copyUriToTemp();
378       // File to be found at destination URI.
379       $this->assertTrue(file_exists($image_file['uri']));
380       // File to be found at local temp URI.
381       $this->assertIdentical(0, strpos($file_system->basename($file_metadata->getLocalTempPath()), 'file_mdm_'));
382       $this->assertTrue(file_exists($file_metadata->getLocalTempPath()));
383       $this->assertEqual($image_file['count_keys'], $this->countMetadataKeys($file_metadata, 'getimagesize'));
384       foreach ($image_file['test_keys'] as $test) {
385         $entry = $file_metadata->getMetadata('getimagesize', $test[0]);
386         $this->assertEqual($test[1], $entry);
387       }
388
389       // Release URI and check metadata was cached.
390       $file_metadata = NULL;
391       $this->assertTrue($fmdm->release($image_file['uri']));
392       $this->assertEqual(0, $fmdm->count());
393       $file_metadata = $fmdm->uri($image_file['uri']);
394       $this->assertNotNull($file_metadata->getMetadata('getimagesize'));
395       $this->assertIdentical(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata->isMetadataLoaded('getimagesize'));
396     }
397   }
398
399   /**
400    * Tests URI sanitization.
401    */
402   public function testSanitizedUri() {
403     // Get the file metadata manager service.
404     $fmdm = $this->container->get('file_metadata_manager');
405     $file_system = $this->container->get('file_system');
406
407     // Copy a test file to test directory.
408     $test_directory = 'public://test-images/';
409     file_prepare_directory($test_directory, FILE_CREATE_DIRECTORY);
410     file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', $test_directory, FILE_EXISTS_REPLACE);
411
412     // Get file metadata object.
413     $file_metadata = $fmdm->uri('public://test-images/test-exif.jpeg');
414     $this->assertEqual(7, $this->countMetadataKeys($file_metadata, 'getimagesize'));
415
416     // Check that the file metadata manager has the URI in different forms.
417     $this->assertTrue($fmdm->has('public://test-images/test-exif.jpeg'));
418     $this->assertTrue($fmdm->has('public:///test-images/test-exif.jpeg'));
419     $this->assertTrue($fmdm->has('public://test-images//test-exif.jpeg'));
420     $this->assertTrue($fmdm->has('public://////test-images////test-exif.jpeg'));
421     $this->assertFalse($fmdm->has('public:/test-images/test-exif.jpeg'));
422   }
423
424 }