ReportTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 203
dl 0
loc 273
rs 10
c 5
b 0
f 1
wmc 5

5 Methods

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