You are here

public function S3fsFileServiceTest::testCacheHeaders in S3 File System 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Functional/S3fsFileServiceTest.php \Drupal\Tests\s3fs\Functional\S3fsFileServiceTest::testCacheHeaders()

Coverage test for the file_system setting cache headers.

Make sure that Cache-Control headers are set on the file.

File

tests/src/Functional/S3fsFileServiceTest.php, line 55

Class

S3fsFileServiceTest
S3 File System Service Decorator Tests.

Namespace

Drupal\Tests\s3fs\Functional

Code

public function testCacheHeaders() {
  $this
    ->config('s3fs.settings')
    ->set('cache_control_header', 'public, max-age=300')
    ->save();

  /** @var \Drupal\s3fs\S3fsFileService $fileSystem */
  $fileSystem = \Drupal::service('file_system');
  $file_contents = file_get_contents(__DIR__ . '/../../fixtures/test.txt');

  // Verify that $filesystem->putObject() sets cache headers.
  $headerTestUri1 = "s3://" . $this
    ->randomMachineName();
  $cacheTestFile = $fileSystem
    ->saveData($file_contents, $headerTestUri1);
  $url = file_create_url($cacheTestFile);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->responseHeaderEquals('cache-control', 'public, max-age=300');

  // Verify that filesystem->copyObject() replaces cache headers.
  $this
    ->config('s3fs.settings')
    ->set('cache_control_header', 'public, max-age=301')
    ->save();
  $headerTestUri2 = "s3://" . $this
    ->randomMachineName();
  $copyTestFile = $fileSystem
    ->copy($cacheTestFile, $headerTestUri2);
  $url = file_create_url($copyTestFile);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->responseHeaderEquals('cache-control', 'public, max-age=301');
}