Newer
Older
Alex Pott
committed
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Config\ConfigTest.
*/
namespace Drupal\Tests\Core\Config;
use Drupal\Core\DependencyInjection\ContainerBuilder;
Alex Bronstein
committed
use Drupal\Core\Render\Markup;
Alex Pott
committed
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Config\Config;
use Drupal\Component\Utility\SafeMarkup;
Alex Pott
committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Tests the Config.
*
* @coversDefaultClass \Drupal\Core\Config\Config
*
* @group Config
*
* @see \Drupal\Core\Config\Config
*/
class ConfigTest extends UnitTestCase {
/**
* Config.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* Storage.
*
* @var \Drupal\Core\Config\StorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storage;
/**
* Event Dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $eventDispatcher;
/**
* Typed Config.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $typedConfig;
/**
* The mocked cache tags invalidator.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $cacheTagsInvalidator;
Alex Pott
committed
public function setUp() {
$this->storage = $this->getMock('Drupal\Core\Config\StorageInterface');
$this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->typedConfig = $this->getMock('\Drupal\Core\Config\TypedConfigManagerInterface');
$this->config = new Config('config.test', $this->storage, $this->eventDispatcher, $this->typedConfig);
$this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
$container = new ContainerBuilder();
$container->set('cache_tags.invalidator', $this->cacheTagsInvalidator);
\Drupal::setContainer($container);
Alex Pott
committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
}
/**
* @covers ::setName
* @dataProvider setNameProvider
*/
public function testSetName($name) {
// Set the name.
$this->config->setName($name);
// Check that the name has been set correctly.
$this->assertEquals($name, $this->config->getName());
// Check that the name validates.
// Should throw \Drupal\Core\Config\ConfigNameException if invalid.
$this->config->validateName($name);
}
/**
* Provides config names to test.
*
* @see \Drupal\Tests\Core\Config\ConfigTest::testSetName()
*/
public function setNameProvider() {
return array(
// Valid name with dot.
array(
'test.name',
),
// Maximum length.
array(
'test.' . str_repeat('a', Config::MAX_NAME_LENGTH - 5),
),
);
}
/**
* @covers ::isNew
*/
public function testIsNew() {
// Config should be new by default.
$this->assertTrue($this->config->isNew());
// Config is no longer new once saved.
$this->config->save();
$this->assertFalse($this->config->isNew());
}
/**
* @covers ::setData
* @dataProvider nestedDataProvider
*/
public function testSetData($data) {
$this->config->setData($data);
$this->assertEquals($data, $this->config->getRawData());
$this->assertConfigDataEquals($data);
}
/**
* @covers ::save
* @dataProvider nestedDataProvider
*/
public function testSaveNew($data) {
$this->cacheTagsInvalidator->expects($this->never())
->method('invalidateTags');
Alex Pott
committed
// Set initial data.
$this->config->setData($data);
// Check that original data has not been set yet.
foreach ($data as $key => $value) {
$this->assertNull($this->config->getOriginal($key, FALSE));
}
// Save so that the original data is set.
$config = $this->config->save();
// Check that returned $config is instance of Config.
$this->assertInstanceOf('\Drupal\Core\Config\Config', $config);
// Check that the original data it saved.
$this->assertOriginalConfigDataEquals($data, TRUE);
}
/**
* @covers ::save
* @dataProvider nestedDataProvider
*/
public function testSaveExisting($data) {
$this->cacheTagsInvalidator->expects($this->once())
->method('invalidateTags')
->with(['config:config.test']);
// Set initial data.
$this->config->setData($data);
$this->config->save();
// Update.
$new_data = $data;
$new_data['a']['d'] = 2;
$this->config->setData($new_data);
$this->config->save();
$this->assertOriginalConfigDataEquals($new_data, TRUE);
}
Alex Pott
committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* @covers ::setModuleOverride
* @covers ::setSettingsOverride
* @covers ::getOriginal
* @dataProvider overrideDataProvider
*/
public function testOverrideData($data, $module_data, $setting_data) {
// Set initial data.
$this->config->setData($data);
// Check original data was set correctly.
$this->assertConfigDataEquals($data);
// Save so that the original data is stored.
$this->config->save();
// Set module override data and check value before and after save.
$this->config->setModuleOverride($module_data);
$this->assertConfigDataEquals($module_data);
$this->config->save();
$this->assertConfigDataEquals($module_data);
// Set setting override data and check value before and after save.
$this->config->setSettingsOverride($setting_data);
$this->assertConfigDataEquals($setting_data);
$this->config->save();
$this->assertConfigDataEquals($setting_data);
// Set module overrides again to ensure override order is correct.
$this->config->setModuleOverride($module_data);
// Setting data should be overriding module data.
$this->assertConfigDataEquals($setting_data);
$this->config->save();
$this->assertConfigDataEquals($setting_data);
// Check original data has not changed.
$this->assertOriginalConfigDataEquals($data, FALSE);
// Check setting overrides are returned with $apply_overrides = TRUE.
$this->assertOriginalConfigDataEquals($setting_data, TRUE);
// Check that $apply_overrides defaults to TRUE.
foreach ($setting_data as $key => $value) {
$config_value = $this->config->getOriginal($key);
$this->assertEquals($value, $config_value);
}
}
/**
* @covers ::set
* @dataProvider nestedDataProvider
*/
public function testSetValue($data) {
foreach ($data as $key => $value) {
$this->config->set($key, $value);
}
$this->assertConfigDataEquals($data);
}
/**
* @covers ::set
* @expectedException \Drupal\Core\Config\ConfigValueException
*/
public function testSetValidation() {
$this->config->set('testData', array('dot.key' => 1));
}
/**
* @covers ::set
* @expectedException PHPUnit_Framework_Error_Warning
*/
public function testSetIllegalOffsetValue() {
// Set a single value.
$this->config->set('testData', 1);
// Attempt to treat the single value as a nested item.
$this->config->set('testData.illegalOffset', 1);
}
/**
* @covers ::initWithData
* @dataProvider nestedDataProvider
*/
public function testInitWithData($data) {
$config = $this->config->initWithData($data);
// Should return the Config object.
$this->assertInstanceOf('\Drupal\Core\Config\Config', $config);
// Check config is not new.
$this->assertEquals(FALSE, $this->config->isNew());
// Check that data value was set correctly.
$this->assertConfigDataEquals($data);
// Check that original data was set.
$this->assertOriginalConfigDataEquals($data, TRUE);
// Check without applying overrides.
$this->assertOriginalConfigDataEquals($data, FALSE);
}
/**
* @covers ::clear
* @dataProvider simpleDataProvider
*/
public function testClear($data) {
foreach ($data as $key => $value) {
// Check that values are cleared.
$this->config->set($key, $value);
$this->assertEquals($value, $this->config->get($key));
$this->config->clear($key);
$this->assertNull($this->config->get($key));
}
}
/**
* @covers ::clear
* @dataProvider nestedDataProvider
*/
public function testNestedClear($data) {
foreach ($data as $key => $value) {
// Check that values are cleared.
$this->config->set($key, $value);
// Check each nested value.
foreach ($value as $nested_key => $nested_value) {
$full_nested_key = $key . '.' . $nested_key;
$this->assertEquals($nested_value, $this->config->get($full_nested_key));
$this->config->clear($full_nested_key);
$this->assertNull($this->config->get($full_nested_key));
}
}
}
/**
* @covers ::delete
* @dataProvider overrideDataProvider
*/
public function testDelete($data, $module_data) {
$this->cacheTagsInvalidator->expects($this->once())
->method('invalidateTags')
->with(['config:config.test']);
Alex Pott
committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Set initial data.
foreach ($data as $key => $value) {
$this->config->set($key, $value);
}
// Set overrides.
$this->config->setModuleOverride($module_data);
// Save.
$this->config->save();
// Check that original data is still correct.
$this->assertOriginalConfigDataEquals($data, FALSE);
// Check overrides have been set.
$this->assertConfigDataEquals($module_data);
$this->assertOriginalConfigDataEquals($module_data, TRUE);
// Check that config is new.
$this->assertFalse($this->config->isNew());
// Delete.
$this->config->delete();
// Check object properties have been reset.
$this->assertTrue($this->config->isNew());
foreach ($data as $key => $value) {
$this->assertEmpty($this->config->getOriginal($key, FALSE));
}
// Check that overrides have persisted.
foreach ($module_data as $key => $value) {
$this->assertConfigDataEquals($module_data);
$this->assertOriginalConfigDataEquals($module_data, TRUE);
}
}
/**
* @covers ::merge
* @dataProvider mergeDataProvider
*/
public function testMerge($data, $data_to_merge, $merged_data) {
// Set initial data.
$this->config->setData($data);
// Data to merge.
$this->config->merge($data_to_merge);
// Check that data has merged correctly.
$this->assertEquals($merged_data, $this->config->getRawData());
}
/**
* Provides data to test merges.
*
* @see \Drupal\Tests\Core\Config\ConfigTest::testMerge()
*/
public function mergeDataProvider() {
return array(
array(
// Data.
array('a' => 1, 'b' => 2, 'c' => array('d' => 3)),
// Data to merge.
array('a' => 2, 'e' => 4, 'c' => array('f' => 5)),
// Data merged.
array('a' => 2, 'b' => 2, 'c' => array('d' => 3, 'f' => 5), 'e' => 4),
),
);
}
/**
* @covers ::validateName
* @expectedException \Drupal\Core\Config\ConfigNameException
* @dataProvider validateNameProvider
*/
public function testValidateNameException($name, $exception_message) {
$this->setExpectedException('\Drupal\Core\Config\ConfigNameException', $exception_message);
$this->config->validateName($name);
}
/**
* @covers ::getCacheTags
*/
public function testGetCacheTags() {
$this->assertSame(['config:' . $this->config->getName()], $this->config->getCacheTags());
}
Alex Pott
committed
/**
* Provides data to test name validation.
*
* @see \Drupal\Tests\Core\Config\ConfigTest::testValidateNameException()
*/
public function validateNameProvider() {
$return = array(
// Name missing namespace (dot).
array(
'MissingNamespace',
catch
committed
'Missing namespace in Config object name MissingNamespace.',
Alex Pott
committed
),
// Exceeds length (max length plus an extra dot).
array(
str_repeat('a', Config::MAX_NAME_LENGTH) . ".",
catch
committed
'Config object name ' . str_repeat('a', Config::MAX_NAME_LENGTH) . '. exceeds maximum allowed length of ' . Config::MAX_NAME_LENGTH . ' characters.',
Alex Pott
committed
),
);
// Name must not contain : ? * < > " ' / \
foreach (array(':', '?', '*', '<', '>', '"', "'", '/', '\\') as $char) {
$name = 'name.' . $char;
$return[] = array(
$name,
catch
committed
"Invalid character in Config object name $name.",
Alex Pott
committed
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
);
}
return $return;
}
/**
* Provides override data.
*
* @see \Drupal\Tests\Core\Config\ConfigTest::testOverrideData()
* @see \Drupal\Tests\Core\Config\ConfigTest::testDelete()
*/
public function overrideDataProvider() {
return array(
array(
// Original data.
array(
'a' => 'originalValue',
),
// Module overrides.
array(
'a' => 'moduleValue',
),
// Setting overrides.
array(
'a' => 'settingValue',
),
),
);
}
/**
* Provides simple test data.
*
* @see \Drupal\Tests\Core\Config\ConfigTest::testClear()
*/
public function simpleDataProvider() {
return array(
array(
array(
'a' => '1',
'b' => '2',
'c' => '3',
),
),
);
}
/**
* Provides nested test data.
*
* @see \Drupal\Tests\Core\Config\ConfigTest::testSetData()
* @see \Drupal\Tests\Core\Config\ConfigTest::testSave()
* @see \Drupal\Tests\Core\Config\ConfigTest::testSetValue()
* @see \Drupal\Tests\Core\Config\ConfigTest::testInitWithData()
* @see \Drupal\Tests\Core\Config\ConfigTest::testNestedClear()
*/
public function nestedDataProvider() {
return array(
array(
array(
'a' => array(
'd' => 1,
),
'b' => array(
'e' => 2,
),
'c' => array(
'f' => 3,
),
),
),
);
}
/**
* Asserts all config data equals $data provided.
*
* @param array $data
* Config data to be checked.
*/
public function assertConfigDataEquals($data) {
foreach ($data as $key => $value) {
$this->assertEquals($value, $this->config->get($key));
}
}
/**
* Asserts all original config data equals $data provided.
*
* @param array $data
* Config data to be checked.
* @param bool $apply_overrides
* Apply any overrides to the original data.
*/
public function assertOriginalConfigDataEquals($data, $apply_overrides) {
foreach ($data as $key => $value) {
$config_value = $this->config->getOriginal($key, $apply_overrides);
$this->assertEquals($value, $config_value);
}
}
/**
* @covers ::setData
* @covers ::set
* @covers ::initWithData
*/
public function testSafeStringHandling() {
// Safe strings are cast when using ::set().
Alex Bronstein
committed
$safe_string = Markup::create('bar');
$this->config->set('foo', $safe_string);
$this->assertSame('bar', $this->config->get('foo'));
$this->config->set('foo', ['bar' => $safe_string]);
$this->assertSame('bar', $this->config->get('foo.bar'));
// Safe strings are cast when using ::setData().
$this->config->setData(['bar' => $safe_string]);
$this->assertSame('bar', $this->config->get('bar'));
// Safe strings are not cast when using ::initWithData().
$this->config->initWithData(['bar' => $safe_string]);
$this->assertSame($safe_string, $this->config->get('bar'));
}