Commit 0d3968ee authored by webchick's avatar webchick

Issue #1935922 by msonnabaum: Convert PhpStorage tests to phpunit.

parent c0475473
......@@ -2,10 +2,10 @@
/**
* @file
* Definition of Drupal\system\Tests\PhpStorage\FileStorageTest.
* Definition of Drupal\Tests\Component\PhpStorage\FileStorageTest.
*/
namespace Drupal\system\Tests\PhpStorage;
namespace Drupal\Tests\Component\PhpStorage;
/**
* Tests the simple file storage.
......@@ -23,7 +23,7 @@ public static function getInfo() {
function setUp() {
global $conf;
parent::setUp();
$dir_path = DRUPAL_ROOT . '/' . $this->public_files_directory . '/php';
$dir_path = sys_get_temp_dir() . '/php';
$conf['php_storage']['simpletest'] = array(
'class' => 'Drupal\Component\PhpStorage\FileStorage',
'directory' => $dir_path,
......@@ -41,7 +41,7 @@ function setUp() {
*/
function testCRUD() {
$php = $this->storageFactory->get('simpletest');
$this->assertIdentical(get_class($php), 'Drupal\Component\PhpStorage\FileStorage');
$this->assertInstanceOf('Drupal\Component\PhpStorage\FileStorage', $php);
$this->assertCRUD($php);
}
......@@ -60,14 +60,14 @@ function testReadOnly() {
// Write out a PHP file and ensure it's successfully loaded.
$code = "<?php\n\$GLOBALS[$random] = TRUE;";
$success = $php->save($name, $code);
$this->assertIdentical($success, TRUE);
$this->assertSame($success, TRUE);
$php_read = $this->storageFactory->get('readonly');
$php_read->load($name);
$this->assertTrue($GLOBALS[$random]);
// If the file was successfully loaded, it must also exist, but ensure the
// exists() method returns that correctly.
$this->assertIdentical($php_read->exists($name), TRUE);
$this->assertSame($php_read->exists($name), TRUE);
// Saving and deleting should always fail.
$this->assertFalse($php_read->save($name, $code));
$this->assertFalse($php_read->delete($name));
......
......@@ -2,10 +2,10 @@
/**
* @file
* Definition of Drupal\system\Tests\PhpStorage\MTimeProtectedFileStorageTest.
* Definition of Drupal\Tests\Component\PhpStorage\MTimeProtectedFileStorageTest.
*/
namespace Drupal\system\Tests\PhpStorage;
namespace Drupal\Tests\Component\PhpStorage;
/**
* Tests the directory mtime based PHP loader implementation.
......
......@@ -2,10 +2,10 @@
/**
* @file
* Definition of Drupal\system\Tests\PhpStorage\MTimeProtectedFileStorageTest.
* Definition of Drupal\Tests\Component\PhpStorage\MTimeProtectedFileStorageTest.
*/
namespace Drupal\system\Tests\PhpStorage;
namespace Drupal\Tests\Component\PhpStorage;
use Drupal\Component\PhpStorage\PhpStorageFactory;
......@@ -38,7 +38,7 @@ function setUp() {
$this->secret = $this->randomName();
$conf['php_storage']['simpletest'] = array(
'class' => $this->storageClass,
'directory' => DRUPAL_ROOT . '/' . $this->public_files_directory . '/php',
'directory' => sys_get_temp_dir() . '/php',
'secret' => $this->secret,
);
}
......@@ -48,7 +48,7 @@ function setUp() {
*/
function testCRUD() {
$php = $this->storageFactory->get('simpletest');
$this->assertIdentical(get_class($php), $this->storageClass);
$this->assertSame(get_class($php), $this->storageClass);
$this->assertCRUD($php);
}
......@@ -62,7 +62,7 @@ function testSecurity() {
$php = $this->storageFactory->get('simpletest');
$name = 'simpletest.php';
$php->save($name, '<?php');
$expected_root_directory = DRUPAL_ROOT . '/' . $this->public_files_directory . '/php/simpletest';
$expected_root_directory = sys_get_temp_dir() . '/php/simpletest';
$expected_directory = $expected_root_directory . '/' . $name;
$directory_mtime = filemtime($expected_directory);
$expected_filename = $expected_directory . '/' . hash_hmac('sha256', $name, $this->secret . $directory_mtime) . '.php';
......@@ -71,12 +71,12 @@ function testSecurity() {
// minimal permissions. fileperms() can return high bits unrelated to
// permissions, so mask with 0777.
$this->assertTrue(file_exists($expected_filename));
$this->assertIdentical(fileperms($expected_filename) & 0777, 0400);
$this->assertIdentical(fileperms($expected_directory) & 0777, 0100);
$this->assertSame(fileperms($expected_filename) & 0777, 0400);
$this->assertSame(fileperms($expected_directory) & 0777, 0100);
// Ensure the root directory for the bin has a .htaccess file denying web
// access.
$this->assertIdentical(file_get_contents($expected_root_directory . '/.htaccess'), "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nDeny from all\nOptions None\nOptions +FollowSymLinks");
$this->assertSame(file_get_contents($expected_root_directory . '/.htaccess'), "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nDeny from all\nOptions None\nOptions +FollowSymLinks");
// Ensure that if the file is replaced with an untrusted one (due to another
// script's file upload vulnerability), it does not get loaded. Since mtime
......@@ -103,10 +103,10 @@ function testSecurity() {
}
chmod($expected_filename, 0400);
chmod($expected_directory, 0100);
$this->assertIdentical(file_get_contents($expected_filename), $untrusted_code);
$this->assertIdentical($php->exists($name), $this->expected[$i]);
$this->assertIdentical($php->load($name), $this->expected[$i]);
$this->assertIdentical($GLOBALS['hacked'], $this->expected[$i]);
$this->assertSame(file_get_contents($expected_filename), $untrusted_code);
$this->assertSame($php->exists($name), $this->expected[$i]);
$this->assertSame($php->load($name), $this->expected[$i]);
$this->assertSame($GLOBALS['hacked'], $this->expected[$i]);
}
}
}
......@@ -2,18 +2,18 @@
/**
* @file
* Definition of Drupal\system\Tests\PhpStorage\PhpStorageTestBase.
* Definition of Drupal\Tests\Component\PhpStorage\PhpStorageTestBase.
*/
namespace Drupal\system\Tests\PhpStorage;
namespace Drupal\Tests\Component\PhpStorage;
use Drupal\simpletest\UnitTestBase;
use Drupal\Tests\UnitTestCase;
use Drupal\Component\PhpStorage\PhpStorageFactory;
/**
* Base test for PHP storage controllers.
*/
abstract class PhpStorageTestBase extends UnitTestBase {
abstract class PhpStorageTestBase extends UnitTestCase {
/**
* The storage factory object.
......@@ -23,7 +23,7 @@ abstract class PhpStorageTestBase extends UnitTestBase {
protected $storageFactory;
/**
* Overrides \Drupal\simpletest\UnitTestBase::setUp()
* Overrides \Drupal\Tests\UnitTestCase::setUp()
*/
function setUp() {
parent::setUp();
......@@ -44,21 +44,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->assertIdentical($success, TRUE);
$this->assertSame($success, TRUE);
$php->load($name);
$this->assertTrue($GLOBALS[$random]);
// If the file was successfully loaded, it must also exist, but ensure the
// exists() method returns that correctly.
$this->assertIdentical($php->exists($name), TRUE);
$this->assertSame($php->exists($name), TRUE);
// Delete the file, and then ensure exists() returns FALSE.
$success = $php->delete($name);
$this->assertIdentical($success, TRUE);
$this->assertIdentical($php->exists($name), FALSE);
$this->assertSame($success, TRUE);
$this->assertSame($php->exists($name), FALSE);
// Ensure delete() can be called on a non-existing file. It should return
// FALSE, but not trigger errors.
$this->assertIdentical($php->delete($name), FALSE);
$this->assertSame($php->delete($name), FALSE);
}
}
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