Completed
Push — ezp_30973 ( 1b82fd...a5777c )
by
unknown
12:50
created

EzPublishCoreExtensionTest   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 876
Duplicated Lines 14.84 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
dl 130
loc 876
rs 9.164
c 0
b 0
f 0
wmc 37
lcom 2
cbo 9

26 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 26 1
A getContainerExtensions() 0 4 1
A getMinimalConfiguration() 0 4 1
B testSiteAccessConfiguration() 9 59 6
A testSiteAccessNoConfiguration() 0 9 1
A testImageMagickConfigurationBasic() 3 18 3
A testImageMagickConfigurationFilters() 3 26 3
A testImagePlaceholderConfiguration() 0 31 1
A testRoutingConfiguration() 0 11 1
A testCacheConfiguration() 0 6 1
A cacheConfigurationProvider() 0 30 1
A testCacheConfigurationCustomPurgeService() 0 12 1
A testLocaleConfiguration() 0 7 1
A testRepositoriesConfiguration() 0 47 2
A testRepositoriesConfigurationFieldGroups() 0 14 2
B repositoriesConfigurationFieldGroupsProvider() 0 119 1
A testRepositoriesConfigurationEmpty() 0 34 1
A testRepositoriesConfigurationStorageEmpty() 39 39 1
A testRepositoriesConfigurationSearchEmpty() 39 39 1
B testRepositoriesConfigurationCompatibility() 0 68 1
A testRepositoriesConfigurationCompatibility2() 37 37 1
B testRelatedSiteAccesses() 0 78 1
A testRegisteredPolicies() 0 51 1
A testUrlAliasConfiguration() 0 33 1
A testQueryTypeAutomaticConfiguration() 0 15 1
A compileCoreContainer() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the EzPublishCoreExtensionTest 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;
10
11
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\QueryTypePass;
12
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser\Common;
13
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser\Content;
14
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\EzPublishCoreExtension;
15
use eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Stub\QueryTypeBundle\QueryType\TestQueryType;
16
use eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Stub\StubPolicyProvider;
17
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\Yaml\Yaml;
20
use ReflectionObject;
21
22
class EzPublishCoreExtensionTest extends AbstractExtensionTestCase
23
{
24
    private $minimalConfig = [];
25
26
    private $siteaccessConfig = [];
27
28
    /** @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\EzPublishCoreExtension */
29
    private $extension;
30
31
    protected function setUp(): void
32
    {
33
        $this->extension = new EzPublishCoreExtension();
34
        $this->siteaccessConfig = [
35
            'siteaccess' => [
36
                'default_siteaccess' => 'ezdemo_site',
37
                'list' => ['ezdemo_site', 'eng', 'fre', 'ezdemo_site_admin'],
38
                'groups' => [
39
                    'ezdemo_group' => ['ezdemo_site', 'eng', 'fre', 'ezdemo_site_admin'],
40
                    'ezdemo_frontend_group' => ['ezdemo_site', 'eng', 'fre'],
41
                ],
42
                'match' => [
43
                    'URILElement' => 1,
44
                    'Map\URI' => ['the_front' => 'ezdemo_site', 'the_back' => 'ezdemo_site_admin'],
45
                ],
46
            ],
47
            'system' => [
48
                'ezdemo_site' => [],
49
                'eng' => [],
50
                'fre' => [],
51
                'ezdemo_site_admin' => [],
52
            ],
53
        ];
54
55
        parent::setUp();
56
    }
57
58
    protected function getContainerExtensions(): array
59
    {
60
        return [$this->extension];
61
    }
62
63
    protected function getMinimalConfiguration(): array
64
    {
65
        return $this->minimalConfig = Yaml::parse(file_get_contents(__DIR__ . '/Fixtures/ezpublish_minimal_no_siteaccess.yml'));
0 ignored issues
show
Documentation Bug introduced by
It seems like \Symfony\Component\Yaml\...al_no_siteaccess.yml')) of type * is incompatible with the declared type array of property $minimalConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
    }
67
68
    public function testSiteAccessConfiguration()
69
    {
70
        // Injecting needed config parsers.
71
        $refExtension = new ReflectionObject($this->extension);
72
        $refMethod = $refExtension->getMethod('getMainConfigParser');
73
        $refMethod->setAccessible(true);
74
        $refMethod->invoke($this->extension);
75
        $refParser = $refExtension->getProperty('mainConfigParser');
76
        $refParser->setAccessible(true);
77
        /** @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigParser $parser */
78
        $parser = $refParser->getValue($this->extension);
79
        $parser->setConfigParsers([new Common(), new Content()]);
80
81
        $this->load($this->siteaccessConfig);
82
        $this->assertContainerBuilderHasParameter(
83
            'ezpublish.siteaccess.list',
84
            $this->siteaccessConfig['siteaccess']['list']
85
        );
86
        $this->assertContainerBuilderHasParameter(
87
            'ezpublish.siteaccess.default',
88
            $this->siteaccessConfig['siteaccess']['default_siteaccess']
89
        );
90
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.groups', $this->siteaccessConfig['siteaccess']['groups']);
91
92
        $expectedMatchingConfig = [];
93
        foreach ($this->siteaccessConfig['siteaccess']['match'] as $key => $val) {
94
            // Value is expected to always be an array (transformed by semantic configuration parser).
95
            $expectedMatchingConfig[$key] = is_array($val) ? $val : ['value' => $val];
96
        }
97
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.match_config', $expectedMatchingConfig);
98
99
        $groupsBySiteaccess = [];
100 View Code Duplication
        foreach ($this->siteaccessConfig['siteaccess']['groups'] as $groupName => $groupMembers) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
            foreach ($groupMembers as $member) {
102
                if (!isset($groupsBySiteaccess[$member])) {
103
                    $groupsBySiteaccess[$member] = [];
104
                }
105
106
                $groupsBySiteaccess[$member][] = $groupName;
107
            }
108
        }
109
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.groups_by_siteaccess', $groupsBySiteaccess);
110
111
        $relatedSiteAccesses = ['ezdemo_site', 'eng', 'fre', 'ezdemo_site_admin'];
112
        $this->assertContainerBuilderHasParameter(
113
            'ezpublish.siteaccess.relation_map',
114
            [
115
                // Empty string is the default repository name
116
                '' => [
117
                    // 2 is the default rootLocationId
118
                    2 => $relatedSiteAccesses,
119
                ],
120
            ]
121
        );
122
123
        $this->assertContainerBuilderHasParameter('ezsettings.ezdemo_site.related_siteaccesses', $relatedSiteAccesses);
124
        $this->assertContainerBuilderHasParameter('ezsettings.eng.related_siteaccesses', $relatedSiteAccesses);
125
        $this->assertContainerBuilderHasParameter('ezsettings.fre.related_siteaccesses', $relatedSiteAccesses);
126
    }
