You are here

public function FileMetadataManagerTest::testFileMetadata in File metadata manager 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/FileMetadataManagerTest.php \Drupal\Tests\file_mdm\Kernel\FileMetadataManagerTest::testFileMetadata()

Tests using the 'getimagesize' plugin.

File

tests/src/Kernel/FileMetadataManagerTest.php, line 25

Class

FileMetadataManagerTest
Tests that File Metadata Manager works properly.

Namespace

Drupal\Tests\file_mdm\Kernel

Code

public function testFileMetadata() {

  // Prepare a copy of test files.
  $this->fileSystem
    ->copy('core/tests/fixtures/files/image-test.png', 'public://', FileSystemInterface::EXISTS_REPLACE);
  $this->fileSystem
    ->copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'public://', FileSystemInterface::EXISTS_REPLACE);

  // The image files that will be tested.
  $image_files = [
    [
      // Pass a path instead of the URI.
      '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',
        ],
      ],
    ],
    [
      // Pass a URI.
      'uri' => 'public://test-exif.jpeg',
      'count_keys' => 7,
      'test_keys' => [
        [
          0,
          100,
        ],
        [
          1,
          75,
        ],
        [
          2,
          IMAGETYPE_JPEG,
        ],
        [
          'bits',
          8,
        ],
        [
          'channels',
          3,
        ],
        [
          'mime',
          'image/jpeg',
        ],
      ],
    ],
    [
      // PHP getimagesize works on remote stream wrappers.
      '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',
        ],
      ],
    ],
    [
      // JPEG Image with GPS data.
      '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',
        ],
      ],
    ],
    [
      // TIFF image.
      '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',
        ],
      ],
    ],
    [
      // PNG image.
      'uri' => 'public://image-test.png',
      'count_keys' => 6,
      'test_keys' => [
        [
          0,
          40,
        ],
        [
          1,
          20,
        ],
        [
          2,
          IMAGETYPE_PNG,
        ],
        [
          'bits',
          8,
        ],
        [
          'mime',
          'image/png',
        ],
      ],
    ],
  ];

  // Get the file metadata manager service.
  $fmdm = $this->container
    ->get('file_metadata_manager');

  // Walk through test files.
  foreach ($image_files as $image_file) {
    $file_metadata = $fmdm
      ->uri($image_file['uri']);
    $this
      ->assertNotNull($file_metadata
      ->getMetadata('getimagesize'));

    // Read from file.
    $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);
    }

    // Try getting an unsupported key.
    $this
      ->assertNull($file_metadata
      ->getMetadata('getimagesize', 'baz'));

    // Try getting an invalid key.
    $this
      ->assertNull($file_metadata
      ->getMetadata('getimagesize', [
      'qux' => 'laa',
    ]));

    // Change MIME type.
    $this
      ->assertTrue($file_metadata
      ->setMetadata('getimagesize', 'mime', 'foo/bar'));
    $this
      ->assertEquals('foo/bar', $file_metadata
      ->getMetadata('getimagesize', 'mime'));

    // Try adding an unsupported key.
    $this
      ->assertFalse($file_metadata
      ->setMetadata('getimagesize', 'baz', 'qux'));
    $this
      ->assertNull($file_metadata
      ->getMetadata('getimagesize', 'baz'));

    // Try adding an invalid key.
    $this
      ->assertFalse($file_metadata
      ->setMetadata('getimagesize', [
      'qux' => 'laa',
    ], 'hoz'));

    // Remove MIME type.
    $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'));

    // Try removing an unsupported key.
    $this
      ->assertFalse($file_metadata
      ->removeMetadata('getimagesize', 'baz'));

    // Try removing an invalid key.
    $this
      ->assertFalse($file_metadata
      ->removeMetadata('getimagesize', [
      'qux' => 'laa',
    ]));

    // Try getting/setting/removing metadata for a non-existing plugin.
    $this
      ->assertNull($file_metadata
      ->getMetadata('laila', 'han'));
    $this
      ->assertFalse($file_metadata
      ->setMetadata('laila', 'han', 'solo'));
    $this
      ->assertFalse($file_metadata
      ->removeMetadata('laila', 'han'));
  }

  // Test releasing URI.
  $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']));

  // Test loading metadata from an in-memory object.
  $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]));
  }
}