Issue #2453399 by neclimdul: Use VFS for FileStorage tests

8.0.x
Alex Pott 2015-03-31 16:15:17 +01:00
parent 2745b6127f
commit 98b74d2d45
7 changed files with 70 additions and 40 deletions

View File

@ -70,7 +70,7 @@ class MTimeProtectedFastFileStorage extends FileStorage {
// Write the file out to a temporary location. Prepend with a '.' to keep it
// 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)) {
return FALSE;
}
@ -103,7 +103,7 @@ class MTimeProtectedFastFileStorage extends FileStorage {
// iteration.
if ($i > 0) {
$this->unlink($temporary_path);
$temporary_path = tempnam($this->directory, '.');
$temporary_path = $this->tempnam($this->directory, '.');
rename($full_path, $temporary_path);
// Make sure to not loop infinitely on a hopelessly slow filesystem.
if ($i > 10) {
@ -184,4 +184,20 @@ class MTimeProtectedFastFileStorage extends FileStorage {
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;
}
}

View File

@ -12,6 +12,8 @@ use Drupal\Component\PhpStorage\FileReadOnlyStorage;
/**
* @coversDefaultClass \Drupal\Component\PhpStorage\FileReadOnlyStorage
*
* @group Drupal
* @group PhpStorage
*/
class FileStorageReadOnlyTest extends PhpStorageTestBase {
@ -49,9 +51,6 @@ class FileStorageReadOnlyTest extends PhpStorageTestBase {
/**
* Tests writing with one class and reading with another.
*
* @group Drupal
* @group PhpStorage
*/
public function testReadOnly() {
$php = new FileStorage($this->standardSettings);
@ -79,10 +78,7 @@ class FileStorageReadOnlyTest extends PhpStorageTestBase {
}
/**
* Tests writeable() method.
*
* @group Drupal
* @group PhpStorage
* @covers ::writeable
*/
public function testWriteable() {
$php_read = new FileReadOnlyStorage($this->readonlyStorage);
@ -90,12 +86,21 @@ class FileStorageReadOnlyTest extends PhpStorageTestBase {
}
/**
* Tests deleteAll() method.
*
* @group Drupal
* @group PhpStorage
* @covers ::deleteAll
*/
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);
$this->assertFalse($php_read->deleteAll());

View File

@ -11,6 +11,7 @@ use Drupal\Component\PhpStorage\FileStorage;
/**
* @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage
* @group Drupal
* @group PhpStorage
*/
class FileStorageTest extends PhpStorageTestBase {
@ -37,8 +38,10 @@ class FileStorageTest extends PhpStorageTestBase {
/**
* Tests basic load/save/delete operations.
*
* @group Drupal
* @group PhpStorage
* @covers ::load
* @covers ::save
* @covers ::exists
* @covers ::delete
*/
public function testCRUD() {
$php = new FileStorage($this->standardSettings);
@ -46,10 +49,7 @@ class FileStorageTest extends PhpStorageTestBase {
}
/**
* Tests writeable() method.
*
* @group Drupal
* @group PhpStorage
* @covers ::writeable
*/
public function testWriteable() {
$php = new FileStorage($this->standardSettings);
@ -57,18 +57,13 @@ class FileStorageTest extends PhpStorageTestBase {
}
/**
* Tests deleteAll() method.
*
* @group Drupal
* @group PhpStorage
* @covers ::deleteAll
*/
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.
$php = new FileStorage($this->standardSettings);
$name = $this->randomMachineName() . '/' . $this->randomMachineName() . '.php';
// 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.
$code = "<?php\n\$GLOBALS[$random] = TRUE;";
$success = $php->save($name, $code);
$this->assertSame($success, TRUE);
$this->assertTrue($php->save($name, $code), 'Saved php file');
$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(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.
$this->assertTrue($php->deleteAll());
$this->assertTrue($php->deleteAll(), 'Delete all succeeds with nothing to delete');
}
}

View File

@ -10,6 +10,9 @@ namespace Drupal\Tests\Component\PhpStorage;
/**
* Tests the MTimeProtectedFastFileStorage implementation.
*
* @coversDefaultClass \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage
*
* @group Drupal
* @group PhpStorage
*/
class MTimeProtectedFastFileStorageTest extends MTimeProtectedFileStorageBase {

View File

@ -50,6 +50,11 @@ abstract class MTimeProtectedFileStorageBase extends PhpStorageTestBase {
/**
* Tests basic load/save/delete operations.
*
* @covers ::load
* @covers ::save
* @covers ::delete
* @covers ::exists
*/
public function testCRUD() {
$php = new $this->storageClass($this->settings);

View File

@ -10,6 +10,9 @@ namespace Drupal\Tests\Component\PhpStorage;
/**
* Tests the MTimeProtectedFileStorage implementation.
*
* @coversDefaultClass \Drupal\Component\PhpStorage\MTimeProtectedFileStorage
*
* @group Drupal
* @group PhpStorage
*/
class MTimeProtectedFileStorageTest extends MTimeProtectedFileStorageBase {

View File

@ -8,6 +8,7 @@
namespace Drupal\Tests\Component\PhpStorage;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
/**
* Base test for PHP storages.
@ -26,7 +27,8 @@ abstract class PhpStorageTestBase extends UnitTestCase {
*/
protected function 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.
$code = "<?php\n\$GLOBALS[$random] = TRUE;";
$success = $php->save($name, $code);
$this->assertSame($success, TRUE);
$this->assertTrue($success, 'Saved php file');
$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
// 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.
$success = $php->delete($name);
$this->assertSame($success, TRUE);
$this->assertSame($php->exists($name), FALSE);
$this->assertTrue($php->delete($name), 'Delete suceeded');
$this->assertFalse($php->exists($name), 'Delete deleted file');
// Ensure delete() can be called on a non-existing file. It should return
// FALSE, but not trigger errors.
$this->assertSame($php->delete($name), FALSE);
$this->assertFalse($php->delete($name), 'Delete fails on missing file');
}
}