You are here

public function FileMetadataManagerTest::testRemoteFileSetLocalPath 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::testRemoteFileSetLocalPath()

Tests remote files, setting local temp path explicitly.

File

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

Class

FileMetadataManagerTest
Tests that File Metadata Manager works properly.

Namespace

Drupal\Tests\file_mdm\Kernel

Code

public function testRemoteFileSetLocalPath() {

  // 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',
      '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',
        ],
      ],
    ],
  ];

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

  // Copy the test file to a temp location.
  $this->fileSystem
    ->copy(drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg', 'temporary://', FileSystemInterface::EXISTS_REPLACE);

  // Test setting local temp path explicitly. The files should be parsed
  // even if not available on the URI.
  foreach ($image_files as $image_file) {
    $file_metadata = $fmdm
      ->uri($image_file['uri']);
    $file_metadata
      ->setLocalTempPath($image_file['local_path']);

    // No file to be found at URI.
    $this
      ->assertFalse(file_exists($image_file['uri']));

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

    // Copies temp to destination URI.
    $this
      ->assertTrue($file_metadata
      ->copyTempToUri());
    $this
      ->assertTrue(file_exists($image_file['uri']));

    // 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'));
  }
}