127
128
    public function testSiteAccessNoConfiguration()
129
    {
130
        $this->load();
131
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.list', ['setup']);
132
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.default', 'setup');
133
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.groups', []);
134
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.groups_by_siteaccess', []);
135
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.match_config', null);
136
    }
137
138
    public function testImageMagickConfigurationBasic()
139
    {
140 View Code Duplication
        if (!isset($_ENV['imagemagickConvertPath']) || !is_executable($_ENV['imagemagickConvertPath'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
            $this->markTestSkipped('Missing or mis-configured Imagemagick convert path.');
142
        }
143
144
        $this->load(
145
            [
146
                'imagemagick' => [
147
                    'enabled' => true,
148
                    'path' => $_ENV['imagemagickConvertPath'],
149
                ],
150
            ]
151
        );
152
        $this->assertContainerBuilderHasParameter('ezpublish.image.imagemagick.enabled', true);
153
        $this->assertContainerBuilderHasParameter('ezpublish.image.imagemagick.executable_path', dirname($_ENV['imagemagickConvertPath']));
154
        $this->assertContainerBuilderHasParameter('ezpublish.image.imagemagick.executable', basename($_ENV['imagemagickConvertPath']));
155
    }
156
157
    public function testImageMagickConfigurationFilters()
158
    {
159 View Code Duplication
        if (!isset($_ENV['imagemagickConvertPath']) || !is_executable($_ENV['imagemagickConvertPath'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
            $this->markTestSkipped('Missing or mis-configured Imagemagick convert path.');
161
        }
162
163
        $customFilters = [
164
            'foobar' => '-foobar',
165
            'wow' => '-amazing',
166
        ];
167
        $this->load(
168
            [
169
                'imagemagick' => [
170
                    'enabled' => true,
171
                    'path' => $_ENV['imagemagickConvertPath'],
172
                    'filters' => $customFilters,
173
                ],
174
            ]
175
        );
176
        $this->assertTrue($this->container->hasParameter('ezpublish.image.imagemagick.filters'));
177
        $filters = $this->container->getParameter('ezpublish.image.imagemagick.filters');
178
        $this->assertArrayHasKey('foobar', $filters);
179
        $this->assertSame($customFilters['foobar'], $filters['foobar']);
180
        $this->assertArrayHasKey('wow', $filters);
181
        $this->assertSame($customFilters['wow'], $filters['wow']);
182
    }
183
184
    public function testImagePlaceholderConfiguration()
185
    {
186
        $this->load([
187
            'image_placeholder' => [
188
                'default' => [
189
                    'provider' => 'generic',
190
                    'options' => [
191
                        'foo' => 'Foo',
192
                        'bar' => 'Bar',
193
                    ],
194
                ],
195
                'fancy' => [
196
                    'provider' => 'remote',
197
                ],
198
            ],
199
        ]);
200
201
        $this->assertEquals([
202
            'default' => [
203
                'provider' => 'generic',
204
                'options' => [
205
                    'foo' => 'Foo',
206
                    'bar' => 'Bar',
207
                ],
208
            ],
209
            'fancy' => [
210
                'provider' => 'remote',
211
                'options' => [],
212
            ],
213
        ], $this->container->getParameter('image_alias.placeholder_providers'));
214
    }
215
216
    public function testRoutingConfiguration()
217
    {
218
        $this->load();
219
        $this->assertContainerBuilderHasAlias('router', 'ezpublish.chain_router');
220
221
        $this->assertTrue($this->container->hasParameter('ezpublish.default_router.non_siteaccess_aware_routes'));
222
        $nonSiteaccessAwareRoutes = $this->container->getParameter('ezpublish.default_router.non_siteaccess_aware_routes');
223
        // See ezpublish_minimal_no_siteaccess.yml fixture
224
        $this->assertContains('foo_route', $nonSiteaccessAwareRoutes);
225
        $this->assertContains('my_prefix_', $nonSiteaccessAwareRoutes);
226
    }
227
228
    /**
229
     * @dataProvider cacheConfigurationProvider
230
     *
231
     * @param array $customCacheConfig
232
     * @param string $expectedPurgeType
233
     */
234
    public function testCacheConfiguration(array $customCacheConfig, $expectedPurgeType)
235
    {
236
        $this->load($customCacheConfig);
237
238
        $this->assertContainerBuilderHasParameter('ezpublish.http_cache.purge_type', $expectedPurgeType);
239
    }
240
241
    public function cacheConfigurationProvider()
242
    {
243
        return [
244
            [[], 'local'],
245
            [
246
                [
247
                    'http_cache' => ['purge_type' => 'local'],
248
                ],
249
                'local',
250
            ],
251
            [
252
                [
253
                    'http_cache' => ['purge_type' => 'multiple_http'],
254
                ],
255
                'http',
256
            ],
257
            [
258
                [
259
                    'http_cache' => ['purge_type' => 'single_http'],
260
                ],
261
                'http',
262
            ],
263
            [
264
                [
265
                    'http_cache' => ['purge_type' => 'http'],
266
                ],
267
                'http',
268
            ],
269
        ];
270
    }
271
272
    public function testCacheConfigurationCustomPurgeService()
273
    {
274
        $serviceId = 'foobar';
275
        $this->setDefinition($serviceId, new Definition());
276
        $this->load(
277
            [
278
                'http_cache' => ['purge_type' => 'foobar', 'timeout' => 12],
279
            ]
280
        );
281
282
        $this->assertContainerBuilderHasParameter('ezpublish.http_cache.purge_type', 'foobar');
283
    }
284
285
    public function testLocaleConfiguration()
286
    {
287
        $this->load(['locale_conversion' => ['foo' => 'bar']]);
288
        $conversionMap = $this->container->getParameter('ezpublish.locale.conversion_map');
289
        $this->assertArrayHasKey('foo', $conversionMap);
290
        $this->assertSame('bar', $conversionMap['foo']);
291
    }
292
293
    public function testRepositoriesConfiguration()
294
    {
295
        $repositories = [
296
            'main' => [
297
                'storage' => [
298
                    'engine' => 'legacy',
299
                    'connection' => 'default',
300
                ],
301
                'search' => [
302
                    'engine' => 'legacy',
303
                    'connection' => 'blabla',
304
                ],
305
                'fields_groups' => [
306
                    'list' => ['content', 'metadata'],
307
                    'default' => '%ezsettings.default.content.field_groups.default%',
308
                ],
309
                'options' => [
310
                    'default_version_archive_limit' => 5,
311
                ],
312
            ],
313
            'foo' => [
314
                'storage' => [
315
                    'engine' => 'sqlng',
316
                    'connection' => 'default',
317
                ],
318
                'search' => [
319
                    'engine' => 'solr',
320
                    'connection' => 'lalala',
321
                ],
322
                'fields_groups' => [
323
                    'list' => ['content', 'metadata'],
324
                    'default' => '%ezsettings.default.content.field_groups.default%',
325
                ],
326
                'options' => [
327
                    'default_version_archive_limit' => 5,
328
                ],
329
            ],
330
        ];
331
        $this->load(['repositories' => $repositories]);
332
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
333
334
        foreach ($repositories as &$repositoryConfig) {
335
            $repositoryConfig['storage']['config'] = [];
336
            $repositoryConfig['search']['config'] = [];
337
        }
338
        $this->assertSame($repositories, $this->container->getParameter('ezpublish.repositories'));
339
    }
340
341
    /**
342
     * @dataProvider repositoriesConfigurationFieldGroupsProvider
343
     */
344
    public function testRepositoriesConfigurationFieldGroups($repositories, $expectedRepositories)
345
    {
346
        $this->load(['repositories' => $repositories]);
347
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
348
349
        $repositoriesPar = $this->container->getParameter('ezpublish.repositories');
350
        $this->assertEquals(count($repositories), count($repositoriesPar));
351
352
        foreach ($repositoriesPar as $key => $repo) {
353
            $this->assertArrayHasKey($key, $expectedRepositories);
354
            $this->assertArrayHasKey('fields_groups', $repo);
355
            $this->assertEqualsCanonicalizing($expectedRepositories[$key]['fields_groups'], $repo['fields_groups'], 'Invalid fields groups element');
356
        }
357
    }
358
359
    public function repositoriesConfigurationFieldGroupsProvider()
360
    {
361
        return [
362
            //empty config
363
            [
364
                ['main' => null],
365
                ['main' => [
366
                        'fields_groups' => [
367
                            'list' => ['content', 'metadata'],
368
                            'default' => '%ezsettings.default.content.field_groups.default%',
369
                        ],
370
                    ],
371
                ],
372
            ],
373
            //single item with custom fields
374
            [
375
                ['foo' => [
376
                        'fields_groups' => [
377
                            'list' => ['bar', 'baz', 'john'],
378
                            'default' => 'bar',
379
                        ],
380
                    ],
381
                ],
382
                ['foo' => [
383
                        'fields_groups' => [
384
                            'list' => ['bar', 'baz', 'john'],
385
                            'default' => 'bar',
386
                        ],
387
                    ],
388
                ],
389
            ],
390
            //mixed item with custom config and empty item
391
            [
392
                [
393
                    'foo' => [
394
                        'fields_groups' => [
395
                            'list' => ['bar', 'baz', 'john', 'doe'],
396
                            'default' => 'bar',
397
                        ],
398
                    ],
399
                    'anotherone' => null,
400
                ],
401
                [
402
                    'foo' => [
403
                        'fields_groups' => [
404
                            'list' => ['bar', 'baz', 'john', 'doe'],
405
                            'default' => 'bar',
406
                        ],
407
                    ],
408
                    'anotherone' => [
409
                        'fields_groups' => [
410
                            'list' => ['content', 'metadata'],
411
                            'default' => '%ezsettings.default.content.field_groups.default%',
412
                        ],
413
                    ],
414
                ],
415
            ],
416
            //items with only one field configured
417
            [
418
                [
419
                    'foo' => [
420
                        'fields_groups' => [
421
                            'list' => ['bar', 'baz', 'john'],
422
                        ],
423
                    ],
424
                    'bar' => [
425
                        'fields_groups' => [
426
                            'default' => 'metadata',
427
                        ],
428
                    ],
429
                ],
430
                [
431
                    'foo' => [
432
                        'fields_groups' => [
433
                            'list' => ['bar', 'baz', 'john'],
434
                            'default' => '%ezsettings.default.content.field_groups.default%',
435
                        ],
436
                    ],
437
                    'bar' => [
438
                        'fields_groups' => [
439
                            'list' => ['content', 'metadata'],
440
                            'default' => 'metadata',
441
                        ],
442
                    ],
443
                ],
444
            ],
445
            //two different repositories
446
            [
447
                [
448
                    'foo' => [
449
                        'fields_groups' => [
450
                            'list' => ['bar', 'baz', 'john', 'doe'],
451
                            'default' => 'bar',
452
                        ],
453
                    ],
454
                    'bar' => [
455
                        'fields_groups' => [
456
                            'list' => ['lorem', 'ipsum'],
457
                            'default' => 'lorem',
458
                        ],
459
                    ],
460
                ],
461
                [
462
                    'foo' => [
463
                        'fields_groups' => [
464
                            'list' => ['bar', 'baz', 'john', 'doe'],
465
                            'default' => 'bar',
466
                        ],
467
                    ],
468
                    'bar' => [
469
                        'fields_groups' => [
470
                            'list' => ['lorem', 'ipsum'],
471
                            'default' => 'lorem',
472
                        ],
473
                    ],
474
                ],
475
            ],
476
        ];
477
    }
478
479
    public function testRepositoriesConfigurationEmpty()
480
    {
481
        $repositories = [
482
            'main' => null,
483
        ];
484
        $expectedRepositories = [
485
            'main' => [
486
                'storage' => [
487
                    'engine' => '%ezpublish.api.storage_engine.default%',
488
                    'connection' => null,
489
                    'config' => [],
490
                ],
491
                'search' => [
492
                    'engine' => '%ezpublish.api.search_engine.default%',
493
                    'connection' => null,
494
                    'config' => [],
495
                ],
496
                'fields_groups' => [
497
                    'list' => ['content', 'metadata'],
498
                    'default' => '%ezsettings.default.content.field_groups.default%',
499
                ],
500
                'options' => [
501
                    'default_version_archive_limit' => 5,
502
                ],
503
            ],
504
        ];
505
        $this->load(['repositories' => $repositories]);
506
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
507
508
        $this->assertSame(
509
            $expectedRepositories,
510
            $this->container->getParameter('ezpublish.repositories')
511
        );
512
    }
513
514 View Code Duplication
    public function testRepositoriesConfigurationStorageEmpty()
515
    {
516
        $repositories = [
517
            'main' => [
518
                'search' => [
519
                    'engine' => 'fantasticfind',
520
                    'connection' => 'french',
521
                ],
522
            ],
523
        ];
524
        $expectedRepositories = [
525
            'main' => [
526
                'search' => [
527
                    'engine' => 'fantasticfind',
528
                    'connection' => 'french',
529
                    'config' => [],
530
                ],
531
                'storage' => [
532
                    'engine' => '%ezpublish.api.storage_engine.default%',
533
                    'connection' => null,
534
                    'config' => [],
535
                ],
536
                'fields_groups' => [
537
                    'list' => ['content', 'metadata'],
538
                    'default' => '%ezsettings.default.content.field_groups.default%',
539
                ],
540
                'options' => [
541
                    'default_version_archive_limit' => 5,
542
                ],
543
            ],
544
        ];
545
        $this->load(['repositories' => $repositories]);
546
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
547
548
        $this->assertSame(
549
            $expectedRepositories,
550
            $this->container->getParameter('ezpublish.repositories')
551
        );
552
    }
553
554 View Code Duplication
    public function testRepositoriesConfigurationSearchEmpty()
555
    {
556
        $repositories = [
557
            'main' => [
558
                'storage' => [
559
                    'engine' => 'persistentprudence',
560
                    'connection' => 'yes',
561
                ],
562
            ],
563
        ];
564
        $expectedRepositories = [
565
            'main' => [
566
                'storage' => [
567
                    'engine' => 'persistentprudence',
568
                    'connection' => 'yes',
569
                    'config' => [],
570
                ],
571
                'search' => [
572
                    'engine' => '%ezpublish.api.search_engine.default%',
573
                    'connection' => null,
574
                    'config' => [],
575
                ],
576
                'fields_groups' => [
577
                    'list' => ['content', 'metadata'],
578
                    'default' => '%ezsettings.default.content.field_groups.default%',
579
                ],
580
                'options' => [
581
                    'default_version_archive_limit' => 5,
582
                ],
583
            ],
584
        ];
585
        $this->load(['repositories' => $repositories]);
586
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
587
588
        $this->assertSame(
589
            $expectedRepositories,
590
            $this->container->getParameter('ezpublish.repositories')
591
        );
592
    }
593
594
    public function testRepositoriesConfigurationCompatibility()
595
    {
596
        $repositories = [
597
            'main' => [
598
                'engine' => 'legacy',
599
                'connection' => 'default',
600
                'search' => [
601
                    'engine' => 'legacy',
602
                    'connection' => 'blabla',
603
                ],
604
            ],
605
            'foo' => [
606
                'engine' => 'sqlng',
607
                'connection' => 'default',
608
                'search' => [
609
                    'engine' => 'solr',
610
                    'connection' => 'lalala',
611
                ],
612
            ],
613
        ];
614
        $expectedRepositories = [
615
            'main' => [
616
                'search' => [
617
                    'engine' => 'legacy',
618
                    'connection' => 'blabla',
619
                    'config' => [],
620
                ],
621
                'storage' => [
622
                    'engine' => 'legacy',
623
                    'connection' => 'default',
624
                    'config' => [],
625
                ],
626
                'fields_groups' => [
627
                    'list' => ['content', 'metadata'],
628
                    'default' => '%ezsettings.default.content.field_groups.default%',
629
                ],
630
                'options' => [
631
                    'default_version_archive_limit' => 5,
632
                ],
633
            ],
634
            'foo' => [
635
                'search' => [
636
                    'engine' => 'solr',
637
                    'connection' => 'lalala',
638
                    'config' => [],
639
                ],
640
                'storage' => [
641
                    'engine' => 'sqlng',
642
                    'connection' => 'default',
643
                    'config' => [],
644
                ],
645
                'fields_groups' => [
646
                    'list' => ['content', 'metadata'],
647
                    'default' => '%ezsettings.default.content.field_groups.default%',
648
                ],
649
                'options' => [
650
                    'default_version_archive_limit' => 5,
651
                ],
652
            ],
653
        ];
654
        $this->load(['repositories' => $repositories]);
655
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
656
657
        $this->assertSame(
658
            $expectedRepositories,
659
            $this->container->getParameter('ezpublish.repositories')
660
        );
661
    }
662
663 View Code Duplication
    public function testRepositoriesConfigurationCompatibility2()
664
    {
665
        $repositories = [
666
            'main' => [
667
                'engine' => 'legacy',
668
                'connection' => 'default',
669
            ],
670
        ];
671
        $expectedRepositories = [
672
            'main' => [
673
                'storage' => [
674
                    'engine' => 'legacy',
675
                    'connection' => 'default',
676
                    'config' => [],
677
                ],
678
                'search' => [
679
                    'engine' => '%ezpublish.api.search_engine.default%',
680
                    'connection' => null,
681
                    'config' => [],
682
                ],
683
                'fields_groups' => [
684
                    'list' => ['content', 'metadata'],
685
                    'default' => '%ezsettings.default.content.field_groups.default%',
686
                ],
687
                'options' => [
688
                    'default_version_archive_limit' => 5,
689
                ],
690
            ],
691
        ];
692
        $this->load(['repositories' => $repositories]);
693
        $this->assertTrue($this->container->hasParameter('ezpublish.repositories'));
694
695
        $this->assertSame(
696
            $expectedRepositories,
697
            $this->container->getParameter('ezpublish.repositories')
698
        );
699
    }
700
701
    public function testRelatedSiteAccesses()
702
    {
703
        $mainRepo = 'main';
704
        $fooRepo = 'foo';
705
        $rootLocationId1 = 123;
706
        $rootLocationId2 = 456;
707
        $rootLocationId3 = 2;
708
        $config = [
709
            'siteaccess' => [
710
                'default_siteaccess' => 'ezdemo_site',
711
                'list' => ['ezdemo_site', 'eng', 'fre', 'ezdemo_site2', 'eng2', 'ezdemo_site3', 'fre3'],
712
                'groups' => [
713
                    'ezdemo_group' => ['ezdemo_site', 'eng', 'fre'],
714
                    'ezdemo_group2' => ['ezdemo_site2', 'eng2'],
715
                    'ezdemo_group3' => ['ezdemo_site3', 'fre3'],
716
                ],
717
                'match' => [],
718
            ],
719
            'repositories' => [
720
                $mainRepo => ['engine' => 'legacy', 'connection' => 'default'],
721
                $fooRepo => ['engine' => 'bar', 'connection' => 'blabla'],
722
            ],
723
            'system' => [
724
                'ezdemo_group' => [
725
                    'repository' => $mainRepo,
726
                    'content' => [
727
                        'tree_root' => ['location_id' => $rootLocationId1],
728
                    ],
729
                ],
730
                'ezdemo_group2' => [
731
                    'repository' => $mainRepo,
732
                    'content' => [
733
                        'tree_root' => ['location_id' => $rootLocationId2],
734
                    ],
735
                ],
736
                'ezdemo_group3' => [
737
                    'repository' => $fooRepo,
738
                ],
739
            ],
740
        ] + $this->siteaccessConfig;
741
742
        // Injecting needed config parsers.
743
        $refExtension = new ReflectionObject($this->extension);
744
        $refMethod = $refExtension->getMethod('getMainConfigParser');
745
        $refMethod->setAccessible(true);
746
        $refMethod->invoke($this->extension);
747
        $refParser = $refExtension->getProperty('mainConfigParser');
748
        $refParser->setAccessible(true);
749
        /** @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigParser $parser */
750
        $parser = $refParser->getValue($this->extension);
751
        $parser->setConfigParsers([new Common(), new Content()]);
752
753
        $this->load($config);
754
755
        $relatedSiteAccesses1 = ['ezdemo_site', 'eng', 'fre'];
756
        $relatedSiteAccesses2 = ['ezdemo_site2', 'eng2'];
757
        $relatedSiteAccesses3 = ['ezdemo_site3', 'fre3'];
758
        $expectedRelationMap = [
759
            $mainRepo => [
760
                $rootLocationId1 => $relatedSiteAccesses1,
761
                $rootLocationId2 => $relatedSiteAccesses2,
762
            ],
763
            $fooRepo => [
764
                $rootLocationId3 => $relatedSiteAccesses3,
765
            ],
766
        ];
767
        $this->assertContainerBuilderHasParameter('ezpublish.siteaccess.relation_map', $expectedRelationMap);
768
769
        $this->assertContainerBuilderHasParameter('ezsettings.ezdemo_site.related_siteaccesses', $relatedSiteAccesses1);
770
        $this->assertContainerBuilderHasParameter('ezsettings.eng.related_siteaccesses', $relatedSiteAccesses1);
771
        $this->assertContainerBuilderHasParameter('ezsettings.fre.related_siteaccesses', $relatedSiteAccesses1);
772
773
        $this->assertContainerBuilderHasParameter('ezsettings.ezdemo_site2.related_siteaccesses', $relatedSiteAccesses2);
774
        $this->assertContainerBuilderHasParameter('ezsettings.eng2.related_siteaccesses', $relatedSiteAccesses2);
775
776
        $this->assertContainerBuilderHasParameter('ezsettings.ezdemo_site3.related_siteaccesses', $relatedSiteAccesses3);
777
        $this->assertContainerBuilderHasParameter('ezsettings.fre3.related_siteaccesses', $relatedSiteAccesses3);
778
    }
779
780
    public function testRegisteredPolicies()
781
    {
782
        $this->load();
783
        self::assertContainerBuilderHasParameter('ezpublish.api.role.policy_map');
784
        $previousPolicyMap = $this->container->getParameter('ezpublish.api.role.policy_map');
785
786
        $policies1 = [
787
            'custom_module' => [
788
                'custom_function_1' => null,
789
                'custom_function_2' => ['CustomLimitation'],
790
            ],
791
            'helloworld' => [
792
                'foo' => ['bar'],
793
                'baz' => null,
794
            ],
795
        ];
796
        $this->extension->addPolicyProvider(new StubPolicyProvider($policies1));
797
798
        $policies2 = [
799
            'custom_module2' => [
800
                'custom_function_3' => null,
801
                'custom_function_4' => ['CustomLimitation2', 'CustomLimitation3'],
802
            ],
803
            'helloworld' => [
804
                'foo' => ['additional_limitation'],
805
                'some' => ['thingy', 'thing', 'but', 'wait'],
806
            ],
807
        ];
808
        $this->extension->addPolicyProvider(new StubPolicyProvider($policies2));
809
810
        $expectedPolicies = [
811
            'custom_module' => [
812
                'custom_function_1' => [],
813
                'custom_function_2' => ['CustomLimitation' => true],
814
            ],
815
            'helloworld' => [
816
                'foo' => ['bar' => true, 'additional_limitation' => true],
817
                'baz' => [],
818
                'some' => ['thingy' => true, 'thing' => true, 'but' => true, 'wait' => true],
819
            ],
820
            'custom_module2' => [
821
                'custom_function_3' => [],
822
                'custom_function_4' => ['CustomLimitation2' => true, 'CustomLimitation3' => true],
823
            ],
824
        ];
825
826
        $this->load();
827
        self::assertContainerBuilderHasParameter('ezpublish.api.role.policy_map');
828
        $expectedPolicies = array_merge_recursive($expectedPolicies, $previousPolicyMap);
829
        self::assertEquals($expectedPolicies, $this->container->getParameter('ezpublish.api.role.policy_map'));
830
    }
831
832
    public function testUrlAliasConfiguration()
833
    {
834
        $configuration = [
835
            'transformation' => 'urlalias_lowercase',
836
            'separator' => 'dash',
837
            'transformation_groups' => [
838
                'urlalias' => [
839
                    'commands' => [
840
                        'ascii_lowercase',
841
                        'cyrillic_lowercase',
842
                    ],
843
                    'cleanup_method' => 'url_cleanup',
844
                ],
845
                'urlalias_compact' => [
846
                    'commands' => [
847
                        'greek_normalize',
848
                        'exta_lowercase',
849
                    ],
850
                    'cleanup_method' => 'compact_cleanup',
851
                ],
852
            ],
853
        ];
854
        $this->load([
855
            'url_alias' => [
856
                'slug_converter' => $configuration,
857
            ],
858
        ]);
859
        $parsedConfig = $this->container->getParameter('ezpublish.url_alias.slug_converter');
860
        $this->assertSame(
861
            $configuration,
862
            $parsedConfig
863
        );
864
    }
865
866
    /**
867
     * Test automatic configuration of services implementing QueryType interface.
868
     *
869
     * @see \eZ\Publish\Core\QueryType\QueryType
870
     */
871
    public function testQueryTypeAutomaticConfiguration(): void
872
    {
873
        $definition = new Definition(TestQueryType::class);
874
        $definition->setAutoconfigured(true);
875
        $this->container->setDefinition(TestQueryType::class, $definition);
876
877
        $this->load();
878
879
        $this->compileCoreContainer();
880
881
        $this->assertContainerBuilderHasServiceDefinitionWithTag(
882
            TestQueryType::class,
883
            QueryTypePass::QUERY_TYPE_SERVICE_TAG
884
        );
885
    }
886
887
    /**
888
     * Prepare Core Container for compilation by mocking required parameters and compile it.
889
     */
890
    private function compileCoreContainer(): void
891
    {
892
        $this->container->setParameter('webroot_dir', __DIR__);
893
        $this->container->setParameter('kernel.root_dir', __DIR__);
894
        $this->container->setParameter('kernel.debug', false);
895
        $this->compile();
896
    }
897
}
898