FileStorageTest.php 2.86 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\Component\PhpStorage;
4

5
use Drupal\Component\PhpStorage\FileStorage;
6
use Drupal\Component\Utility\Random;
7
8
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit_Framework_Error_Warning;
9

10
/**
11
 * @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage
12
 * @group Drupal
13
 * @group PhpStorage
14
15
16
 */
class FileStorageTest extends PhpStorageTestBase {

17
18
19
20
21
22
23
24
25
26
  /**
   * Standard test settings to pass to storage instances.
   *
   * @var array
   */
  protected $standardSettings;

  /**
   * {@inheritdoc}
   */
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
40
41
42
   * @covers ::load
   * @covers ::save
   * @covers ::exists
   * @covers ::delete
43
   */
44
  public function testCRUD() {
45
    $php = new FileStorage($this->standardSettings);
46
47
48
    $this->assertCRUD($php);
  }

49
  /**
50
   * @covers ::writeable
51
52
   */
  public function testWriteable() {
53
    $php = new FileStorage($this->standardSettings);
54
55
56
57
    $this->assertTrue($php->writeable());
  }

  /**
58
   * @covers ::deleteAll
59
60
   */
  public function testDeleteAll() {
61
62
    // Random generator.
    $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
70
71
72
73
74
75

    // Find a global that doesn't exist.
    do {
      $random = mt_rand(10000, 100000);
    } while (isset($GLOBALS[$random]));

    // Write out a PHP file and ensure it's successfully loaded.
    $code = "<?php\n\$GLOBALS[$random] = TRUE;";
76
    $this->assertTrue($php->save($name, $code), 'Saved php file');
77
    $php->load($name);
78
79
80
81
    $this->assertTrue($GLOBALS[$random], 'File saved correctly with correct value');

    // Make sure directory exists prior to removal.
    $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
94
95
96
97
98
99
100
101
102
103
104
105
  /**
   * @covers ::createDirectory
   */
  public function testCreateDirectoryFailWarning() {
    $directory = new vfsStreamDirectory('permissionDenied', 0200);
    $storage = new FileStorage([
      'directory' => $directory->url(),
      'bin' => 'test',
    ]);
    $code = "<?php\n echo 'here';";
    $this->setExpectedException(PHPUnit_Framework_Error_Warning::class, 'mkdir(): Permission Denied');
    $storage->save('subdirectory/foo.php', $code);
  }

106
}