You are here

public function FileMetadataExifTest::testJpegExifSaveToFile 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::testJpegExifSaveToFile()

Test writing metadata to JPEG file.

File

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

Class

FileMetadataExifTest
Tests that File Metadata EXIF plugin works properly.

Namespace

Drupal\Tests\file_mdm_exif\Kernel

Code

public function testJpegExifSaveToFile() {
  $fmdm = $this->container
    ->get('file_metadata_manager');

  // Copy test file to public://.
  file_unmanaged_copy(drupal_get_path('module', 'image_effects') . '/tests/images/portrait-painting.jpg', 'public://', FILE_EXISTS_REPLACE);
  $file_uri = 'public://portrait-painting.jpg';
  $file_metadata = $fmdm
    ->uri($file_uri);

  // Check values via exif_read_data before operations.
  $data = @exif_read_data($file_uri);
  $this
    ->assertEquals(8, $data['Orientation']);
  $this
    ->assertFalse(isset($data['Artist']));
  $this
    ->assertEquals('Canon', $data['Make']);
  $this
    ->assertEquals(800, $data['ISOSpeedRatings']);

  // Change the Orientation tag from IFD0.
  $this
    ->assertEquals(8, $file_metadata
    ->getMetadata('exif', 'orientation')['value']);
  $this
    ->assertTrue($file_metadata
    ->setMetadata('exif', 'orientation', 4));
  $this
    ->assertEquals(4, $file_metadata
    ->getMetadata('exif', 'orientation')['value']);

  // Add the Artist tag to IFD0.
  $this
    ->assertEquals(48, $this
    ->countMetadataKeys($file_metadata, 'exif'));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', 'artist'));
  $artist_tag = $this->container
    ->get('file_mdm_exif.tag_mapper')
    ->resolveKeyToIfdAndTag('artist');
  $artist = new PelEntryAscii($artist_tag['tag'], 'shot by foo!');
  $file_metadata
    ->setMetadata('exif', 'artist', $artist);
  $this
    ->assertEquals('shot by foo!', $file_metadata
    ->getMetadata('exif', 'artist')['value']);
  $this
    ->assertEquals(49, $this
    ->countMetadataKeys($file_metadata, 'exif'));

  // Setting an unknown tag leads to failure.
  $this
    ->assertFalse($file_metadata
    ->setMetadata('exif', 'bar', 'qux'));

  // Remove the Make tag from IFD0.
  $this
    ->assertEquals('Canon', $file_metadata
    ->getMetadata('exif', 'make')['value']);
  $this
    ->assertTrue($file_metadata
    ->removeMetadata('exif', 'make'));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', 'make'));
  $this
    ->assertEquals(48, $this
    ->countMetadataKeys($file_metadata, 'exif'));

  // Add the ImageDescription tag to IFD1.
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', [
    1,
    'imagedescription',
  ]));
  $desc_tag = $this->container
    ->get('file_mdm_exif.tag_mapper')
    ->resolveKeyToIfdAndTag([
    1,
    'imagedescription',
  ]);
  $desc = new PelEntryAscii($desc_tag['tag'], 'awesome!');
  $file_metadata
    ->setMetadata('exif', [
    1,
    'imagedescription',
  ], $desc);
  $this
    ->assertEquals('awesome!', $file_metadata
    ->getMetadata('exif', [
    1,
    'imagedescription',
  ])['value']);
  $this
    ->assertEquals(49, $this
    ->countMetadataKeys($file_metadata, 'exif'));

  // Remove the Compression tag from IFD1.
  $this
    ->assertEquals(6, $file_metadata
    ->getMetadata('exif', [
    1,
    'compression',
  ])['value']);
  $this
    ->assertTrue($file_metadata
    ->removeMetadata('exif', [
    1,
    'compression',
  ]));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', [
    1,
    'compression',
  ]));
  $this
    ->assertEquals(48, $this
    ->countMetadataKeys($file_metadata, 'exif'));

  // Add the BrightnessValue tag to EXIF.
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', [
    'exif',
    'brightnessvalue',
  ]));
  $brightness_tag = $this->container
    ->get('file_mdm_exif.tag_mapper')
    ->resolveKeyToIfdAndTag([
    'exif',
    'brightnessvalue',
  ]);
  $brightness = new PelEntrySRational($brightness_tag['tag'], [
    12,
    4,
  ]);
  $file_metadata
    ->setMetadata('exif', [
    'exif',
    'brightnessvalue',
  ], $brightness);
  $this
    ->assertEquals('12/4', $file_metadata
    ->getMetadata('exif', [
    'exif',
    'brightnessvalue',
  ])['text']);
  $this
    ->assertEquals(49, $this
    ->countMetadataKeys($file_metadata, 'exif'));

  // Remove the ISOSpeedRatings tag from EXIF.
  $this
    ->assertEquals(800, $file_metadata
    ->getMetadata('exif', [
    'exif',
    'isospeedratings',
  ])['value']);
  $this
    ->assertTrue($file_metadata
    ->removeMetadata('exif', [
    'exif',
    'isospeedratings',
  ]));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', [
    'exif',
    'isospeedratings',
  ]));
  $this
    ->assertEquals(48, $this
    ->countMetadataKeys($file_metadata, 'exif'));

  // Add the RelatedImageFileFormat tag to INTEROP.
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', [
    'interop',
    'RelatedImageFileFormat',
  ]));
  $ff_tag = $this->container
    ->get('file_mdm_exif.tag_mapper')
    ->resolveKeyToIfdAndTag([
    'interop',
    'RelatedImageFileFormat',
  ]);
  $ff = new PelEntryAscii($ff_tag['tag'], 'qux');
  $file_metadata
    ->setMetadata('exif', [
    'interop',
    'RelatedImageFileFormat',
  ], $ff);
  $this
    ->assertEquals('qux', $file_metadata
    ->getMetadata('exif', [
    'interop',
    'RelatedImageFileFormat',
  ])['text']);
  $this
    ->assertEquals(49, $this
    ->countMetadataKeys($file_metadata, 'exif'));

  // Remove the InteroperabilityIndex tag from INTEROP.
  $this
    ->assertEquals('R98', $file_metadata
    ->getMetadata('exif', [
    'interop',
    'InteroperabilityIndex',
  ])['value']);
  $this
    ->assertTrue($file_metadata
    ->removeMetadata('exif', [
    'interop',
    'InteroperabilityIndex',
  ]));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', [
    'interop',
    'InteroperabilityIndex',
  ]));
  $this
    ->assertEquals(48, $this
    ->countMetadataKeys($file_metadata, 'exif'));

  // Add Longitude/Latitude tags to GPS.
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', 'GPSLatitudeRef'));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', 'GPSLatitude'));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', 'GPSLongitudeRef'));
  $this
    ->assertNull($file_metadata
    ->getMetadata('exif', 'GPSLongitude'));
  $atr_tag = $this->container
    ->get('file_mdm_exif.tag_mapper')
    ->resolveKeyToIfdAndTag('GPSLatitudeRef');
  $at_tag = $this->container
    ->get('file_mdm_exif.tag_mapper')
    ->resolveKeyToIfdAndTag('GPSLatitude');
  $otr_tag = $this->container
    ->get('file_mdm_exif.tag_mapper')
    ->resolveKeyToIfdAndTag('GPSLongitudeRef');
  $ot_tag = $this->container
    ->get('file_mdm_exif.tag_mapper')
    ->resolveKeyToIfdAndTag('GPSLongitude');
  $atr = new PelEntryAscii($atr_tag['tag'], 'N');
  $at = new PelEntryRational($at_tag['tag'], [
    46,
    1,
  ], [
    37,
    1,
  ], [
    59448,
    10000,
  ]);
  $otr = new PelEntryAscii($otr_tag['tag'], 'E');
  $ot = new PelEntryRational($ot_tag['tag'], [
    12,
    1,
  ], [
    17,
    1,
  ], [
    488112,
    10000,
  ]);
  $file_metadata
    ->setMetadata('exif', 'GPSLatitudeRef', $atr);
  $file_metadata
    ->setMetadata('exif', 'GPSLatitude', $at);
  $file_metadata
    ->setMetadata('exif', 'GPSLongitudeRef', $otr);
  $file_metadata
    ->setMetadata('exif', 'GPSLongitude', $ot);
  $this
    ->assertEquals('N', $file_metadata
    ->getMetadata('exif', 'GPSLatitudeRef')['text']);
  $this
    ->assertNotNull($file_metadata
    ->getMetadata('exif', 'GPSLatitude')['text']);
  $this
    ->assertEquals('E', $file_metadata
    ->getMetadata('exif', 'GPSLongitudeRef')['text']);
  $this
    ->assertNotNull($file_metadata
    ->getMetadata('exif', 'GPSLongitude')['text']);
  $this
    ->assertEquals(52, $this
    ->countMetadataKeys($file_metadata, 'exif'));

  // Save metadata to file.
  $this
    ->assertTrue($file_metadata
    ->saveMetadataToFile('exif'));

  // Check results via exif_read_data.
  $data = @exif_read_data($file_uri);
  $this
    ->assertEquals(4, $data['Orientation']);
  $this
    ->assertEquals('shot by foo!', $data['Artist']);
  $this
    ->assertFalse(isset($data['Make']));
  $this
    ->assertEquals('12/4', $data['BrightnessValue']);
  $this
    ->assertFalse(isset($data['ISOSpeedRatings']));
  $this
    ->assertEquals('qux', $data['RelatedFileFormat']);
  $this
    ->assertFalse(isset($data['InterOperabilityIndex']));
  $this
    ->assertEquals('N', $data['GPSLatitudeRef']);
  $this
    ->assertEquals([
    '46/1',
    '37/1',
    '59448/10000',
  ], $data['GPSLatitude']);
  $this
    ->assertEquals('E', $data['GPSLongitudeRef']);
  $this
    ->assertEquals([
    '12/1',
    '17/1',
    '488112/10000',
  ], $data['GPSLongitude']);

  // Test writing metadata to a file that has no EXIF info.
  file_unmanaged_copy(drupal_get_path('module', 'simpletest') . '/files/image-2.jpg', 'public://', FILE_EXISTS_REPLACE);
  $test_2 = $fmdm
    ->uri('public://image-2.jpg');
  $this
    ->assertEquals(0, $this
    ->countMetadataKeys($test_2, 'exif'));

  // Load EXIF metadata from previous file processed.
  $test_2
    ->loadMetadata('exif', $file_metadata
    ->getMetadata('exif'));

  // Save metadata to file.
  $this
    ->assertTrue($test_2
    ->saveMetadataToFile('exif'));
  $this
    ->assertEquals(52, $this
    ->countMetadataKeys($test_2, 'exif'));

  // Check results via exif_read_data.
  $data = @exif_read_data('public://image-2.jpg');
  $this
    ->assertEquals(4, $data['Orientation']);
  $this
    ->assertEquals('shot by foo!', $data['Artist']);
  $this
    ->assertEquals('12/4', $data['BrightnessValue']);
  $this
    ->assertEquals('qux', $data['RelatedFileFormat']);
  $this
    ->assertEquals('N', $data['GPSLatitudeRef']);
  $this
    ->assertEquals([
    '46/1',
    '37/1',
    '59448/10000',
  ], $data['GPSLatitude']);
  $this
    ->assertEquals('E', $data['GPSLongitudeRef']);
  $this
    ->assertEquals([
    '12/1',
    '17/1',
    '488112/10000',
  ], $data['GPSLongitude']);

  // Check that after save, file metadata is retrieved from file first time,
  // then from cache in further requests.
  $file_metadata = NULL;
  $this
    ->assertTrue($fmdm
    ->release($file_uri));
  $file_metadata = $fmdm
    ->uri($file_uri);
  $this
    ->assertEquals(52, $this
    ->countMetadataKeys($file_metadata, 'exif'));
  $this
    ->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $file_metadata
    ->isMetadataLoaded('exif'));
  $file_metadata = NULL;
  $this
    ->assertTrue($fmdm
    ->release($file_uri));
  $file_metadata = $fmdm
    ->uri($file_uri);
  $this
    ->assertEquals(52, $this
    ->countMetadataKeys($file_metadata, 'exif'));
  $this
    ->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata
    ->isMetadataLoaded('exif'));
}