Issues (54)

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/Controller/Api/PostControllerTest.php (1 issue)

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\NewsBundle\Tests\Controller\Api;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\FormatterBundle\Formatter\FormatterInterface;
18
use Sonata\FormatterBundle\Formatter\Pool;
19
use Sonata\NewsBundle\Controller\Api\PostController;
20
use Sonata\NewsBundle\Model\CommentInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
23
/**
24
 * @author Hugo Briand <[email protected]>
25
 */
26
class PostControllerTest extends TestCase
27
{
28
    public function testGetPostsAction(): void
29
    {
30
        $paramFetcher = $this->createMock('FOS\RestBundle\Request\ParamFetcherInterface');
31
        $paramFetcher->expects($this->once())->method('all')->willReturn([]);
32
33
        $pager = $this->createMock('Sonata\DatagridBundle\Pager\PagerInterface');
34
35
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
36
        $postManager->expects($this->once())->method('getPager')->willReturn($pager);
37
38
        $this->assertSame($pager, $this->createPostController($postManager)->getPostsAction($paramFetcher));
39
    }
40
41
    public function testGetPostAction(): void
42
    {
43
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
44
45
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
46
        $postManager->expects($this->once())->method('find')->willReturn($post);
47
48
        $this->assertSame($post, $this->createPostController($postManager)->getPostAction(1));
49
    }
50
51
    public function testGetPostNotFoundExceptionAction(): void
52
    {
53
        $this->expectException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class);
54
        $this->expectExceptionMessage('Post (42) not found');
55
56
        $this->createPostController()->getPostAction(42);
57
    }
58
59
    public function testGetPostCommentsAction(): void
60
    {
61
        $parameters = [
62
            'page' => 2,
63
            'count' => 5,
64
        ];
65
66
        $paramFetcher = $this->createMock('FOS\RestBundle\Request\ParamFetcherInterface');
67
        $paramFetcher->expects($this->once())->method('all')->willReturn([]);
68
        $paramFetcher->expects($this->exactly(2))->method('get')
69
            ->with($this->logicalOr($this->equalTo('page'), $this->equalTo('count')))
70
            ->willReturnCallback(static function ($parameter) use ($parameters) {
71
                return $parameters[$parameter];
72
            });
73
74
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
75
76
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
77
        $postManager->expects($this->once())->method('find')->willReturn($post);
78
79
        $pager = $this->createMock('Sonata\DatagridBundle\Pager\PagerInterface');
80
81
        // Will assert that param fetcher parameters are used for the pager
82
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
83
        $commentManager->expects($this->once())
84
            ->method('getPager')
85
            ->with($this->anything(), $this->equalTo($parameters['page']), $this->equalTo($parameters['count']))
86
            ->willReturn($pager);
87
88
        $this->assertSame($pager, $this->createPostController($postManager, $commentManager)->getPostCommentsAction(1, $paramFetcher));
89
    }
90
91
    public function testGetPostCommentsActionNotFoundExceptionAction(): void
92
    {
93
        $this->expectException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class);
94
        $this->expectExceptionMessage('Post (42) not found');
95
96
        $paramFetcher = $this->createMock('FOS\RestBundle\Request\ParamFetcherInterface');
97
98
        $this->createPostController()->getPostCommentsAction(42, $paramFetcher);
99
    }
100
101
    public function testPostPostAction(): void
102
    {
103
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
104
        $post->expects($this->once())->method('setContent');
105
        $post->expects($this->once())->method('getContentFormatter')->willReturn('text');
106
        $post->expects($this->once())->method('getRawContent')->willReturn('');
107
108
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
109
        $postManager->expects($this->once())->method('save')->willReturn($post);
110
111
        $formatter = $this->createMock(FormatterInterface::class);
112
        $formatter->expects($this->once())->method('transform')->willReturn('');
113
        $formatterPool = new Pool('text');
114
        $formatterPool->add('text', $formatter);
115
116
        $form = $this->createMock('Symfony\Component\Form\Form');
117
        $form->expects($this->once())->method('handleRequest');
118
        $form->expects($this->once())->method('isValid')->willReturn(true);
119
        $form->expects($this->once())->method('getData')->willReturn($post);
120
121
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
122
        $formFactory->expects($this->once())->method('createNamed')->willReturn($form);
123
124
        $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->postPostAction(new Request());
125
126
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
127
    }
128
129
    public function testPostPostInvalidAction(): void
