More updates to stop using dev or alpha or beta versions.
[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 use Drupal\Component\Utility\Random;
7 use org\bovigo\vfs\vfsStreamDirectory;
8 use PHPUnit\Framework\Error\Warning;
9
10 /**
11  * @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage
12  * @group Drupal
13  * @group PhpStorage
14  */
15 class FileStorageTest extends PhpStorageTestBase {
16
17   /**
18    * Standard test settings to pass to storage instances.
19    *
20    * @var array
21    */
22   protected $standardSettings;
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     $this->standardSettings = [
31       'directory' => $this->directory,
32       'bin' => 'test',
33     ];
34   }
35
36   /**
37    * Tests basic load/save/delete operations.
38    *
39    * @covers ::load
40    * @covers ::save
41    * @covers ::exists
42    * @covers ::delete
43    */
44   public function testCRUD() {
45     $php = new FileStorage($this->standardSettings);
46     $this->assertCRUD($php);
47   }
48
49   /**
50    * @covers ::writeable
51    */
52   public function testWriteable() {
53     $php = new FileStorage($this->standardSettings);
54     $this->assertTrue($php->writeable());
55   }
56
57   /**
58    * @covers ::deleteAll
59    */
60   public function testDeleteAll() {
61     // Random generator.
62     $random_generator = new Random();
63
64     // Write out some files.
65     $php = new FileStorage($this->standardSettings);
66
67     $name = $random_generator->name(8, TRUE) . '/' . $random_generator->name(8, TRUE) . '.php';
68
69     // Find a global that doesn't exist.
70     do {
71       $random = mt_rand(10000, 100000);
72     } while (isset($GLOBALS[$random]));
73
74     // Write out a PHP file and ensure it's successfully loaded.
75     $code = "<?php\n\$GLOBALS[$random] = TRUE;";
76     $this->assertTrue($php->save($name, $code), 'Saved php file');
77     $php->load($name);
78     $this->assertTrue($GLOBALS[$random], 'File saved correctly with correct value');
79
80     // Make sure directory exists prior to removal.
81     $this->assertTrue(file_exists($this->directory . '/test'), 'File storage directory does not exist.');
82
83     $this->assertTrue($php->deleteAll(), 'Delete all reported success');
84     $this->assertFalse($php->load($name));
85     $this->assertFalse(file_exists($this->directory . '/test'), 'File storage directory does not exist after call to deleteAll()');
86
87     // Should still return TRUE if directory has already been deleted.
88     $this->assertTrue($php->deleteAll(), 'Delete all succeeds with nothing to delete');
89     unset($GLOBALS[$random]);
90   }
91
92   /**
93    * @covers ::createDirectory
94    */
95   public function testCreateDirectoryFailWarning() {
96     $directory = new vfsStreamDirectory('permissionDenied', 0200);
97     $storage = new FileStorage([
98       'directory' => $directory->url(),
99       'bin' => 'test',
100     ]);
101     $code = "<?php\n echo 'here';";
102     if (method_exists($this, 'expectException')) {
103       $this->expectException(Warning::class);
104       $this->expectExceptionMessage('mkdir(): Permission Denied');
105     }
106     else {
107       $this->setExpectedException(\PHPUnit_Framework_Error_Warning::class, 'mkdir(): Permission Denied');
108     }
109     $storage->save('subdirectory/foo.php', $code);
110   }
111
112 }