Issues (1752)

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/FwlibTest/Db/DbDiffTest.php (14 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
namespace FwlibTest\Db;
3
4
use Fwlib\Bridge\Adodb;
5
use Fwlib\Db\DbDiff;
6
use Fwlib\Test\AbstractDbRelateTest;
7
use Fwlib\Util\UtilContainer;
8
use Fwlib\Util\UtilContainerAwareTrait;
9
10
/**
11
 * @copyright   Copyright 2012-2015 Fwolf
12
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
13
 */
14
class DbDiffTest extends AbstractDbRelateTest
15
{
16
    use UtilContainerAwareTrait;
17
18
19
    protected static $dbUsing = 'default';
20
21
    protected static $uuid1;
22
    protected static $uuid2;
23
    protected static $uuid3;
24
25
    public static $getErrorCode;
26
    public static $getErrorMessage;
27
28
29
    protected function buildMock()
30
    {
31
        $dbDiff = new DbDiff(self::$db);
0 ignored issues
show
Deprecated Code introduced by
The class Fwlib\Db\DbDiff has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
32
33
        return $dbDiff;
34
    }
35
36
37
    /**
38
     * @return DbDiff
39
     */
40 View Code Duplication
    protected function buildMockWithFakeDb()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $db = $this->getMockBuilder(Adodb::class)
43
            ->disableOriginalConstructor()
44
            ->getMock(
45
                Adodb::class,
0 ignored issues
show
The call to PHPUnit_Framework_MockOb..._MockBuilder::getMock() has too many arguments starting with \Fwlib\Bridge\Adodb::class.

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...
46
                [
47
                    'BeginTrans', 'CommitTrans', 'RollbackTrans',
48
                    'getErrorCode', 'getErrorMessage',
49
                    'execute'
50
                ]
51
            );
52
53
        $db->expects($this->any())
54
            ->method('getErrorCode')
55
            ->will($this->returnCallback(function () {
56
                return DbDiffTest::$getErrorCode;
57
            }));
58
59
        $db->expects($this->any())
60
            ->method('getErrorMessage')
61
            ->will($this->returnCallback(function () {
62
                return DbDiffTest::$getErrorMessage;
63
            }));
64
65
        $dbDiff = new DbDiff($db);
0 ignored issues
show
Deprecated Code introduced by
The class Fwlib\Db\DbDiff has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
66
67
        return $dbDiff;
68
    }
69
70
71 View Code Duplication
    public static function setUpBeforeClass()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        parent::setUpBeforeClass();
74
75
        $uuidGenerator = UtilContainer::getInstance()->getUuidBase36();
76
77
        self::$uuid1 = $uuidGenerator->generate();
78
        self::$uuid2 = $uuidGenerator->generate();
79
        self::$uuid3 = $uuidGenerator->generate();
80
    }
81
82
83
    /**
84
     * @expectedException \Exception
85
     * @expectedExceptionMessage can't commit again
86
     */
87 View Code Duplication
    public function testCommitAgain()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $dbDiff = $this->buildMock();
90
91
        $json = '{
92
            "rowCount": 0,
93
            "executeStatus": 100,
94
            "diff": {
95
                "' . self::$tableUser .  '": [
96
                    {
97
                        "mode": "Whatever",
98
                        "pk": [],
99
                        "column": []
100
                    }
101
                ]
102
            }
103
        }';
104
105
        $dbDiff->import($json);
106
        $dbDiff->commit();
107
    }
108
109
110
    /**
111
     * @expectedException \Exception
112
     * @expectedExceptionMessage Db execute fail
113
     */
114
    public function testCommitWithDbFail()
115
    {
116
        $dbDiff = $this->buildMockWithFakeDb();
117
118
        $dataNew = [
0 ignored issues
show
$dataNew 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...
119
            self::$tableUser => [
120
                'uuid'  => self::$uuid1,
121
            ],
122
        ];
123
124
        self::$getErrorCode = -1;
125
        self::$getErrorMessage = 'Db execute fail';
126
127
        $json = '{
128
            "rowCount": 0,
129
            "executeStatus": 0,
130
            "diff": {
131
                "' . self::$tableUser .  '": [
132
                    {
133
                        "mode": "INSERT",
134
                        "pk": {
135
                            "uuid": {
136
                                "new": "' . self::$uuid1 . '",
137
                                "old": null
138
                            }
139
                        },
140
                        "column": []
141
                    }
142
                ]
143
            }
144
        }';
