5558b26e8367dac1ba359e845c84893caa668f40
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Config / Entity / Query / QueryFactoryTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Config\Entity\Query;
4
5 use Drupal\Core\Config\Config;
6 use Drupal\Core\Config\Entity\Query\QueryFactory;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Config\Entity\Query\QueryFactory
11  * @group Config
12  */
13 class QueryFactoryTest extends UnitTestCase {
14
15   /**
16    * @covers ::getKeys
17    * @covers ::getValues
18    *
19    * @dataProvider providerTestGetKeys
20    */
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);
29
30     $actual = $method->invoke($query_factory, $config, $key, 'get', $config_entity_type);
31     $this->assertEquals($expected, $actual);
32   }
33
34   public function providerTestGetKeys() {
35     $tests = [];
36
37     $tests[] = [
38       ['uuid:abc'],
39       'uuid',
40       $this->getConfigObject('test')->set('uuid', 'abc'),
41     ];
42
43     // Tests a lookup being set to a top level key when sub-keys exist.
44     $tests[] = [
45       [],
46       'uuid',
47       $this->getConfigObject('test')->set('uuid.blah', 'abc'),
48     ];
49
50     // Tests a non existent key.
51     $tests[] = [
52       [],
53       'uuid',
54       $this->getConfigObject('test'),
55     ];
56
57     // Tests a non existent sub key.
58     $tests[] = [
59       [],
60       'uuid.blah',
61       $this->getConfigObject('test')->set('uuid', 'abc'),
62     ];
63
64     // Tests a existent sub key.
65     $tests[] = [
66       ['uuid.blah:abc'],
67       'uuid.blah',
68       $this->getConfigObject('test')->set('uuid.blah', 'abc'),
69     ];
70
71     // One wildcard.
72     $tests[] = [
73       ['test.*.value:a', 'test.*.value:b'],
74       'test.*.value',
75       $this->getConfigObject('test')->set('test.a.value', 'a')->set('test.b.value', 'b'),
76     ];
77
78     // Three wildcards.
79     $tests[] = [
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'),
86     ];
87
88     // Three wildcards in a row.
89     $tests[] = [
90       ['test.*.*.*.value:abc', 'test.*.*.*.value:abd'],
91       'test.*.*.*.value',
92       $this->getConfigObject('test')->set('test.a.b.c.value', 'abc')->set('test.a.b.d.value', 'abd'),
93     ];
94
95     return $tests;
96   }
97
98   /**
99    * @covers ::getKeys
100    * @covers ::getValues
101    */
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())
108       ->method('id')
109       ->willReturn('test_config_entity_type');
110     $query_factory = new QueryFactory($config_factory, $key_value_factory, $config_manager);
111
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);
116   }
117
118   /**
119    * Gets a test configuration object.
120    *
121    * @param string $name
122    *   The config name.
123    *
124    * @return \Drupal\Core\Config\Config|\PHPUnit_Framework_MockObject_MockObject
125    *   The test configuration object.
126    */
127   protected function getConfigObject($name) {
128     $config = $this->getMockBuilder('Drupal\Core\Config\Config')
129       ->disableOriginalConstructor()
130       ->setMethods(['save', 'delete'])
131       ->getMock();
132     return $config->setName($name);
133   }
134
135 }