diff --git a/core/modules/system/lib/Drupal/system/PhpStorage/MockPhpStorage.php b/core/modules/system/lib/Drupal/system/PhpStorage/MockPhpStorage.php index 0874148..0ef5e62 100644 --- a/core/modules/system/lib/Drupal/system/PhpStorage/MockPhpStorage.php +++ b/core/modules/system/lib/Drupal/system/PhpStorage/MockPhpStorage.php @@ -7,6 +7,39 @@ 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; + } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/PhpStorage/PhpStorageFactoryTest.php b/core/modules/system/lib/Drupal/system/Tests/PhpStorage/PhpStorageFactoryTest.php index f788a0f..5ff16d3 100644 --- a/core/modules/system/lib/Drupal/system/Tests/PhpStorage/PhpStorageFactoryTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/PhpStorage/PhpStorageFactoryTest.php @@ -7,12 +7,12 @@ namespace Drupal\system\Tests\PhpStorage; -use Drupal\Component\PhpStorage\FileReadOnlyStorage; use Drupal\Component\PhpStorage\MTimeProtectedFileStorage; -use Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage; 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. @@ -42,32 +42,39 @@ public function testGetNoSettings() { } /** + * 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 FileReadOnlyStorage, 'A FileReadOnlyStorage instance was returned from overridden settings.'); + $this->assertTrue($php instanceof MockPhpStorage, 'A MockPhpStorage instance was returned from overridden settings.'); - // Test with a protected storage class. - $this->setSettings('test', 'Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage'); + // 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 MTimeProtectedFastFileStorage, 'An MTimeProtectedFastFileStorage instance was returned from overridden settings.'); + $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 the name is used for the bin when it is NULL. - $this->setSettings('test', 'Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage', NULL); + // 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 MTimeProtectedFastFileStorage, 'An MTimeProtectedFastFileStorage instance was returned from overridden settings.'); - } + $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.'); - /** - * Tests the get() method using the 'default' settings. - */ - public function testGetDefault() { - $this->setSettings(); + // 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 FileReadOnlyStorage, 'A FileReadOnlyStorage instance was returned with default settings.'); + $this->assertTrue($php instanceof MTimeProtectedFileStorage, 'An MTimeProtectedFileStorage instance was returned from overridden settings with no class.'); } /** @@ -75,12 +82,12 @@ public function testGetDefault() { * * @param string $name * The storage bin name to set. - * @param string $class - * The class that should be set for this bin. + * @param array $configuration + * An array of configuration to set. Will be merged with default values. */ - protected function setSettings($name = 'default', $class = 'Drupal\Component\PhpStorage\FileReadOnlyStorage') { - $settings['php_storage'][$name] = array( - 'class' => $class, + 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',