3 namespace Drupal\Tests\Core\Config\Entity\Query;
5 use Drupal\Core\Config\Config;
6 use Drupal\Core\Config\Entity\Query\QueryFactory;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\Core\Config\Entity\Query\QueryFactory
13 class QueryFactoryTest extends UnitTestCase {
19 * @dataProvider providerTestGetKeys
21 public function testGetKeys(array $expected, $key, Config $config) {
22 $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
23 $key_value_factory = $this->getMock('Drupal\Core\KeyValueStore\KeyValueFactoryInterface');
24 $config_manager = $this->getMock('Drupal\Core\Config\ConfigManagerInterface');
25 $config_entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
26 $query_factory = new QueryFactory($config_factory, $key_value_factory, $config_manager);
27 $method = new \ReflectionMethod($query_factory, 'getKeys');
28 $method->setAccessible(TRUE);
30 $actual = $method->invoke($query_factory, $config, $key, 'get', $config_entity_type);
31 $this->assertEquals($expected, $actual);
34 public function providerTestGetKeys() {
40 $this->getConfigObject('test')->set('uuid', 'abc'),
43 // Tests a lookup being set to a top level key when sub-keys exist.
47 $this->getConfigObject('test')->set('uuid.blah', 'abc'),
50 // Tests a non existent key.
54 $this->getConfigObject('test'),
57 // Tests a non existent sub key.
61 $this->getConfigObject('test')->set('uuid', 'abc'),
64 // Tests a existent sub key.
68 $this->getConfigObject('test')->set('uuid.blah', 'abc'),
73 ['test.*.value:a', 'test.*.value:b'],
75 $this->getConfigObject('test')->set('test.a.value', 'a')->set('test.b.value', 'b'),
80 ['test.*.sub2.*.sub4.*.value:aaa', 'test.*.sub2.*.sub4.*.value:aab', 'test.*.sub2.*.sub4.*.value:bab'],
81 'test.*.sub2.*.sub4.*.value',
82 $this->getConfigObject('test')
83 ->set('test.a.sub2.a.sub4.a.value', 'aaa')
84 ->set('test.a.sub2.a.sub4.b.value', 'aab')
85 ->set('test.b.sub2.a.sub4.b.value', 'bab'),
88 // Three wildcards in a row.
90 ['test.*.*.*.value:abc', 'test.*.*.*.value:abd'],
92 $this->getConfigObject('test')->set('test.a.b.c.value', 'abc')->set('test.a.b.d.value', 'abd'),
100 * @covers ::getValues
102 public function testGetKeysWildCardEnd() {
103 $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
104 $key_value_factory = $this->getMock('Drupal\Core\KeyValueStore\KeyValueFactoryInterface');
105 $config_manager = $this->getMock('Drupal\Core\Config\ConfigManagerInterface');
106 $config_entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
107 $config_entity_type->expects($this->atLeastOnce())
109 ->willReturn('test_config_entity_type');
110 $query_factory = new QueryFactory($config_factory, $key_value_factory, $config_manager);
112 $method = new \ReflectionMethod($query_factory, 'getKeys');
113 $method->setAccessible(TRUE);
114 $this->setExpectedException(\LogicException::class, 'test_config_entity_type lookup key test.* ends with a wildcard this can not be used as a lookup');
115 $method->invoke($query_factory, $this->getConfigObject('test'), 'test.*', 'get', $config_entity_type);
119 * Gets a test configuration object.
121 * @param string $name
124 * @return \Drupal\Core\Config\Config|\PHPUnit_Framework_MockObject_MockObject
125 * The test configuration object.
127 protected function getConfigObject($name) {
128 $config = $this->getMockBuilder('Drupal\Core\Config\Config')
129 ->disableOriginalConstructor()
130 ->setMethods(['save', 'delete'])
132 return $config->setName($name);