File size: 17,922 Bytes
a7a7ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CustomAlerts\tests\Integration;

use Piwik\Cache as PiwikCache;
use Piwik\Container\StaticContainer;
use Piwik\Plugin;
use Piwik\Plugins\CustomAlerts\Controller;
use Piwik\SettingsPiwik;
use Piwik\Tests\Framework\Fixture;
use Piwik\Version;

class CustomController extends Controller
{
    public function __construct()
    {
        parent::__construct(StaticContainer::get('Piwik\Plugins\API\ProcessedReport'));
    }

    public function enrichTriggeredAlerts($triggeredAlerts)
    {
        return parent::enrichTriggeredAlerts($triggeredAlerts);
    }
}

/**
 * @group CustomAlerts
 * @group ControllerTest
 * @group Plugins
 */
class ControllerTest extends BaseTest
{
    /**
     * @var CustomController
     */
    private $controller;

    public function setUp(): void
    {
        parent::setUp();

        // make sure templates will be found
        Plugin\Manager::getInstance()->loadPlugin('CustomAlerts');
        Plugin\Manager::getInstance()->loadPlugin('Morpheus');

        PiwikCache::flushAll();

        Fixture::loadAllTranslations();

        $this->controller = new CustomController();
    }

    public function tearDown(): void
    {
        parent::tearDown();

        Fixture::resetTranslations();
    }

    public function test_formatAlerts_asText()
    {
        $alerts = $this->getTriggeredAlerts();

        $host = SettingsPiwik::getPiwikUrl();

        $expected = <<<FORMATTED
MyName1 has been triggered as the metric Visits in report Single Website dashboard decreased more than 5000 from 228.128 to 4493.
>> Edit Alert {$host}index.php?module=CustomAlerts&action=editAlert&idAlert=1&idSite=1&period=week&date=yesterday

MyName2 has been triggered as the metric Visits in report Single Website dashboard decreased more than 5000 from 228.128 to 4493.
>> Edit Alert {$host}index.php?module=CustomAlerts&action=editAlert&idAlert=2&idSite=1&period=week&date=yesterday


FORMATTED;

        $rendered = $this->controller->formatAlerts($alerts, 'text');

        $this->assertEquals($expected, $rendered);
    }

    /**
     * @return array
     */
    private function getTriggeredAlerts()
    {
        return array(
            $this->buildAlert(1, 'MyName1'),
            $this->buildAlert(2, 'MyName2'),
        );
    }

    private function buildAlert(
        $id,
        $name,
        $period = 'week',
        $idSite = 1,
        $siteName = 'Piwik test',
        $login = 'superUserLogin',
        $metric = 'nb_visits',
        $metricCondition = 'decrease_more_than',
        $metricMatched = 5000,
        $report = 'MultiSites_getOne',
        $reportCondition = 'matches_exactly',
        $reportMatched = 'Piwik'
    ) {
        return array(
            'idalert'           => $id,
            'idsite'            => $idSite,
            'name'              => $name,
            'period'            => $period,
            'login'             => $login,
            'report'            => $report,
            'report_condition'  => $reportCondition,
            'report_matched'    => $reportMatched,
            'metric'            => $metric,
            'metric_condition'  => $metricCondition,
            'metric_matched'    => $metricMatched,
            'additional_emails' => array('test5@example.com'),
            'phone_numbers'     => array('+1234567890', '232'),
            'email_me'          => 1,
            'value_new'         => '4493.000',
            'value_old'         => '228.128',
            'ts_triggered'      => time()
        );
    }

    public function test_formatAlerts_asSms()
    {
        $alerts = $this->getTriggeredAlerts();

        $expected = <<<FORMATTED
MyName1 has been triggered for website Piwik test as the metric Visits in report Single Website dashboard decreased more than 5000 from 228.128 to 4493. MyName2 has been triggered for website Piwik test as the metric Visits in report Single Website dashboard decreased more than 5000 from 228.128 to 4493.
FORMATTED;

        $rendered = $this->controller->formatAlerts($alerts, 'sms');

        $this->assertEquals($expected, $rendered);
    }

    public function test_formatAlerts_ShouldThrowException_IfInvalidFormatGiven()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Unsupported format');

