View source
<?php
namespace Drupal\config_split\Tests;
use Drupal\config_split\Plugin\ConfigFilter\SplitFilter;
use Drupal\Core\Config\DatabaseStorage;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\NullStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamFile;
use Prophecy\Argument;
class SplitFilterTest extends UnitTestCase {
public function testBlacklist() {
$configuration = [];
$configuration['blacklist'] = [
'a',
'b',
];
$configuration['graylist'] = [];
$configuration['module'] = [
'module1' => 0,
'module2' => 0,
];
$configuration['theme'] = [
'theme1' => 0,
];
$configuration['graylist_dependents'] = FALSE;
$manager = $this
->prophesize('Drupal\\Core\\Config\\ConfigManagerInterface');
$manager
->findConfigEntityDependents(Argument::exact('module'), Argument::exact([
'module1',
'module2',
]))
->willReturn([
'c' => 0,
'd' => 0,
'a' => 0,
]);
$manager
->findConfigEntityDependents(Argument::exact('theme'), Argument::exact([
'theme1',
]))
->willReturn([
'e' => 0,
'f' => 0,
'c' => 0,
]);
$all_config = array_merge(array_fill_keys(range("a", "z"), []), [
'module1.settings' => [],
'module3.settings' => [],
]);
$manager
->getConfigFactory()
->willReturn($this
->getConfigStorageStub($all_config));
$manager
->findConfigEntityDependents(Argument::exact('config'), Argument::cetera())
->willReturn([
'f' => 0,
'g' => 0,
'b' => 0,
]);
$filter = new SplitFilter($configuration, 'config_split', [], $manager
->reveal());
$actual = $filter
->getBlacklist();
sort($actual);
$expected = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'module1.settings',
];
$this
->assertArrayEquals($expected, $actual);
}
public function testGraylist() {
$configuration = [];
$configuration['blacklist'] = [];
$configuration['graylist'] = [
'a',
'b',
];
$configuration['module'] = [];
$configuration['theme'] = [];
$configuration['graylist_dependents'] = TRUE;
$manager = $this
->prophesize('Drupal\\Core\\Config\\ConfigManagerInterface');
$manager
->findConfigEntityDependents(Argument::exact('module'), Argument::cetera())
->willReturn([]);
$manager
->findConfigEntityDependents(Argument::exact('theme'), Argument::cetera())
->willReturn([]);
$all_config = array_merge(array_fill_keys(range("a", "z"), []), [
'module1.settings' => [],
'module3.settings' => [],
]);
$manager
->getConfigFactory()
->willReturn($this
->getConfigStorageStub($all_config));
$manager
->findConfigEntityDependents(Argument::exact('config'), Argument::exact([]))
->willReturn([]);
$manager
->findConfigEntityDependents(Argument::exact('config'), Argument::exact([
'a',
'b',
]))
->willReturn([
'f' => 0,
'g' => 0,
'b' => 0,
]);
$filter = new SplitFilter($configuration, 'config_split', [], $manager
->reveal());
$actual = $filter
->getGraylist();
sort($actual);
$this
->assertArrayEquals([
'a',
'b',
'f',
'g',
], $actual);
}
public function testConditionallySplitInCompleteSplit() {
$configuration = [];
$configuration['blacklist'] = [
'b',
'c',
'd',
];
$configuration['graylist'] = [
'a',
];
$configuration['module'] = [];
$configuration['theme'] = [];
$configuration['graylist_dependents'] = TRUE;
$manager = $this
->prophesize('Drupal\\Core\\Config\\ConfigManagerInterface');
$manager
->findConfigEntityDependents(Argument::exact('module'), Argument::cetera())
->willReturn([]);
$manager
->findConfigEntityDependents(Argument::exact('theme'), Argument::cetera())
->willReturn([]);
$all_config = array_merge(array_fill_keys(range("a", "z"), []), [
'module1.settings' => [],
'module3.settings' => [],
]);
$manager
->getConfigFactory()
->willReturn($this
->getConfigStorageStub($all_config));
$manager
->findConfigEntityDependents(Argument::exact('config'), Argument::exact([
'b',
'c',
'd',
]))
->willReturn([
'e' => 0,
]);
$manager
->findConfigEntityDependents(Argument::exact('config'), Argument::exact([
'a',
]))
->willReturn([
'f' => 0,
'b' => 0,
]);
$filter = new SplitFilter($configuration, 'config_split', [], $manager
->reveal());
$actual = $filter
->getBlacklist();
sort($actual);
$this
->assertArrayEquals([
'c',
'd',
'e',
], $actual);
}
public function testWildcards($name, $method) {
$list = [
'a',
'b',
'contact*',
'*.d',
'e*i',
'f*de*',
'x',
];
$configuration = [];
$configuration['blacklist'] = [];
$configuration['graylist'] = [];
$configuration['module'] = [];
$configuration['theme'] = [];
$configuration['graylist_dependents'] = TRUE;
$configuration[$name] = $list;
$manager = $this
->prophesize('Drupal\\Core\\Config\\ConfigManagerInterface');
$manager
->findConfigEntityDependents(Argument::exact('module'), Argument::cetera())
->willReturn([]);
$manager
->findConfigEntityDependents(Argument::exact('theme'), Argument::cetera())
->willReturn([]);
$all_config = array_merge(array_fill_keys(range("a", "z"), []), [
'contact' => [],
'contacts' => [],
'contact.form' => [],
'form' => [],
'form.d' => [],
'form.demo' => [],
'formed' => [],
'ei' => [],
'efi' => [],
'efghi' => [],
'efghijk' => [],
'abcdefghijk' => [],
]);
$manager
->getConfigFactory()
->willReturn($this
->getConfigStorageStub($all_config));
$expected = [
'a',
'b',
'contact',
'contact.form',
'contacts',
'efghi',
'efi',
'ei',
'form.d',
'form.demo',
'x',
];
$manager
->findConfigEntityDependents(Argument::exact('config'), Argument::exact($expected))
->willReturn([]);
$manager
->findConfigEntityDependents(Argument::exact('config'), Argument::exact([]))
->willReturn([]);
$filter = new SplitFilter($configuration, 'config_split', [], $manager
->reveal());
$actual = $filter
->{$method}();
$this
->assertArrayEquals($expected, $actual);
}
public function wildcardsData() {
return [
[
'blacklist',
'getBlacklist',
],
[
'graylist',
'getGraylist',
],
];
}
public function testFilterRead() {
$name = $this
->randomMachineName();
$data = (array) $this
->getRandomGenerator()
->object();
$filter = $this
->getFilter();
$this
->assertEquals($data, $filter
->filterRead($name, $data));
$name2 = $this
->randomMachineName();
$data2 = (array) $this
->getRandomGenerator()
->object();
$storage = $this
->prophesize(StorageInterface::class);
$storage
->read($name)
->willReturn(NULL);
$storage
->read($name2)
->willReturn($data2);
$filter = $this
->getFilter($storage
->reveal());
$this
->assertEquals($data, $filter
->filterRead($name, $data));
$this
->assertEquals($data2, $filter
->filterRead($name2, $data));
$extensions = [
'module' => [
'config' => 0,
'user' => 0,
'views_ui' => 0,
'menu_link_content' => 1,
'views' => 10,
],
'theme' => [
'stable' => 0,
'classy' => 0,
],
];
$modules = [
'module1' => 0,
'module2' => 1,
];
$themes = [
'custom_theme' => 0,
];
$extensions_extra = [
'module' => [
'config' => 0,
'module1' => 0,
'user' => 0,
'views_ui' => 0,
'menu_link_content' => 1,
'module2' => 1,
'views' => 10,
],
'theme' => [
'stable' => 0,
'classy' => 0,
'custom_theme' => 0,
],
];
$filter = $this
->getFilter(NULL, [], $modules, $themes);
$this
->assertEquals($extensions_extra, $filter
->filterRead('core.extension', $extensions));
$this
->assertEquals($extensions_extra, $filter
->filterRead('core.extension', $extensions_extra));
$filter = $this
->getFilter(NULL, [], [
'none' => 0,
], [
'none' => 0,
], [], $name);
$storage = $this
->prophesize(StorageInterface::class);
$storage
->read($name)
->willReturn([
'module' => $modules,
'theme' => $themes,
]);
$filter
->setFilteredStorage($storage
->reveal());
$this
->assertEquals($extensions_extra, $filter
->filterRead('core.extension', $extensions));
$this
->assertEquals($extensions_extra, $filter
->filterRead('core.extension', $extensions_extra));
$filter = $this
->getFilter(NULL, [], [
'none' => 0,
], [
'none' => 0,
], [], $name);
$storage = $this
->prophesize(StorageInterface::class);
$storage
->read($name)
->willReturn(FALSE);
$filter
->setFilteredStorage($storage
->reveal());
$this
->assertEquals($extensions, $filter
->filterRead('core.extension', $extensions));
$this
->assertEquals($extensions_extra, $filter
->filterRead('core.extension', $extensions_extra));
}
public function testFilterWrite() {
$name = $this
->randomMachineName();
$data = (array) $this
->getRandomGenerator()
->object();
try {
$filter = $this
->getFilter();
$filter
->filterWrite($name, $data);
$this
->fail('The filter needs a storage.');
} catch (\InvalidArgumentException $exception) {
$this
->assertTrue(TRUE, 'Exception thrown.');
}
$filter = $this
->getFilter(new NullStorage());
$this
->assertEquals($data, $filter
->filterWrite($name, $data));
$name2 = $this
->randomMachineName();
$filter = $this
->getFilter(new NullStorage(), [
$name2,
], [], []);
$this
->assertEquals($data, $filter
->filterWrite($name, $data));
$this
->assertNull($filter
->filterWrite($name2, $data));
$storage = $this
->prophesize(StorageInterface::class);
$storage
->write(Argument::cetera())
->willReturn(TRUE);
$storage
->exists($name)
->willReturn(FALSE);
$filter = $this
->getFilter($storage
->reveal(), [
$name2,
], [], []);
$this
->assertEquals($data, $filter
->filterWrite($name, $data));
$this
->assertNull($filter
->filterWrite($name2, $data));
$name3 = $this
->randomMachineName();
$data3 = (array) $this
->getRandomGenerator()
->object();
$storage = $this
->prophesize(StorageInterface::class);
$storage
->write(Argument::cetera())
->willReturn(TRUE);
$storage
->read($name3)
->willReturn($data3);
$storage
->exists($name)
->willReturn(TRUE);
$storage
->delete($name)
->willReturn(TRUE)
->shouldBeCalled();
$storage = $storage
->reveal();
$filter = $this
->getFilter($storage, [
$name2,
], [], [], [
$name3,
]);
$filter
->setSourceStorage($storage);
$this
->assertEquals($data, $filter
->filterWrite($name, $data));
$this
->assertNull($filter
->filterWrite($name2, $data));
$this
->assertEquals($data3, $filter
->filterWrite($name3, $data));
$primary = $this
->prophesize(StorageInterface::class);
$primary
->read($name3)
->willReturn($data3);
$primary = $primary
->reveal();
$secondary = $this
->prophesize(StorageInterface::class);
$secondary
->exists($name)
->willReturn(FALSE);
$secondary
->write($name2, $data)
->willReturn(TRUE);
$secondary
->write($name3, $data)
->willReturn(TRUE);
$secondary
->exists($name3)
->willReturn(TRUE);
$secondary
->delete($name3)
->willReturn(TRUE)
->shouldBeCalled();
$secondary = $secondary
->reveal();
$filter = $this
->getFilter($secondary, [
$name2,
], [], [], [
$name3,
], 'test', TRUE);
$filter
->setSourceStorage($primary);
$this
->assertEquals($data, $filter
->filterWrite($name, $data));
$this
->assertNull($filter
->filterWrite($name2, $data));
$this
->assertEquals($data3, $filter
->filterWrite($name3, $data));
$this
->assertEquals($data3, $filter
->filterWrite($name3, $data3));
$extensions = [
'module' => [
'config' => 0,
'user' => 0,
'views_ui' => 0,
'menu_link_content' => 1,
'views' => 10,
],
'theme' => [
'stable' => 0,
'classy' => 0,
],
];
$modules = [
'module1' => 0,
'module2' => 1,
];
$themes = [
'custom_theme' => 0,
];
$extensions_extra = [
'module' => [
'config' => 0,
'module1' => 0,
'user' => 0,
'views_ui' => 0,
'menu_link_content' => 1,
'module2' => 1,
'views' => 10,
],
'theme' => [
'stable' => 0,
'classy' => 0,
'custom_theme' => 0,
],
];
$filter = $this
->getFilter(new NullStorage(), [], $modules, $themes);
$this
->assertEquals($extensions, $filter
->filterWrite('core.extension', $extensions));
$this
->assertEquals($extensions, $filter
->filterWrite('core.extension', $extensions_extra));
$storage = $this
->prophesize(StorageInterface::class);
$storage
->write($name2, [])
->shouldNotBeCalled();
$storage
->write($name3, [])
->shouldNotBeCalled();
$filter = $this
->getFilter($storage
->reveal(), [
$name2,
], [], [], [
$name3,
], 'test', TRUE);
$this
->assertNull($filter
->filterWrite($name2, []));
$this
->assertNull($filter
->filterWrite($name3, []));
}
public function testFilterExists() {
$storage = $this
->prophesize(StorageInterface::class);
$storage
->exists('Yes')
->willReturn(TRUE);
$storage
->exists('No')
->willReturn(FALSE);
$transparent = $this
->getFilter(NULL);
$filter = $this
->getFilter($storage
->reveal());
$this
->assertTrue($transparent
->filterExists('Yes', TRUE));
$this
->assertTrue($transparent
->filterExists('No', TRUE));
$this
->assertFalse($transparent
->filterExists('Yes', FALSE));
$this
->assertFalse($transparent
->filterExists('No', FALSE));
$this
->assertTrue($filter
->filterExists('Yes', TRUE));
$this
->assertTrue($filter
->filterExists('No', TRUE));
$this
->assertTrue($filter
->filterExists('Yes', FALSE));
$this
->assertFalse($filter
->filterExists('No', FALSE));
}
public function testFilterDelete() {
$storage = $this
->prophesize(StorageInterface::class);
$storage
->exists('Yes')
->willReturn(TRUE);
$storage
->delete('Yes')
->willReturn(TRUE);
$transparent = $this
->getFilter(NULL);
$filter = $this
->getFilter($storage
->reveal());
$this
->assertTrue($transparent
->filterDelete('Yes', TRUE));
$this
->assertFalse($transparent
->filterDelete('No', FALSE));
$this
->assertTrue($filter
->filterDelete('Yes', TRUE));
$this
->assertFalse($filter
->filterDelete('No', FALSE));
}
public function testFilterReadMultiple() {
$primary = (array) $this
->getRandomGenerator()
->object(rand(3, 10));
$secondary = (array) $this
->getRandomGenerator()
->object(rand(3, 10));
$merged = array_merge($primary, $secondary);
$storage = $this
->prophesize(StorageInterface::class);
$storage
->readMultiple(Argument::cetera())
->willReturn($secondary);
$transparent = $this
->getFilter(NULL);
$filter = $this
->getFilter($storage
->reveal());
$this
->assertArrayEquals($primary, $transparent
->filterReadMultiple(array_keys($merged), $primary));
$this
->assertArrayEquals($merged, $filter
->filterReadMultiple(array_keys($merged), $primary));
}
public function testFilterListAll() {
$primary = (array) $this
->getRandomGenerator()
->object(rand(3, 10));
$secondary = (array) $this
->getRandomGenerator()
->object(rand(3, 10));
$merged = array_merge($primary, $secondary);
$storage = $this
->getConfigStorageStub($secondary);
$transparent = $this
->getFilter(NULL);
$filter = $this
->getFilter($storage);
$this
->assertArrayEquals(array_keys($primary), $transparent
->filterListAll('', array_keys($primary)));
$this
->assertArrayEquals(array_keys($merged), $filter
->filterListAll('', array_keys($primary)));
}
public function testFilterDeleteAll() {
$storage = $this
->prophesize(StorageInterface::class);
$storage
->deleteAll('Yes')
->willReturn(TRUE);
$transparent = $this
->getFilter(NULL);
$filter = $this
->getFilter($storage
->reveal());
$this
->assertTrue($transparent
->filterDeleteAll('Yes', TRUE));
$this
->assertFalse($transparent
->filterDeleteAll('No', FALSE));
$this
->assertTrue($filter
->filterDeleteAll('Yes', TRUE));
$this
->assertFalse($filter
->filterDeleteAll('No', FALSE));
$failing = $this
->prophesize(StorageInterface::class);
$failing
->deleteAll('Yes')
->willThrow('\\UnexpectedValueException');
$filter = $this
->getFilter($failing
->reveal());
$this
->assertTrue($filter
->filterDeleteAll('Yes', TRUE));
}
public function testFilterCreateCollection() {
$collection = $this
->randomMachineName();
$collection_storage = new NullStorage();
$storage = $this
->prophesize(StorageInterface::class);
$storage
->createCollection($collection)
->willReturn($collection_storage);
$transparent = $this
->getFilter(NULL);
$this
->assertEquals($transparent, $transparent
->filterCreateCollection($collection));
$filter = $this
->getFilter($storage
->reveal());
$new_filter = $filter
->filterCreateCollection($collection);
$internal = new \ReflectionProperty(SplitFilter::class, 'secondaryStorage');
$internal
->setAccessible(TRUE);
$actual = $internal
->getValue($new_filter);
$this
->assertEquals($collection_storage, $actual);
}
public function testFilterGetAllCollectionNames() {
$collections = array_keys((array) $this
->getRandomGenerator()
->object(rand(3, 10)));
$extra = array_keys((array) $this
->getRandomGenerator()
->object(rand(3, 10)));
$storage = $this
->prophesize(StorageInterface::class);
$storage
->getAllCollectionNames()
->willReturn($extra);
$transparent = $this
->getFilter(NULL);
$filter = $this
->getFilter($storage
->reveal());
$this
->assertArrayEquals($collections, $transparent
->filterGetAllCollectionNames($collections));
$this
->assertArrayEquals(array_merge($collections, $extra), $filter
->filterGetAllCollectionNames($collections));
}
public function testSplitFilterCreate() {
$name = 'config_split.' . $this
->getRandomGenerator()
->name();
$folder = vfsStream::setup($name);
$container = $this
->prophesize('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$container
->get('config.manager')
->willReturn($this
->getConfigManagerMock());
$container
->get('config.factory')
->willReturn($this
->getConfigFactoryStub([
$name => [
'folder' => $folder
->url(),
'module' => [],
'theme' => [],
'blacklist' => [],
'graylist' => [],
],
]));
$database = $this
->prophesize('Drupal\\Core\\Database\\Connection');
$container
->get('database')
->willReturn($database
->reveal());
$configuration = [
'config_name' => $name,
];
$filter = SplitFilter::create($container
->reveal(), $configuration, $this
->getRandomGenerator()
->name(), []);
$this
->assertTrue($folder
->hasChild('.htaccess'), 'htaccess written to split folder.');
$folder
->addChild(new vfsStreamFile($name . '.' . FileStorage::getFileExtension()));
$this
->assertTrue($filter
->filterExists($name, FALSE), 'Assert filename');
$name = 'config_split.' . $this
->getRandomGenerator()
->name();
$container = $this
->prophesize('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$container
->get('config.manager')
->willReturn($this
->getConfigManagerMock());
$container
->get('config.factory')
->willReturn($this
->getConfigFactoryStub([
$name => [
'folder' => '',
'module' => [],
'theme' => [],
'blacklist' => [],
'graylist' => [],
],
]));
$database = $this
->prophesize('Drupal\\Core\\Database\\Connection')
->reveal();
$container
->get('database')
->willReturn($database);
$configuration = [
'config_name' => $name,
];
$filter = SplitFilter::create($container
->reveal(), $configuration, $this
->getRandomGenerator()
->name(), []);
$storage = new \ReflectionProperty(SplitFilter::class, 'secondaryStorage');
$storage
->setAccessible(TRUE);
$secondary = $storage
->getValue($filter);
$this
->assertInstanceOf(DatabaseStorage::class, $secondary);
}
protected function getFilter(StorageInterface $storage = NULL, array $blacklist = [], array $modules = [], array $themes = [], array $graylist = [], $name = 'config_split.config_split.test', $skip_equal = FALSE) {
$configuration = [];
$configuration['blacklist'] = $blacklist;
$configuration['graylist'] = $graylist;
$configuration['graylist_dependents'] = TRUE;
$configuration['graylist_skip_equal'] = $skip_equal;
$configuration['module'] = $modules;
$configuration['theme'] = $themes;
$configuration['config_name'] = $name;
return new SplitFilter($configuration, 'config_split', [], $this
->getConfigManagerMock($blacklist, $graylist, $modules, $themes), $storage);
}
protected function getConfigManagerMock(array $blacklist = [], array $graylist = [], array $modules = [], array $themes = []) {
$manager = $this
->prophesize('Drupal\\Core\\Config\\ConfigManagerInterface');
$manager
->findConfigEntityDependents(Argument::cetera())
->willReturn([]);
$all_config = array_fill_keys(array_merge($blacklist, $graylist, array_keys($modules), array_keys($themes)), []);
$manager
->getConfigFactory()
->willReturn($this
->getConfigStorageStub($all_config));
return $manager
->reveal();
}
}