Issues (41)

Security Analysis    no request data  

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/functional/FriendsTest.php (6 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

Code
1
<?php
2
/**
3
 * This file is part of Friends.
4
 *
5
 * (c) Christopher Lass <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
use Arubacao\Friends\FriendshipStatus;
12
13
class FriendsTest extends AbstractTestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
{
15
    use \Illuminate\Foundation\Testing\DatabaseTransactions;
16
//    use \Illuminate\Foundation\Testing\DatabaseMigrations;
17
18
    /**
19
     * @test
20
     */
21
    public function a_user_can_send_a_user_a_friend_request() {
22
        $sender = factory(User::class)->create();
23
        $recipient = factory(User::class)->create();
24
25
        $bool = $sender->sendFriendRequestTo($recipient);
26
27
        $this->assertTrue($bool);
28
        $this->seeInDatabase('friends', [
29
            'sender_id' => $sender->id,
30
            'recipient_id' => $recipient->id,
31
            'friendship_status' => FriendshipStatus::PENDING,
32
        ]);
33
        $this->dontSeeInDatabase('friends', [
34
            'recipient_id' => $sender->id,
35
            'sender_id' => $recipient->id,
36
            'friendship_status' => FriendshipStatus::PENDING,
37
        ]);
38
        $this->assertCount(1, $sender->any_friends());
39
        $this->assertCount(1, $recipient->any_friends());
40
        $this->assertTrue($recipient->hasPendingRequestFrom($sender));
41
        $this->assertFalse($sender->hasPendingRequestFrom($recipient));
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function a_user_can_not_send_a_user_a_friend_request_to_himself() {
48
        $sender = factory(User::class)->create();
49
50
        $bool = $sender->sendFriendRequestTo($sender);
51
52
        $this->assertFalse($bool);
53
        $this->dontSeeInDatabase('friends', [
54
            'recipient_id' => $sender->id,
55
        ]);
56
        $this->dontSeeInDatabase('friends', [
57
            'sender_id' => $sender->id,
58
        ]);
59
        $this->assertCount(0, $sender->any_friends());
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function a_user_can_only_send_one_friend_request_to_a_user() {
66
        $sender = factory(User::class)->create();
67
        $recipient = factory(User::class)->create();
68
69
        $true = $sender->sendFriendRequestTo($recipient);
70
        $false1 = $sender->sendFriendRequestTo($recipient);
71
        $false2 = $sender->sendFriendRequestTo($recipient);
72
        $false3 = $sender->sendFriendRequestTo($recipient);
73
74
75
        $this->assertTrue($true);
76
        $this->assertFalse($false1);
77
        $this->assertFalse($false2);
78
        $this->assertFalse($false3);
79
        $this->seeInDatabase('friends', [
80
            'sender_id' => $sender->id,
81
            'recipient_id' => $recipient->id,
82
            'friendship_status' => FriendshipStatus::PENDING,
83
        ]);
84
        $this->dontSeeInDatabase('friends', [
85
            'recipient_id' => $sender->id,
86
            'sender_id' => $recipient->id,
87
            'friendship_status' => FriendshipStatus::PENDING,
88
        ]);
89
        $this->assertCount(1, $sender->any_friends());
90
        $this->assertCount(1, $recipient->any_friends());
91
        $this->assertTrue($recipient->hasPendingRequestFrom($sender));
92
        $this->assertFalse($sender->hasPendingRequestFrom($recipient));
93
    }
94
95
    /**
96
     * @test
97
     */
98
    public function a_user_can_not_send_a_new_friend_request_to_a_friend() {
99
        $sender = factory(User::class)->create();
100
        $recipient = factory(User::class)->create();
101
102
        $true1 = $sender->sendFriendRequestTo($recipient);
103
        $true2 = $recipient->acceptFriendRequestFrom($sender);
104
        $false = $sender->sendFriendRequestTo($recipient);
105
106
        $this->assertTrue($true1);
107
        $this->assertTrue($true2);
108
        $this->assertFalse($false);
109
        $this->seeInDatabase('friends', [
110
            'sender_id' => $sender->id,
111
            'recipient_id' => $recipient->id,
112
            'friendship_status' => FriendshipStatus::ACCEPTED,
113
        ]);
114
        $this->dontSeeInDatabase('friends', [
115
            'sender_id' => $sender->id,
116
            'recipient_id' => $recipient->id,
117
            'friendship_status' => FriendshipStatus::PENDING,
118
        ]);
119
        $this->assertCount(1, $sender->any_friends());
120
        $this->assertCount(1, $sender->friends());
121
        $this->assertCount(1, $recipient->any_friends());
122
        $this->assertCount(1, $recipient->friends());
123
        $this->assertFalse($recipient->hasPendingRequestFrom($sender));
124
        $this->assertFalse($sender->hasPendingRequestFrom($recipient));
125
    }
126
127
    /**
128
     * @test
129
     */
130
    public function a_user_can_not_send_a_new_friend_request_to_a_friend_2() {
131
        $sender = factory(User::class)->create();
132
        $recipient = factory(User::class)->create();
133
134
        $true1 = $sender->sendFriendRequestTo($recipient);
135
        $true2 = $recipient->acceptFriendRequestFrom($sender);
136
        $false = $recipient->sendFriendRequestTo($sender);
137
138
        $this->assertTrue($true1);
139
        $this->assertTrue($true2);
140
        $this->assertFalse($false);
141
        $this->seeInDatabase('friends', [
142
            'sender_id' => $sender->id,
143
            'recipient_id' => $recipient->id,
144
            'friendship_status' => FriendshipStatus::ACCEPTED,
145
        ]);
146
        $this->dontSeeInDatabase('friends', [
147
            'sender_id' => $sender->id,
148
            'recipient_id' => $recipient->id,
149
            'friendship_status' => FriendshipStatus::PENDING,
150
        ]);
151
        $this->dontSeeInDatabase('friends', [
152
            'sender_id' => $recipient->id,
153
            'recipient_id' => $sender->id,
154
            'friendship_status' => FriendshipStatus::PENDING,
155
        ]);
156
        $this->assertCount(1, $sender->any_friends());
157
        $this->assertCount(1, $sender->friends());
158
        $this->assertCount(1, $recipient->any_friends());
159
        $this->assertCount(1, $recipient->friends());
160
        $this->assertFalse($recipient->hasPendingRequestFrom($sender));
161
        $this->assertFalse($sender->hasPendingRequestFrom($recipient));
162
    }
163
164
    /**
165
     * @test
166
     */
167
    public function a_user_can_delete_a_friend() {
168
        $sender = factory(User::class)->create();
169
        $recipient = factory(User::class)->create();
170
171
        $sender->sendFriendRequestTo($recipient);
172
        $recipient->acceptFriendRequestFrom($sender);
173
        $sender->deleteFriend($recipient);
174
175
        $this->dontSeeInDatabase('friends', [
176
            'sender_id' => $sender->id,
177
        ]);
178
        $this->assertCount(0, $sender->any_friends());
179
        $this->assertCount(0, $recipient->any_friends());
180
        $this->assertFalse($recipient->isFriendWith($sender));
181
        $this->assertFalse($sender->isFriendWith($recipient));
182
    }
183
184
    /**
185
     * @test
186
     */
187
    public function a_user_can_accept_a_friend_request() {
188
        $sender = factory(User::class)->create();
189
        $recipient = factory(User::class)->create();
190
191
        $sender->sendFriendRequestTo($recipient);
192
        $recipient->acceptFriendRequestFrom($sender);
193
194
        $this->seeInDatabase('friends', [
195
            'sender_id' => $sender->id,
196
            'recipient_id' => $recipient->id,
197
            'friendship_status' => FriendshipStatus::ACCEPTED,
198
        ]);
199
        $this->dontSeeInDatabase('friends', [
200
            'sender_id' => $sender->id,
201
            'recipient_id' => $recipient->id,
202
            'friendship_status' => FriendshipStatus::PENDING,
203
        ]);
204
205
        $this->assertCount(1, $sender->friends());
206
        $this->assertCount(1, $recipient->friends());
207
    }
208
209
    /**
210
     * @test
211
     */
212
    public function a_user_is_friend_with_a_user_after_a_friend_request() {
213
        $sender = factory(User::class)->create();
214
        $recipient = factory(User::class)->create();
215
216
        $sender->sendFriendRequestTo($recipient);
217
        $recipient->sendFriendRequestTo($sender);
218
219
        $this->assertTrue($sender->isFriendWith($recipient));
220
        $this->assertTrue($recipient->isFriendWith($sender));
221
    }
222
223
    /**
224
     * @test
225
     */
226
    public function a_user_can_deny_a_friend_request() {
227
        $sender = factory(User::class)->create();
228
        $recipient = factory(User::class)->create();
229
230
        $sender->sendFriendRequestTo($recipient);
231
        $recipient->denyFriendRequestFrom($sender);
232
233
        $this->dontSeeInDatabase('friends', [
234
            'sender_id' => $sender->id,
235
        ]);
236
        $this->dontSeeInDatabase('friends', [
237
            'recipient_id' => $recipient->id,
238
        ]);
239
240
        $this->assertCount(0, $sender->any_friends());
241
        $this->assertCount(0, $recipient->any_friends());
242
    }
243
244
    /**
245
     * @test
246
     */
247
    public function a_user_can_not_accept_a_non_existing_friend_request() {
248
        $sender = factory(User::class)->create();
249
        $recipient = factory(User::class)->create();
250
251
        $bool = $recipient->acceptFriendRequestFrom($sender);
252
253
        $this->assertFalse($bool);
254
        $this->dontSeeInDatabase('friends', [
255
            'sender_id' => $recipient->id,
256
            'recipient_id' => $sender->id,
257
        ]);
258
        $this->dontSeeInDatabase('friends', [
259
            'sender_id' => $sender->id,
260
            'recipient_id' => $recipient->id,
261
        ]);
262
263
        $this->assertCount(0, $sender->friends());
264
        $this->assertCount(0, $recipient->friends());
265
        $this->assertCount(0, $sender->any_friends());
266
        $this->assertCount(0, $recipient->any_friends());
267
    }
268
269
    /**
270
     * @test
271
     */
272
    public function a_friend_request_results_in_a_accepted_if_pending_request_is_available_from_recipient() {
273
        $sender = factory(User::class)->create();
274
        $recipient = factory(User::class)->create();
275
276
        $sender->sendFriendRequestTo($recipient);
277
        $recipient->sendFriendRequestTo($sender);
278
279
        $this->seeInDatabase('friends', [
280
            'sender_id' => $sender->id,
281
            'recipient_id' => $recipient->id,
282
            'friendship_status' => FriendshipStatus::ACCEPTED,
283
        ]);
284
        $this->dontSeeInDatabase('friends', [
285
            'sender_id' => $sender->id,
286
            'recipient_id' => $recipient->id,
287
            'friendship_status' => FriendshipStatus::PENDING,
288
        ]);
289
        $this->dontSeeInDatabase('friends', [
290
            'sender_id' => $recipient->id,
291
            'recipient_id' => $sender->id,
292
            'friendship_status' => FriendshipStatus::PENDING,
293
        ]);
294
295
        $this->assertCount(1, $sender->friends());
296
        $this->assertCount(1, $recipient->friends());
297
    }
298
299
    /**
300
     * @test
301
     */
302
    public function friends_method_returns_empty_array_after_friend_request() {
303
        $sender = factory(User::class)->create();
304
        $recipient = factory(User::class)->create();
305
306
        $sender->sendFriendRequestTo($recipient);
307
308
        $this->assertCount(0, $sender->friends());
309
        $this->assertCount(0, $recipient->friends());
310
    }
311
312
    /**
313
     * @test
314
     */
315
    public function friends_method_returns_correct_count_of_friends__2_requests_and_2_accepts() {
316
        $sender = factory(User::class)->create();
317
        $recipient1 = factory(User::class)->create();
318
        $recipient2 = factory(User::class)->create();
319
320
        $sender->sendFriendRequestTo($recipient1);
321
        $sender->sendFriendRequestTo($recipient2);
322
        $recipient1->acceptFriendRequestFrom($sender);
323
        $recipient2->acceptFriendRequestFrom($sender);
324
325
        $this->seeInDatabase('friends', [
326
            'sender_id' => $sender->id,
327
            'recipient_id' => $recipient1->id,
328
            'friendship_status' => FriendshipStatus::ACCEPTED,
329
        ]);
330
        $this->seeInDatabase('friends', [
331
            'sender_id' => $sender->id,
332
            'recipient_id' => $recipient2->id,
333
            'friendship_status' => FriendshipStatus::ACCEPTED,
334
        ]);
335
        $this->assertCount(2, $sender->friends());
336
        $this->assertCount(1, $recipient1->friends());
337
        $this->assertCount(1, $recipient2->friends());
338
    }
339
340
    /**
341
     * @test
342
     */
343
    public function friends_method_returns_correct_count_of_friends__50_requests_and_50_accepts() {
344
        $sender = factory(User::class)->create();
345
        $recipients = factory(User::class)->times(50)->create();
346
347
        foreach ($recipients as $recipient) {
0 ignored issues
show
The expression $recipients of type object<Illuminate\Databa...se\Eloquent\Collection> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
348
            $sender->sendFriendRequestTo($recipient);
349
            $recipient->acceptFriendRequestFrom($sender);
350
            $this->assertCount(1, $recipient->friends());
351
        }
352
353
        $this->assertCount(50, $sender->friends());
354
    }
355
356
    /**
357
     * @test
358
     */
359
    public function friends_method_returns_correct_count_of_friends__50_requests_and_25_accepts() {
360
        $sender = factory(User::class)->create();
361
        $recipients = factory(User::class)->times(50)->create();
362
363
        foreach ($recipients as $key => $recipient) {
0 ignored issues
show
The expression $recipients of type object<Illuminate\Databa...se\Eloquent\Collection> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
364
            $sender->sendFriendRequestTo($recipient);
365
            if ($key % 2) {
366
                $recipient->acceptFriendRequestFrom($sender);
367
                $this->assertCount(1, $recipient->friends());
368
                continue;
369
            }
370
            $this->assertCount(0, $recipient->friends());
371
        }
372
373
        $this->assertCount(25, $sender->friends());
374
    }
375
376
    /**
377
     * @test
378
     */
379
    public function friends_method_returns_correct_friends__2_requests_and_2_accepts() {
380
        $sender = factory(User::class)->create();
381
        $recipient0 = factory(User::class)->create();
382
        $recipient1 = factory(User::class)->create();
383
384
        $sender->sendFriendRequestTo($recipient0);
385
        $sender->sendFriendRequestTo($recipient1);
386
        $recipient0->acceptFriendRequestFrom($sender);
387
        $recipient1->acceptFriendRequestFrom($sender);
388
389
        $this->seeInDatabase('friends', [
390
            'sender_id' => $sender->id,
391
            'recipient_id' => $recipient0->id,
392
            'friendship_status' => FriendshipStatus::ACCEPTED,
393
        ]);
394
        $this->seeInDatabase('friends', [
395
            'sender_id' => $sender->id,
396
            'recipient_id' => $recipient1->id,
397
            'friendship_status' => FriendshipStatus::ACCEPTED,
398
        ]);
399
        $friends = $sender->friends();
400
        $this->assertEquals($recipient0->id, $friends[0]->id);
401
        $this->assertEquals($recipient1->id, $friends[1]->id);
402
    }
403
404
    /**
405
     * @test
406
     */
407
    public function any_friends_method_returns_1_user_after_friend_request() {
408
        $sender = factory(User::class)->create();
409
        $recipient = factory(User::class)->create();
410
411
        $sender->sendFriendRequestTo($recipient);
412
413
        $this->assertCount(1, $sender->any_friends());
414
        $this->assertCount(1, $recipient->any_friends());
415
    }
416
417
    /**
418
     * @test
419
     */
420
    public function any_friends_method_returns_correct_count_of_any_friends__50_requests_and_25_accepts() {
421
        $sender = factory(User::class)->create();
422
        $recipients = factory(User::class)->times(50)->create();
423
424
        foreach ($recipients as $key => $recipient) {
0 ignored issues
show
The expression $recipients of type object<Illuminate\Databa...se\Eloquent\Collection> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
425
            $sender->sendFriendRequestTo($recipient);
426
            if ($key % 2) {
427
                $recipient->acceptFriendRequestFrom($sender);
428
                $this->assertCount(1, $recipient->any_friends());
429
                continue;
430
            }
431
            $this->assertCount(1, $recipient->any_friends());
432
        }
433
434
        $this->assertCount(50, $sender->any_friends());
435
    }
436
437
    /**
438
     * @test
439
     */
440
    public function any_friends_method_returns_correct_count_of_any_friends__50_requests_and_25_accepts_25_denies() {
441
        $sender = factory(User::class)->create();
442
        $recipients = factory(User::class)->times(50)->create();
443
444
        foreach ($recipients as $key => $recipient) {
0 ignored issues
show
The expression $recipients of type object<Illuminate\Databa...se\Eloquent\Collection> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
445
            $sender->sendFriendRequestTo($recipient);
446
            if ($key % 2) {
447
                $recipient->acceptFriendRequestFrom($sender);
448
                $this->assertCount(1, $recipient->any_friends());
449
                continue;
450
            }
451
            $recipient->denyFriendRequestFrom($sender);
452
            $this->assertCount(0, $recipient->any_friends());
453
        }
454
455
        $this->assertCount(25, $sender->any_friends());
456
    }
457
458
    /**
459
     * @test
460
     */
461
    public function incoming_friends_method_returns_1_user_after_friend_request() {
462
        $sender = factory(User::class)->create();
463
        $recipient = factory(User::class)->create();
464
465
        $sender->sendFriendRequestTo($recipient);
466
467
        $this->assertCount(0, $sender->incoming_friends());
468
        $this->assertCount(1, $recipient->incoming_friends());
469
    }
470
471
    /**
472
     * @test
473
     */
474
    public function incoming_friends_method_returns_correct_count_of_any_friends__50_requests_and_25_accepts() {
475
        $recipient = factory(User::class)->create();
476
        $senders = factory(User::class)->times(50)->create();
477
478
        foreach ($senders as $key => $sender) {
0 ignored issues
show
The expression $senders of type object<Illuminate\Databa...se\Eloquent\Collection> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
479
            $sender->sendFriendRequestTo($recipient);
480
            if ($key % 2) {
481
                $recipient->acceptFriendRequestFrom($sender);
482
                continue;
483
            }
484
        }
485
486
        $this->assertCount(25, $recipient->incoming_friends());
487
    }
488
}