S3fsFileServiceTest.php in S3 File System 8.3
File
tests/src/Functional/S3fsFileServiceTest.php
View source
<?php
namespace Drupal\Tests\s3fs\Functional;
use Drupal\Core\File\Exception\FileNotExistsException;
class S3fsFileServiceTest extends S3fsTestBase {
protected static $modules = [
's3fs',
];
public function testFileService() {
$testUri1 = "{$this->remoteTestsFolderUri}/test_file1.txt";
$testUri2 = "{$this->remoteTestsFolderUri}/test_file2.txt";
$testUri3 = "{$this->remoteTestsFolderUri}/test_file3.txt";
$testUri4 = "{$this->remoteTestsFolderUri}/test_file4.txt";
$fileSystem = \Drupal::service('file_system');
$file_contents = file_get_contents(__DIR__ . '/../../fixtures/test.txt');
$temp_file = $fileSystem
->saveData($file_contents, 'temporary://123');
$this
->assertTrue($fileSystem
->moveUploadedFile($temp_file, $testUri1), 'Uploaded file with S3fsFileService');
$this
->assertFalse($fileSystem
->moveUploadedFile('/tmp/invalidFile', $testUri1), 'Did not upload nonexistent file');
$this
->assertEquals($testUri2, $fileSystem
->move($testUri1, $testUri2), 'Moved file with S3fsFileService');
$this
->expectException(FileNotExistsException::class);
$fileSystem
->move($testUri1, $testUri3);
$this
->assertEquals($testUri4, $fileSystem
->copy($testUri2, $testUri4), 'Copied file from uri2 to uri4');
$this
->expectException(FileNotExistsException::class);
$fileSystem
->copy($testUri1, $testUri2);
}
public function testCacheHeaders() {
$this
->config('s3fs.settings')
->set('cache_control_header', 'public, max-age=300')
->save();
$fileSystem = \Drupal::service('file_system');
$file_contents = file_get_contents(__DIR__ . '/../../fixtures/test.txt');
$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');
$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');
}
}