Issue #2453399 by neclimdul: Use VFS for FileStorage tests
parent
2745b6127f
commit
98b74d2d45
|
@ -70,7 +70,7 @@ class MTimeProtectedFastFileStorage extends FileStorage {
|
||||||
|
|
||||||
// Write the file out to a temporary location. Prepend with a '.' to keep it
|
// Write the file out to a temporary location. Prepend with a '.' to keep it
|
||||||
// hidden from listings and web servers.
|
// hidden from listings and web servers.
|
||||||
$temporary_path = tempnam($this->directory, '.');
|
$temporary_path = $this->tempnam($this->directory, '.');
|
||||||
if (!$temporary_path || !@file_put_contents($temporary_path, $data)) {
|
if (!$temporary_path || !@file_put_contents($temporary_path, $data)) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
@ -103,7 +103,7 @@ class MTimeProtectedFastFileStorage extends FileStorage {
|
||||||
// iteration.
|
// iteration.
|
||||||
if ($i > 0) {
|
if ($i > 0) {
|
||||||
$this->unlink($temporary_path);
|
$this->unlink($temporary_path);
|
||||||
$temporary_path = tempnam($this->directory, '.');
|
$temporary_path = $this->tempnam($this->directory, '.');
|
||||||
rename($full_path, $temporary_path);
|
rename($full_path, $temporary_path);
|
||||||
// Make sure to not loop infinitely on a hopelessly slow filesystem.
|
// Make sure to not loop infinitely on a hopelessly slow filesystem.
|
||||||
if ($i > 10) {
|
if ($i > 10) {
|
||||||
|
@ -184,4 +184,20 @@ class MTimeProtectedFastFileStorage extends FileStorage {
|
||||||
return filemtime($directory);
|
return filemtime($directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A brute force tempnam implementation supporting streams.
|
||||||
|
*
|
||||||
|
* @param $directory
|
||||||
|
* The directory where the temporary filename will be created.
|
||||||
|
* @param $prefix
|
||||||
|
* The prefix of the generated temporary filename.
|
||||||
|
* @return string
|
||||||
|
* Returns the new temporary filename (with path), or FALSE on failure.
|
||||||
|
*/
|
||||||
|
protected function tempnam($directory, $prefix) {
|
||||||
|
do {
|
||||||
|
$path = $directory . '/' . $prefix . substr(str_shuffle(hash('sha256', microtime())), 0, 10);
|
||||||
|
} while (file_exists($path));
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,8 @@ use Drupal\Component\PhpStorage\FileReadOnlyStorage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @coversDefaultClass \Drupal\Component\PhpStorage\FileReadOnlyStorage
|
* @coversDefaultClass \Drupal\Component\PhpStorage\FileReadOnlyStorage
|
||||||
|
*
|
||||||
|
* @group Drupal
|
||||||
* @group PhpStorage
|
* @group PhpStorage
|
||||||
*/
|
*/
|
||||||
class FileStorageReadOnlyTest extends PhpStorageTestBase {
|
class FileStorageReadOnlyTest extends PhpStorageTestBase {
|
||||||
|
@ -49,9 +51,6 @@ class FileStorageReadOnlyTest extends PhpStorageTestBase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests writing with one class and reading with another.
|
* Tests writing with one class and reading with another.
|
||||||
*
|
|
||||||
* @group Drupal
|
|
||||||
* @group PhpStorage
|
|
||||||
*/
|
*/
|
||||||
public function testReadOnly() {
|
public function testReadOnly() {
|
||||||
$php = new FileStorage($this->standardSettings);
|
$php = new FileStorage($this->standardSettings);
|
||||||
|
@ -79,10 +78,7 @@ class FileStorageReadOnlyTest extends PhpStorageTestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests writeable() method.
|
* @covers ::writeable
|
||||||
*
|
|
||||||
* @group Drupal
|
|
||||||
* @group PhpStorage
|
|
||||||
*/
|
*/
|
||||||
public function testWriteable() {
|
public function testWriteable() {
|
||||||
$php_read = new FileReadOnlyStorage($this->readonlyStorage);
|
$php_read = new FileReadOnlyStorage($this->readonlyStorage);
|
||||||
|
@ -90,12 +86,21 @@ class FileStorageReadOnlyTest extends PhpStorageTestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests deleteAll() method.
|
* @covers ::deleteAll
|
||||||
*
|
|
||||||
* @group Drupal
|
|
||||||
* @group PhpStorage
|
|
||||||
*/
|
*/
|
||||||
public function testDeleteAll() {
|
public function testDeleteAll() {
|
||||||
|
$php = new FileStorage($this->standardSettings);
|
||||||
|
$name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
|
||||||
|
|
||||||
|
// Find a global that doesn't exist.
|
||||||
|
do {
|
||||||
|
$random = mt_rand(10000, 100000);
|
||||||
|
} while (isset($GLOBALS[$random]));
|
||||||
|
|
||||||
|
// Write our the file so we can test deleting.
|
||||||
|
$code = "<?php\n\$GLOBALS[$random] = TRUE;";
|
||||||
|
$this->assertTrue($php->save($name, $code));
|
||||||
|
|
||||||
$php_read = new FileReadOnlyStorage($this->readonlyStorage);
|
$php_read = new FileReadOnlyStorage($this->readonlyStorage);
|
||||||
$this->assertFalse($php_read->deleteAll());
|
$this->assertFalse($php_read->deleteAll());
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ use Drupal\Component\PhpStorage\FileStorage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage
|
* @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage
|
||||||
|
* @group Drupal
|
||||||
* @group PhpStorage
|
* @group PhpStorage
|
||||||
*/
|
*/
|
||||||
class FileStorageTest extends PhpStorageTestBase {
|
class FileStorageTest extends PhpStorageTestBase {
|
||||||
|
@ -37,8 +38,10 @@ class FileStorageTest extends PhpStorageTestBase {
|
||||||
/**
|
/**
|
||||||
* Tests basic load/save/delete operations.
|
* Tests basic load/save/delete operations.
|
||||||
*
|
*
|
||||||
* @group Drupal
|
* @covers ::load
|
||||||
* @group PhpStorage
|
* @covers ::save
|
||||||
|
* @covers ::exists
|
||||||
|
* @covers ::delete
|
||||||
*/
|
*/
|
||||||
public function testCRUD() {
|
public function testCRUD() {
|
||||||
$php = new FileStorage($this->standardSettings);
|
$php = new FileStorage($this->standardSettings);
|
||||||
|
@ -46,10 +49,7 @@ class FileStorageTest extends PhpStorageTestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests writeable() method.
|
* @covers ::writeable
|
||||||
*
|
|
||||||
* @group Drupal
|
|
||||||
* @group PhpStorage
|
|
||||||
*/
|
*/
|
||||||
public function testWriteable() {
|
public function testWriteable() {
|
||||||
$php = new FileStorage($this->standardSettings);
|
$php = new FileStorage($this->standardSettings);
|
||||||
|
@ -57,18 +57,13 @@ class FileStorageTest extends PhpStorageTestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests deleteAll() method.
|
* @covers ::deleteAll
|
||||||
*
|
|
||||||
* @group Drupal
|
|
||||||
* @group PhpStorage
|
|
||||||
*/
|
*/
|
||||||
public function testDeleteAll() {
|
public function testDeleteAll() {
|
||||||
|
|
||||||
// Make sure directory exists prior to removal.
|
|
||||||
$this->assertTrue(file_exists($this->directory . '/test'), 'File storage directory does not exist.');
|
|
||||||
|
|
||||||
// Write out some files.
|
// Write out some files.
|
||||||
$php = new FileStorage($this->standardSettings);
|
$php = new FileStorage($this->standardSettings);
|
||||||
|
|
||||||
$name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
|
$name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
|
||||||
|
|
||||||
// Find a global that doesn't exist.
|
// Find a global that doesn't exist.
|
||||||
|
@ -78,17 +73,19 @@ class FileStorageTest extends PhpStorageTestBase {
|
||||||
|
|
||||||
// Write out a PHP file and ensure it's successfully loaded.
|
// Write out a PHP file and ensure it's successfully loaded.
|
||||||
$code = "<?php\n\$GLOBALS[$random] = TRUE;";
|
$code = "<?php\n\$GLOBALS[$random] = TRUE;";
|
||||||
$success = $php->save($name, $code);
|
$this->assertTrue($php->save($name, $code), 'Saved php file');
|
||||||
$this->assertSame($success, TRUE);
|
|
||||||
$php->load($name);
|
$php->load($name);
|
||||||
$this->assertTrue($GLOBALS[$random]);
|
$this->assertTrue($GLOBALS[$random], 'File saved correctly with correct value');
|
||||||
|
|
||||||
$this->assertTrue($php->deleteAll());
|
// Make sure directory exists prior to removal.
|
||||||
|
$this->assertTrue(file_exists($this->directory . '/test'), 'File storage directory does not exist.');
|
||||||
|
|
||||||
|
$this->assertTrue($php->deleteAll(), 'Delete all reported success');
|
||||||
$this->assertFalse($php->load($name));
|
$this->assertFalse($php->load($name));
|
||||||
$this->assertFalse(file_exists($this->directory . '/test'), 'File storage directory still exists after call to deleteAll().');
|
$this->assertFalse(file_exists($this->directory . '/test'), 'File storage directory does not exist after call to deleteAll()');
|
||||||
|
|
||||||
// Should still return TRUE if directory has already been deleted.
|
// Should still return TRUE if directory has already been deleted.
|
||||||
$this->assertTrue($php->deleteAll());
|
$this->assertTrue($php->deleteAll(), 'Delete all succeeds with nothing to delete');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,9 @@ namespace Drupal\Tests\Component\PhpStorage;
|
||||||
/**
|
/**
|
||||||
* Tests the MTimeProtectedFastFileStorage implementation.
|
* Tests the MTimeProtectedFastFileStorage implementation.
|
||||||
*
|
*
|
||||||
|
* @coversDefaultClass \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage
|
||||||
|
*
|
||||||
|
* @group Drupal
|
||||||
* @group PhpStorage
|
* @group PhpStorage
|
||||||
*/
|
*/
|
||||||
class MTimeProtectedFastFileStorageTest extends MTimeProtectedFileStorageBase {
|
class MTimeProtectedFastFileStorageTest extends MTimeProtectedFileStorageBase {
|
||||||
|
|
|
@ -50,6 +50,11 @@ abstract class MTimeProtectedFileStorageBase extends PhpStorageTestBase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests basic load/save/delete operations.
|
* Tests basic load/save/delete operations.
|
||||||
|
*
|
||||||
|
* @covers ::load
|
||||||
|
* @covers ::save
|
||||||
|
* @covers ::delete
|
||||||
|
* @covers ::exists
|
||||||
*/
|
*/
|
||||||
public function testCRUD() {
|
public function testCRUD() {
|
||||||
$php = new $this->storageClass($this->settings);
|
$php = new $this->storageClass($this->settings);
|
||||||
|
|
|
@ -10,6 +10,9 @@ namespace Drupal\Tests\Component\PhpStorage;
|
||||||
/**
|
/**
|
||||||
* Tests the MTimeProtectedFileStorage implementation.
|
* Tests the MTimeProtectedFileStorage implementation.
|
||||||
*
|
*
|
||||||
|
* @coversDefaultClass \Drupal\Component\PhpStorage\MTimeProtectedFileStorage
|
||||||
|
*
|
||||||
|
* @group Drupal
|
||||||
* @group PhpStorage
|
* @group PhpStorage
|
||||||
*/
|
*/
|
||||||
class MTimeProtectedFileStorageTest extends MTimeProtectedFileStorageBase {
|
class MTimeProtectedFileStorageTest extends MTimeProtectedFileStorageBase {
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
namespace Drupal\Tests\Component\PhpStorage;
|
namespace Drupal\Tests\Component\PhpStorage;
|
||||||
|
|
||||||
use Drupal\Tests\UnitTestCase;
|
use Drupal\Tests\UnitTestCase;
|
||||||
|
use org\bovigo\vfs\vfsStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base test for PHP storages.
|
* Base test for PHP storages.
|
||||||
|
@ -26,7 +27,8 @@ abstract class PhpStorageTestBase extends UnitTestCase {
|
||||||
*/
|
*/
|
||||||
protected function setUp() {
|
protected function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->directory = sys_get_temp_dir() . '/php' . str_replace('\\','_', get_class($this));
|
vfsStream::setup('exampleDir');
|
||||||
|
$this->directory = vfsStream::url('exampleDir');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,22 +45,21 @@ abstract class PhpStorageTestBase extends UnitTestCase {
|
||||||
// Write out a PHP file and ensure it's successfully loaded.
|
// Write out a PHP file and ensure it's successfully loaded.
|
||||||
$code = "<?php\n\$GLOBALS[$random] = TRUE;";
|
$code = "<?php\n\$GLOBALS[$random] = TRUE;";
|
||||||
$success = $php->save($name, $code);
|
$success = $php->save($name, $code);
|
||||||
$this->assertSame($success, TRUE);
|
$this->assertTrue($success, 'Saved php file');
|
||||||
$php->load($name);
|
$php->load($name);
|
||||||
$this->assertTrue($GLOBALS[$random]);
|
$this->assertTrue($GLOBALS[$random], 'File saved correctly with correct value');
|
||||||
|
|
||||||
// If the file was successfully loaded, it must also exist, but ensure the
|
// If the file was successfully loaded, it must also exist, but ensure the
|
||||||
// exists() method returns that correctly.
|
// exists() method returns that correctly.
|
||||||
$this->assertSame($php->exists($name), TRUE);
|
$this->assertTrue($php->exists($name), 'Exists works correctly');
|
||||||
|
|
||||||
// Delete the file, and then ensure exists() returns FALSE.
|
// Delete the file, and then ensure exists() returns FALSE.
|
||||||
$success = $php->delete($name);
|
$this->assertTrue($php->delete($name), 'Delete suceeded');
|
||||||
$this->assertSame($success, TRUE);
|
$this->assertFalse($php->exists($name), 'Delete deleted file');
|
||||||
$this->assertSame($php->exists($name), FALSE);
|
|
||||||
|
|
||||||
// Ensure delete() can be called on a non-existing file. It should return
|
// Ensure delete() can be called on a non-existing file. It should return
|
||||||
// FALSE, but not trigger errors.
|
// FALSE, but not trigger errors.
|
||||||
$this->assertSame($php->delete($name), FALSE);
|
$this->assertFalse($php->delete($name), 'Delete fails on missing file');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue