ConfigNamesMapperTest.php 20.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php

/**
 * @file
 * Contains \Drupal\config_translation\Tests\ConfigNamesMapperTest.
 */

namespace Drupal\config_translation\Tests;

use Drupal\config_translation\ConfigNamesMapper;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;

/**
 * Tests ConfigNamesMapper.
 *
 * @group Drupal
 * @group Config_translation
 */
class ConfigNamesMapperTest extends UnitTestCase {

  /**
   * The plugin definition of the test mapper.
   *
   * @var array
   */
  protected $pluginDefinition;

  /**
   * The configuration names mapper to test.
   *
   * @see \Drupal\config_translation\ConfigNamesMapper
   *
   * @var \Drupal\config_translation\Tests\TestConfigNamesMapper
   */
  protected $configNamesMapper;

  /**
   * The locale configuration manager.
   *
   * @var \Drupal\locale\LocaleConfigManager|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $localeConfigManager;

  /**
   * The configuration mapper manager.
   *
   * @var \Drupal\config_translation\ConfigMapperManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configMapperManager;

  /**
   * The base route used for testing.
   *
   * @var \Symfony\Component\Routing\Route
   */
  protected $baseRoute;

  /**
   * The route provider used for testing.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $routeProvider;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Configuration names mapper',
      'description' => 'Tests the functionality provided by the configuration names mapper.',
      'group' => 'Configuration Translation',
    );
  }

  public function setUp() {
    $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');

    $this->pluginDefinition = array(
      'class' => '\Drupal\config_translation\ConfigNamesMapper',
      'base_route_name' => 'system.site_information_settings',
      'title' => 'System information',
      'names' => array('system.site'),
      'weight' => 42,
    );

    $this->localeConfigManager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
      ->disableOriginalConstructor()
      ->getMock();

    $this->configMapperManager = $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface');

    $this->baseRoute = new Route('/admin/config/system/site-information');

    $this->routeProvider
      ->expects($this->once())
      ->method('getRouteByName')
      ->with('system.site_information_settings')
      ->will($this->returnValue($this->baseRoute));

    $this->configNamesMapper = new TestConfigNamesMapper(
      'system.site_information_settings',
      $this->pluginDefinition,
      $this->getConfigFactoryStub(),
      $this->localeConfigManager,
      $this->configMapperManager,
      $this->routeProvider,
      $this->getStringTranslationStub()
    );
  }

  /**
   * Tests ConfigNamesMapper::getTitle().
   */
  public function testGetTitle() {
    $result = $this->configNamesMapper->getTitle();
    $this->assertSame($this->pluginDefinition['title'], $result);
  }

  /**
   * Tests ConfigNamesMapper::getBaseRouteName().
   */
  public function testGetBaseRouteName() {
    $result = $this->configNamesMapper->getBaseRouteName();
    $this->assertSame($this->pluginDefinition['base_route_name'], $result);
  }

  /**
   * Tests ConfigNamesMapper::getBaseRouteParameters().
   */
  public function testGetBaseRouteParameters() {
    $result = $this->configNamesMapper->getBaseRouteParameters();
    $this->assertSame(array(), $result);
  }

  /**
   * Tests ConfigNamesMapper::getBaseRoute().
   */
  public function testGetBaseRoute() {
    $result = $this->configNamesMapper->getBaseRoute();
    $this->assertSame($this->baseRoute, $result);
  }

  /**
   * Tests ConfigNamesMapper::getBasePath().
   */
  public function testGetBasePath() {
    $result = $this->configNamesMapper->getBasePath();
    $this->assertSame('/admin/config/system/site-information', $result);
  }

  /**
   * Tests ConfigNamesMapper::getOverviewRouteName().
   */
  public function testGetOverviewRouteName() {
    $result = $this->configNamesMapper->getOverviewRouteName();
    $expected = 'config_translation.item.overview.' . $this->pluginDefinition['base_route_name'];
    $this->assertSame($expected, $result);
  }

  /**
   * Tests ConfigNamesMapper::getOverviewRouteParameters().
   */
  public function testGetOverviewRouteParameters() {
    $result = $this->configNamesMapper->getOverviewRouteParameters();
    $this->assertSame(array(), $result);
  }

  /**
   * Tests ConfigNamesMapper::getOverviewRoute().
   */
  public function testGetOverviewRoute() {
    $expected = new Route('/admin/config/system/site-information/translate',
      array(
179
        '_content' => '\Drupal\config_translation\Controller\ConfigTranslationController::itemPage',
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
317
318
319
320
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
        'plugin_id' => 'system.site_information_settings',
      ),
      array(
        '_config_translation_overview_access' => 'TRUE',
      )
    );
    $result = $this->configNamesMapper->getOverviewRoute();
    $this->assertSame(serialize($expected), serialize($result));
  }

  /**
   * Tests ConfigNamesMapper::getOverviewPath().
   */
  public function testGetOverviewPath() {
    $result = $this->configNamesMapper->getOverviewPath();
    $this->assertSame('/admin/config/system/site-information/translate', $result);
  }

  /**
   * Tests ConfigNamesMapper::getAddRouteName().
   */
  public function testGetAddRouteName() {
    $result = $this->configNamesMapper->getAddRouteName();
    $expected = 'config_translation.item.add.' . $this->pluginDefinition['base_route_name'];
    $this->assertSame($expected, $result);
  }

  /**
   * Tests ConfigNamesMapper::getAddRouteParameters().
   */
  public function testGetAddRouteParameters() {
    $request = Request::create('');
    $request->attributes->set('langcode', 'xx');
    $this->configNamesMapper->populateFromRequest($request);

    $expected = array('langcode' => 'xx');
    $result = $this->configNamesMapper->getAddRouteParameters();
    $this->assertSame($expected, $result);
  }

  /**
   * Tests ConfigNamesMapper::getAddRoute().
   */
  public function testGetAddRoute() {
    $expected = new Route('/admin/config/system/site-information/translate/{langcode}/add',
      array(
        '_form' => '\Drupal\config_translation\Form\ConfigTranslationAddForm',
        'plugin_id' => 'system.site_information_settings',
      ),
      array(
        '_config_translation_form_access' => 'TRUE',
      )
    );
    $result = $this->configNamesMapper->getAddRoute();
    $this->assertSame(serialize($expected), serialize($result));
  }

  /**
   * Tests ConfigNamesMapper::getEditRouteName().
   */
  public function testGetEditRouteName() {
    $result = $this->configNamesMapper->getEditRouteName();
    $expected = 'config_translation.item.edit.' . $this->pluginDefinition['base_route_name'];
    $this->assertSame($expected, $result);
  }

  /**
   * Tests ConfigNamesMapper::getEditRouteParameters().
   */
  public function testGetEditRouteParameters() {
    $request = Request::create('');
    $request->attributes->set('langcode', 'xx');
    $this->configNamesMapper->populateFromRequest($request);

    $expected = array('langcode' => 'xx');
    $result = $this->configNamesMapper->getEditRouteParameters();
    $this->assertSame($expected, $result);
  }

  /**
   * Tests ConfigNamesMapper::getEditRoute().
   */
  public function testGetEditRoute() {
    $expected = new Route('/admin/config/system/site-information/translate/{langcode}/edit',
      array(
        '_form' => '\Drupal\config_translation\Form\ConfigTranslationEditForm',
        'plugin_id' => 'system.site_information_settings',
      ),
      array(
        '_config_translation_form_access' => 'TRUE',
      )
    );
    $result = $this->configNamesMapper->getEditRoute();
    $this->assertSame(serialize($expected), serialize($result));
  }

  /**
   * Tests ConfigNamesMapper::getDeleteRouteName().
   */
  public function testGetDeleteRouteName() {
    $result = $this->configNamesMapper->getDeleteRouteName();
    $expected = 'config_translation.item.delete.' . $this->pluginDefinition['base_route_name'];
    $this->assertSame($expected, $result);
  }

  /**
   * Tests ConfigNamesMapper::getDeleteRouteParameters().
   */
  public function testGetDeleteRouteParameters() {
    $request = Request::create('');
    $request->attributes->set('langcode', 'xx');
    $this->configNamesMapper->populateFromRequest($request);

    $expected = array('langcode' => 'xx');    $result = $this->configNamesMapper->getDeleteRouteParameters();
    $this->assertSame($expected, $result);
  }

  /**
   * Tests ConfigNamesMapper::getRoute().
   */
  public function testGetDeleteRoute() {
    $expected = new Route('/admin/config/system/site-information/translate/{langcode}/delete',
      array(
        '_form' => '\Drupal\config_translation\Form\ConfigTranslationDeleteForm',
        'plugin_id' => 'system.site_information_settings',
      ),
      array(
        '_config_translation_form_access' => 'TRUE',
      )
    );
    $result = $this->configNamesMapper->getDeleteRoute();
    $this->assertSame(serialize($expected), serialize($result));
  }

  /**
   * Tests ConfigNamesMapper::getConfigNames().
   */
  public function testGetConfigNames() {
    $result = $this->configNamesMapper->getConfigNames();
    $this->assertSame($this->pluginDefinition['names'], $result);
  }

  /**
   * Tests ConfigNamesMapper::addConfigName().
   */
  public function testAddConfigName() {
    $names = $this->configNamesMapper->getConfigNames();
    $this->configNamesMapper->addConfigName('test');
    $names[] = 'test';
    $result = $this->configNamesMapper->getConfigNames();
    $this->assertSame($names, $result);
  }

  /**
   * Tests ConfigNamesMapper::getWeight().
   */
  public function testGetWeight() {
    $result = $this->configNamesMapper->getWeight();
    $this->assertSame($this->pluginDefinition['weight'], $result);
  }

  /**
   * Tests ConfigNamesMapper::populateFromRequest().
   */
  public function testPopulateFromRequest() {
    // Make sure the language code is not set initially.
    $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode());

