Issues (154)

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/Builder/ListBuilderTest.php (8 issues)

Labels
Severity

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\DoctrineMongoDBAdminBundle\Tests\Builder;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
17
use PHPUnit\Framework\TestCase;
18
use Prophecy\Argument;
19
use Sonata\AdminBundle\Admin\AbstractAdmin;
20
use Sonata\AdminBundle\Admin\AdminInterface;
21
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
22
use Sonata\AdminBundle\Templating\TemplateRegistry;
23
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription;
24
use Sonata\DoctrineMongoDBAdminBundle\Builder\ListBuilder;
25
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager;
26
use Symfony\Component\Form\Guess\Guess;
27
use Symfony\Component\Form\Guess\TypeGuess;
28
29
/**
30
 * @author Andrew Mor-Yaroslavtsev <[email protected]>
31
 */
32
class ListBuilderTest extends TestCase
33
{
34
    /**
35
     * @var TypeGuesserInterface|\Prophecy\Prophecy\ObjectProphecy
36
     */
37
    protected $typeGuesser;
38
39
    /**
40
     * @var ListBuilder
41
     */
42
    protected $listBuilder;
43
44
    /**
45
     * @var AdminInterface|\Prophecy\Prophecy\ObjectProphecy
46
     */
47
    protected $admin;
48
49
    /**
50
     * @var ModelManager|\Prophecy\Prophecy\ObjectProphecy
51
     */
52
    protected $modelManager;
53
54
    protected function setUp(): void
55
    {
56
        $this->typeGuesser = $this->prophesize(TypeGuesserInterface::class);
57
58
        $this->modelManager = $this->prophesize(ModelManager::class);
59
        $this->modelManager->hasMetadata(Argument::any())->willReturn(false);
60
61
        $this->admin = $this->prophesize(AbstractAdmin::class);
62
        $this->admin->getClass()->willReturn('Foo');
63
        $this->admin->getModelManager()->willReturn($this->modelManager);
64
        $this->admin->addListFieldDescription(Argument::any(), Argument::any())
65
            ->willReturn();
66
67
        $this->listBuilder = new ListBuilder($this->typeGuesser->reveal(), [
68
            'fakeTemplate' => 'fake',
69
            TemplateRegistry::TYPE_STRING => '@SonataAdmin/CRUD/list_string.html.twig',
70
        ]);
71
    }
72
73
    public function testAddListActionField(): void
74
    {
75
        $fieldDescription = new FieldDescription();
76
        $fieldDescription->setName('foo');
77
        $list = $this->listBuilder->getBaseList();
78
        $this->listBuilder
79
            ->addField($list, 'actions', $fieldDescription, $this->admin->reveal());
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
81
        $this->assertSame(
82
            '@SonataAdmin/CRUD/list__action.html.twig',
83
            $list->get('foo')->getTemplate(),
84
            'Custom list action field has a default list action template assigned'
85
        );
86
    }
87
88
    public function testCorrectFixedActionsFieldType(): void
89
    {
90
        $this->typeGuesser->guessType(
91
            Argument::any(),
92
            Argument::any(),
93
            Argument::any()
94
        )->willReturn(
95
            new TypeGuess('actions', [], Guess::LOW_CONFIDENCE)
96
        );
97
98
        $fieldDescription = new FieldDescription();
99
        $fieldDescription->setName('_action');
100
        $list = $this->listBuilder->getBaseList();
101
        $this->listBuilder->addField($list, null, $fieldDescription, $this->admin->reveal());
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
103
        $this->assertSame(
104
            'actions',
105
            $list->get('_action')->getType(),
106
            'Standard list _action field has "actions" type'
107
        );
108
    }
109
110
    public function testFixFieldDescriptionWithFieldMapping(): void
111
    {
112
        $classMetadata = $this->prophesize(ClassMetadata::class);
113
        $this->modelManager->hasMetadata(Argument::any())->willReturn(true);
114
        $fieldDescription = new FieldDescription();
115
        $fieldDescription->setName('test');
116
        $fieldDescription->setOption('sortable', true);
117
        $fieldDescription->setType('string');
118
119
        $classMetadata->fieldMappings = ['test' => ['type' => 'string']];
120
        $this->modelManager->getParentMetadataForProperty(Argument::cetera())
0 ignored issues
show
The call to getParentMetadataForProperty() misses a required argument $propertyFullName.

This check looks for function calls that miss required arguments.

Loading history...
121
            ->willReturn([$classMetadata, 'test', $parentAssociationMapping = []]);
122
123
        $this->listBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription);
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
125
        $this->assertSame('@SonataAdmin/CRUD/list_string.html.twig', $fieldDescription->getTemplate());
126
        $this->assertSame(['type' => 'string'], $fieldDescription->getFieldMapping());
127
    }