145
        $dbDiff->import($json);
146
147
        $dbDiff->commit();
148
    }
149
150
151
    /**
152
     * @expectedException \Exception
153
     * @expectedExceptionMessage Invalid mode
154
     */
155 View Code Duplication
    public function testCommitWithInvalidMode()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        $dbDiff = $this->buildMock();
158
159
        $json = '{
160
            "rowCount": 0,
161
            "executeStatus": 0,
162
            "diff": {
163
                "' . self::$tableUser .  '": [
164
                    {
165
                        "mode": "InvalidMode",
166
                        "pk": [],
167
                        "column": []
168
                    }
169
                ]
170
            }
171
        }';
172
173
        $dbDiff->import($json);
174
        $dbDiff->commit();
175
    }
176
177
178
    /**
179
     * @expectedException \Exception
180
     * @expectedExceptionMessage No diff data
181
     */
182
    public function testCommitWithoutDiffData()
183
    {
184
        $dbDiff = $this->buildMock();
185
        $dbDiff->commit();
186
    }
187
188
189
    public function testCompareWithDeleteMode()
190
    {
191
        $dbDiff = $this->buildMock();
192
        $dataNew = [
193
            self::$tableUser => [
194
                'uuid'  => null,
195
            ],
196
        ];
197
        $dataOld = [
198
            self::$tableUser => [
199
                'uuid'  => self::$uuid1,
200
                'title' => 'User Title The Third',
201
                'age'   => 4200,
202
                'credit'    => '42',
203
                'joindate'  => '2012-01-02',
204
            ],
205
        ];
206
207
        $dbDiff->compare($dataNew, $dataOld);
208
        $diff = $dbDiff->getDiff();
209
        $diff = $diff[self::$tableUser][0];
210
211
        $this->assertEquals('DELETE', $diff['mode']);
212
        $this->assertEquals(4, count($diff['column']));
213
    }
214
215
216
    /**
217
     * @expectedException \Exception
218
     * @expectedExceptionMessage all null
219
     */
220
    public function testCompareWithNullPkInBothDataNewAndOld()
221
    {
222
        $dbDiff = $this->buildMock();
223
        $dataNew = [
224
            self::$tableUser => [
225
                'uuid'  => null,
226
            ],
227
        ];
228
229
        $dbDiff->compare($dataNew);
230
    }
231
232
233
    public function testCompareWithUpdateModeWithSameDataNewAndOld()
234
    {
235
        $dbDiff = $this->buildMock();
236
        $dataNew = [
237
            self::$tableUser => [
238
                'uuid'  => self::$uuid1,
239
                'title' => 'User Title The Third',
240
                'age'   => 4200,
241
                'credit'    => '42',
242
                'joindate'  => '2012-01-02',
243
            ],
244
        ];
245
246
        $dbDiff->compare($dataNew, $dataNew);
247
248
        $this->assertEmpty($dbDiff->getDiff());
249
    }
250
251
252
    public function testExecute()
