Completed
Push — master ( 41e548...922b05 )
by Sullivan
04:01
created

testSecurityConfigurationDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 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\Processor;
17
18
class ConfigurationTest extends TestCase
19
{
20
    public function testOptions()
21
    {
22
        $config = $this->process([]);
23
24
        $this->assertTrue($config['options']['html5_validate']);
25
        $this->assertNull($config['options']['pager_links']);
26
        $this->assertTrue($config['options']['confirm_exit']);
27
        $this->assertTrue($config['options']['use_icheck']);
28
    }
29
30
    public function testBreadcrumbsChildRouteDefaultsToEdit()
31
    {
32
        $config = $this->process([]);
33
34
        $this->assertSame('edit', $config['breadcrumbs']['child_admin_route']);
35
    }
36
37
    public function testOptionsWithInvalidFormat()
38
    {
39
        $this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
40
41
        $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...
42
            'options' => [
43
                'html5_validate' => '1',
44
            ],
45
        ]]);
46
    }
47
48
    public function testCustomTemplatesPerAdmin()
49
    {
50
        $config = $this->process([[
51
            'admin_services' => [
52
                'my_admin_id' => [
53
                    'templates' => [
54
                        'form' => ['form.twig.html', 'form_extra.twig.html'],
55
                        'view' => ['user_block' => 'SonataAdminBundle:mycustomtemplate.html.twig'],
56
                        'filter' => [],
57
                    ],
58
                ],
59
            ],
60
        ]]);
61
62
        $this->assertSame('SonataAdminBundle:mycustomtemplate.html.twig', $config['admin_services']['my_admin_id']['templates']['view']['user_block']);
63
    }
64
65
    public function testAdminServicesDefault()
66
    {
67
        $config = $this->process([[
68
            'admin_services' => ['my_admin_id' => []],
69
        ]]);
70
71
        $this->assertSame([
72
            'model_manager' => null,
73
            'form_contractor' => null,
74
            'show_builder' => null,
75
            'list_builder' => null,
76
            'datagrid_builder' => null,
77
            'translator' => null,
78
            'configuration_pool' => null,
79
            'route_generator' => null,
80
            'validator' => null,
81
            'security_handler' => null,
82
            'label' => null,
83
            'menu_factory' => null,
84
            'route_builder' => null,
85
            'label_translator_strategy' => null,
86
            'pager_type' => null,
87
            'templates' => [
88
                'form' => [],
89
                'filter' => [],
90
                'view' => [],
91
            ],
92
        ], $config['admin_services']['my_admin_id']);
93
    }
94
95
    public function testDashboardWithoutRoles()
96
    {
97
        $config = $this->process([]);
98
99
        $this->assertEmpty($config['dashboard']['blocks'][0]['roles']);
100
    }
101
102
    public function testDashboardWithRoles()
103
    {
104
        $config = $this->process([[
105
            'dashboard' => [
106
                'blocks' => [[
107
                    'roles' => ['ROLE_ADMIN'],
108
                    'type' => 'my.type',
109
                ]],
110
            ],
111
        ]]);
112
113
        $this->assertSame($config['dashboard']['blocks'][0]['roles'], ['ROLE_ADMIN']);
114
    }
115
116
    public function testDashboardGroups()
117
    {
118
        $config = $this->process([[
119
            'dashboard' => [
120
                'groups' => [
121
                    'bar' => [
122
                        'label' => 'foo',
123
                        'icon' => '<i class="fa fa-edit"></i>',
124
                        'items' => [
125
                            'item1',
126
                            'item2',
127
                            [
128
                                'label' => 'fooLabel',
129
                                'route' => 'fooRoute',
130
                                'route_params' => ['bar' => 'foo'],
131
                                'route_absolute' => true,
132
                            ],
133
                            [
134
                                'label' => 'barLabel',
135
                                'route' => 'barRoute',
136
                            ],
137
                        ],
138
                    ],
139
                ],
140
            ],
141
        ]]);
142
143
        $this->assertCount(4, $config['dashboard']['groups']['bar']['items']);
144
        $this->assertSame(
145
            $config['dashboard']['groups']['bar']['items'][0],
146
            [
147
                'admin' => 'item1',
148
                'label' => '',
149
                'route' => '',
150
                'route_params' => [],
151
                'route_absolute' => false,
152
                'roles' => [],
153
            ]
154
        );
155
        $this->assertSame(
156
            $config['dashboard']['groups']['bar']['items'][1],
157
            [
158
                'admin' => 'item2',
159
                'label' => '',
160
                'route' => '',
161
                'route_params' => [],
162
                'route_absolute' => false,
163
                'roles' => [],
164
            ]
165
        );
166
        $this->assertSame(
167
            $config['dashboard']['groups']['bar']['items'][2],
168
            [
169
                'label' => 'fooLabel',
170
                'route' => 'fooRoute',
171
                'route_params' => ['bar' => 'foo'],
172
                'route_absolute' => true,
173
                'admin' => '',
174
                'roles' => [],
175
            ]
176
        );
177
        $this->assertSame(
178
            $config['dashboard']['groups']['bar']['items'][3],
179
            [
180
                'label' => 'barLabel',
181
                'route' => 'barRoute',
182
                'route_params' => [],
183
                'admin' => '',
184
                'roles' => [],
185
                'route_absolute' => false,
186
            ]
187
        );
188
    }
189
190
    public function testDashboardGroupsWithBadItemsParams()
191
    {
192
        $this->expectException('\InvalidArgumentException', 'Expected either parameters "route" and "label" for array items');
0 ignored issues
show
Unused Code introduced by
The call to ConfigurationTest::expectException() has too many arguments starting with 'Expected either paramet...label" for array items'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
193
194
        $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...
195
            'dashboard' => [
196
                'groups' => [
197
                    'bar' => [
198
                        'label' => 'foo',
199
                        'icon' => '<i class="fa fa-edit"></i>',
200
                        'items' => [
201
                            'item1',
202
                            'item2',
203
                            [
204
                                'route' => 'fooRoute',
205
                            ],
206
                        ],
207
                    ],
208
                ],
209
            ],
210
        ]]);
211
    }
212
213
    public function testSecurityConfigurationDefaults()
214
    {
215
        $config = $this->process([[]]);
216
217
        $this->assertSame('ROLE_SONATA_ADMIN', $config['security']['role_admin']);
218
        $this->assertSame('ROLE_SUPER_ADMIN', $config['security']['role_super_admin']);
219
    }
220
221
    /**
222
     * Processes an array of configurations and returns a compiled version.
223
     *
224
     * @param array $configs An array of raw configurations
225
     *
226
     * @return array A normalized array
227
     */
228
    protected function process($configs)
229
    {
230
        $processor = new Processor();
231
232
        return $processor->processConfiguration(new Configuration(), $configs);
233
    }
234
}
235