Commit 61645174 authored by bircher's avatar bircher

Issue #2925084 by bircher, tstoeckler: FilteredStorage::createCollection()...

Issue #2925084 by bircher, tstoeckler: FilteredStorage::createCollection() needs to set the correct source and filtered storages on filters
parent 1c4a1a0c
......@@ -202,10 +202,10 @@ class FilteredStorage implements FilteredStorageInterface {
*/
public function createCollection($collection) {
$filters = [];
foreach ($this->filters as $filter) {
foreach ($this->filters as $key => $filter) {
$filter = $filter->filterCreateCollection($collection);
if ($filter) {
$filters[] = $filter;
$filters[$key] = $filter;
}
}
......
......@@ -189,7 +189,8 @@ interface StorageFilterInterface {
*
* @return \Drupal\config_filter\Config\StorageFilterInterface|null
* Return a filter that should participate in the collection. This allows
* filters to act on different collections.
* filters to act on different collections. Note that a new instance of the
* filter should be created rather than returning $this directly.
*/
public function filterCreateCollection($collection);
......
......@@ -125,7 +125,7 @@ abstract class ConfigFilterBase extends PluginBase implements ConfigFilterInterf
* {@inheritdoc}
*/
public function filterCreateCollection($collection) {
return $this;
return clone $this;
}
/**
......
......@@ -3,6 +3,7 @@
namespace Drupal\config_filter\Tests;
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\Core\Config\CachedStorage;
......@@ -49,6 +50,44 @@ class FilteredStorageTest extends CachedStorageTest {
}
}
/**
* Test that creating collections keeps filters set to the correct storages.
*/
public function testCollectionStorages() {
$collection = $this->randomString();
// The storage is in its default state.
$this->assertEquals(StorageInterface::DEFAULT_COLLECTION, $this->storage->getCollectionName());
/** @var \Drupal\config_filter\Tests\TransparentFilter[] $filters */
$filters = static::getProtectedFilters($this->storage);
foreach ($filters as $filter) {
// Test that the filters have the correct storage set.
$this->assertEquals($this->storage, $filter->getPrivateFilteredStorage());
$this->assertEquals(StorageInterface::DEFAULT_COLLECTION, $filter->getPrivateSourceStorage()->getCollectionName());
}
// Create a collection which creates a clone of the storage and filters.
$collectionStorage = $this->storage->createCollection($collection);
$this->assertInstanceOf(FilteredStorageInterface::class, $collectionStorage);
/** @var \Drupal\config_filter\Tests\TransparentFilter[] $collectionFilters */
$collectionFilters = static::getProtectedFilters($collectionStorage);
foreach ($collectionFilters as $filter) {
// Test that the cloned filter has the correct storage set.
$this->assertEquals($collectionStorage, $filter->getPrivateFilteredStorage());
$this->assertEquals($collection, $filter->getPrivateSourceStorage()->getCollectionName());
}
/** @var \Drupal\config_filter\Tests\TransparentFilter[] $filters */
$filters = static::getProtectedFilters($this->storage);
foreach ($filters as $filter) {
// Test that the filters on the original storage are still correctly set.
$this->assertEquals($this->storage, $filter->getPrivateFilteredStorage());
$this->assertEquals(StorageInterface::DEFAULT_COLLECTION, $filter->getPrivateSourceStorage()->getCollectionName());
}
}
/**
* Test setting up filters in FilteredStorage::createCollection().
*/
......
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