253
    {
254
        $dbDiff = $this->buildMock();
255
256
        // Normal insert
257
        $dataNew1 = [
258
            self::$tableUser => [
259
                'uuid'  => self::$uuid1,
260
                'title' => 'User Title',
261
                'age'   => 42,
262
                'credit'    => '0.42',
263
                'joindate'  => '2014-01-02',
264
            ],
265
        ];
266
267
        $dbDiff->execute($dataNew1);
268
        $this->assertEquals(1, $dbDiff->getRowCount());
269
270
        $diff = $dbDiff->getDiff();
271
        $this->assertEquals('INSERT', $diff[self::$tableUser][0]['mode']);
272
        $this->assertEquals(1, count($diff[self::$tableUser][0]['pk']));
273
        $this->assertEquals(4, count($diff[self::$tableUser][0]['column']));
274
275
        $this->assertTrue($dbDiff->isCommitted());
276
        $this->assertTrue($dbDiff->isExecuted());
277
278
279
        // Insert with PK column only
280
        $dataNew2 = [
281
            self::$tableUser => [
282
                'uuid'  => self::$uuid2,
283
            ],
284
        ];
285
286
        $dbDiff->execute($dataNew2);
287
        $this->assertEquals(1, $dbDiff->getRowCount());
288
        $diff = $dbDiff->getDiff();
289
        $this->assertEquals(0, count($diff[self::$tableUser][0]['column']));
290
291
        $this->assertEquals(2, self::$db->getRowCount(self::$tableUser));
292
293
294
        // Update row with $uuid1, and delete row with $uuid2
295
        $dataNewChanged = [
296
            self::$tableUser => [
297
                // Modify from $dataNew1
298
                [
299
                    'uuid'  => self::$uuid1,
300
                    'title' => 'User Title Changed',
301
                    'age'   => 420,
302
                    'credit'    => '4.2',
303
                    'joindate'  => '2013-01-02',
304
                ],
305
                [
306
                    'uuid'  => null,
307
                ]
308
            ],
309
        ];
310
        $dataOld = [
311
            self::$tableUser => [
312
                $dataNew1[self::$tableUser],
313
                $dataNew2[self::$tableUser],
314
            ],
315
        ];
316
        $dbDiff->execute($dataNewChanged, $dataOld);
317
318
        $this->assertEquals(2, $dbDiff->getRowCount());
319
        $this->assertEquals(
320
            420,
321
            self::$db->getByKey(self::$tableUser, self::$uuid1, 'age', 'uuid')
322
        );
323
        $this->assertEquals(1, self::$db->getRowCount(self::$tableUser));
324
325
326
        // Rollback last update and delete
327
        $dbDiff->rollback();
328
329
        $this->assertTrue($dbDiff->isRollbacked());
330
        $this->assertEquals(
331
            42,
332
            self::$db->getByKey(self::$tableUser, self::$uuid1, 'age', 'uuid')
333
        );
334
        $this->assertEquals(2, self::$db->getRowCount(self::$tableUser));
335
336
337
        // Then commit again
338
        $dbDiff->commit();
339
340
        $this->assertTrue($dbDiff->isCommitted());
341
        $this->assertEquals(
342
            420,
343
            self::$db->getByKey(self::$tableUser, self::$uuid1, 'age', 'uuid')
344
        );
345
        $this->assertEquals(1, self::$db->getRowCount(self::$tableUser));
346
    }
347
348
349
    /**
350
     * @expectedException \Exception
351
     * @expectedExceptionMessage can't execute again
352
     */
353 View Code Duplication
    public function testExecuteAgain()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
354
    {
355
        $dbDiff = $this->buildMock();
356
357
        $json = '{
358
            "rowCount": 0,
359
            "executeStatus": 100,
360
            "diff": {
361
                "' . self::$tableUser .  '": [
362
                    {
363
                        "mode": "Whatever",
364
                        "pk": [],
365
                        "column": []
366
                    }
367
                ]
368
            }
369
        }';
370
371
        $dbDiff->import($json);
372
        $dbDiff->execute();
373
    }
374
375
376
    public function testExecuteInsertThenRollback()
377
    {
378
        $dbDiff = $this->buildMock();
379
380
        $dataNew = [
381
            self::$tableUser => [
382
                'uuid'  => self::$uuid3,
383
            ],
384
        ];
385
386
        $condition = "WHERE uuid = '" . self::$uuid3 . "'";
387
388
389
        // Insert
390
        $dbDiff->execute($dataNew);
391
        $this->assertEquals(
392
            1,
393
            self::$db->getRowCount(self::$tableUser, $condition)
394
        );
395
396
397
        // Export to json and import back
398
        $dbDiff->import($dbDiff->export());
399
400
401
        // Rollback
402
        $dbDiff->rollback();
403
        $this->assertEquals(
404
            0,
405
            self::$db->getRowCount(self::$tableUser, $condition)
406
        );
407
    }
408
409
410
    /**
411
     * @expectedException \Exception
412
     * @expectedExceptionMessage empty
413
     */
414
    public function testExecuteWithEmptyDataNew()
