IncidentsControllerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 11
Bugs 1 Features 0
Metric Value
eloc 98
dl 0
loc 153
rs 10
c 11
b 1
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testCreate() 0 76 1
A testJson() 0 38 1
A testView() 0 10 1
1
<?php
2
3
namespace App\Test\TestCase\Controller;
4
5
use App\Model\Table\IncidentsTable;
6
use Cake\Core\Configure;
7
use Cake\ORM\TableRegistry;
8
use Cake\TestSuite\IntegrationTestTrait;
9
use Cake\TestSuite\TestCase;
10
11
use function count;
12
use function file_get_contents;
13
use function json_decode;
14
15
use const DS;
16
17
class IncidentsControllerTest extends TestCase
18
{
19
    use IntegrationTestTrait;
20
21
    /**
22
     * Fixtures.
23
     *
24
     * @var array
25
     */
26
    public $fixtures = [
27
        'app.Notifications',
28
        'app.Developers',
29
        'app.Reports',
30
        'app.Incidents',
31
    ];
32
33
    public function setUp(): void
34
    {
35
        $this->Incidents = TableRegistry::getTableLocator()->get('Incidents');
36
        //$Session = new SessionComponent(new ComponentRegistry());
37
        $this->session(['Developer.id' => 1]);
38
        $this->Reports = TableRegistry::getTableLocator()->get('Reports');
39
    }
40
41
    public function testView(): void
42
    {
43
        $this->get('/incidents/view/1');
44
45
        $this->assertNotEmpty($this->viewVariable('incident'));
46
        $this->assertIsArray(
47
            $this->viewVariable('incident')['stacktrace']
48
        );
49
        $this->assertIsArray(
50
            $this->viewVariable('incident')['full_report']
51
        );
52
    }
53
54
    public function testJson(): void
55
    {
56
        $this->get('/incidents/json/1');
57
        $incident = json_decode($this->_response->getBody(), true);
58
        $expected = [
59
            'id' => 1,
60
            'error_name' => 'Lorem ipsum dolor sit amet',
61
            'error_message' => 'Lorem ipsum dolor sit amet',
62
            'pma_version' => 'Lorem ipsum dolor sit amet',
63
            'php_version' => '5.5',
64
            'browser' => 'Lorem ipsum dolor sit amet',
65
            'user_os' => 'Lorem ipsum dolor sit amet',
66
            'locale' => 'Lorem ipsum dolor sit amet',
67
            'server_software' => 'Lorem ipsum dolor sit amet',
68
            'stackhash' => 'hash1',
69
            'configuration_storage' => 'Lorem ipsum dolor sit amet',
70
            'script_name' => 'Lorem ipsum dolor sit amet',
71
            'steps' => 'Lorem ipsum dolor sit amet',
72
            'stacktrace' => [['context' => ['test']]],
73
            'full_report' => [
74
                'pma_version' => '',
75
                'php_version' => '',
76
                'browser_name' => '',
77
                'browser_version' => '',
78
                'user_agent_string' => '',
79
                'server_software' => '',
80
                'locale' => '',
81
                'exception' => ['uri' => ''],
82
                'configuration_storage' => '',
83
                'microhistory' => '',
84
            ],
85
            'report_id' => 1,
86
            'created' => '2013-08-29T18:10:01+00:00',
87
            'modified' => '2013-08-29T18:10:01+00:00',
88
            'exception_type' => null,
89
        ];
90
91
        $this->assertEquals($expected, $incident);
92
    }
93
94
    public function testCreate(): void
95
    {
96
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_js.json');
97
        $bugReportDecoded = json_decode($bugReport, true);
98
        $this->configRequest(['input' => $bugReport]);
99
        $this->post('/incidents/create');
100
101
        $report = $this->Reports->find(
102
            'all',
103
            ['order' => 'Reports.created desc']
104
        )->all()->first();
105
        $subject = 'A new report has been submitted '
106
            . 'on the Error Reporting Server: '
107
            . $report['id'];
108
        $this->assertEquals('4.5.4.1', $report['pma_version']);
109
110
        //FIXME: test email sending
111
        // Test notification email
112
        //$emailContent = Configure::read('test_transport_email');
113
114
        //$this->assertEquals(Configure::read('NotificationEmailsFrom'), $emailContent['headers']['From']);
115
        //$this->assertEquals(Configure::read('NotificationEmailsTo'), $emailContent['headers']['To']);
116
        //$this->assertEquals($subject, $emailContent['headers']['Subject']);
117
118
        //Configure::write('test_transport_email', null);
119
120
        $this->post('/incidents/create');
121
122
        $report = $this->Reports->find(
123
            'all',
124
            ['order' => 'Reports.created desc']
125
        )->all()->first();
126
        $this->Reports->id = $report['id'];
127
        $incidents = $this->Reports->getIncidents();
128
        $incidents = $incidents->enableHydration(false)->toArray();
129
        $this->assertEquals(2, count($incidents));
130
        $this->assertEquals(
131
            $bugReportDecoded['exception']['message'],
132
            $report['error_message']
133
        );
134
        $this->assertEquals(
135
            $bugReportDecoded['exception']['name'],
136
            $report['error_name']
137
        );
138
        $this->assertEquals(
139
            IncidentsTable::getStrippedPmaVersion($bugReportDecoded['pma_version']),
140
            $report['pma_version']
141
        );
142
143
        $this->configRequest(['input' => '']);
144
        $this->post('/incidents/create');
145
        $result = json_decode($this->_response->getBody(), true);
146
        $this->assertNotNull($result);
147
        $this->assertEquals(false, $result['success']);
148
149
        // Test invalid Notification email configuration
150
        Configure::write('NotificationEmailsTo', '');
151
152
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_php.json');
153
        $bugReportDecoded = json_decode($bugReport, true);
154
        $this->configRequest(['input' => $bugReport]);
155
        $this->post('/incidents/create');
156
157
        $report = $this->Reports->find(
158
            'all',
159
            ['order' => 'Reports.created desc']
160
        )->all()->first();
161
        $subject = 'A new report has been submitted '
162
            . 'on the Error Reporting Server: '
163
            . $report['id'];
164
        $this->assertEquals('4.5.4.1', $report['pma_version']);
165
166
        $emailContent = Configure::read('test_transport_email');
167
168
        // Since no email sent
169
        $this->assertEquals(null, $emailContent);
170
    }
171
}
172