a1a11ec38dba22af45ecfacb0b568b4e4b5f2ba2
[yaffs-website] / web / core / tests / Drupal / Tests / Component / PhpStorage / FileStorageTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\PhpStorage;
4
5 use Drupal\Component\PhpStorage\FileStorage;
6
7 /**
8  * @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage
9  * @group Drupal
10  * @group PhpStorage
11  */
12 class FileStorageTest extends PhpStorageTestBase {
13
14   /**
15    * Standard test settings to pass to storage instances.
16    *
17    * @var array
18    */
19   protected $standardSettings;
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26
27     $this->standardSettings = [
28       'directory' => $this->directory,
29       'bin' => 'test',
30     ];
31   }
32
33   /**
34    * Tests basic load/save/delete operations.
35    *
36    * @covers ::load
37    * @covers ::save
38    * @covers ::exists
39    * @covers ::delete
40    */
41   public function testCRUD() {
42     $php = new FileStorage($this->standardSettings);
43     $this->assertCRUD($php);
44   }
45
46   /**
47    * @covers ::writeable
48    */
49   public function testWriteable() {
50     $php = new FileStorage($this->standardSettings);
51     $this->assertTrue($php->writeable());
52   }
53
54   /**
55    * @covers ::deleteAll
56    */
57   public function testDeleteAll() {
58
59     // Write out some files.
60     $php = new FileStorage($this->standardSettings);
61
62     $name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
63
64     // Find a global that doesn't exist.
65     do {
66       $random = mt_rand(10000, 100000);
67     } while (isset($GLOBALS[$random]));
68
69     // Write out a PHP file and ensure it's successfully loaded.
70     $code = "<?php\n\$GLOBALS[$random] = TRUE;";
71     $this->assertTrue($php->save($name, $code), 'Saved php file');
72     $php->load($name);
73     $this->assertTrue($GLOBALS[$random], 'File saved correctly with correct value');
74
75     // Make sure directory exists prior to removal.
76     $this->assertTrue(file_exists($this->directory . '/test'), 'File storage directory does not exist.');
77
78     $this->assertTrue($php->deleteAll(), 'Delete all reported success');
79     $this->assertFalse($php->load($name));
80     $this->assertFalse(file_exists($this->directory . '/test'), 'File storage directory does not exist after call to deleteAll()');
81
82     // Should still return TRUE if directory has already been deleted.
83     $this->assertTrue($php->deleteAll(), 'Delete all succeeds with nothing to delete');
84     unset($GLOBALS[$random]);
85   }
86
87 }