Completed
Push — master ( 2ff8b4...848fca )
by Grégoire
20s
created

ConfigurationTest::testRemoveAssetsDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Tests;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\DependencyInjection\Configuration;
18
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
19
use Symfony\Component\Config\Definition\Processor;
20
21
class ConfigurationTest extends TestCase
22
{
23
    public function testOptions(): void
24
    {
25
        $config = $this->process([]);
26
27
        $this->assertTrue($config['options']['html5_validate']);
28
        $this->assertNull($config['options']['pager_links']);
29
        $this->assertTrue($config['options']['confirm_exit']);
30
        $this->assertTrue($config['options']['use_icheck']);
31
    }
32
33
    public function testBreadcrumbsChildRouteDefaultsToEdit(): void
34
    {
35
        $config = $this->process([]);
36
37
        $this->assertSame('edit', $config['breadcrumbs']['child_admin_route']);
38
    }
39
40
    public function testOptionsWithInvalidFormat(): void
41
    {
42
        $this->expectException(InvalidTypeException::class);
43
44
        $config = $this->process([[
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
            'options' => [
46
                'html5_validate' => '1',
47
            ],
48
        ]]);
49
    }
50
51
    public function testCustomTemplatesPerAdmin(): void
52
    {
53
        $config = $this->process([[
54
            'admin_services' => [
55
                'my_admin_id' => [
56
                    'templates' => [
57
                        'form' => ['form.twig.html', 'form_extra.twig.html'],
58
                        'view' => ['user_block' => 'SonataAdminBundle:mycustomtemplate.html.twig'],
59
                        'filter' => [],
60
                    ],
61
                ],
62
            ],
63
        ]]);
64
65
        $this->assertSame('SonataAdminBundle:mycustomtemplate.html.twig', $config['admin_services']['my_admin_id']['templates']['view']['user_block']);
66
    }
67
68
    public function testAdminServicesDefault(): void
69
    {
70
        $config = $this->process([[
71
            'admin_services' => ['my_admin_id' => []],
72
        ]]);
73
74
        $this->assertSame([
75
            'model_manager' => null,
76
            'form_contractor' => null,
77
            'show_builder' => null,
78
            'list_builder' => null,
79
            'datagrid_builder' => null,
80
            'translator' => null,
81
            'configuration_pool' => null,
82
            'route_generator' => null,
83
            'validator' => null,
84
            'security_handler' => null,
85
            'label' => null,
86
            'menu_factory' => null,
87
            'route_builder' => null,
88
            'label_translator_strategy' => null,
89
            'pager_type' => null,
90
            'templates' => [
91
                'form' => [],
92
                'filter' => [],
93
                'view' => [],
94
            ],
95
        ], $config['admin_services']['my_admin_id']);
96
    }
97
98
    public function testDashboardWithoutRoles(): void
99
    {
100
        $config = $this->process([]);
101
102
        $this->assertEmpty($config['dashboard']['blocks'][0]['roles']);
103
    }
104
105
    public function testDashboardWithRoles(): void
106
    {
107
        $config = $this->process([[
108
            'dashboard' => [
109
                'blocks' => [[
110
                    'roles' => ['ROLE_ADMIN'],
111
                    'type' => 'my.type',
112
                ]],
113
            ],
114
        ]]);
115
116
        $this->assertSame($config['dashboard']['blocks'][0]['roles'], ['ROLE_ADMIN']);
117
    }
118
119
    public function testDashboardGroups(): void
120
    {
121
        $config = $this->process([[
122
            'dashboard' => [
123
                'groups' => [
124
                    'bar' => [
125
                        'label' => 'foo',
126
                        'icon' => '<i class="fa fa-edit"></i>',
127
                        'items' => [
128
                            'item1',
129
                            'item2',
130
                            [
131
                                'label' => 'fooLabel',
132
                                'route' => 'fooRoute',
133
                                'route_params' => ['bar' => 'foo'],
134
                                'route_absolute' => true,
135
                            ],
136
                            [
137
                                'label' => 'barLabel',
138
                                'route' => 'barRoute',
139
                            ],
140
                        ],
141
                    ],
142
                ],
143
            ],
144
        ]]);
145
146
        $this->assertCount(4, $config['dashboard']['groups']['bar']['items']);
147
        $this->assertSame(
148
            $config['dashboard']['groups']['bar']['items'][0],
149
            [
150
                'admin' => 'item1',
151
                'label' => '',
152
                'route' => '',
153
                'route_params' => [],
154
                'route_absolute' => false,
155
                'roles' => [],
156
            ]
157
        );
158
        $this->assertSame(
159
            $config['dashboard']['groups']['bar']['items'][1],
160
            [
161
                'admin' => 'item2',
162
                'label' => '',
163
                'route' => '',
164
                'route_params' => [],
165
                'route_absolute' => false,
166
                'roles' => [],
167
            ]
168
        );
169
        $this->assertSame(
170
            $config['dashboard']['groups']['bar']['items'][2],
171
            [
172
                'label' => 'fooLabel',
173
                'route' => 'fooRoute',
174
                'route_params' => ['bar' => 'foo'],
175
                'route_absolute' => true,
176
                'admin' => '',
177
                'roles' => [],
178
            ]
179
        );
180
        $this->assertSame(
181
            $config['dashboard']['groups']['bar']['items'][3],
182
            [
183
                'label' => 'barLabel',
184
                'route' => 'barRoute',
185
                'route_params' => [],
186
                'admin' => '',
187
                'roles' => [],
188
                'route_absolute' => false,
189
            ]
190
        );
191
    }
192
193
    public function testDashboardGroupsWithBadItemsParams(): void
194
    {
195
        $this->expectException(\InvalidArgumentException::class, 'Expected either parameters "route" and "label" for array items');
196
197
        $config = $this->process([[
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
198
            'dashboard' => [
199
                'groups' => [
200
                    'bar' => [
201
                        'label' => 'foo',
202
                        'icon' => '<i class="fa fa-edit"></i>',
203
                        'items' => [
204
                            'item1',
205
                            'item2',
206
                            [
207
                                'route' => 'fooRoute',
208
                            ],
209
                        ],
210
                    ],
211
                ],
212
            ],
213
        ]]);
214
    }
215
216
    public function testSecurityConfigurationDefaults(): void
217
    {
218
        $config = $this->process([[]]);
219
220
        $this->assertSame('ROLE_SONATA_ADMIN', $config['security']['role_admin']);
221
        $this->assertSame('ROLE_SUPER_ADMIN', $config['security']['role_super_admin']);
222
    }
223
224
    public function testExtraAssetsDefaults(): void
225
    {
226
        $config = $this->process([[]]);
227
228
        $this->assertSame([], $config['assets']['extra_stylesheets']);
229
        $this->assertSame([], $config['assets']['extra_javascripts']);
230
    }
231
232
    public function testRemoveAssetsDefaults(): void
233
    {
234
        $config = $this->process([[]]);
235
236
        $this->assertSame([], $config['assets']['remove_stylesheets']);
237
        $this->assertSame([], $config['assets']['remove_javascripts']);
238
    }
239
240
    /**
241
     * Processes an array of configurations and returns a compiled version.
242
     *
243
     * @param array $configs An array of raw configurations
244
     *
245
     * @return array A normalized array
246
     */
247
    protected function process($configs)
248
    {
249
        $processor = new Processor();
250
251
        return $processor->processConfiguration(new Configuration(), $configs);
252
    }
253
}
254