You are here

public function FileMetadataManagerTest::testRemoteFileCopy in File metadata manager 8

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

Tests remote files, letting file_mdm manage setting local temp path.

File

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

Class

FileMetadataManagerTest
Tests that File Metadata Manager works properly.

Namespace

Drupal\Tests\file_mdm\Kernel

Code

public function testRemoteFileCopy() {

  // The image files that will be tested.
  $image_files = [
    [
      // Remote storage file. Pass the path to a local copy of the file.
      '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',
        ],
      ],
    ],
  ];

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

  // Copy the test file to dummy-remote wrapper.
  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();

    // File to be found at destination URI.
    $this
      ->assertTrue(file_exists($image_file['uri']));

    // File to be found at local temp 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);
    }

    // Release URI and check metadata was cached.
    $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'));
  }
}