1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the ConfigurationProcessorTest class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Configuration\SiteAccessAware; |
10
|
|
|
|
11
|
|
|
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ConfigurationProcessor; |
12
|
|
|
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ConfigurationMapperInterface; |
13
|
|
|
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface; |
14
|
|
|
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\HookableConfigurationMapperInterface; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
use stdClass; |
18
|
|
|
|
19
|
|
|
class ConfigurationProcessorTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testConstruct() |
22
|
|
|
{ |
23
|
|
|
$namespace = 'ez_test'; |
24
|
|
|
$siteAccessNodeName = 'foo'; |
25
|
|
|
$container = $this->getContainerMock(); |
26
|
|
|
$siteAccessList = array('test', 'bar'); |
27
|
|
|
$groupsBySa = array('test' => array('group1', 'group2'), 'bar' => array('group1', 'group3')); |
28
|
|
|
ConfigurationProcessor::setAvailableSiteAccesses($siteAccessList); |
29
|
|
|
ConfigurationProcessor::setGroupsBySiteAccess($groupsBySa); |
30
|
|
|
$processor = new ConfigurationProcessor($container, $namespace, $siteAccessNodeName); |
31
|
|
|
|
32
|
|
|
$contextualizer = $processor->getContextualizer(); |
33
|
|
|
$this->assertInstanceOf(ContextualizerInterface::class, $contextualizer); |
34
|
|
|
$this->assertSame($container, $contextualizer->getContainer()); |
35
|
|
|
$this->assertSame($namespace, $contextualizer->getNamespace()); |
36
|
|
|
$this->assertSame($siteAccessNodeName, $contextualizer->getSiteAccessNodeName()); |
37
|
|
|
$this->assertSame($siteAccessList, $contextualizer->getAvailableSiteAccesses()); |
38
|
|
|
$this->assertSame($groupsBySa, $contextualizer->getGroupsBySiteAccess()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testGetSetContextualizer() |
42
|
|
|
{ |
43
|
|
|
$namespace = 'ez_test'; |
44
|
|
|
$siteAccessNodeName = 'foo'; |
45
|
|
|
$container = $this->getContainerMock(); |
46
|
|
|
$processor = new ConfigurationProcessor($container, $namespace, $siteAccessNodeName); |
47
|
|
|
|
48
|
|
|
$this->assertInstanceOf( |
49
|
|
|
ContextualizerInterface::class, |
50
|
|
|
$processor->getContextualizer() |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$newContextualizer = $this->getContextualizerMock(); |
54
|
|
|
$processor->setContextualizer($newContextualizer); |
55
|
|
|
$this->assertSame($newContextualizer, $processor->getContextualizer()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @expectedException \InvalidArgumentException |
60
|
|
|
*/ |
61
|
|
|
public function testMapConfigWrongMapper() |
62
|
|
|
{ |
63
|
|
|
$namespace = 'ez_test'; |
64
|
|
|
$siteAccessNodeName = 'foo'; |
65
|
|
|
$container = $this->getContainerMock(); |
66
|
|
|
$processor = new ConfigurationProcessor($container, $namespace, $siteAccessNodeName); |
67
|
|
|
|
68
|
|
|
$processor->mapConfig(array(), new stdClass()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testMapConfigClosure() |
72
|
|
|
{ |
73
|
|
|
$namespace = 'ez_test'; |
74
|
|
|
$saNodeName = 'foo'; |
75
|
|
|
$container = $this->getContainerMock(); |
76
|
|
|
$processor = new ConfigurationProcessor($container, $namespace, $saNodeName); |
77
|
|
|
$expectedContextualizer = $processor->getContextualizer(); |
78
|
|
|
|
79
|
|
|
$sa1Name = 'sa1'; |
80
|
|
|
$sa2Name = 'sa2'; |
81
|
|
|
$availableSAs = array($sa1Name => true, $sa2Name => true); |
82
|
|
|
$sa1Config = array( |
83
|
|
|
'foo' => 'bar', |
84
|
|
|
'hello' => 'world', |
85
|
|
|
'an_integer' => 123, |
86
|
|
|
'a_bool' => true, |
87
|
|
|
); |
88
|
|
|
$sa2Config = array( |
89
|
|
|
'foo' => 'bar2', |
90
|
|
|
'hello' => 'universe', |
91
|
|
|
'an_integer' => 456, |
92
|
|
|
'a_bool' => false, |
93
|
|
|
); |
94
|
|
|
$config = array( |
95
|
|
|
'not_sa_aware' => 'blabla', |
96
|
|
|
$saNodeName => array( |
97
|
|
|
'sa1' => $sa1Config, |
98
|
|
|
'sa2' => $sa2Config, |
99
|
|
|
), |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$mapperClosure = function (array &$scopeSettings, $currentScope, ContextualizerInterface $contextualizer) use ($config, $availableSAs, $saNodeName, $expectedContextualizer) { |
103
|
|
|
self::assertTrue(isset($availableSAs[$currentScope])); |
104
|
|
|
self::assertTrue(isset($config[$saNodeName][$currentScope])); |
105
|
|
|
self::assertSame($config[$saNodeName][$currentScope], $scopeSettings); |
106
|
|
|
self::assertSame($expectedContextualizer, $contextualizer); |
107
|
|
|
}; |
108
|
|
|
$processor->mapConfig($config, $mapperClosure); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testMapConfigMapperObject() |
112
|
|
|
{ |
113
|
|
|
$namespace = 'ez_test'; |
114
|
|
|
$saNodeName = 'foo'; |
115
|
|
|
$container = $this->getContainerMock(); |
116
|
|
|
$processor = new ConfigurationProcessor($container, $namespace, $saNodeName); |
117
|
|
|
$contextualizer = $processor->getContextualizer(); |
118
|
|
|
|
119
|
|
|
$sa1Name = 'sa1'; |
120
|
|
|
$sa2Name = 'sa2'; |
121
|
|
|
$sa1Config = array( |
122
|
|
|
'foo' => 'bar', |
123
|
|
|
'hello' => 'world', |
124
|
|
|
'an_integer' => 123, |
125
|
|
|
'a_bool' => true, |
126
|
|
|
); |
127
|
|
|
$sa2Config = array( |
128
|
|
|
'foo' => 'bar2', |
129
|
|
|
'hello' => 'universe', |
130
|
|
|
'an_integer' => 456, |
131
|
|
|
'a_bool' => false, |
132
|
|
|
); |
133
|
|
|
$config = array( |
134
|
|
|
'not_sa_aware' => 'blabla', |
135
|
|
|
$saNodeName => array( |
136
|
|
|
'sa1' => $sa1Config, |
137
|
|
|
'sa2' => $sa2Config, |
138
|
|
|
), |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$mapper = $this->createMock(ConfigurationMapperInterface::class); |
142
|
|
|
$mapper |
143
|
|
|
->expects($this->exactly(count($config[$saNodeName]))) |
144
|
|
|
->method('mapConfig') |
145
|
|
|
->will( |
146
|
|
|
$this->returnValueMap( |
147
|
|
|
array( |
148
|
|
|
array($sa1Config, $sa1Name, $contextualizer, null), |
149
|
|
|
array($sa2Config, $sa2Name, $contextualizer, null), |
150
|
|
|
) |
151
|
|
|
) |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$processor->mapConfig($config, $mapper); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testMapConfigHookableMapperObject() |
158
|
|
|
{ |
159
|
|
|
$namespace = 'ez_test'; |
160
|
|
|
$saNodeName = 'foo'; |
161
|
|
|
$container = $this->getContainerMock(); |
162
|
|
|
$processor = new ConfigurationProcessor($container, $namespace, $saNodeName); |
163
|
|
|
$contextualizer = $processor->getContextualizer(); |
164
|
|
|
|
165
|
|
|
$sa1Name = 'sa1'; |
166
|
|
|
$sa2Name = 'sa2'; |
167
|
|
|
$sa1Config = array( |
168
|
|
|
'foo' => 'bar', |
169
|
|
|
'hello' => 'world', |
170
|
|
|
'an_integer' => 123, |
171
|
|
|
'a_bool' => true, |
172
|
|
|
); |
173
|
|
|
$sa2Config = array( |
174
|
|
|
'foo' => 'bar2', |
175
|
|
|
'hello' => 'universe', |
176
|
|
|
'an_integer' => 456, |
177
|
|
|
'a_bool' => false, |
178
|
|
|
); |
179
|
|
|
$config = array( |
180
|
|
|
'not_sa_aware' => 'blabla', |
181
|
|
|
$saNodeName => array( |
182
|
|
|
'sa1' => $sa1Config, |
183
|
|
|
'sa2' => $sa2Config, |
184
|
|
|
), |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$mapper = $this->createMock(HookableConfigurationMapperInterface::class); |
188
|
|
|
$mapper |
189
|
|
|
->expects($this->once()) |
190
|
|
|
->method('preMap') |
191
|
|
|
->with($config, $contextualizer); |
192
|
|
|
$mapper |
193
|
|
|
->expects($this->once()) |
194
|
|
|
->method('postMap') |
195
|
|
|
->with($config, $contextualizer); |
196
|
|
|
$mapper |
197
|
|
|
->expects($this->exactly(count($config[$saNodeName]))) |
198
|
|
|
->method('mapConfig') |
199
|
|
|
->will( |
200
|
|
|
$this->returnValueMap( |
201
|
|
|
array( |
202
|
|
|
array($sa1Config, $sa1Name, $contextualizer, null), |
203
|
|
|
array($sa2Config, $sa2Name, $contextualizer, null), |
204
|
|
|
) |
205
|
|
|
) |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$processor->mapConfig($config, $mapper); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function testMapSetting() |
212
|
|
|
{ |
213
|
|
|
$namespace = 'ez_test'; |
214
|
|
|
$saNodeName = 'foo'; |
215
|
|
|
$container = $this->getContainerMock(); |
216
|
|
|
$processor = new ConfigurationProcessor($container, $namespace, $saNodeName); |
217
|
|
|
$contextualizer = $this->getContextualizerMock(); |
218
|
|
|
$processor->setContextualizer($contextualizer); |
219
|
|
|
|
220
|
|
|
$sa1Config = array( |
221
|
|
|
'foo' => 'bar', |
222
|
|
|
'hello' => 'world', |
223
|
|
|
'an_integer' => 123, |
224
|
|
|
'a_bool' => true, |
225
|
|
|
); |
226
|
|
|
$sa2Config = array( |
227
|
|
|
'foo' => 'bar2', |
228
|
|
|
'hello' => 'universe', |
229
|
|
|
'an_integer' => 456, |
230
|
|
|
'a_bool' => false, |
231
|
|
|
); |
232
|
|
|
$config = array( |
233
|
|
|
'not_sa_aware' => 'blabla', |
234
|
|
|
$saNodeName => array( |
235
|
|
|
'sa1' => $sa1Config, |
236
|
|
|
'sa2' => $sa2Config, |
237
|
|
|
), |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$contextualizer |
241
|
|
|
->expects($this->once()) |
242
|
|
|
->method('mapSetting') |
243
|
|
|
->with('foo', $config); |
244
|
|
|
$processor->mapSetting('foo', $config); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testMapConfigArray() |
248
|
|
|
{ |
249
|
|
|
$namespace = 'ez_test'; |
250
|
|
|
$saNodeName = 'foo'; |
251
|
|
|
$container = $this->getContainerMock(); |
252
|
|
|
$processor = new ConfigurationProcessor($container, $namespace, $saNodeName); |
253
|
|
|
$contextualizer = $this->getContextualizerMock(); |
254
|
|
|
$processor->setContextualizer($contextualizer); |
255
|
|
|
|
256
|
|
|
$sa1Config = array( |
257
|
|
|
'foo' => 'bar', |
258
|
|
|
'hello' => array('world'), |
259
|
|
|
'an_integer' => 123, |
260
|
|
|
'a_bool' => true, |
261
|
|
|
); |
262
|
|
|
$sa2Config = array( |
263
|
|
|
'foo' => 'bar2', |
264
|
|
|
'hello' => array('universe'), |
265
|
|
|
'an_integer' => 456, |
266
|
|
|
'a_bool' => false, |
267
|
|
|
); |
268
|
|
|
$config = array( |
269
|
|
|
'not_sa_aware' => 'blabla', |
270
|
|
|
$saNodeName => array( |
271
|
|
|
'sa1' => $sa1Config, |
272
|
|
|
'sa2' => $sa2Config, |
273
|
|
|
), |
274
|
|
|
); |
275
|
|
|
|
276
|
|
|
$contextualizer |
277
|
|
|
->expects($this->once()) |
278
|
|
|
->method('mapConfigArray') |
279
|
|
|
->with('hello', $config, ContextualizerInterface::MERGE_FROM_SECOND_LEVEL); |
280
|
|
|
$processor->mapConfigArray('hello', $config, ContextualizerInterface::MERGE_FROM_SECOND_LEVEL); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
protected function getContainerMock() |
284
|
|
|
{ |
285
|
|
|
return $this->createMock(ContainerInterface::class); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
protected function getContextualizerMock() |
289
|
|
|
{ |
290
|
|
|
return $this->createMock(ContextualizerInterface::class); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|