3 namespace Drupal\Tests\Component\PhpStorage;
5 use Drupal\Component\PhpStorage\FileStorage;
6 use Drupal\Component\PhpStorage\FileReadOnlyStorage;
7 use Drupal\Component\Utility\Random;
10 * @coversDefaultClass \Drupal\Component\PhpStorage\FileReadOnlyStorage
15 class FileStorageReadOnlyTest extends PhpStorageTestBase {
18 * Standard test settings to pass to storage instances.
22 protected $standardSettings;
25 * Read only test settings to pass to storage instances.
29 protected $readonlyStorage;
34 protected function setUp() {
37 $this->standardSettings = [
38 'directory' => $this->directory,
41 $this->readonlyStorage = [
42 'directory' => $this->directory,
43 // Let this read from the bin where the other instance is writing.
49 * Tests writing with one class and reading with another.
51 public function testReadOnly() {
53 $random = new Random();
55 $php = new FileStorage($this->standardSettings);
56 $name = $random->name(8, TRUE) . '/' . $random->name(8, TRUE) . '.php';
58 // Find a global that doesn't exist.
60 $random = mt_rand(10000, 100000);
61 } while (isset($GLOBALS[$random]));
63 // Write out a PHP file and ensure it's successfully loaded.
64 $code = "<?php\n\$GLOBALS[$random] = TRUE;";
65 $success = $php->save($name, $code);
66 $this->assertSame(TRUE, $success);
67 $php_read = new FileReadOnlyStorage($this->readonlyStorage);
68 $php_read->load($name);
69 $this->assertTrue($GLOBALS[$random]);
71 // If the file was successfully loaded, it must also exist, but ensure the
72 // exists() method returns that correctly.
73 $this->assertSame(TRUE, $php_read->exists($name));
74 // Saving and deleting should always fail.
75 $this->assertFalse($php_read->save($name, $code));
76 $this->assertFalse($php_read->delete($name));
77 unset($GLOBALS[$random]);
83 public function testWriteable() {
84 $php_read = new FileReadOnlyStorage($this->readonlyStorage);
85 $this->assertFalse($php_read->writeable());
91 public function testDeleteAll() {
93 $random = new Random();
95 $php = new FileStorage($this->standardSettings);
96 $name = $random->name(8, TRUE) . '/' . $random->name(8, TRUE) . '.php';
98 // Find a global that doesn't exist.
100 $random = mt_rand(10000, 100000);
101 } while (isset($GLOBALS[$random]));
103 // Write our the file so we can test deleting.
104 $code = "<?php\n\$GLOBALS[$random] = TRUE;";
105 $this->assertTrue($php->save($name, $code));
107 $php_read = new FileReadOnlyStorage($this->readonlyStorage);
108 $this->assertFalse($php_read->deleteAll());
110 // Make sure directory exists prior to removal.
111 $this->assertTrue(file_exists($this->directory . '/test'), 'File storage directory does not exist.');