View source
<?php
namespace Drupal\system\Tests\File;
class ReadOnlyStreamWrapperTest extends FileTestBase {
protected $scheme = 'dummy-readonly';
protected $classname = 'Drupal\\file_test\\StreamWrapper\\DummyReadOnlyStreamWrapper';
function testReadOnlyBehavior() {
$filename = $this
->randomMachineName();
$site_path = $this->container
->get('site.path');
$filepath = $site_path . '/files/' . $filename;
file_put_contents($filepath, $filename);
$uri = $this->scheme . '://' . $filename;
\Drupal::service('stream_wrapper_manager')
->getViaScheme($this->scheme);
$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
->assertTrue($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
->assertTrue($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
->assertTrue($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
->assertFalse(@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 . '://newname.txt'), 'Unable to rename files using the read-only stream wrapper.');
$this
->assertTrue(@drupal_unlink($uri), 'Able to unlink file using read-only stream wrapper.');
$this
->assertTrue(file_exists($filepath), 'Unlink File was not actually deleted.');
$dirname = $this
->randomMachineName();
$dir = $site_path . '/files/' . $dirname;
$readonlydir = $this->scheme . '://' . $dirname;
$this
->assertFalse(@drupal_mkdir($readonlydir, 0775, 0), 'Unable to create directory with read-only stream wrapper.');
$this
->assertTrue(drupal_mkdir($dir), 'Test directory created.');
$this
->assertFalse(@drupal_rmdir($readonlydir), 'Unable to delete directory with read-only stream wrapper.');
drupal_rmdir($dir);
}
}