3 namespace Drupal\KernelTests\Core\File;
5 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
6 use Drupal\file_test\StreamWrapper\DummyReadOnlyStreamWrapper;
9 * Tests the read-only stream wrapper write functions.
13 class ReadOnlyStreamWrapperTest extends FileTestBase {
16 * A stream wrapper scheme to register for the test.
20 protected $scheme = 'dummy-readonly';
23 * A fully-qualified stream wrapper class name to register for the test.
27 protected $classname = 'Drupal\file_test\StreamWrapper\DummyReadOnlyStreamWrapper';
30 * Test read-only specific behavior.
32 public function testReadOnlyBehavior() {
33 $type = DummyReadOnlyStreamWrapper::getType();
34 // Checks that the stream wrapper type is not declared as writable.
35 $this->assertSame(0, $type & StreamWrapperInterface::WRITE);
36 // Checks that the stream wrapper type is declared as local.
37 $this->assertSame(1, $type & StreamWrapperInterface::LOCAL);
39 // Generate a test file
40 $filename = $this->randomMachineName();
41 $site_path = $this->container->get('site.path');
42 $filepath = $site_path . '/files/' . $filename;
43 file_put_contents($filepath, $filename);
45 // Generate a read-only stream wrapper instance
46 $uri = $this->scheme . '://' . $filename;
47 \Drupal::service('stream_wrapper_manager')->getViaScheme($this->scheme);
49 // Attempt to open a file in read/write mode
50 $handle = @fopen($uri, 'r+');
51 $this->assertFalse($handle, 'Unable to open a file for reading and writing with the read-only stream wrapper.');
52 // Attempt to open a file in binary read mode
53 $handle = fopen($uri, 'rb');
54 $this->assertTrue($handle, 'Able to open a file for reading in binary mode with the read-only stream wrapper.');
55 $this->assertTrue(fclose($handle), 'Able to close file opened in binary mode using the read_only stream wrapper.');
56 // Attempt to open a file in text read mode
57 $handle = fopen($uri, 'rt');
58 $this->assertTrue($handle, 'Able to open a file for reading in text mode with the read-only stream wrapper.');
59 $this->assertTrue(fclose($handle), 'Able to close file opened in text mode using the read_only stream wrapper.');
60 // Attempt to open a file in read mode
61 $handle = fopen($uri, 'r');
62 $this->assertTrue($handle, 'Able to open a file for reading with the read-only stream wrapper.');
63 // Attempt to change file permissions
64 $this->assertFalse(@chmod($uri, 0777), 'Unable to change file permissions when using read-only stream wrapper.');
65 // Attempt to acquire an exclusive lock for writing
66 $this->assertFalse(@flock($handle, LOCK_EX | LOCK_NB), 'Unable to acquire an exclusive lock using the read-only stream wrapper.');
67 // Attempt to obtain a shared lock
68 $this->assertTrue(flock($handle, LOCK_SH | LOCK_NB), 'Able to acquire a shared lock using the read-only stream wrapper.');
69 // Attempt to release a shared lock
70 $this->assertTrue(flock($handle, LOCK_UN | LOCK_NB), 'Able to release a shared lock using the read-only stream wrapper.');
71 // Attempt to truncate the file
72 $this->assertFalse(@ftruncate($handle, 0), 'Unable to truncate using the read-only stream wrapper.');
73 // Attempt to write to the file
74 $this->assertFalse(@fwrite($handle, $this->randomMachineName()), 'Unable to write to file using the read-only stream wrapper.');
75 // Attempt to flush output to the file
76 $this->assertFalse(@fflush($handle), 'Unable to flush output to file using the read-only stream wrapper.');
77 // Attempt to close the stream. (Suppress errors, as fclose triggers fflush.)
78 $this->assertTrue(fclose($handle), 'Able to close file using the read_only stream wrapper.');
79 // Test the rename() function
80 $this->assertFalse(@rename($uri, $this->scheme . '://newname.txt'), 'Unable to rename files using the read-only stream wrapper.');
81 // Test the unlink() function
82 $this->assertTrue(@drupal_unlink($uri), 'Able to unlink file using read-only stream wrapper.');
83 $this->assertTrue(file_exists($filepath), 'Unlink File was not actually deleted.');
85 // Test the mkdir() function by attempting to create a directory.
86 $dirname = $this->randomMachineName();
87 $dir = $site_path . '/files/' . $dirname;
88 $readonlydir = $this->scheme . '://' . $dirname;
89 $this->assertFalse(@drupal_mkdir($readonlydir, 0775, 0), 'Unable to create directory with read-only stream wrapper.');
90 // Create a temporary directory for testing purposes
91 $this->assertTrue(drupal_mkdir($dir), 'Test directory created.');
92 // Test the rmdir() function by attempting to remove the directory.
93 $this->assertFalse(@drupal_rmdir($readonlydir), 'Unable to delete directory with read-only stream wrapper.');
94 // Remove the temporary directory.