<?php
namespace Drupal\system\Tests\File;
class ReadOnlyStreamWrapperTest extends FileTestBase {
public static $modules = array(
'file_test',
);
protected $scheme = 'dummy-readonly';
protected $classname = 'Drupal\\file_test\\DummyReadOnlyStreamWrapper';
public static function getInfo() {
return array(
'name' => 'Read only stream wrapper',
'description' => 'Tests the read-only stream wrapper write functions.',
'group' => 'File API',
);
}
function setUp() {
parent::setUp();
drupal_static_reset('file_get_stream_wrappers');
}
function tearDown() {
parent::tearDown();
stream_wrapper_unregister($this->scheme);
}
function testWriteFunctions() {
$filename = $this
->randomName();
$filepath = conf_path() . '/files/' . $filename;
file_put_contents($filepath, $filename);
$uri = $this->scheme . '://' . $filename;
$instance = file_stream_wrapper_get_instance_by_scheme($this->scheme);
$handle = @fopen($uri, 'w+');
$this
->assertFalse($handle, 'Unable to open a file for writing with 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(@drupal_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(@fwrite($handle, $this
->randomName()), '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
->randomName();
$dir = conf_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);
}
}