Commit 2aefd896 authored by alexpott's avatar alexpott

Issue #2210631 by damiankloip: Move PhpStorageFactory to the Core namespace.

parent 2826a61d
......@@ -15,7 +15,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Exception\ParseException;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\EventSubscriber\HtmlViewSubscriber;
......
......@@ -6,7 +6,7 @@
*/
use Drupal\Component\Utility\Variable;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Cache\Cache;
/**
......
......@@ -7,7 +7,7 @@
namespace Drupal\Core;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Config\BootstrapConfigStorageFactory;
use Drupal\Core\Config\NullStorage;
use Drupal\Core\CoreServiceProvider;
......
......@@ -5,7 +5,7 @@
* Definition of Drupal\Component\PhpStorage\PhpStorageFactory.
*/
namespace Drupal\Component\PhpStorage;
namespace Drupal\Core\PhpStorage;
use Drupal\Component\Utility\Settings;
use Drupal\Core\StreamWrapper\PublicStream;
......
......@@ -7,7 +7,7 @@
namespace Drupal\Core\Template;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
......
......@@ -5,7 +5,7 @@
* Add language handling functionality to Drupal.
*/
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Language\Language;
use Drupal\language\ConfigurableLanguageManager;
use Drupal\language\ConfigurableLanguageManagerInterface;
......
......@@ -7,7 +7,7 @@
namespace Drupal\language;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\Language;
......
......@@ -7,7 +7,7 @@
namespace Drupal\language\EventSubscriber;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......
<?php
/**
* @file
* Contains \Drupal\system\PhpStorage\MockPhpStorage.
*/
namespace Drupal\system\PhpStorage;
/**
* Mock PHP storage class used for testing.
*/
class MockPhpStorage {
/**
* The storage configuration.
*
* @var array
*/
protected $configuration;
/**
* Constructs a MockPhpStorage object.
*
* @param array $configuration
*/
public function __construct(array $configuration) {
$this->configuration = $configuration;
}
/**
* Gets the configuration data.
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* Gets a single configuration key.
*/
public function getConfigurationValue($key) {
return isset($this->configuration[$key]) ? $this->configuration[$key] : NULL;
}
}
<?php
/**
* @file
* Contains \Drupal\system\Tests\Session\PhpStorage\PhpStorageFactoryTest.
*/
namespace Drupal\system\Tests\PhpStorage;
use Drupal\Component\PhpStorage\MTimeProtectedFileStorage;
use Drupal\Component\Utility\Settings;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\system\PhpStorage\MockPhpStorage;
/**
* Tests the PHP storage factory.
*
* @see \Drupal\Core\PhpStorage\PhpStorageFactory
*/
class PhpStorageFactoryTest extends DrupalUnitTestBase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'PHP storage factory tests',
'description' => 'Tests the PHP storage factory..',
'group' => 'PhpStorage',
);
}
/**
* Tests the get() method with no settings.
*/
public function testGetNoSettings() {
$php = PhpStorageFactory::get('test');
// This should be the default class used.
$this->assertTrue($php instanceof MTimeProtectedFileStorage, 'An MTimeProtectedFileStorage instance was returned with no settings.');
}
/**
* Tests the get() method using the 'default' settings.
*/
public function testGetDefault() {
$this->setSettings();
$php = PhpStorageFactory::get('test');
$this->assertTrue($php instanceof MockPhpStorage, 'A FileReadOnlyStorage instance was returned with default settings.');
}
/**
* Tests the get() method with overridden settings.
*/
public function testGetOverride() {
$this->setSettings('test');
$php = PhpStorageFactory::get('test');
// The FileReadOnlyStorage should be used from settings.
$this->assertTrue($php instanceof MockPhpStorage, 'A MockPhpStorage instance was returned from overridden settings.');
// Test that the name is used for the bin when it is NULL.
$this->setSettings('test', array('bin' => NULL));
$php = PhpStorageFactory::get('test');
$this->assertTrue($php instanceof MockPhpStorage, 'An MockPhpStorage instance was returned from overridden settings.');
$this->assertIdentical('test', $php->getConfigurationValue('bin'), 'Name value was used for bin.');
// Test that a default directory is set if it's empty.
$this->setSettings('test', array('directory' => NULL));
$php = PhpStorageFactory::get('test');
$this->assertTrue($php instanceof MockPhpStorage, 'An MockPhpStorage instance was returned from overridden settings.');
$this->assertIdentical(DRUPAL_ROOT . '/' . PublicStream::basePath() . '/php', $php->getConfigurationValue('directory'), 'Default file directory was used.');
// Test that a default storage class is set if it's empty.
$this->setSettings('test', array('class' => NULL));
$php = PhpStorageFactory::get('test');
$this->assertTrue($php instanceof MTimeProtectedFileStorage, 'An MTimeProtectedFileStorage instance was returned from overridden settings with no class.');
}
/**
* Sets the Settings() singleton.
*
* @param string $name
* The storage bin name to set.
* @param array $configuration
* An array of configuration to set. Will be merged with default values.
*/
protected function setSettings($name = 'default', array $configuration = array()) {
$settings['php_storage'][$name] = $configuration + array(
'class' => 'Drupal\system\PhpStorage\MockPhpStorage',
'directory' => 'tmp://',
'secret' => $this->randomString(),
'bin' => 'test',
);
new Settings($settings);
}
}
......@@ -8,7 +8,7 @@
namespace Drupal\system\Tests\Theme;
use Drupal\simpletest\WebTestBase;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
/**
* Tests Twig engine configuration via settings.php.
......
......@@ -7,13 +7,37 @@
namespace Drupal\Tests\Component\PhpStorage;
use Drupal\Component\Utility\Settings;
use Drupal\Component\PhpStorage\FileStorage;
use Drupal\Component\PhpStorage\FileReadOnlyStorage;
/**
* Tests the simple file storage.
*
* @group Drupal
* @group PhpStorage
*
* @coversDefaultClass \Drupal\Component\PhpStorage\FileStorage
* @coversDefaultClass \Drupal\Component\PhpStorage\FileReadOnlyStorage
*/
class FileStorageTest extends PhpStorageTestBase {
/**
* Standard test settings to pass to storage instances.
*
* @var array
*/
protected $standardSettings;
/**
* Read only test settings to pass to storage instances.
*
* @var array
*/
protected $readonlyStorage;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Simple file storage',
......@@ -22,20 +46,23 @@ public static function getInfo() {
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$dir_path = sys_get_temp_dir() . '/php';
$settings['php_storage']['simpletest'] = array(
'class' => 'Drupal\Component\PhpStorage\FileStorage',
$this->standardSettings = array(
'directory' => $dir_path,
'bin' => 'test',
);
$settings['php_storage']['readonly'] = array(
'class' => 'Drupal\Component\PhpStorage\FileReadOnlyStorage',
$this->readonlyStorage = array(
'directory' => $dir_path,
// Let this read from the bin where the other instance is writing.
'bin' => 'simpletest',
'bin' => 'test',
);
new Settings($settings);
}
/**
......@@ -45,8 +72,7 @@ public function setUp() {
* @group PhpStorage
*/
public function testCRUD() {
$php = $this->storageFactory->get('simpletest');
$this->assertInstanceOf('Drupal\Component\PhpStorage\FileStorage', $php);
$php = new FileStorage($this->standardSettings);
$this->assertCRUD($php);
}
......@@ -57,7 +83,7 @@ public function testCRUD() {
* @group PhpStorage
*/
public function testReadOnly() {
$php = $this->storageFactory->get('simpletest');
$php = new FileStorage($this->standardSettings);
$name = $this->randomName() . '/' . $this->randomName() . '.php';
// Find a global that doesn't exist.
......@@ -69,7 +95,7 @@ public function testReadOnly() {
$code = "<?php\n\$GLOBALS[$random] = TRUE;";
$success = $php->save($name, $code);
$this->assertSame($success, TRUE);
$php_read = $this->storageFactory->get('readonly');
$php_read = new FileReadOnlyStorage($this->readonlyStorage);
$php_read->load($name);
$this->assertTrue($GLOBALS[$random]);
......@@ -88,10 +114,10 @@ public function testReadOnly() {
* @group PhpStorage
*/
public function testWriteable() {
$php_read = $this->storageFactory->get('readonly');
$php_read = new FileReadOnlyStorage($this->readonlyStorage);
$this->assertFalse($php_read->writeable());
$php = $this->storageFactory->get('simpletest');
$php = new FileStorage($this->standardSettings);
$this->assertTrue($php->writeable());
}
......@@ -102,14 +128,14 @@ public function testWriteable() {
* @group PhpStorage
*/
public function testDeleteAll() {
$php_read = $this->storageFactory->get('readonly');
$php_read = new FileReadOnlyStorage($this->readonlyStorage);
$this->assertFalse($php_read->deleteAll());
// Make sure directory exists prior to removal.
$this->assertTrue(file_exists(sys_get_temp_dir() . '/php/simpletest'), 'File storage directory does not exist.');
// Write out some files.
$php = $this->storageFactory->get('simpletest');
$php = new FileStorage($this->standardSettings);
$name = $this->randomName() . '/' . $this->randomName() . '.php';
// Find a global that doesn't exist.
......@@ -126,7 +152,7 @@ public function testDeleteAll() {
$this->assertTrue($php->deleteAll());
$this->assertFalse($php->load($name));
$this->assertFalse(file_exists(sys_get_temp_dir() . '/php/simpletest'), 'File storage directory still exists after call to deleteAll().');
$this->assertFalse(file_exists(sys_get_temp_dir() . '/php/test'), 'File storage directory still exists after call to deleteAll().');
// Should still return TRUE if directory has already been deleted.
$this->assertTrue($php->deleteAll());
......
......@@ -7,8 +7,6 @@
namespace Drupal\Tests\Component\PhpStorage;
use Drupal\Component\Utility\Settings;
/**
* Base test class for MTime protected storage.
*/
......@@ -28,6 +26,13 @@ abstract class MTimeProtectedFileStorageBase extends PhpStorageTestBase {
*/
protected $secret;
/**
* Test settings to pass to storage instances.
*
* @var array
*/
protected $settings;
/**
* {@inheritdoc}
*/
......@@ -35,20 +40,19 @@ public function setUp() {
parent::setUp();
$this->secret = $this->randomName();
$settings['php_storage']['simpletest'] = array(
'class' => $this->storageClass,
$this->settings = array(
'directory' => sys_get_temp_dir() . '/php',
'bin' => 'test',
'secret' => $this->secret,
);
new Settings($settings);
}
/**
* Tests basic load/save/delete operations.
*/
public function testCRUD() {
$php = $this->storageFactory->get('simpletest');
$this->assertSame(get_class($php), $this->storageClass);
$php = new $this->storageClass($this->settings);
$this->assertCRUD($php);
}
......@@ -62,10 +66,10 @@ public function testCRUD() {
* @medium
*/
public function testSecurity() {
$php = $this->storageFactory->get('simpletest');
$php = new $this->storageClass($this->settings);
$name = 'simpletest.php';
$php->save($name, '<?php');
$expected_root_directory = sys_get_temp_dir() . '/php/simpletest';
$expected_root_directory = sys_get_temp_dir() . '/php/test';
if (substr($name, -4) === '.php') {
$expected_directory = $expected_root_directory . '/' . substr($name, 0, -4);
}
......@@ -92,7 +96,7 @@ public function testSecurity() {
// a second of the initial save().
sleep(1);
for ($i = 0; $i < 2; $i++) {
$php = $this->storageFactory->get('simpletest');
$php = new $this->storageClass($this->settings);
$GLOBALS['hacked'] = FALSE;
$untrusted_code = "<?php\n" . '$GLOBALS["hacked"] = TRUE;';
chmod($expected_directory, 0700);
......
......@@ -8,7 +8,7 @@
namespace Drupal\Tests\Component\PhpStorage;
use Drupal\Tests\UnitTestCase;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
/**
* Base test for PHP storages.
......
......@@ -7,7 +7,7 @@
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Tests\UnitTestCase;
/**
......
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