Same filename and directory in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/File/ReadOnlyStreamWrapperTest.php
- 9 core/tests/Drupal/KernelTests/Core/File/ReadOnlyStreamWrapperTest.php
Namespace
Drupal\KernelTests\Core\File
File
core/tests/Drupal/KernelTests/Core/File/ReadOnlyStreamWrapperTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\KernelTests\Core\File;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\file_test\StreamWrapper\DummyReadOnlyStreamWrapper;
class ReadOnlyStreamWrapperTest extends FileTestBase {
protected $scheme = 'dummy-readonly';
protected $classname = 'Drupal\\file_test\\StreamWrapper\\DummyReadOnlyStreamWrapper';
public function testReadOnlyBehavior() {
$type = DummyReadOnlyStreamWrapper::getType();
$this
->assertSame(0, $type & StreamWrapperInterface::WRITE);
$this
->assertSame(1, $type & StreamWrapperInterface::LOCAL);
$filename = $this
->randomMachineName();
$site_path = $this->container
->getParameter('site.path');
$filepath = $site_path . '/files/' . $filename;
file_put_contents($filepath, $filename);
$uri = $this->scheme . '://' . $filename;
\Drupal::service('stream_wrapper_manager')
->getViaScheme($this->scheme);
$file_system = \Drupal::service('file_system');
$handle = @fopen($uri, 'r+');
$this
->assertFalse($handle, 'Unable to open a file for reading and writing with the read-only stream wrapper.');
$handle = fopen($uri, 'rb');
$this
->assertNotFalse($handle, 'Able to open a file for reading in binary mode with the read-only stream wrapper.');
$this
->assertTrue(fclose($handle), 'Able to close file opened in binary mode using the read_only stream wrapper.');
$handle = fopen($uri, 'rt');
$this
->assertNotFalse($handle, 'Able to open a file for reading in text mode with the read-only stream wrapper.');
$this
->assertTrue(fclose($handle), 'Able to close file opened in text mode using the read_only stream wrapper.');
$handle = fopen($uri, 'r');
$this
->assertNotFalse($handle, 'Able to open a file for reading with the read-only stream wrapper.');
$this
->assertFalse(@chmod($uri, 0777), 'Unable to change file permissions when using read-only stream wrapper.');
$this
->assertFalse(@flock($handle, LOCK_EX | LOCK_NB), 'Unable to acquire an exclusive lock using the read-only stream wrapper.');
$this
->assertTrue(flock($handle, LOCK_SH | LOCK_NB), 'Able to acquire a shared lock using the read-only stream wrapper.');
$this
->assertTrue(flock($handle, LOCK_UN | LOCK_NB), 'Able to release a shared lock using the read-only stream wrapper.');
$this
->assertFalse(@ftruncate($handle, 0), 'Unable to truncate using the read-only stream wrapper.');
$this
->assertEquals(0, @fwrite($handle, $this
->randomMachineName()), 'Unable to write to file using the read-only stream wrapper.');
$this
->assertFalse(@fflush($handle), 'Unable to flush output to file using the read-only stream wrapper.');
$this
->assertTrue(fclose($handle), 'Able to close file using the read_only stream wrapper.');
$this
->assertFalse(@rename($uri, $this->scheme . '://new_name.txt'), 'Unable to rename files using the read-only stream wrapper.');
$this
->assertTrue(@$file_system
->unlink($uri), 'Able to unlink file using read-only stream wrapper.');
$this
->assertFileExists($filepath);
$dirname = $this
->randomMachineName();
$dir = $site_path . '/files/' . $dirname;
$read_only_dir = $this->scheme . '://' . $dirname;
$file_system = \Drupal::service('file_system');
$this
->assertFalse(@$file_system
->mkdir($read_only_dir, 0775, 0), 'Unable to create directory with read-only stream wrapper.');
$this
->assertTrue($file_system
->mkdir($dir), 'Test directory created.');
$this
->assertFalse(@$file_system
->rmdir($read_only_dir), 'Unable to delete directory with read-only stream wrapper.');
$file_system
->rmdir($dir);
}
}
Classes