From 98b74d2d45e4716da504993c6d41f63db94c01fb Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 31 Mar 2015 16:15:17 +0100 Subject: [PATCH] Issue #2453399 by neclimdul: Use VFS for FileStorage tests --- .../MTimeProtectedFastFileStorage.php | 20 +++++++++-- .../PhpStorage/FileStorageReadOnlyTest.php | 27 ++++++++------ .../Component/PhpStorage/FileStorageTest.php | 35 +++++++++---------- .../MTimeProtectedFastFileStorageTest.php | 3 ++ .../MTimeProtectedFileStorageBase.php | 5 +++ .../MTimeProtectedFileStorageTest.php | 3 ++ .../PhpStorage/PhpStorageTestBase.php | 17 ++++----- 7 files changed, 70 insertions(+), 40 deletions(-) diff --git a/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php b/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php index 73486a86ca3..e31a603e6c3 100644 --- a/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php +++ b/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php @@ -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; + } } diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageReadOnlyTest.php b/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageReadOnlyTest.php index 5c96fe4e380..8cf9642276c 100644 --- a/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageReadOnlyTest.php +++ b/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageReadOnlyTest.php @@ -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 = "assertTrue($php->save($name, $code)); + $php_read = new FileReadOnlyStorage($this->readonlyStorage); $this->assertFalse($php_read->deleteAll()); diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php b/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php index a083dd3c9ff..e0a7d817f34 100644 --- a/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php +++ b/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php @@ -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 = "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'); } } diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFastFileStorageTest.php b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFastFileStorageTest.php index 2fad11a8d92..91a486277eb 100644 --- a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFastFileStorageTest.php +++ b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFastFileStorageTest.php @@ -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 { diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageBase.php b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageBase.php index 9640dbecf6a..5667e42b021 100644 --- a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageBase.php +++ b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageBase.php @@ -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); diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php index edf9e323f62..6105e4f3214 100644 --- a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php +++ b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php @@ -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 { diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php b/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php index f3e6a4f07c7..fabf30108b8 100644 --- a/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php +++ b/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php @@ -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 = "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'); } }