Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Component / PhpStorage / FileStorageReadOnlyTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\PhpStorage;
4
5 use Drupal\Component\PhpStorage\FileStorage;
6 use Drupal\Component\PhpStorage\FileReadOnlyStorage;
7
8 /**
9  * @coversDefaultClass \Drupal\Component\PhpStorage\FileReadOnlyStorage
10  *
11  * @group Drupal
12  * @group PhpStorage
13  */
14 class FileStorageReadOnlyTest extends PhpStorageTestBase {
15
16   /**
17    * Standard test settings to pass to storage instances.
18    *
19    * @var array
20    */
21   protected $standardSettings;
22
23   /**
24    * Read only test settings to pass to storage instances.
25    *
26    * @var array
27    */
28   protected $readonlyStorage;
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     $this->standardSettings = [
37       'directory' => $this->directory,
38       'bin' => 'test',
39     ];
40     $this->readonlyStorage = [
41       'directory' => $this->directory,
42       // Let this read from the bin where the other instance is writing.
43       'bin' => 'test',
44     ];
45   }
46
47   /**
48    * Tests writing with one class and reading with another.
49    */
50   public function testReadOnly() {
51     $php = new FileStorage($this->standardSettings);
52     $name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
53
54     // Find a global that doesn't exist.
55     do {
56       $random = mt_rand(10000, 100000);
57     } while (isset($GLOBALS[$random]));
58
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]);
66
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]);
74   }
75
76   /**
77    * @covers ::writeable
78    */
79   public function testWriteable() {
80     $php_read = new FileReadOnlyStorage($this->readonlyStorage);
81     $this->assertFalse($php_read->writeable());
82   }
83
84   /**
85    * @covers ::deleteAll
86    */
87   public function testDeleteAll() {
88     $php = new FileStorage($this->standardSettings);
89     $name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
90
91     // Find a global that doesn't exist.
92     do {
93       $random = mt_rand(10000, 100000);
94     } while (isset($GLOBALS[$random]));
95
96     // Write our the file so we can test deleting.
97     $code = "<?php\n\$GLOBALS[$random] = TRUE;";
98     $this->assertTrue($php->save($name, $code));
99
100     $php_read = new FileReadOnlyStorage($this->readonlyStorage);
101     $this->assertFalse($php_read->deleteAll());
102
103     // Make sure directory exists prior to removal.
104     $this->assertTrue(file_exists($this->directory . '/test'), 'File storage directory does not exist.');
105   }
106
107 }