130
    {
131
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
132
        $post->expects($this->never())->method('setContent');
133
134
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
135
        $postManager->expects($this->never())->method('save')->willReturn($post);
136
137
        $formatter = $this->createMock(FormatterInterface::class);
138
        $formatter->expects($this->never())->method('transform');
139
        $formatterPool = new Pool('text');
140
        $formatterPool->add('text', $formatter);
141
142
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
143
        $form->expects($this->once())->method('handleRequest');
144
        $form->expects($this->once())->method('isValid')->willReturn(false);
145
146
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
147
        $formFactory->expects($this->once())->method('createNamed')->willReturn($form);
148
149
        $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->postPostAction(new Request());
150
151
        $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
152
    }
153
154
    public function testPutPostAction(): void
155
    {
156
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
157
        $post->expects($this->once())->method('setContent');
158
        $post->expects($this->once())->method('getContentFormatter')->willReturn('text');
159
        $post->expects($this->once())->method('getRawContent')->willReturn('');
160
        $post->expects($this->once())->method('getContent')->willReturn('');
161
162
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
163
        $postManager->expects($this->once())->method('find')->willReturn($post);
164
        $postManager->expects($this->once())->method('save')->willReturn($post);
165
166
        $formatter = $this->createMock(FormatterInterface::class);
167
        $formatter->expects($this->once())->method('transform')->willReturn($post->getContent());
168
        $formatterPool = new Pool('text');
169
        $formatterPool->add('text', $formatter);
170
171
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
172
        $form->expects($this->once())->method('handleRequest');
173
        $form->expects($this->once())->method('isValid')->willReturn(true);
174
        $form->expects($this->once())->method('getData')->willReturn($post);
175
176
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
177
        $formFactory->expects($this->once())->method('createNamed')->willReturn($form);
178
179
        $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->putPostAction(1, new Request());
180
181
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
182
    }
183
184
    public function testPutPostInvalidAction(): void
185
    {
186
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
187
        $post->expects($this->never())->method('setContent');
188
189
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
190
        $postManager->expects($this->once())->method('find')->willReturn($post);
191
        $postManager->expects($this->never())->method('save')->willReturn($post);
192
193
        $formatter = $this->createMock(FormatterInterface::class);
194
        $formatter->expects($this->never())->method('transform');
195
        $formatterPool = new Pool('text');
196
        $formatterPool->add('text', $formatter);
197
198
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
199
        $form->expects($this->once())->method('handleRequest');
200
        $form->expects($this->once())->method('isValid')->willReturn(false);
201
202
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
203
        $formFactory->expects($this->once())->method('createNamed')->willReturn($form);
204
205
        $view = $this->createPostController($postManager, null, null, $formFactory, $formatterPool)->putPostAction(1, new Request());
206
207
        $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
208
    }
209
210
    public function testDeletePostAction(): void
211
    {
212
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
213
214
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
215
        $postManager->expects($this->once())->method('find')->willReturn($post);
216
        $postManager->expects($this->once())->method('delete');
217
218
        $view = $this->createPostController($postManager)->deletePostAction(1);
219
220
        $this->assertSame(['deleted' => true], $view);
221
    }
222
223
    public function testDeletePostInvalidAction(): void
224
    {
225
        $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
226
227
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
228
        $postManager->expects($this->once())->method('find')->willReturn(null);
229
        $postManager->expects($this->never())->method('delete');
230
231
        $this->createPostController($postManager)->deletePostAction(1);
232
    }
233
234
    public function testPostPostCommentsAction(): void
235
    {
236
        $comment = $this->createMock(CommentInterface::class);
237
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
238
        $post->expects($this->once())->method('isCommentable')->willReturn(true);
239
240
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
241
        $postManager->expects($this->once())->method('find')->willReturn($post);
242
243
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
244
        $commentManager->expects($this->once())->method('save');
245
        $commentManager->expects($this->once())->method('create')->willReturn($comment);
246
247
        $mailer = $this->createMock('Sonata\NewsBundle\Mailer\MailerInterface');
248
        $mailer->expects($this->once())->method('sendCommentNotification');
249
250
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
251
        $form->expects($this->once())->method('handleRequest');
252
        $form->expects($this->once())->method('isValid')->willReturn(true);
253
        $form->expects($this->once())->method('getData')->willReturn($comment);
254
255
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
256
        $formFactory->expects($this->once())->method('createNamed')->willReturn($form);
257
258
        $postController = $this->createPostController($postManager, $commentManager, $mailer, $formFactory);
259
260
        $postController->postPostCommentsAction(1, new Request());
261
262
        $this->assertInstanceOf(CommentInterface::class, $comment);
263
    }
264
265
    public function testPostPostCommentsInvalidFormAction(): void