415
    {
416
        $dbDiff = $this->buildMock();
417
        $dbDiff->execute([], null);
418
    }
419
420
421
    /**
422
     * @expectedException \Exception
423
     * @expectedExceptionMessage PK not all assigned
424
     */
425 View Code Duplication
    public function testExecuteWithNotEnoughPkInDataNew()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
426
    {
427
        $dbDiff = $this->buildMock();
428
429
        // No PK column uuid
430
        $dataNew = [
431
            self::$tableUser => [
432
                'title' => 'User Title',
433
            ],
434
        ];
435
436
        $dbDiff->execute($dataNew);
437
    }
438
439
440
    /**
441
     * @expectedException \Exception
442
     * @expectedExceptionMessage must have PK
443
     */
444 View Code Duplication
    public function testExecuteWithTableHaveNoPk()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
445
    {
446
        $dbDiff = $this->buildMock();
447
448
        // No PK column uuid
449
        $dataNew = [
450
            'table_not_exist' => [
451
                'title' => 'User Title',
452
            ],
453
        ];
454
455
        $dbDiff->execute($dataNew);
456
    }
457
458
459
    /**
460
     * @expectedException \Exception
461
     * @expectedExceptionMessage Invalid json
462
     */
463
    public function testImportInvalidJson()
464
    {
465
        $dbDiff = $this->buildMock();
466
467
        $dbDiff->import('{}');
468
    }
469
470
471
    /**
472
     * @expectedException \Exception
473
     * @expectedExceptionMessage can't rollback again
474
     */
475 View Code Duplication
    public function testRollbackAgain()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
476
    {
477
        $dbDiff = $this->buildMock();
478
479
        $json = '{
480
            "rowCount": 0,
481
            "executeStatus": -100,
482
            "diff": {
483
                "' . self::$tableUser .  '": [
484
                    {
485
                        "mode": "Whatever",
486
                        "pk": [],
487
                        "column": []
488
                    }
489
                ]
490
            }
491
        }';
492
493
        $dbDiff->import($json);
494
        $dbDiff->rollback();
495
    }
496
497
498
    /**
499
     * @expectedException \Exception
500
     * @expectedExceptionMessage Db execute fail
501
     */
502
    public function testRollbackWithDbFail()
503
    {
504
        $dbDiff = $this->buildMockWithFakeDb();
505
506
        $dataNew = [
0 ignored issues
show
$dataNew 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...
507
            self::$tableUser => [
508
                'uuid'  => self::$uuid1,
509
            ],
510
        ];
511
512
        self::$getErrorCode = -1;
513
        self::$getErrorMessage = 'Db execute fail';
514
515
        $json = '{
516
            "rowCount": 0,
517
            "executeStatus": 100,
518
            "diff": {
519
                "' . self::$tableUser .  '": [
520
                    {
521
                        "mode": "INSERT",
522
                        "pk": {
523
                            "uuid": {
524
                                "new": "' . self::$uuid1 . '",
525
                                "old": null
526
                            }
527
                        },
528
                        "column": []
529
                    }
530
                ]
531
            }
532
        }';
533
        $dbDiff->import($json);
534
535
        $dbDiff->rollback();
536
    }
537
538
539
    /**
540
     * @expectedException \Exception
541
     * @expectedExceptionMessage Invalid mode
542
     */
543 View Code Duplication
    public function testRollbackWithInvalidMode()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
544
    {
545
        $dbDiff = $this->buildMock();
546
547
        $json = '{
548
            "rowCount": 0,
549
            "executeStatus": 0,
550
            "diff": {
551
                "' . self::$tableUser .  '": [
552
                    {
553
                        "mode": "InvalidMode",
554
                        "pk": [],
555
                        "column": []
556
                    }
557
                ]
558
            }
559
        }';
560
561
        $dbDiff->import($json);
562
        $dbDiff->rollback();
563
    }
564
565
566
    /**
567
     * @expectedException \Exception
568
     * @expectedExceptionMessage No diff data
569
     */
570
    public function testRollbackWithoutDiffData()
571
    {
572
        $dbDiff = $this->buildMock();
573
        $dbDiff->rollback();
574
    }
575
}
576