Passed
Push — master ( c9be10...b13c48 )
by William
19:22
created

ReportTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 194
c 3
b 0
f 1
dl 0
loc 262
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testUserId() 0 54 1
A setUp() 0 4 1
B testReportRoutingSystem() 0 83 1
B testReportFromString() 0 99 1
A testReportFromObject() 0 10 1
1
<?php
2
3
namespace App\Test;
4
5
use App\Report;
6
use Cake\Utility\Security;
7
use PHPUnit\Framework\TestCase;
8
9
use function file_get_contents;
10
use function json_decode;
11
12
class ReportTest extends TestCase
13
{
14
    public function setUp(): void
15
    {
16
        Security::setSalt(
17
            'Ue%n8*v*QPejgq*v2^k5JSEMXx9Cer*jConpbjp&&vGK89jGRVBnyWaPBhY5s$87'
18
        );
19
    }
20
21
    public function testReportFromObject(): void
22
    {
23
        $contents = file_get_contents(
24
            __DIR__ . '/../Fixture/report_js.json'
25
        );
26
        $obj = json_decode($contents);
27
        $report1 = Report::fromObject($obj);
28
        $report2 = Report::fromString($contents);
29
30
        $this->assertEquals($report1, $report2);
31
    }
32
33
    public function testUserId(): void
34
    {
35
        $contents = file_get_contents(
36
            __DIR__ . '/../Fixture/report_issue_16853_js.json'
37
        );
38
        $report = Report::fromString($contents);
39
40
        $tags = $report->getTags();
41
42
        $this->assertEquals((object) [
43
            'server_software' => 'nginx/1.14.2',
44
            'user_agent_string' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0',
45
            'locale' => 'en',
46
            'configuration_storage' => false,
47
            'php_version' => '7.4.14',
48
            'exception_type' => 'js',
49
        ], $tags);
50
        $this->assertEquals(
51
            (object) [
52
                'ip_address' => '0.0.0.0',
53
                'id' => '99a62784e874c1a5b4625660a94c736bbb8ca46f53d321ab8ce700050e30cb97',
54
            ],
55
            $report->getUser()
56
        );
57
58
        $oldForwardedValue = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null;
59
60
        $_SERVER['HTTP_X_FORWARDED_FOR'] = '1.1.1.1';
61
62
        $this->assertEquals(
63
            (object) [
64
                'ip_address' => '0.0.0.0',
65
                'id' => '747901a7736cb599fefec3b52bb939a511fb7662a66ba30e6049b128f2114cec',
66
            ],
67
            $report->getUser()
68
        );
69
        $_SERVER['HTTP_X_FORWARDED_FOR'] = $oldForwardedValue;
70
        $this->assertEquals(
71
            (object) [
72
                'ip_address' => '0.0.0.0',
73
                'id' => '99a62784e874c1a5b4625660a94c736bbb8ca46f53d321ab8ce700050e30cb97',
74
            ],
75
            $report->getUser()
76
        );
77
        Security::setSalt(
78
            'N8KB3iKch@xT4nP$WcPoF!9fwxtT7pNY5Hm4*Ld6G^Ux3tQ57iJzPYumu@jXVzue'
79
        );
80
81
        $this->assertEquals(
82
            (object) [
83
                'ip_address' => '0.0.0.0',
84
                'id' => '883a06cb74b037abf70b14ddd6f5518b04c10031648b9af72bc24ddb97fbfd52',
85
            ],
86
            $report->getUser()
87
        );
88
    }
89
90
    public function testReportRoutingSystem(): void
91
    {
92
        $contents = file_get_contents(
93
            __DIR__ . '/../Fixture/report_issue_16853_js.json'
94
        );
95
        $report = Report::fromString($contents);
96
97
        $tags = $report->getTags();
98
        $contexts = $report->getContexts();
99
        $extras = $report->getExtras();
100
        $reports = $report->getReports();
101
        $user = $report->getUser();
102
        $message = $report->getUserMessage();
103
        $toJson = $report->toJson();
104
105
        $this->assertEquals((object) [
106
            'server_software' => 'nginx/1.14.2',
107
            'user_agent_string' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0',
108
            'locale' => 'en',
109
            'configuration_storage' => false,
110
            'php_version' => '7.4.14',
111
            'exception_type' => 'js',
112
        ], $tags);
113
        $this->assertEquals((object) [
114
            'os' => (object) ['name' => 'Win'],
115
            'browser' => (object) [
116
                'name' => 'FIREFOX',
117
                'version' => '90.0',
118
            ],
119
        ], $contexts);
120
        $this->assertEquals((object) [], $extras);
121
        $this->assertEquals((object) ['message' => ''], $message);
122
        $this->assertEquals(
123
            (object) [
124
                'ip_address' => '0.0.0.0',
125
                'id' => '99a62784e874c1a5b4625660a94c736bbb8ca46f53d321ab8ce700050e30cb97',
126
            ],
127
            $user
128
        );
129
        $this->assertFalse($report->isMultiReports());
130
        $this->assertFalse($report->hasUserFeedback());
131
        $this->assertEquals([
132
            [
133
                'sentry.interfaces.Message' => $message,
134
                'release' => '5.2.0-dev',
135
                'dist' => '5.2.0-dev',
136
                'platform' => 'javascript',
137
                'environment' => 'development',
138
                'transaction' => '/database/designer',
139
                'timestamp' => $report->getTimestampUTC(),
140
                'tags' => $tags,
141
                'contexts' => $contexts,
142
                'extra' => $extras,
143
                'user' => $user,
144
                'exception' => [
145
                    'values' => [
146
                        (object) [
147
                            'type' => 'TypeError',
148
                            'value' => 'can\'t access property "transaction", db is null',
149
                            'stacktrace' => (object) [
150
                                'frames' => $reports[0]['exception']['values'][0]->stacktrace->frames,
151
                            ],
152
                        ],
153
                    ],
154
                ],
155
                'message' => 'can\'t access property "transaction", db is null',
156
                'culprit' => 'index.php',
157
                'event_id' => $report->getEventId(),
158
            ],
159
        ], $reports);
160
        $this->assertEquals([
161
            'sentry.interfaces.Message' => $message,
162
            'release' => '5.2.0-dev',
163
            'dist' => '5.2.0-dev',
164
            'platform' => 'javascript',
165
            'environment' => 'development',
166
            'transaction' => '/database/designer',
167
            'timestamp' => $report->getTimestampUTC(),
168
            'tags' => $tags,
169
            'contexts' => $contexts,
170
            'extra' => $extras,
171
            'user' => $user,
172
        ], $toJson);
173
    }
174
175
    public function testReportFromString(): void
176
    {
177
        $contents = file_get_contents(
178
            __DIR__ . '/../Fixture/report_js.json'
179
        );
180
        $report = Report::fromString($contents);
181
182
        $tags = $report->getTags();
183
        $contexts = $report->getContexts();
184
        $extras = $report->getExtras();
185
        $reports = $report->getReports();
186
        $user = $report->getUser();
187
        $message = $report->getUserMessage();
188
        $toJson = $report->toJson();
189
190
        $this->assertEquals((object) [
191
            'server_software' => 'NginX/1.17 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.1e DAV/2 PHP/5.4.17',
192
            'user_agent_string' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
193
            'locale' => 'en',
194
            'configuration_storage' => true,
195
            'php_version' => '5.2.2',
196
            'exception_type' => null,
197
        ], $tags);
198
        $this->assertEquals((object) [
199
            'os' => (object) ['name' => 'Windows'],
200
            'browser' => (object) [
201
                'name' => 'FIREFOX',
202
                'version' => '17.5',
203
            ],
204
        ], $contexts);
205
        $this->assertEquals((object) [], $extras);
206
        $this->assertEquals((object) ['message' => ''], $message);
207
        $this->assertEquals(
208
            (object) [
209
                'ip_address' => '0.0.0.0',
210
                'id' => '90244836bc888d4b4bb8449010dba862ebc5a8905a79da8269b4e76d08831226',
211
            ],
212
            $user
213
        );
214
        $this->assertFalse($report->isMultiReports());
215
        $this->assertTrue($report->hasUserFeedback());
216
        $this->assertSame('<script>test steps', $report->getUserFeedback());
217
        $this->assertEquals([
218
            [
219
                'sentry.interfaces.Message' => $message,
220
                'release' => '4.5.4.1',
221
                'dist' => '4.5.4.1deb2ubuntu2',
222
                'platform' => 'javascript',
223
                'environment' => 'production',
224
                'timestamp' => $report->getTimestampUTC(),
225
                'tags' => $tags,
226
                'contexts' => $contexts,
227
                'extra' => $extras,
228
                'user' => $user,
229
                'exception' => [
230
                    'values' => [
231
                        (object) [
232
                            'type' => 'ReferenceError',
233
                            'value' => 'a is not defined',
234
                            'stacktrace' => (object) [
235
                                'frames' => [
236
                                    [
237
                                        'platform' => 'javascript',
238
                                        'function' => 'PMA_exception',
239
                                        'lineno' => 312,
240
                                        'colno' => 5,
241
                                        'abs_path' => '',
242
                                        'filename' => '',
243
                                    ],
244
                                    [
245
                                        'platform' => 'javascript',
246
                                        'function' => 'new_func',
247
                                        'lineno' => 257,
248
                                        'colno' => 33,
249
                                        'abs_path' => '',
250
                                        'filename' => '',
251
                                    ],
252
                                ],
253
                            ],
254
                        ],
255
                    ],
256
                ],
257
                'message' => 'a is not defined',
258
                'culprit' => 'tbl_relation.php',
259
                'event_id' => $report->getEventId(),
260
            ],
261
        ], $reports);
262
        $this->assertEquals([
263
            'sentry.interfaces.Message' => $message,
264
            'release' => '4.5.4.1',
265
            'dist' => '4.5.4.1deb2ubuntu2',
266
            'platform' => 'javascript',
267
            'timestamp' => $report->getTimestampUTC(),
268
            'tags' => $tags,
269
            'contexts' => $contexts,
270
            'extra' => $extras,
271
            'user' => $user,
272
            'environment' => 'production',
273
        ], $toJson);
274
    }
275
}
276