        $alerts = $this->getTriggeredAlerts();
        $this->controller->formatAlerts($alerts, 'php');
    }

    public function test_formatAlerts_asHtml()
    {
        $alerts = $this->getTriggeredAlerts();

        $host = SettingsPiwik::getPiwikUrl();

        $rendered = $this->controller->formatAlerts($alerts, 'html');

        $expected = <<<FORMATTED
<table style='width:100%;border-collapse: collapse; border:1px solid rgb(231,231,231); padding:5px;  margin:30px 0;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; ' class="tableForm dataTable card entityTable card-table">
    <thead style="background-color:#f2f2f2;color:#0d0d0d;">
    <tr>
        <th style="padding:13px 0 13px 10px;text-align: left;font-weight:normal;font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; ">Alert Name</th>
                <th style="padding:13px 0 13px 10px;text-align: left;font-weight:normal;font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; ">Report</th>
        <th style="padding:13px 0 13px 10px;text-align: left;font-weight:normal;font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; ">Alert Condition</th>
        <th style="padding:13px 0 13px 10px;text-align: left;font-weight:normal;font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; ">Alert</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;"><a style="color:#439fe0; text-decoration:none;" href="{$host}index.php?module=CustomAlerts&action=editAlert&idAlert=1&idSite=1&period=week&date=yesterday">MyName1</a></td>
                <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;">Single Website dashboard</td>
        <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;">Website is 'Piwik'</td>
        <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;padding:17px 10px;">Visits decreased more than 5000 from 228.128 to 4493</td>
    </tr>

    <tr>
        <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;"><a style="color:#439fe0; text-decoration:none;" href="{$host}index.php?module=CustomAlerts&action=editAlert&idAlert=2&idSite=1&period=week&date=yesterday">MyName2</a></td>
                <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;">Single Website dashboard</td>
        <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;">Website is 'Piwik'</td>
        <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;padding:17px 10px;">Visits decreased more than 5000 from 228.128 to 4493</td>
    </tr>

    </tbody>
</table>

FORMATTED;

        $this->assertEquals($expected, $rendered, "Got following HTML response: " . var_export($rendered, true));
    }

    public function test_formatAlertsNoConditions_asHtml()
    {
        $alerts = array(
            $this->buildAlert(1, 'My Alert', 'week', 1, 'Piwik Site', 'superUserLogin', 'nb_visits', 'decrease_more_than', 5000, 'MultiSites_getOne', null, null)
        );

        $host = SettingsPiwik::getPiwikUrl();

        $rendered = $this->controller->formatAlerts($alerts, 'html');

        $expected = <<<FORMATTED
<table style='width:100%;border-collapse: collapse; border:1px solid rgb(231,231,231); padding:5px;  margin:30px 0;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; ' class="tableForm dataTable card entityTable card-table">
    <thead style="background-color:#f2f2f2;color:#0d0d0d;">
    <tr>
        <th style="padding:13px 0 13px 10px;text-align: left;font-weight:normal;font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; ">Alert Name</th>
                <th style="padding:13px 0 13px 10px;text-align: left;font-weight:normal;font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; ">Report</th>
        <th style="padding:13px 0 13px 10px;text-align: left;font-weight:normal;font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; ">Alert</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;"><a style="color:#439fe0; text-decoration:none;" href="{$host}index.php?module=CustomAlerts&action=editAlert&idAlert=1&idSite=1&period=week&date=yesterday">My Alert</a></td>
                <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;">Single Website dashboard</td>
        <td style="border-bottom:1px solid rgb(231,231,231);font-size: 15px;color:#0d0d0d;font-family:-apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Cantarell, &quot;Helvetica Neue&quot;, sans-serif; padding:17px 0 17px 10px;padding:17px 10px;">Visits decreased more than 5000 from 228.128 to 4493</td>
    </tr>

    </tbody>
</table>

FORMATTED;

        $this->assertEquals($expected, $rendered, "Got following HTML response: " . var_export($rendered, true));
    }

    public function test_formatAlerts_asHtml_shouldEscapeReportMatched()
    {
        $payload = '<img src=x onerror=alert(1)>';
        $alerts = array(
            $this->buildAlert(1, 'MyName1', 'week', 1, 'Piwik test', 'superUserLogin', 'nb_visits', 'decrease_more_than', 5000, 'MultiSites_getOne', 'matches_exactly', $payload)
        );

        $rendered = $this->controller->formatAlerts($alerts, 'html');

        $this->assertStringContainsString("Website is '&lt;img src=x onerror=alert(1)&gt;'", $rendered);
        $this->assertStringNotContainsString("'{$payload}'", $rendered);
        $this->assertStringNotContainsString("<img src=x onerror=alert(1)>", $rendered);
    }

    public function test_enrichTriggeredAlerts_shouldEnrichAlerts_IfReportExistsAndMetricIsValid()
    {
        $timestamp = 1389824417;
        $alerts    = array(
            array(
                'idsite'           => 1,
                'period'           => 'day',
                'ts_triggered'     => $timestamp,
                'metric'           => 'nb_visits',
                'report'           => 'MultiSites_getAll',
                'report_condition' => 'matches_any',
                'value_old'        => '228.001',
                'value_new'        => '1.0'
            ),
            array(
                'idsite'           => 1,
                'period'           => 'week',
                'ts_triggered'     => $timestamp,
                'metric'           => 'nb_visits',
                'report'           => 'NotExistingModule_Action',
                'report_condition' => 'matches_exactly',
                'value_old'        => '228.000',
                'value_new'        => '1.0'
            ),
            array(
                'idsite'           => 1,
                'period'           => 'month',
                'ts_triggered'     => $timestamp,
                'metric'           => 'bounce_rate',
                'report'           => 'Actions_getPageUrls',
                'report_condition' => 'matches_exactly',
                'value_old'        => '228.999',
                'value_new'        => '1.0'
            ),
            array(
                'idsite'           => 1,
                'period'           => 'day',
                'ts_triggered'     => $timestamp,
                'metric'           => 'not_valid',
                'report'           => 'Actions_getPageUrls',
                'report_condition' => 'contains',
                'value_old'        => '228.001',
                'value_new'        => '1.01'
            ),
            // no dimension ,
            array(
                'idsite'           => 1,
                'period'           => 'day',
                'ts_triggered'     => $timestamp,
                'metric'           => 'nb_visits',
                'report'           => 'VisitsSummary_get',
                'report_condition' => 'matches_any',
                'value_old'        => '228.001',
                'value_new'        => '10'
            )
        );

        $enriched = $this->controller->enrichTriggeredAlerts($alerts);


        $alerts[0]['reportName']          = 'All Websites dashboard';
        $alerts[0]['reportMetric']        = 'Visits';
        $alerts[0]['dimension']           = 'Website';
        $alerts[0]['reportConditionName'] = 'matches any';
        $alerts[0]['value_old']           = '228.001';
        $alerts[0]['value_new']           = 1;
        $alerts[0]['siteName']            = 'Piwik test';
        $alerts[0]['ts_triggered']        = 'Tue, Jan 14';
        self::assertIsInt($alerts[0]['value_new']);
        $alerts[1]['reportName']          = null;
        $alerts[1]['reportMetric']        = null;
        $alerts[1]['dimension']           = null;
        $alerts[1]['reportConditionName'] = null;
        $alerts[1]['value_old']           = 228;
        $alerts[1]['value_new']           = 1;
        $alerts[1]['siteName']            = 'Piwik test';
        $alerts[1]['ts_triggered']        = 'Jan 6 – 12, 2014';
        if (version_compare(Version::VERSION, '4.13.0-rc1', '<')) {
            // intl format changed in 4.13.0
            $alerts[1]['ts_triggered'] = 'Jan 6 – 12, 2014';
        }
        self::assertIsInt($alerts[1]['value_old']);
        self::assertIsInt($alerts[1]['value_new']);
        $alerts[2]['reportName']          = 'Page URLs';
        $alerts[2]['reportMetric']        = 'Bounce Rate';
        $alerts[2]['dimension']           = 'Page URL';
        $alerts[2]['reportConditionName'] = 'is';
        $alerts[2]['value_old']           = '228.999';
        $alerts[2]['value_new']           = '1';
        $alerts[2]['siteName']            = 'Piwik test';
        $alerts[2]['ts_triggered']        = 'Dec 2013';
        $alerts[3]['reportName']          = 'Page URLs';
        $alerts[3]['reportMetric']        = null;
        $alerts[3]['dimension']           = 'Page URL';
        $alerts[3]['reportConditionName'] = 'contains';
        $alerts[3]['value_old']           = '228.001';
        $alerts[3]['value_new']           = '1.01';
        $alerts[3]['siteName']            = 'Piwik test';
        $alerts[3]['ts_triggered']        = 'Tue, Jan 14';
        $alerts[4]['reportName']          = 'Visits Summary';
        $alerts[4]['reportMetric']        = 'Visits';
        $alerts[4]['dimension']           = null;
        $alerts[4]['reportConditionName'] = 'matches any';
        $alerts[4]['value_old']           = '228.001';
        $alerts[4]['value_new']           = 10;
        $alerts[4]['siteName']            = 'Piwik test';
        $alerts[4]['ts_triggered']        = 'Tue, Jan 14';
        self::assertIsInt($alerts[4]['value_new']);

        $this->assertEquals($alerts, $enriched);
    }
}