    // Test that an empty request does not set the language code.
    $request = Request::create('');
    $this->configNamesMapper->populateFromRequest($request);
    $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode());

    // Test that a request with a 'langcode' attribute sets the language code.
    $request->attributes->set('langcode', 'xx');
    $this->configNamesMapper->populateFromRequest($request);
    $this->assertSame('xx', $this->configNamesMapper->getInternalLangcode());

    // Test that the language code gets unset with the wrong request.
    $request->attributes->remove('langcode');
    $this->configNamesMapper->populateFromRequest($request);
    $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode());
  }

  /**
   * Tests ConfigNamesMapper::getTypeLabel().
   */
  public function testGetTypeLabel() {
    $result = $this->configNamesMapper->getTypeLabel();
    $this->assertSame($this->pluginDefinition['title'], $result);
  }

  /**
   * Tests ConfigNamesMapper::getLangcode().
   */
  public function testGetLangcode() {
    // Test that the getLangcode() falls back to 'en', if no explicit language
    // code is provided.
    $config_factory = $this->getConfigFactoryStub(array(
      'system.site' => array('key' => 'value'),
    ));
    $this->configNamesMapper->setConfigFactory($config_factory);
    $result = $this->configNamesMapper->getLangcode();
    $this->assertSame('en', $result);

    // Test that getLangcode picks up the language code provided by the
    // configuration.
    $config_factory = $this->getConfigFactoryStub(array(
      'system.site' => array('langcode' => 'xx'),
    ));
    $this->configNamesMapper->setConfigFactory($config_factory);
    $result = $this->configNamesMapper->getLangcode();
    $this->assertSame('xx', $result);

    // Test that getLangcode() works for multiple configuration names.
    $this->configNamesMapper->addConfigName('system.maintenance');
    $config_factory = $this->getConfigFactoryStub(array(
      'system.site' => array('langcode' => 'xx'),
      'system.maintenance' => array('langcode' => 'xx'),
    ));
    $this->configNamesMapper->setConfigFactory($config_factory);
    $result = $this->configNamesMapper->getLangcode();
    $this->assertSame('xx', $result);

    // Test that getLangcode() throws an exception when different language codes
    // are given.
    $config_factory = $this->getConfigFactoryStub(array(
      'system.site' => array('langcode' => 'xx'),
      'system.maintenance' => array('langcode' => 'yy'),
    ));
    $this->configNamesMapper->setConfigFactory($config_factory);
    try {
      $this->configNamesMapper->getLangcode();
      $this->fail();
    }
    catch (\RuntimeException $e) {}
  }

  // @todo Test ConfigNamesMapper::getLanguageWithFallback() once
  //   https://drupal.org/node/1862202 lands in core, because then we can
  //   remove the direct language_load() call.

  /**
   * Tests ConfigNamesMapper::getConfigData().
   */
  public function testGetConfigData() {
    $configs = array(
      'system.site' => array(
        'name' => 'Drupal',
        'slogan' => 'Come for the software, stay for the community!',
      ),
      'system.maintenance' => array(
        'enabled' => FALSE,
        'message' => '@site is currently under maintenance.',
      ),
      'system.rss' => array(
        'items' => array(
          'limit' => 10,
          'view_mode' => 'rss',
        ),
      ),
    );

    $this->configNamesMapper->setConfigNames(array_keys($configs));
    $config_factory = $this->getConfigFactoryStub($configs);
    $this->configNamesMapper->setConfigFactory($config_factory);

    $result = $this->configNamesMapper->getConfigData();
    $this->assertSame($configs, $result);
  }

  /**
   * Tests ConfigNamesMapper::hasSchema().
   *
   * @param array $mock_return_values
   *   An array of values that the mocked locale configuration manager should
   *   return for hasConfigSchema().
   * @param bool $expected
   *   The expected return value of ConfigNamesMapper::hasSchema().
   *
   * @dataProvider providerTestHasSchema
   */
  public function testHasSchema(array $mock_return_values, $expected) {
    // As the configuration names are arbitrary, simply use integers.
    $config_names = range(1, count($mock_return_values));
    $this->configNamesMapper->setConfigNames($config_names);

    $map = array();
    foreach ($config_names as $i => $config_name) {
      $map[] = array($config_name, $mock_return_values[$i]);
    }
    $this->localeConfigManager
      ->expects($this->any())
      ->method('hasConfigSchema')
      ->will($this->returnValueMap($map));

    $result = $this->configNamesMapper->hasSchema();
    $this->assertSame($expected, $result);
  }

  /**
   * Provides data for for ConfigMapperTest::testHasSchema().
   *
   * @return array
   *   An array of arrays, where each inner array has an array of values that
   *   the mocked locale configuration manager should return for
   *   hasConfigSchema() as the first value and the expected return value of
   *   ConfigNamesMapper::hasSchema() as the second value.
   */
  public function providerTestHasSchema() {
    return array(
      array(array(TRUE), TRUE),
      array(array(FALSE), FALSE),
      array(array(TRUE, TRUE, TRUE), TRUE),
      array(array(TRUE, FALSE, TRUE), FALSE),
    );
  }

  /**
   * Tests ConfigNamesMapper::hasTranslatable().
   *
   * @param array $mock_return_values
   *   An array of values that the mocked configuration mapper manager should
   *   return for hasTranslatable().
   * @param bool $expected
   *   The expected return value of ConfigNamesMapper::hasTranslatable().
   *
   * @dataProvider providerTestHasTranslatable
   */
  public function testHasTranslatable(array $mock_return_values, $expected) {
    // As the configuration names are arbitrary, simply use integers.
    $config_names = range(1, count($mock_return_values));
    $this->configNamesMapper->setConfigNames($config_names);

    $map = array();
    foreach ($config_names as $i => $config_name) {
      $map[] = array($config_name, $mock_return_values[$i]);
    }
    $this->configMapperManager
      ->expects($this->any())
      ->method('hasTranslatable')
      ->will($this->returnValueMap($map));

    $result = $this->configNamesMapper->hasTranslatable();
    $this->assertSame($expected, $result);
  }

  /**
   * Provides data for ConfigNamesMapperTest::testHasTranslatable().
   *
   * @return array
   *   An array of arrays, where each inner array has an array of values that
   *   the mocked configuration mapper manager should return for
   *   hasTranslatable() as the first value and the expected return value of
   *   ConfigNamesMapper::hasTranslatable() as the second value.
   */
  public function providerTestHasTranslatable() {
    return array(
      array(array(TRUE), TRUE),
      array(array(FALSE), FALSE),
      array(array(TRUE, TRUE, TRUE), TRUE),
      array(array(TRUE, FALSE, TRUE), FALSE),
    );
  }

  /**
   * Tests ConfigNamesMapper::hasTranslation().
   *
   * @param array $mock_return_values
   *   An array of values that the mocked configuration mapper manager should
   *   return for hasTranslation().
   * @param bool $expected
   *   The expected return value of ConfigNamesMapper::hasTranslation().
   *
   * @dataProvider providerTestHasTranslation
   */
  public function testHasTranslation(array $mock_return_values, $expected) {
    $language = new Language();

    // As the configuration names are arbitrary, simply use integers.
    $config_names = range(1, count($mock_return_values));
    $this->configNamesMapper->setConfigNames($config_names);

    $map = array();
    foreach ($config_names as $i => $config_name) {
      $map[] = array($config_name, $language, $mock_return_values[$i]);
    }
    $this->localeConfigManager
      ->expects($this->any())
      ->method('hasTranslation')
      ->will($this->returnValueMap($map));

    $result = $this->configNamesMapper->hasTranslation($language);
    $this->assertSame($expected, $result);
  }

  /**
   * Provides data for for ConfigNamesMapperTest::testHasTranslation().
   *
   * @return array
   *   An array of arrays, where each inner array has an array of values that
   *   the mocked configuration mapper manager should return for
   *   hasTranslation() as the first value and the expected return value of
   *   ConfigNamesMapper::hasTranslation() as the second value.
   */
  public function providerTestHasTranslation() {
    return array(
      array(array(TRUE), TRUE),
      array(array(FALSE), FALSE),
      array(array(TRUE, TRUE, TRUE), TRUE),
      array(array(FALSE, FALSE, TRUE), TRUE),
      array(array(FALSE, FALSE, FALSE), FALSE),
    );
  }

  /**
   * Tests ConfigNamesMapper::getTypeName().
   */
  public function testGetTypeName() {
    $result = $this->configNamesMapper->getTypeName();
    $this->assertSame('Settings', $result);
  }

  /**
   * Tests ConfigNamesMapper::hasTranslation().
   */
  public function testGetOperations() {
    $expected = array(
      'translate' => array(
        'title' => 'Translate',
        'href' => '/admin/config/system/site-information/translate',
      ),
    );
    $result = $this->configNamesMapper->getOperations();
    $this->assertEquals($expected, $result);
  }

}

/**
 * Defines a test mapper class.
 */
class TestConfigNamesMapper extends ConfigNamesMapper {

  /**
   * Gets the internal language code of this mapper, if any.
   *
   * This method is not to be confused with
   * ConfigMapperInterface::getLangcode().
   *
   * @return string|null
   *   The language code of this mapper if it is set; NULL otherwise.
   */
  public function getInternalLangcode() {
    return isset($this->langcode) ? $this->langcode : NULL;
  }

  /**
   * Sets the list of configuration names.
   *
   * @param array $config_names
   */
  public function setConfigNames(array $config_names) {
    $this->pluginDefinition['names'] = $config_names;
  }

  /**
   * Sets the configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory $config_factory
   *   The config factory to set.
   */
  public function setConfigFactory(ConfigFactory $config_factory) {
    $this->configFactory = $config_factory;
  }

}