266
    {
267
        $comment = $this->createMock(CommentInterface::class);
268
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
269
        $post->expects($this->once())->method('isCommentable')->willReturn(true);
270
271
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
272
        $postManager->expects($this->once())->method('find')->willReturn($post);
273
274
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
275
        $commentManager->expects($this->once())->method('create')->willReturn($comment);
276
277
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
278
        $form->expects($this->once())->method('handleRequest');
279
        $form->expects($this->once())->method('isValid')->willReturn(false);
280
281
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
282
        $formFactory->expects($this->once())->method('createNamed')->willReturn($form);
283
284
        $this->assertInstanceOf('Symfony\Component\Form\Form', $this->createPostController($postManager, $commentManager, null, $formFactory)->postPostCommentsAction(1, new Request()));
285
    }
286
287
    public function testPostPostCommentsNotCommentableAction(): void
288
    {
289
        $this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
290
        $this->expectExceptionMessage('Post (42) not commentable');
291
292
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
293
        $post->expects($this->once())->method('isCommentable')->willReturn(false);
294
295
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
296
        $postManager->expects($this->once())->method('find')->willReturn($post);
297
298
        $this->createPostController($postManager)->postPostCommentsAction(42, new Request());
299
    }
300
301
    public function testPutPostCommentAction(): void
302
    {
303
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
304
        $post->expects($this->once())->method('isCommentable')->willReturn(true);
305
306
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
307
        $postManager->expects($this->once())->method('find')->willReturn($post);
308
309
        $comment = $this->createMock(CommentInterface::class);
310
311
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
312
        $commentManager->expects($this->once())->method('find')->willReturn($comment);
313
        $commentManager->expects($this->once())->method('save');
314
315
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
316
        $form->expects($this->once())->method('handleRequest');
317
        $form->expects($this->once())->method('getData')->willReturn($comment);
318
        $form->expects($this->once())->method('isValid')->willReturn(true);
319
320
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
321
        $formFactory->expects($this->once())->method('createNamed')->willReturn($form);
322
323
        $postController = $this->createPostController($postManager, $commentManager, null, $formFactory);
324
325
        $comment = $postController->putPostCommentsAction(1, 1, new Request());
326
327
        $this->assertInstanceOf(CommentInterface::class, $comment);
328
    }
329
330
    public function testPutPostCommentInvalidAction(): void
331
    {
332
        $post = $this->createMock('Sonata\NewsBundle\Model\PostInterface');
333
        $post->expects($this->once())->method('isCommentable')->willReturn(true);
334
335
        $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
336
        $postManager->expects($this->once())->method('find')->willReturn($post);
337
338
        $comment = $this->createMock(CommentInterface::class);
339
340
        $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
341
        $commentManager->expects($this->once())->method('find')->willReturn($comment);
342
        $commentManager->expects($this->never())->method('save');
343
344
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
345
        $form->expects($this->once())->method('handleRequest');
346
        $form->expects($this->once())->method('isValid')->willReturn(false);
347
348
        $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
349
        $formFactory->expects($this->once())->method('createNamed')->willReturn($form);
350
351
        $view = $this->createPostController($postManager, $commentManager, null, $formFactory)->putPostCommentsAction(1, 1, new Request());
352
353
        $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
354
    }
355
356
    /**
357
     * @param null $postManager
358
     * @param null $commentManager
359
     * @param null $mailer
360
     * @param null $formFactory
361
     * @param null $formatterPool
362
     *
363
     * @return PostController
364
     */
365
    protected function createPostController($postManager = null, $commentManager = null, $mailer = null, $formFactory = null, $formatterPool = null)
366
    {
367
        if (null === $postManager) {
368
            $postManager = $this->createMock('Sonata\NewsBundle\Model\PostManagerInterface');
369
        }
370
        if (null === $commentManager) {
371
            $commentManager = $this->createMock('Sonata\NewsBundle\Model\CommentManagerInterface');
372
        }
373
        if (null === $mailer) {
374
            $mailer = $this->createMock('Sonata\NewsBundle\Mailer\MailerInterface');
375
        }
376
        if (null === $formFactory) {
377
            $formFactory = $this->createMock('Symfony\Component\Form\FormFactoryInterface');
378
        }
379
        if (null === $formatterPool) {
380
            $formatterPool = new Pool('text');
381
            $formatterPool->add('text', $this->createMock(FormatterInterface::class));
382
        }
383
384
        return new PostController($postManager, $commentManager, $mailer, $formFactory, $formatterPool);
0 ignored issues
show
It seems like $formatterPool defined by parameter $formatterPool on line 365 can be null; however, Sonata\NewsBundle\Contro...ntroller::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
385
    }
386
}
387