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