Commit 98b74d2d authored by alexpott's avatar alexpott
Browse files

Issue #2453399 by neclimdul: Use VFS for FileStorage tests

parent 2745b612
......@@ -70,7 +70,7 @@ public function save($name, $data) {
// 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 @@ public function save($name, $data) {
// 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 @@ protected function getUncachedMTime($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 @@
/**
* @coversDefaultClass \Drupal\Component\PhpStorage\FileReadOnlyStorage
*
* @group Drupal
* @group PhpStorage
*/
class FileStorageReadOnlyTest extends PhpStorageTestBase {
......@@ -49,9 +51,6 @@ protected function setUp() {
/**
* 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 @@ public function testReadOnly() {
}
/**
* Tests writeable() method.
*
* @group Drupal
* @group PhpStorage
* @covers ::writeable
*/
public function testWriteable() {
$php_read = new FileReadOnlyStorage($this->readonlyStorage);
......@@ -90,12 +86,21 @@ public function testWriteable() {
}
/**
* 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());
......
......@@ -11,6 +11,7 @@
/**
* @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage
* @group Drupal
* @group PhpStorage
*/
class FileStorageTest extends PhpStorageTestBase {
......@@ -37,8 +38,10 @@ protected function setUp() {
/**
* 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 @@ public function testCRUD() {
}
/**
* Tests writeable() method.
*
* @group Drupal
* @group PhpStorage
* @covers ::writeable
*/
public function testWriteable() {
$php = new FileStorage($this->standardSettings);
......@@ -57,18 +57,13 @@ public function testWriteable() {
}
/**
* 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 @@ public function testDeleteAll() {
// 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');
// Make sure directory exists prior to removal.
$this->assertTrue(file_exists($this->directory . '/test'), 'File storage directory does not exist.');
$this->assertTrue($php->deleteAll());
$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');
}
}
......@@ -10,6 +10,9 @@
/**
* Tests the MTimeProtectedFastFileStorage implementation.
*
* @coversDefaultClass \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage
*
* @group Drupal
* @group PhpStorage
*/
class MTimeProtectedFastFileStorageTest extends MTimeProtectedFileStorageBase {
......
......@@ -50,6 +50,11 @@ protected function setUp() {
/**
* Tests basic load/save/delete operations.
*
* @covers ::load
* @covers ::save
* @covers ::delete
* @covers ::exists
*/
public function testCRUD() {
$php = new $this->storageClass($this->settings);
......
......@@ -10,6 +10,9 @@
/**
* Tests the MTimeProtectedFileStorage implementation.
*
* @coversDefaultClass \Drupal\Component\PhpStorage\MTimeProtectedFileStorage
*
* @group Drupal
* @group PhpStorage
*/
class MTimeProtectedFileStorageTest extends MTimeProtectedFileStorageBase {
......
......@@ -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 @@ public function assertCRUD($php) {
// 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');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment