3 namespace Drupal\Tests\file_mdm\Kernel;
5 use Drupal\file_mdm\FileMetadataInterface;
8 * Tests that File Metadata Manager works properly.
10 * @group File Metadata
12 class FileMetadataManagerTest extends FileMetadataManagerTestBase {
19 public static $modules = ['system', 'simpletest', 'file_mdm', 'file_test'];
22 * Tests using the 'getimagesize' plugin.
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.
31 // Pass a path instead of the URI.
32 'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg',
40 ['mime', 'image/jpeg'],
45 'uri' => 'public://test-exif.jpeg',
53 ['mime', 'image/jpeg'],
57 // PHP getimagesize works on remote stream wrappers.
58 'uri' => 'dummy-remote://test-exif.jpeg',
66 ['mime', 'image/jpeg'],
70 // JPEG Image with GPS data.
71 'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/1024-2006_1011_093752.jpg',
79 ['mime', 'image/jpeg'],
84 'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/sample-1.tiff',
89 [2, IMAGETYPE_TIFF_MM],
90 ['mime', 'image/tiff'],
95 'uri' => 'public://image-test.png',
102 ['mime', 'image/png'],
107 // Get the file metadata manager service.
108 $fmdm = $this->container->get('file_metadata_manager');
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'));
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);
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']));
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'));
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'));
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']));
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]));
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);
177 // The image files that will be tested.
181 'uri' => 'public://image-test.gif',
189 ['mime', 'image/gif'],
193 // Pass a path instead of the URI.
194 'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg',
202 ['mime', 'image/jpeg'],
206 // PHP getimagesize works on remote stream wrappers.
207 'uri' => 'dummy-remote://image-test.png',
215 ['mime', 'image/png'],
220 // Get the file metadata manager service.
221 $fmdm = $this->container->get('file_metadata_manager');
223 // Walk through test files.
224 foreach ($image_files as $image_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'));
231 $file_metadata = NULL;
232 $this->assertTrue($fmdm->release($image_file['uri']));
233 $this->assertEqual(0, $fmdm->count());
235 if ($image_file['delete']) {
237 file_unmanaged_delete($image_file['uri']);
238 // No file to be found at URI.
239 $this->assertFalse(file_exists($image_file['uri']));
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'));
249 $this->assertIdentical(FileMetadataInterface::LOADED_FROM_FILE, $file_metadata->isMetadataLoaded('getimagesize'));
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);
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));
264 // Save again to cache.
265 if ($image_file['cache']) {
266 $this->assertTrue($file_metadata->saveMetadataToCache('getimagesize'));
269 $this->assertFalse($file_metadata->saveMetadataToCache('getimagesize'));
272 if ($image_file['cache']) {
274 $file_metadata = NULL;
275 $this->assertTrue($fmdm->release($image_file['uri']));
276 $this->assertIdentical(0, $fmdm->count());
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'));
285 $file_metadata = NULL;
286 $this->assertTrue($fmdm->release($image_file['uri']));
287 $this->assertEqual(0, $fmdm->count());
292 * Tests remote files, setting local temp path explicitly.
294 public function testRemoteFileSetLocalPath() {
295 // The image files that will be tested.
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'),
308 ['mime', 'image/jpeg'],
313 // Get the file metadata manager service.
314 $fmdm = $this->container->get('file_metadata_manager');
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);
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);
333 // Copies temp to destination URI.
334 $this->assertTrue($file_metadata->copyTempToUri());
335 $this->assertTrue(file_exists($image_file['uri']));
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'));
348 * Tests remote files, letting file_mdm manage setting local temp path.
350 public function testRemoteFileCopy() {
351 // The image files that will be tested.
354 // Remote storage file. Pass the path to a local copy of the file.
355 'uri' => 'dummy-remote://test-exif.jpeg',
363 ['mime', 'image/jpeg'],
368 // Get the file metadata manager service.
369 $fmdm = $this->container->get('file_metadata_manager');
370 $file_system = $this->container->get('file_system');
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);
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);
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'));
400 * Tests URI sanitization.
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');
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);
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'));
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'));