View source
<?php
namespace Drupal\Tests\file_mdm\Kernel;
use Drupal\file_mdm\FileMetadataInterface;
class FileMetadataManagerTest extends FileMetadataManagerTestBase {
public static $modules = [
'system',
'simpletest',
'file_mdm',
'file_test',
];
public function testFileMetadata() {
file_unmanaged_copy(drupal_get_path('module', 'simpletest') . '/files/image-test.png', 'public://', FILE_EXISTS_REPLACE);
file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'public://', FILE_EXISTS_REPLACE);
$image_files = [
[
'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg',
'count_keys' => 7,
'test_keys' => [
[
0,
100,
],
[
1,
75,
],
[
2,
IMAGETYPE_JPEG,
],
[
'bits',
8,
],
[
'channels',
3,
],
[
'mime',
'image/jpeg',
],
],
],
[
'uri' => 'public://test-exif.jpeg',
'count_keys' => 7,
'test_keys' => [
[
0,
100,
],
[
1,
75,
],
[
2,
IMAGETYPE_JPEG,
],
[
'bits',
8,
],
[
'channels',
3,
],
[
'mime',
'image/jpeg',
],
],
],
[
'uri' => 'dummy-remote://test-exif.jpeg',
'count_keys' => 7,
'test_keys' => [
[
0,
100,
],
[
1,
75,
],
[
2,
IMAGETYPE_JPEG,
],
[
'bits',
8,
],
[
'channels',
3,
],
[
'mime',
'image/jpeg',
],
],
],
[
'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/1024-2006_1011_093752.jpg',
'count_keys' => 7,
'test_keys' => [
[
0,
1024,
],
[
1,
768,
],
[
2,
IMAGETYPE_JPEG,
],
[
'bits',
8,
],
[
'channels',
3,
],
[
'mime',
'image/jpeg',
],
],
],
[
'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/sample-1.tiff',
'count_keys' => 5,
'test_keys' => [
[
0,
174,
],
[
1,
38,
],
[
2,
IMAGETYPE_TIFF_MM,
],
[
'mime',
'image/tiff',
],
],
],
[
'uri' => 'public://image-test.png',
'count_keys' => 6,
'test_keys' => [
[
0,
40,
],
[
1,
20,
],
[
2,
IMAGETYPE_PNG,
],
[
'bits',
8,
],
[
'mime',
'image/png',
],
],
],
];
$fmdm = $this->container
->get('file_metadata_manager');
foreach ($image_files as $image_file) {
$file_metadata = $fmdm
->uri($image_file['uri']);
$this
->assertNotNull($file_metadata
->getMetadata('getimagesize'));
$this
->assertEquals($image_file['count_keys'], $this
->countMetadataKeys($file_metadata, 'getimagesize'));
foreach ($image_file['test_keys'] as $test) {
$entry = $file_metadata
->getMetadata('getimagesize', $test[0]);
$this
->assertEquals($test[1], $entry);
}
$this
->assertNull($file_metadata
->getMetadata('getimagesize', 'baz'));
$this
->assertNull($file_metadata
->getMetadata('getimagesize', [
'qux' => 'laa',
]));
$this
->assertTrue($file_metadata
->setMetadata('getimagesize', 'mime', 'foo/bar'));
$this
->assertEquals('foo/bar', $file_metadata
->getMetadata('getimagesize', 'mime'));
$this
->assertFalse($file_metadata
->setMetadata('getimagesize', 'baz', 'qux'));
$this
->assertNull($file_metadata
->getMetadata('getimagesize', 'baz'));
$this
->assertFalse($file_metadata
->setMetadata('getimagesize', [
'qux' => 'laa',
], 'hoz'));
$this
->assertTrue($file_metadata
->removeMetadata('getimagesize', 'mime'));
$this
->assertEquals($image_file['count_keys'] - 1, $this
->countMetadataKeys($file_metadata, 'getimagesize'));
$this
->assertNull($file_metadata
->getMetadata('getimagesize', 'mime'));
$this
->assertFalse($file_metadata
->removeMetadata('getimagesize', 'baz'));
$this
->assertFalse($file_metadata
->removeMetadata('getimagesize', [
'qux' => 'laa',
]));
$this
->assertNull($file_metadata
->getMetadata('laila', 'han'));
$this
->assertFalse($file_metadata
->setMetadata('laila', 'han', 'solo'));
$this
->assertFalse($file_metadata
->removeMetadata('laila', 'han'));
}
$this
->assertEquals(6, $fmdm
->count());
$this
->assertTrue($fmdm
->has($image_files[0]['uri']));
$this
->assertTrue($fmdm
->release($image_files[0]['uri']));
$this
->assertEquals(5, $fmdm
->count());
$this
->assertFalse($fmdm
->has($image_files[0]['uri']));
$this
->assertFalse($fmdm
->release($image_files[0]['uri']));
$file_metadata_from = $fmdm
->uri($image_files[0]['uri']);
$this
->assertEquals(6, $fmdm
->count());
$metadata = $file_metadata_from
->getMetadata('getimagesize');
$new_file_metadata = $fmdm
->uri('public://test-output.jpeg');
$this
->assertEquals(7, $fmdm
->count());
$new_file_metadata
->loadMetadata('getimagesize', $metadata);
$this
->assertEquals($image_files[0]['count_keys'], $this
->countMetadataKeys($new_file_metadata, 'getimagesize'));
foreach ($image_files[0]['test_keys'] as $test) {
$entry = $file_metadata
->getMetadata('getimagesize', $test[0]);
$this
->assertEquals($test[1], $new_file_metadata
->getMetadata('getimagesize', $test[0]));
}
}
public function testFileMetadataCaching() {
file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'public://', FILE_EXISTS_REPLACE);
file_unmanaged_copy(drupal_get_path('module', 'simpletest') . '/files/image-test.gif', 'public://', FILE_EXISTS_REPLACE);
file_unmanaged_copy(drupal_get_path('module', 'simpletest') . '/files/image-test.png', 'public://', FILE_EXISTS_REPLACE);
$image_files = [
[
'uri' => 'public://image-test.gif',
'cache' => TRUE,
'delete' => TRUE,
'count_keys' => 7,
'test_keys' => [
[
0,
40,
],
[
1,
20,
],
[
2,
IMAGETYPE_GIF,
],
[
'mime',
'image/gif',
],
],
],
[
'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg',
'cache' => FALSE,
'delete' => FALSE,
'count_keys' => 7,
'test_keys' => [
[
0,
100,
],
[
1,
75,
],
[
2,
IMAGETYPE_JPEG,
],
[
'mime',
'image/jpeg',
],
],
],
[
'uri' => 'dummy-remote://image-test.png',
'cache' => TRUE,
'delete' => TRUE,
'count_keys' => 6,
'test_keys' => [
[
0,
40,
],
[
1,
20,
],
[
2,
IMAGETYPE_PNG,
],
[
'mime',
'image/png',
],
],
],
];
$fmdm = $this->container
->get('file_metadata_manager');
foreach ($image_files as $image_file) {
$file_metadata = $fmdm
->uri($image_file['uri']);
$this
->assertNotNull($file_metadata
->getMetadata('getimagesize'));
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $file_metadata
->isMetadataLoaded('getimagesize'));
$file_metadata = NULL;
$this
->assertTrue($fmdm
->release($image_file['uri']));
$this
->assertEquals(0, $fmdm
->count());
if ($image_file['delete']) {
file_unmanaged_delete($image_file['uri']);
$this
->assertFalse(file_exists($image_file['uri']));
}
$file_metadata = $fmdm
->uri($image_file['uri']);
$this
->assertNotNull($file_metadata
->getMetadata('getimagesize'));
if ($image_file['cache']) {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata
->isMetadataLoaded('getimagesize'));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $file_metadata
->isMetadataLoaded('getimagesize'));
}
$this
->assertEquals($image_file['count_keys'], $this
->countMetadataKeys($file_metadata, 'getimagesize'));
foreach ($image_file['test_keys'] as $test) {
$entry = $file_metadata
->getMetadata('getimagesize', $test[0]);
$this
->assertEquals($test[1], $entry);
}
$this
->assertTrue($file_metadata
->setMetadata('getimagesize', 'mime', 'foo/bar'));
$this
->assertTrue($file_metadata
->removeMetadata('getimagesize', 0));
$this
->assertTrue($file_metadata
->removeMetadata('getimagesize', 1));
$this
->assertTrue($file_metadata
->removeMetadata('getimagesize', 2));
$this
->assertTrue($file_metadata
->removeMetadata('getimagesize', 3));
if ($image_file['cache']) {
$this
->assertTrue($file_metadata
->saveMetadataToCache('getimagesize'));
}
else {
$this
->assertFalse($file_metadata
->saveMetadataToCache('getimagesize'));
}
if ($image_file['cache']) {
$file_metadata = NULL;
$this
->assertTrue($fmdm
->release($image_file['uri']));
$this
->assertSame(0, $fmdm
->count());
$file_metadata = $fmdm
->uri($image_file['uri']);
$this
->assertSame($image_file['count_keys'] - 4, $this
->countMetadataKeys($file_metadata, 'getimagesize'));
$this
->assertSame('foo/bar', $file_metadata
->getMetadata('getimagesize', 'mime'));
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata
->isMetadataLoaded('getimagesize'));
}
$file_metadata = NULL;
$this
->assertTrue($fmdm
->release($image_file['uri']));
$this
->assertEquals(0, $fmdm
->count());
}
}
public function testRemoteFileSetLocalPath() {
$image_files = [
[
'uri' => 'dummy-remote://test-exif.jpeg',
'local_path' => $this->container
->get('file_system')
->realpath('temporary://test-exif.jpeg'),
'count_keys' => 7,
'test_keys' => [
[
0,
100,
],
[
1,
75,
],
[
2,
IMAGETYPE_JPEG,
],
[
'bits',
8,
],
[
'channels',
3,
],
[
'mime',
'image/jpeg',
],
],
],
];
$fmdm = $this->container
->get('file_metadata_manager');
file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'temporary://', FILE_EXISTS_REPLACE);
foreach ($image_files as $image_file) {
$file_metadata = $fmdm
->uri($image_file['uri']);
$file_metadata
->setLocalTempPath($image_file['local_path']);
$this
->assertFalse(file_exists($image_file['uri']));
$this
->assertTrue(file_exists($file_metadata
->getLocalTempPath()));
$this
->assertEquals($image_file['count_keys'], $this
->countMetadataKeys($file_metadata, 'getimagesize'));
foreach ($image_file['test_keys'] as $test) {
$entry = $file_metadata
->getMetadata('getimagesize', $test[0]);
$this
->assertEquals($test[1], $entry);
}
$this
->assertTrue($file_metadata
->copyTempToUri());
$this
->assertTrue(file_exists($image_file['uri']));
$file_metadata = NULL;
$this
->assertTrue($fmdm
->release($image_file['uri']));
$this
->assertEquals(0, $fmdm
->count());
$file_metadata = $fmdm
->uri($image_file['uri']);
$this
->assertNotNull($file_metadata
->getMetadata('getimagesize'));
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata
->isMetadataLoaded('getimagesize'));
}
}
public function testRemoteFileCopy() {
$image_files = [
[
'uri' => 'dummy-remote://test-exif.jpeg',
'count_keys' => 7,
'test_keys' => [
[
0,
100,
],
[
1,
75,
],
[
2,
IMAGETYPE_JPEG,
],
[
'bits',
8,
],
[
'channels',
3,
],
[
'mime',
'image/jpeg',
],
],
],
];
$fmdm = $this->container
->get('file_metadata_manager');
$file_system = $this->container
->get('file_system');
file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'dummy-remote://', FILE_EXISTS_REPLACE);
foreach ($image_files as $image_file) {
$file_metadata = $fmdm
->uri($image_file['uri']);
$file_metadata
->copyUriToTemp();
$this
->assertTrue(file_exists($image_file['uri']));
$this
->assertSame(0, strpos($file_system
->basename($file_metadata
->getLocalTempPath()), 'file_mdm_'));
$this
->assertTrue(file_exists($file_metadata
->getLocalTempPath()));
$this
->assertEquals($image_file['count_keys'], $this
->countMetadataKeys($file_metadata, 'getimagesize'));
foreach ($image_file['test_keys'] as $test) {
$entry = $file_metadata
->getMetadata('getimagesize', $test[0]);
$this
->assertEquals($test[1], $entry);
}
$file_metadata = NULL;
$this
->assertTrue($fmdm
->release($image_file['uri']));
$this
->assertEquals(0, $fmdm
->count());
$file_metadata = $fmdm
->uri($image_file['uri']);
$this
->assertNotNull($file_metadata
->getMetadata('getimagesize'));
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata
->isMetadataLoaded('getimagesize'));
}
}
public function testSanitizedUri() {
$fmdm = $this->container
->get('file_metadata_manager');
$test_directory = 'public://test-images/';
file_prepare_directory($test_directory, FILE_CREATE_DIRECTORY);
file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', $test_directory, FILE_EXISTS_REPLACE);
$file_metadata = $fmdm
->uri('public://test-images/test-exif.jpeg');
$this
->assertEquals(7, $this
->countMetadataKeys($file_metadata, 'getimagesize'));
$this
->assertTrue($fmdm
->has('public://test-images/test-exif.jpeg'));
$this
->assertTrue($fmdm
->has('public:///test-images/test-exif.jpeg'));
$this
->assertTrue($fmdm
->has('public://test-images//test-exif.jpeg'));
$this
->assertTrue($fmdm
->has('public://////test-images////test-exif.jpeg'));
$this
->assertFalse($fmdm
->has('public:/test-images/test-exif.jpeg'));
}
}