128
129
    /**
130
     * @dataProvider fixFieldDescriptionData
131
     */
132
    public function testFixFieldDescriptionWithAssociationMapping(string $type, string $template): void
133
    {
134
        $classMetadata = $this->prophesize(ClassMetadata::class);
135
        $this->modelManager->hasMetadata(Argument::any())->willReturn(true);
136
        $fieldDescription = new FieldDescription();
137
        $fieldDescription->setName('test');
138
        $fieldDescription->setOption('sortable', true);
139
        $fieldDescription->setType($type);
140
        $fieldDescription->setMappingType($type);
141
142
        $this->admin->attachAdminClass(Argument::any())->shouldBeCalledTimes(1);
143
144
        $associationMapping = [
145
            'fieldName' => 'associatedDocument',
146
            'name' => 'associatedDocument',
147
        ];
148
149
        $classMetadata->associationMappings = [
150
            'test' => $associationMapping,
151
        ];
152
153
        $this->modelManager->getParentMetadataForProperty(Argument::cetera())
0 ignored issues
show
The call to getParentMetadataForProperty() misses a required argument $propertyFullName.

This check looks for function calls that miss required arguments.

Loading history...
154
            ->willReturn([$classMetadata, 'test', $parentAssociationMapping = []]);
155
156
        $this->listBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription);
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
158
        $this->assertSame($template, $fieldDescription->getTemplate());
159
        $this->assertSame($associationMapping, $fieldDescription->getAssociationMapping());
160
    }
161
162
    public function fixFieldDescriptionData(): array
163
    {
164
        return [
165
            'one-to-one' => [
166
                ClassMetadata::ONE,
167
                '@SonataAdmin/CRUD/Association/list_many_to_one.html.twig',
168
            ],
169
            'many-to-one' => [
170
                ClassMetadata::MANY,
171
                '@SonataAdmin/CRUD/Association/list_many_to_many.html.twig',
172
            ],
173
        ];
174
    }
175
176
    /**
177
     * @dataProvider fixFieldDescriptionTypes
178
     */
179
    public function testFixFieldDescriptionFixesType(string $expectedType, string $type): void
180
    {
181
        $this->modelManager->hasMetadata(Argument::any())->willReturn(false);
182
        $fieldDescription = new FieldDescription();
183
        $fieldDescription->setName('test');
184
        $fieldDescription->setType($type);
185
186
        $this->listBuilder->fixFieldDescription($this->admin->reveal(), $fieldDescription);
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
188
        $this->assertSame($expectedType, $fieldDescription->getType());
189
    }
190
191
    public function fixFieldDescriptionTypes(): array
192
    {
193
        return [
194
            ['string', 'id'],
195
            ['integer', 'int'],
196
        ];
197
    }
198
199
    public function testFixFieldDescriptionException(): void
200
    {
201
        $this->expectException(\RuntimeException::class);
202
        $this->listBuilder->fixFieldDescription($this->admin->reveal(), new FieldDescription());
0 ignored issues
show
The method reveal() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
    }
204
}
205