Issues (306)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/Admin/FieldDescriptionTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\DoctrineORMAdminBundle\Tests\Admin;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AbstractAdmin;
18
use Sonata\AdminBundle\Admin\AdminInterface;
19
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
20
use Sonata\AdminBundle\Exception\NoValueException;
21
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription;
22
23
class FieldDescriptionTest extends TestCase
24
{
25
    public function testOptions(): void
26
    {
27
        $field = new FieldDescription();
28
        $field->setOptions([
29
            'template' => 'foo',
30
            'type' => 'bar',
31
            'misc' => 'foobar',
32
        ]);
33
34
        // test method shortcut
35
        $this->assertNull($field->getOption('template'));
36
        $this->assertNull($field->getOption('type'));
37
38
        $this->assertSame('foo', $field->getTemplate());
39
        $this->assertSame('bar', $field->getType());
40
41
        // test the default value option
42
        $this->assertSame('default', $field->getOption('template', 'default'));
43
44
        // test the merge options
45
        $field->setOption('array', ['key1' => 'val1']);
46
        $field->mergeOption('array', ['key1' => 'key_1', 'key2' => 'key_2']);
47
48
        $this->assertSame(['key1' => 'key_1', 'key2' => 'key_2'], $field->getOption('array'));
49
50
        $field->mergeOption('non_existent', ['key1' => 'key_1', 'key2' => 'key_2']);
51
52
        $this->assertSame(['key1' => 'key_1', 'key2' => 'key_2'], $field->getOption('array'));
53
54
        $field->mergeOptions(['array' => ['key3' => 'key_3']]);
55
56
        $this->assertSame(['key1' => 'key_1', 'key2' => 'key_2', 'key3' => 'key_3'], $field->getOption('array'));
57
58
        $field->setOption('integer', 1);
59
60
        try {
61
            $field->mergeOption('integer', []);
62
            $this->fail('no exception raised !!');
63
        } catch (\RuntimeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
64
        }
65
66
        $field->mergeOptions(['final' => 'test']);
67
68
        $expected = [
69
            'misc' => 'foobar',
70
            'placeholder' => 'short_object_description_placeholder',
71
            'link_parameters' => [],
72
            'array' => [
73
                'key1' => 'key_1',
74
                'key2' => 'key_2',
75
                'key3' => 'key_3',
76
            ],
77
            'non_existent' => [
78
                'key1' => 'key_1',
79
                'key2' => 'key_2',
80
            ],
81
            'integer' => 1,
82
            'final' => 'test',
83
        ];
84
85
        $this->assertSame($expected, $field->getOptions());
86
    }
87
88
    public function testAssociationMapping(): void
89
    {
90
        $field = new FieldDescription();
91
        $field->setAssociationMapping([
92
            'type' => 'integer',
93
            'fieldName' => 'position',
94
        ]);
95
96
        $this->assertSame('integer', $field->getType());
97
        $this->assertSame('integer', $field->getMappingType());
98
        $this->assertSame('position', $field->getFieldName());
99
100
        // cannot overwrite defined definition
101
        $field->setAssociationMapping([
102
            'type' => 'overwrite?',
103
            'fieldName' => 'overwritten',
104
        ]);
105
106
        $this->assertSame('integer', $field->getType());
107
        $this->assertSame('integer', $field->getMappingType());
108
        $this->assertSame('overwritten', $field->getFieldName());
109
110
        $field->setMappingType('string');
111
        $this->assertSame('string', $field->getMappingType());
112
        $this->assertSame('integer', $field->getType());
113
    }
114
115
    public function testSetName(): void
116
    {
117
        $field = new FieldDescription();
118
        $field->setName('New field description name');
119
120
        $this->assertSame($field->getName(), 'New field description name');
121
    }
122
123
    public function testSetNameSetFieldNameToo(): void
124
    {
125
        $field = new FieldDescription();
126
        $field->setName('New field description name');
127
128
        $this->assertSame($field->getFieldName(), 'New field description name');
129
    }
130
131
    public function testSetNameDoesNotSetFieldNameWhenSetBefore(): void
132
    {
133
        $field = new FieldDescription();
134
        $field->setFieldName('field name');
135
        $field->setName('New field description name');
136
137
        $this->assertSame($field->getFieldName(), 'field name');
138
    }
139
140
    public function testGetParent(): void
141
    {
142
        $adminMock = $this->createMock(AdminInterface::class);
143
        $field = new FieldDescription();
144
        $field->setParent($adminMock);
145
146
        $this->assertSame($adminMock, $field->getParent());
147
    }
148
149
    public function testGetAdmin(): void
150
    {
151
        $adminMock = $this->createMock(AdminInterface::class);
152
        $field = new FieldDescription();
153
        $field->setAdmin($adminMock);
154
155
        $this->assertSame($adminMock, $field->getAdmin());
156
    }
157
158
    public function testGetAssociationAdmin(): void
159
    {
160
        $adminMock = $this->createMock(AbstractAdmin::class);
161
        $adminMock->expects($this->once())
162
            ->method('setParentFieldDescription')
163
            ->with($this->isInstanceOf(FieldDescriptionInterface::class));
164
165
        $field = new FieldDescription();
166
        $field->setAssociationAdmin($adminMock);
167
168
        $this->assertSame($adminMock, $field->getAssociationAdmin());
169
    }
170
171
    public function testHasAssociationAdmin(): void
172
    {
173
        $adminMock = $this->createMock(AbstractAdmin::class);
174
        $adminMock->expects($this->once())
175
            ->method('setParentFieldDescription')
176
            ->with($this->isInstanceOf(FieldDescriptionInterface::class));
177
178
        $field = new FieldDescription();
179
180
        $this->assertFalse($field->hasAssociationAdmin());
181
182
        $field->setAssociationAdmin($adminMock);
183
184
        $this->assertTrue($field->hasAssociationAdmin());
185
    }
186
187
    public function testGetValue(): void
188
    {
189
        $mockedObject = $this->getMockBuilder('stdClass')
190
            ->setMethods(['myMethod'])
191
            ->getMock();
192
        $mockedObject->expects($this->once())
193
            ->method('myMethod')
194
            ->willReturn('myMethodValue');
195
196
        $field = new FieldDescription();
197
        $field->setFieldName('any string, but not null');
198
        $field->setOption('code', 'myMethod');
199
200
        $this->assertSame($field->getValue($mockedObject), 'myMethodValue');
201
    }
202
203
    public function testGetValueWhenCannotRetrieve(): void
204
    {
205
        $this->expectException(NoValueException::class);
206
207
        $mockedObject = $this->getMockBuilder('stdClass')
208
            ->setMethods(['myMethod'])
209
            ->getMock();
210
        $mockedObject->expects($this->never())
211
            ->method('myMethod')
212
            ->willReturn('myMethodValue');
213
214
        $field = new FieldDescription();
215
        $field->setFieldName('any string, but not null');
216
217
        $this->assertSame($field->getValue($mockedObject), 'myMethodValue');
218
    }
219
220
    public function testGetAssociationMapping(): void
221
    {
222
        $assocationMapping = [
223
            'type' => 'integer',
224
            'fieldName' => 'position',
225
        ];
226
227
        $field = new FieldDescription();
228
        $field->setAssociationMapping($assocationMapping);
229
230
        $this->assertSame($assocationMapping, $field->getAssociationMapping());
231
    }
232
233
    public function testSetAssociationMappingAllowOnlyForArray(): void
234
    {
235
        $this->expectException(\RuntimeException::class);
236
237
        $field = new FieldDescription();
238
        $field->setAssociationMapping('test');
0 ignored issues
show
'test' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
239
    }
240
241
    public function testSetFieldMappingAllowOnlyForArray(): void
242
    {
243
        $this->expectException(\RuntimeException::class);
244
245
        $field = new FieldDescription();
246
        $field->setFieldMapping('test');
0 ignored issues
show
'test' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
247
    }
248
249
    public function testSetFieldMappingSetType(): void
250
    {
251
        $fieldMapping = [
252
            'type' => 'integer',
253
            'fieldName' => 'position',
254
        ];
255
256
        $field = new FieldDescription();
257
        $field->setFieldMapping($fieldMapping);
258
259
        $this->assertSame('integer', $field->getType());
260
    }
261
262
    public function testSetFieldMappingSetMappingType(): void
263
    {
264
        $fieldMapping = [
265
            'type' => 'integer',
266
            'fieldName' => 'position',
267
        ];
268
269
        $field = new FieldDescription();
270
        $field->setFieldMapping($fieldMapping);
271
272
        $this->assertSame('integer', $field->getMappingType());
273
    }
274
275
    public function testSetFieldMappingSetFieldName(): void
276
    {
277
        $fieldMapping = [
278
            'type' => 'integer',
279
            'fieldName' => 'position',
280
        ];
281
282
        $field = new FieldDescription();
283
        $field->setFieldMapping($fieldMapping);
284
285
        $this->assertSame('position', $field->getFieldName());
286
    }
287
288
    public function testGetTargetEntity(): void
289
    {
290
        $assocationMapping = [
291
            'type' => 'integer',
292
            'fieldName' => 'position',
293
            'targetEntity' => 'someValue',
294
        ];
295
296
        $field = new FieldDescription();
297
298
        $this->assertNull($field->getTargetModel());
299
300
        $field->setAssociationMapping($assocationMapping);
301
302
        $this->assertSame('someValue', $field->getTargetModel());
303
    }
304
305
    public function testIsIdentifierFromFieldMapping(): void
306
    {
307
        $fieldMapping = [
308
            'type' => 'integer',
309
            'fieldName' => 'position',
310
            'id' => true,
311
        ];
312
313
        $field = new FieldDescription();
314
        $field->setFieldMapping($fieldMapping);
315
316
        $this->assertTrue($field->isIdentifier());
317
    }
318
319
    public function testGetFieldMapping(): void
320
    {
321
        $fieldMapping = [
322
            'type' => 'integer',
323
            'fieldName' => 'position',
324
            'id' => 'someId',
325
        ];
326
327
        $field = new FieldDescription();
328
        $field->setFieldMapping($fieldMapping);
329
330
        $this->assertSame($fieldMapping, $field->getFieldMapping());
331
    }
332
333
    public function testGetValueForEmbeddedObject(): void
334
    {
335
        $mockedEmbeddedObject = $this->getMockBuilder('stdClass')
336
            ->setMethods(['myMethod'])
337
            ->getMock();
338
        $mockedEmbeddedObject->expects($this->once())
339
                    ->method('myMethod')
340
                    ->willReturn('myMethodValue');
341
342
        $mockedObject = $this->getMockBuilder('stdClass')
343
            ->setMethods(['getMyEmbeddedObject'])
344
            ->getMock();
345
        $mockedObject->expects($this->once())
346
            ->method('getMyEmbeddedObject')
347
            ->willReturn($mockedEmbeddedObject);
348
349
        $field = new FieldDescription();
350
        $field->setFieldMapping([
351
            'declaredField' => 'myEmbeddedObject', 'type' => 'string', 'fieldName' => 'myEmbeddedObject.myMethod',
352
        ]);
353
        $field->setFieldName('myMethod');
354
        $field->setOption('code', 'myMethod');
355
356
        $this->assertSame('myMethodValue', $field->getValue($mockedObject));
357
    }
358
359
    public function testGetValueForMultiLevelEmbeddedObject(): void
360
    {
361
        $mockedChildEmbeddedObject = $this->getMockBuilder('stdClass')
362
            ->setMethods(['myMethod'])
363
            ->getMock();
364
        $mockedChildEmbeddedObject->expects($this->once())
365
            ->method('myMethod')
366
            ->willReturn('myMethodValue');
367
        $mockedEmbeddedObject = $this->getMockBuilder('stdClass')
368
            ->setMethods(['getChild'])
369
            ->getMock();
370
        $mockedEmbeddedObject->expects($this->once())
371
            ->method('getChild')
372
            ->willReturn($mockedChildEmbeddedObject);
373
        $mockedObject = $this->getMockBuilder('stdClass')
374
            ->setMethods(['getMyEmbeddedObject'])
375
            ->getMock();
376
        $mockedObject->expects($this->once())
377
            ->method('getMyEmbeddedObject')
378
            ->willReturn($mockedEmbeddedObject);
379
        $field = new FieldDescription();
380
        $field->setFieldMapping([
381
            'declaredField' => 'myEmbeddedObject.child', 'type' => 'string', 'fieldName' => 'myMethod',
382
        ]);
383
        $field->setFieldName('myMethod');
384
        $field->setOption('code', 'myMethod');
385
        $this->assertSame('myMethodValue', $field->getValue($mockedObject));
386
    }
387
}
388