You are here

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

Test caching.

File

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

Class

FileMetadataManagerTest
Tests that File Metadata Manager works properly.

Namespace

Drupal\Tests\file_mdm\Kernel

Code

public function testFileMetadataCaching() {

  // Prepare a copy of test files.
  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', 'simpletest') . '/files/image-test.gif', 'public://', FILE_EXISTS_REPLACE);
  file_unmanaged_copy(drupal_get_path('module', 'simpletest') . '/files/image-test.png', 'public://', FILE_EXISTS_REPLACE);

  // The image files that will be tested.
  $image_files = [
    [
      // Pass a URI.
      'uri' => 'public://image-test.gif',
      'cache' => TRUE,
      'delete' => TRUE,
      'count_keys' => 7,
      'test_keys' => [
        [
          0,
          40,
        ],
        [
          1,
          20,
        ],
        [
          2,
          IMAGETYPE_GIF,
        ],
        [
          'mime',
          'image/gif',
        ],
      ],
    ],
    [
      // Pass a path instead of the URI.
      'uri' => drupal_get_path('module', 'file_mdm') . '/tests/files/test-exif.jpeg',
      'cache' => FALSE,
      'delete' => FALSE,
      'count_keys' => 7,
      'test_keys' => [
        [
          0,
          100,
        ],
        [
          1,
          75,
        ],
        [
          2,
          IMAGETYPE_JPEG,
        ],
        [
          'mime',
          'image/jpeg',
        ],
      ],
    ],
    [
      // PHP getimagesize works on remote stream wrappers.
      'uri' => 'dummy-remote://image-test.png',
      'cache' => TRUE,
      'delete' => TRUE,
      'count_keys' => 6,
      'test_keys' => [
        [
          0,
          40,
        ],
        [
          1,
          20,
        ],
        [
          2,
          IMAGETYPE_PNG,
        ],
        [
          '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) {

    // Read from file.
    $file_metadata = $fmdm
      ->uri($image_file['uri']);
    $this
      ->assertNotNull($file_metadata
      ->getMetadata('getimagesize'));
    $this
      ->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $file_metadata
      ->isMetadataLoaded('getimagesize'));

    // Release URI.
    $file_metadata = NULL;
    $this
      ->assertTrue($fmdm
      ->release($image_file['uri']));
    $this
      ->assertEquals(0, $fmdm
      ->count());
    if ($image_file['delete']) {

      // Delete file.
      file_unmanaged_delete($image_file['uri']);

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

    // Read from cache if possible.
    $file_metadata = $fmdm
      ->uri($image_file['uri']);
    $this
      ->assertNotNull($file_metadata
      ->getMetadata('getimagesize'));
    if ($image_file['cache']) {
      $this
        ->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata
        ->isMetadataLoaded('getimagesize'));
    }
    else {
      $this
        ->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $file_metadata
        ->isMetadataLoaded('getimagesize'));
    }
    $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);
    }

    // Change MIME type and remove 0, 1, 2, 3.
    $this
      ->assertTrue($file_metadata
      ->setMetadata('getimagesize', 'mime', 'foo/bar'));
    $this
      ->assertTrue($file_metadata
      ->removeMetadata('getimagesize', 0));
    $this
      ->assertTrue($file_metadata
      ->removeMetadata('getimagesize', 1));
    $this
      ->assertTrue($file_metadata
      ->removeMetadata('getimagesize', 2));
    $this
      ->assertTrue($file_metadata
      ->removeMetadata('getimagesize', 3));

    // Save again to cache.
    if ($image_file['cache']) {
      $this
        ->assertTrue($file_metadata
        ->saveMetadataToCache('getimagesize'));
    }
    else {
      $this
        ->assertFalse($file_metadata
        ->saveMetadataToCache('getimagesize'));
    }
    if ($image_file['cache']) {

      // Release URI.
      $file_metadata = NULL;
      $this
        ->assertTrue($fmdm
        ->release($image_file['uri']));
      $this
        ->assertSame(0, $fmdm
        ->count());

      // Read from cache.
      $file_metadata = $fmdm
        ->uri($image_file['uri']);
      $this
        ->assertSame($image_file['count_keys'] - 4, $this
        ->countMetadataKeys($file_metadata, 'getimagesize'));
      $this
        ->assertSame('foo/bar', $file_metadata
        ->getMetadata('getimagesize', 'mime'));
      $this
        ->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $file_metadata
        ->isMetadataLoaded('getimagesize'));
    }
    $file_metadata = NULL;
    $this
      ->assertTrue($fmdm
      ->release($image_file['uri']));
    $this
      ->assertEquals(0, $fmdm
      ->count());
  }
}