Completed
Push — 3.x ( 06cb47...ee8105 )
by Grégoire
06:40 queued 02:46
created

testDashboardGroupsWithBadItemsParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\AdminBundle\DependencyInjection\Configuration;
16
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
17
use Symfony\Component\Config\Definition\Processor;
18
19
class ConfigurationTest extends TestCase
20
{
21
    public function testOptions()
22
    {
23
        $config = $this->process([]);
24
25
        $this->assertTrue($config['options']['html5_validate']);
26
        $this->assertNull($config['options']['pager_links']);
27
        $this->assertTrue($config['options']['confirm_exit']);
28
        $this->assertTrue($config['options']['use_icheck']);
29
    }
30
31
    public function testBreadcrumbsChildRouteDefaultsToEdit()
32
    {
33
        $config = $this->process([]);
34
35
        $this->assertSame('edit', $config['breadcrumbs']['child_admin_route']);
36
    }
37
38
    public function testOptionsWithInvalidFormat()
39
    {
40
        $this->expectException(InvalidTypeException::class);
41
42
        $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...
43
            'options' => [
44
                'html5_validate' => '1',
45
            ],
46
        ]]);
47
    }
48
49
    public function testCustomTemplatesPerAdmin()
50
    {
51
        $config = $this->process([[
52
            'admin_services' => [
53
                'my_admin_id' => [
54
                    'templates' => [
55
                        'form' => ['form.twig.html', 'form_extra.twig.html'],
56
                        'view' => ['user_block' => 'SonataAdminBundle:mycustomtemplate.html.twig'],
57
                        'filter' => [],
58
                    ],
59
                ],
60
            ],
61
        ]]);
62
63
        $this->assertSame('SonataAdminBundle:mycustomtemplate.html.twig', $config['admin_services']['my_admin_id']['templates']['view']['user_block']);
64
    }
65
66
    public function testAdminServicesDefault()
67
    {
68
        $config = $this->process([[
69
            'admin_services' => ['my_admin_id' => []],
70
        ]]);
71
72
        $this->assertSame([
73
            'model_manager' => null,
74
            'form_contractor' => null,
75
            'show_builder' => null,
76
            'list_builder' => null,
77
            'datagrid_builder' => null,
78
            'translator' => null,
79
            'configuration_pool' => null,
80
            'route_generator' => null,
81
            'validator' => null,
82
            'security_handler' => null,
83
            'label' => null,
84
            'menu_factory' => null,
85
            'route_builder' => null,
86
            'label_translator_strategy' => null,
87
            'pager_type' => null,
88
            'templates' => [
89
                'form' => [],
90
                'filter' => [],
91
                'view' => [],
92
            ],
93
        ], $config['admin_services']['my_admin_id']);
94
    }
95
96
    public function testDashboardWithoutRoles()
97
    {
98
        $config = $this->process([]);
99
100
        $this->assertEmpty($config['dashboard']['blocks'][0]['roles']);
101
    }
102
103
    public function testDashboardWithRoles()
104
    {
105
        $config = $this->process([[
106
            'dashboard' => [
107
                'blocks' => [[
108
                    'roles' => ['ROLE_ADMIN'],
109
                    'type' => 'my.type',
110
                ]],
111
            ],
112
        ]]);
113
114
        $this->assertSame($config['dashboard']['blocks'][0]['roles'], ['ROLE_ADMIN']);
115
    }
116
117
    public function testDashboardGroups()
118
    {
119
        $config = $this->process([[
120
            'dashboard' => [
121
                'groups' => [
122
                    'bar' => [
123
                        'label' => 'foo',
124
                        'icon' => '<i class="fa fa-edit"></i>',
125
                        'items' => [
126
                            'item1',
127
                            'item2',
128
                            [
129
                                'label' => 'fooLabel',
130
                                'route' => 'fooRoute',
131
                                'route_params' => ['bar' => 'foo'],
132
                                'route_absolute' => true,
133
                            ],
134
                            [
135
                                'label' => 'barLabel',
136
                                'route' => 'barRoute',
137
                            ],
138
                        ],
139
                    ],
140
                ],
141
            ],
142
        ]]);
143
144
        $this->assertCount(4, $config['dashboard']['groups']['bar']['items']);
145
        $this->assertSame(
146
            $config['dashboard']['groups']['bar']['items'][0],
147
            [
148
                'admin' => 'item1',
149
                'label' => '',
150
                'route' => '',
151
                'route_params' => [],
152
                'route_absolute' => false,
153
                'roles' => [],
154
            ]
155
        );
156
        $this->assertSame(
157
            $config['dashboard']['groups']['bar']['items'][1],
158
            [
159
                'admin' => 'item2',
160
                'label' => '',
161
                'route' => '',
162
                'route_params' => [],
163
                'route_absolute' => false,
164
                'roles' => [],
165
            ]
166
        );
167
        $this->assertSame(
168
            $config['dashboard']['groups']['bar']['items'][2],
169
            [
170
                'label' => 'fooLabel',
171
                'route' => 'fooRoute',
172
                'route_params' => ['bar' => 'foo'],
173
                'route_absolute' => true,
174
                'admin' => '',
175
                'roles' => [],
176
            ]
177
        );
178
        $this->assertSame(
179
            $config['dashboard']['groups']['bar']['items'][3],
180
            [
181
                'label' => 'barLabel',
182
                'route' => 'barRoute',
183
                'route_params' => [],
184
                'admin' => '',
185
                'roles' => [],
186
                'route_absolute' => false,
187
            ]
188
        );
189
    }
190
191
    public function testDashboardGroupsWithBadItemsParams()
192
    {
193
        $this->expectException(\InvalidArgumentException::class, 'Expected either parameters "route" and "label" for array items');
194
195
        $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...
196
            'dashboard' => [
197
                'groups' => [
198
                    'bar' => [
199
                        'label' => 'foo',
200
                        'icon' => '<i class="fa fa-edit"></i>',
201
                        'items' => [
202
                            'item1',
203
                            'item2',
204
                            [
205
                                'route' => 'fooRoute',
206
                            ],
207
                        ],
208
                    ],
209
                ],
210
            ],
211
        ]]);
212
    }
213
214
    public function testSecurityConfigurationDefaults()
215
    {
216
        $config = $this->process([[]]);
217
218
        $this->assertSame('ROLE_SONATA_ADMIN', $config['security']['role_admin']);
219
        $this->assertSame('ROLE_SUPER_ADMIN', $config['security']['role_super_admin']);
220
    }
221
222
    public function testExtraAssetsDefaults()
223
    {
224
        $config = $this->process([[]]);
225
226
        $this->assertSame([], $config['assets']['extra_stylesheets']);
227
        $this->assertSame([], $config['assets']['extra_javascripts']);
228
    }
229
230
    public function testRemoveAssetsDefaults()
231
    {
232
        $config = $this->process([[]]);
233
234
        $this->assertSame([], $config['assets']['remove_stylesheets']);
235
        $this->assertSame([], $config['assets']['remove_javascripts']);
236
    }
237
238
    /**
239
     * Processes an array of configurations and returns a compiled version.
240
     *
241
     * @param array $configs An array of raw configurations
242
     *
243
     * @return array A normalized array
244
     */
245
    protected function process($configs)
246
    {
247
        $processor = new Processor();
248
249
        return $processor->processConfiguration(new Configuration(), $configs);
250
    }
251
}
252