3 namespace Drupal\Tests\Component\PhpStorage;
5 use Drupal\Component\PhpStorage\FileStorage;
6 use Drupal\Component\PhpStorage\FileReadOnlyStorage;
9 * @coversDefaultClass \Drupal\Component\PhpStorage\FileReadOnlyStorage
14 class FileStorageReadOnlyTest extends PhpStorageTestBase {
17 * Standard test settings to pass to storage instances.
21 protected $standardSettings;
24 * Read only test settings to pass to storage instances.
28 protected $readonlyStorage;
33 protected function setUp() {
36 $this->standardSettings = [
37 'directory' => $this->directory,
40 $this->readonlyStorage = [
41 'directory' => $this->directory,
42 // Let this read from the bin where the other instance is writing.
48 * Tests writing with one class and reading with another.
50 public function testReadOnly() {
51 $php = new FileStorage($this->standardSettings);
52 $name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
54 // Find a global that doesn't exist.
56 $random = mt_rand(10000, 100000);
57 } while (isset($GLOBALS[$random]));
59 // Write out a PHP file and ensure it's successfully loaded.
60 $code = "<?php\n\$GLOBALS[$random] = TRUE;";
61 $success = $php->save($name, $code);
62 $this->assertSame($success, TRUE);
63 $php_read = new FileReadOnlyStorage($this->readonlyStorage);
64 $php_read->load($name);
65 $this->assertTrue($GLOBALS[$random]);
67 // If the file was successfully loaded, it must also exist, but ensure the
68 // exists() method returns that correctly.
69 $this->assertSame($php_read->exists($name), TRUE);
70 // Saving and deleting should always fail.
71 $this->assertFalse($php_read->save($name, $code));
72 $this->assertFalse($php_read->delete($name));
73 unset($GLOBALS[$random]);
79 public function testWriteable() {
80 $php_read = new FileReadOnlyStorage($this->readonlyStorage);
81 $this->assertFalse($php_read->writeable());
87 public function testDeleteAll() {
88 $php = new FileStorage($this->standardSettings);
89 $name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
91 // Find a global that doesn't exist.
93 $random = mt_rand(10000, 100000);
94 } while (isset($GLOBALS[$random]));
96 // Write our the file so we can test deleting.
97 $code = "<?php\n\$GLOBALS[$random] = TRUE;";
98 $this->assertTrue($php->save($name, $code));
100 $php_read = new FileReadOnlyStorage($this->readonlyStorage);
101 $this->assertFalse($php_read->deleteAll());
103 // Make sure directory exists prior to removal.
104 $this->assertTrue(file_exists($this->directory . '/test'), 'File storage directory does not exist.');