Commit 66a090ea authored by bircher's avatar bircher

Issue #2927316 by bircher: Throw useful exception when a filter passed to...

Issue #2927316 by bircher: Throw useful exception when a filter passed to FilteredStorage is not a StorageFilterInterface
parent 119b88ba
......@@ -2,6 +2,7 @@
namespace Drupal\config_filter\Config;
use Drupal\config_filter\Exception\InvalidStorageFilterException;
use Drupal\Core\Config\StorageInterface;
/**
......@@ -43,6 +44,9 @@ class FilteredStorage implements FilteredStorageInterface {
// Set the storage to all the filters.
foreach ($this->filters as $filter) {
if (!$filter instanceof StorageFilterInterface) {
throw new InvalidStorageFilterException();
}
$filter->setSourceStorage(new ReadOnlyStorage($storage));
$filter->setFilteredStorage($this);
}
......
<?php
namespace Drupal\config_filter\Exception;
use Drupal\config_filter\Config\StorageFilterInterface;
/**
* Thrown when a StorageFilterInterface is expected but not present.
*/
class InvalidStorageFilterException extends \InvalidArgumentException {
/**
* InvalidStorageFilterException constructor.
*/
public function __construct() {
parent::__construct("An argument does not implement " . StorageFilterInterface::class);
}
}
......@@ -6,6 +6,7 @@ use Drupal\config_filter\Config\FilteredStorage;
use Drupal\config_filter\Config\FilteredStorageInterface;
use Drupal\config_filter\Config\ReadOnlyStorage;
use Drupal\config_filter\Config\StorageFilterInterface;
use Drupal\config_filter\Exception\InvalidStorageFilterException;
use Drupal\Core\Config\CachedStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\KernelTests\Core\Config\Storage\CachedStorageTest;
......@@ -338,6 +339,23 @@ class FilteredStorageTest extends CachedStorageTest {
];
}
/**
* Test that an exception is thrown when invalid arguments are passed.
*/
public function testInvalidStorageFilterArgument() {
$source = $this->prophesize(StorageInterface::class);
// We would do this with $this->expectException but alas drupal is stuck on
// phpunit 4 and we try not to add deprecated code.
try {
new FilteredStorage($source->reveal(), [new \stdClass()]);
$this->fail('An exception should have been thrown.');
}
catch (InvalidStorageFilterException $exception) {
$this->assertTrue(TRUE);
}
}
/**
* Prophesize a StorageFilter.
*/
......
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