|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Log; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use Ecodev\Felix\Log\RecordCompleter; |
|
9
|
|
|
use Ecodev\Felix\Model\CurrentUser; |
|
10
|
|
|
use Ecodev\Felix\Model\User; |
|
11
|
|
|
use Monolog\Level; |
|
12
|
|
|
use Monolog\LogRecord; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
|
|
15
|
|
|
class EventCompleterTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
protected function tearDown(): void |
|
18
|
|
|
{ |
|
19
|
|
|
CurrentUser::set(null); |
|
20
|
|
|
$_REQUEST = []; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testProcessMinimal(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$completed = new RecordCompleter('https://example.com'); |
|
26
|
|
|
$completedRecord = $completed->__invoke( |
|
27
|
|
|
new LogRecord( |
|
28
|
|
|
new DateTimeImmutable(), |
|
29
|
|
|
'', |
|
30
|
|
|
Level::Error, |
|
31
|
|
|
'some message', |
|
32
|
|
|
), |
|
33
|
|
|
); |
|
34
|
|
|
$actual = $completedRecord->extra; |
|
35
|
|
|
|
|
36
|
|
|
self::assertNull($actual['creator_id']); |
|
37
|
|
|
self::assertSame('<anonymous>', $actual['login']); |
|
38
|
|
|
self::assertIsString($actual['url']); |
|
39
|
|
|
self::assertIsString($actual['referer']); |
|
40
|
|
|
self::assertIsString($actual['request']); |
|
41
|
|
|
self::assertSame('script', $actual['ip']); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testProcess(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$user = $this->createMock(User::class); |
|
47
|
|
|
$user->expects(self::once()) |
|
48
|
|
|
->method('getId') |
|
49
|
|
|
->willReturn(123); |
|
50
|
|
|
|
|
51
|
|
|
$user->expects(self::once()) |
|
52
|
|
|
->method('getLogin') |
|
53
|
|
|
->willReturn('my login'); |
|
54
|
|
|
|
|
55
|
|
|
CurrentUser::set($user); |
|
56
|
|
|
putenv('REMOTE_ADDR=127.0.0.1'); |
|
57
|
|
|
$_REQUEST = [ |
|
58
|
|
|
'password' => 'sensitive', |
|
59
|
|
|
'variables' => [ |
|
60
|
|
|
'other' => [ |
|
61
|
|
|
'password' => 'sensitive', |
|
62
|
|
|
'passwordConfirmation' => 'sensitive', |
|
63
|
|
|
'foo' => 123, |
|
64
|
|
|
], |
|
65
|
|
|
], |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
$completed = new RecordCompleter('https://example.com'); |
|
69
|
|
|
$completedRecord = $completed->__invoke( |
|
70
|
|
|
new LogRecord( |
|
71
|
|
|
new DateTimeImmutable(), |
|
72
|
|
|
'', |
|
73
|
|
|
Level::Error, |
|
74
|
|
|
'some message', |
|
75
|
|
|
[ |
|
76
|
|
|
'errno' => 1, |
|
77
|
|
|
'password' => 'sensitive', |
|
78
|
|
|
], |
|
79
|
|
|
), |
|
80
|
|
|
); |
|
81
|
|
|
$actual = $completedRecord->extra; |
|
82
|
|
|
|
|
83
|
|
|
self::assertSame([ |
|
84
|
|
|
'errno' => 1, |
|
85
|
|
|
'password' => '***REDACTED***', |
|
86
|
|
|
], $completedRecord->context); |
|
87
|
|
|
self::assertSame(123, $actual['creator_id']); |
|
88
|
|
|
self::assertSame('my login', $actual['login']); |
|
89
|
|
|
self::assertIsString($actual['url']); |
|
90
|
|
|
self::assertIsString($actual['referer']); |
|
91
|
|
|
self::assertSame([ |
|
92
|
|
|
'password' => '***REDACTED***', |
|
93
|
|
|
'variables' => [ |
|
94
|
|
|
'other' => [ |
|
95
|
|
|
'password' => '***REDACTED***', |
|
96
|
|
|
'passwordConfirmation' => '***REDACTED***', |
|
97
|
|
|
'foo' => 123, |
|
98
|
|
|
], |
|
99
|
|
|
], |
|
100
|
|
|
], json_decode($actual['request'], true)); |
|
101
|
|
|
self::assertSame('127.0.0.1', $actual['ip']); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|