You are here

public function FileMetadataExifTest::testExifPlugin in File metadata manager 8

Same name and namespace in other branches
  1. 8.2 file_mdm_exif/tests/src/Kernel/FileMetadataExifTest.php \Drupal\Tests\file_mdm_exif\Kernel\FileMetadataExifTest::testExifPlugin()

Test EXIF plugin.

File

file_mdm_exif/tests/src/Kernel/FileMetadataExifTest.php, line 43

Class

FileMetadataExifTest
Tests that File Metadata EXIF plugin works properly.

Namespace

Drupal\Tests\file_mdm_exif\Kernel

Code

public function testExifPlugin() {

  // Prepare a copy of test files.
  file_unmanaged_copy(drupal_get_path('module', 'simpletest') . '/files/image-test.jpg', 'public://', FILE_EXISTS_REPLACE);
  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);
  file_unmanaged_copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'temporary://', FILE_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' => 48,
      'test_keys' => [
        [
          'Orientation',
          8,
        ],
        [
          'orientation',
          8,
        ],
        [
          'OrIeNtAtIoN',
          8,
        ],
        [
          'ShutterSpeedValue',
          [
            106,
            32,
          ],
        ],
        [
          'ApertureValue',
          [
            128,
            32,
          ],
        ],
        [
          [
            'exif',
            'aperturevalue',
          ],
          [
            128,
            32,
          ],
        ],
        [
          [
            2,
            'aperturevalue',
          ],
          [
            128,
            32,
          ],
        ],
        [
          [
            'exif',
            0x9202,
          ],
          [
            128,
            32,
          ],
        ],
        [
          [
            2,
            0x9202,
          ],
          [
            128,
            32,
          ],
        ],
      ],
    ],
    [
      // Pass a URI.
      'uri' => 'public://test-exif.jpeg',
      'count_keys' => 48,
      'test_keys' => [
        [
          'Orientation',
          8,
        ],
        [
          'ShutterSpeedValue',
          [
            106,
            32,
          ],
        ],
      ],
    ],
    [
      // Remote storage file. Let the file be copied to a local temp copy.
      'uri' => 'dummy-remote://test-exif.jpeg',
      'copy_to_temp' => TRUE,
      'count_keys' => 48,
      'test_keys' => [
        [
          'Orientation',
          8,
        ],
        [
          'ShutterSpeedValue',
          [
            106,
            32,
          ],
        ],
      ],
    ],
    [
      // JPEG Image with GPS data.
      'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/1024-2006_1011_093752.jpg',
      'count_keys' => 59,
      'test_keys' => [
        [
          'Orientation',
          1,
        ],
        [
          'FocalLength',
          [
            8513,
            256,
          ],
        ],
        [
          'GPSLatitudeRef',
          'S',
        ],
        [
          'GPSLatitude',
          [
            [
              33,
              1,
            ],
            [
              51,
              1,
            ],
            [
              2191,
              100,
            ],
          ],
        ],
        [
          'GPSLongitudeRef',
          'E',
        ],
        [
          'GPSLongitude',
          [
            [
              151,
              1,
            ],
            [
              13,
              1,
            ],
            [
              1173,
              100,
            ],
          ],
        ],
      ],
    ],
    [
      // JPEG Image with no EXIF data.
      'uri' => 'public://image-test.jpg',
      'count_keys' => 0,
      'test_keys' => [],
    ],
    [
      // TIFF image.
      'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/sample-1.tiff',
      'count_keys' => 11,
      'test_keys' => [
        [
          'Orientation',
          1,
        ],
        [
          'BitsPerSample',
          [
            8,
            8,
            8,
            8,
          ],
        ],
      ],
    ],
    [
      // PNG should not have any data.
      'uri' => 'public://image-test.png',
      'count_keys' => 0,
      'test_keys' => [],
    ],
  ];
  $fmdm = $this->container
    ->get('file_metadata_manager');

  // Walk through test files.
  foreach ($image_files as $image_file) {
    $file_metadata = $fmdm
      ->uri($image_file['uri']);
    if (!$file_metadata) {
      $this
        ->fail("File not found: {$image_file['uri']}");
      continue;
    }
    if (isset($image_file['copy_to_temp'])) {
      $file_metadata
        ->copyUriToTemp();
    }
    $this
      ->assertEquals($image_file['count_keys'], $this
      ->countMetadataKeys($file_metadata, 'exif'));
    foreach ($image_file['test_keys'] as $test) {
      $entry = $file_metadata
        ->getMetadata('exif', $test[0]);
      $this
        ->assertEquals($test[1], $entry ? $entry['value'] : NULL);
    }
  }

  // Test loading metadata from an in-memory object.
  $file_metadata_from = $fmdm
    ->uri($image_files[0]['uri']);
  $metadata = $file_metadata_from
    ->getMetadata('exif');
  $new_file_metadata = $fmdm
    ->uri('public://test-output.jpeg');
  $new_file_metadata
    ->loadMetadata('exif', $metadata);
  $this
    ->assertEquals($image_files[0]['count_keys'], $this
    ->countMetadataKeys($new_file_metadata, 'exif'));
  foreach ($image_files[0]['test_keys'] as $test) {
    $entry = $file_metadata
      ->getMetadata('exif', $test[0]);
    $this
      ->assertEquals($test[1], $new_file_metadata
      ->getMetadata('exif', $test[0])['value']);
  }

  // Test removing metadata.
  $fmdm
    ->release($image_files[0]['uri']);
  $this
    ->assertFalse($fmdm
    ->has($image_files[0]['uri']));
  $file_metadata = $fmdm
    ->uri($image_files[0]['uri']);
  $this
    ->assertEquals($image_files[0]['count_keys'], $this
    ->countMetadataKeys($file_metadata, 'exif'));
  $this
    ->assertTrue($file_metadata
    ->removeMetadata('exif', 'shutterspeedValue'));
  $this
    ->assertTrue($file_metadata
    ->removeMetadata('exif', 'apertureValue'));
  $this
    ->assertFalse($file_metadata
    ->removeMetadata('exif', 'bar'));
  $this
    ->assertEquals($image_files[0]['count_keys'] - 2, $this
    ->countMetadataKeys($file_metadata, 'exif'));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', 'shutterspeedValue'));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', 'apertureValue'));
  $this
    ->assertNotNull($file_metadata
    ->getMetadata('exif', 'orientation'));
}