File size: 9,344 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
<?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 PHPMailer\PHPMailer\PHPMailer;
use Piwik\Date;
use Piwik\Mail;
use Piwik\Plugin;
use Piwik\Plugins\CustomAlerts\Notifier;
use Piwik\Tests\Framework\Fixture;
use Piwik\Version;

class CustomNotifier extends Notifier
{
    private $alerts = array();

    public function setTriggeredAlerts($alerts)
    {
        $this->alerts = $alerts;
    }

    public function sendAlertsPerEmailToRecipient($alerts, \Piwik\Mail $mail, $recipient, $period, $idSite)
    {
        parent::sendAlertsPerEmailToRecipient($alerts, $mail, $recipient, $period, $idSite);
    }

    public function sendAlertsPerSmsToRecipient($alerts, $mobileMessagingAPI, $phoneNumber)
    {
        parent::sendAlertsPerSmsToRecipient($alerts, $mobileMessagingAPI, $phoneNumber);
    }

    protected function getToday()
    {
        return Date::factory('2010-01-01');
    }

    protected function getTriggeredAlerts($period, $idSite)
    {
        return $this->alerts;
    }
}

/**
 * @group CustomAlerts
 * @group NotifierTest
 * @group Plugins
 */
class NotifierTest extends BaseTest
{
    /**
     * @var CustomNotifier
     */
    private $notifier;

    /**
     * @var PHPMailer
     */
    private $mail;

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

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

        Fixture::loadAllTranslations();

        \Piwik\Plugins\UsersManager\API::getInstance()->addUser('login1', 'p2kK2msAw1', 'test1@example.com');
        \Piwik\Plugins\UsersManager\API::getInstance()->addUser('login2', 'p2kK2msAw1', 'test2@example.com');
        \Piwik\Plugins\UsersManager\API::getInstance()->addUser('login3', 'p2kK2msAw1', 'test3@example.com');

        $this->notifier = new CustomNotifier();
    }

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

        Fixture::resetTranslations();
    }

    public function test_sendAlertsPerEmailToRecipient()
    {
        $alerts = $this->getTriggeredAlerts();
        $mail   = new Mail();

        $this->notifier->sendAlertsPerEmailToRecipient($alerts, $mail, 'test@example.com', 'day', 1);

        $expectedHtml = <<<HTML
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html style=3D"background-color:#edecec">

<head>
    <meta charset=3D"utf-8">
    <meta name=3D"robots" content=3D"noindex,nofollow">
    <meta name=3D"generator" content=3D"Matomo Analytics">
</head>
HTML;

        $expectedText = 'Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Hello,=0A=0AThe triggered alerts are listed in the table below. To adjust y=';

        $renderedBody = html_entity_decode($this->mail->createBody(), ENT_COMPAT | ENT_HTML401, 'UTF-8');
        $this->assertStringContainsString(str_replace("\n", PHPMailer::getLE(), $expectedHtml), $renderedBody);
        $this->assertStringContainsString(str_replace("\n", PHPMailer::getLE(), $expectedText), $renderedBody);
        $this->assertEquals(array('test@example.com'), array_keys($mail->getRecipients()));
    }

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

    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(
            'idtriggered'       => 1,
            '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_sendAlertsPerEmailToRecipient_shouldUseDifferentSubjectDependingOnPeriod()
    {
        if (version_compare(Version::VERSION, '4.13.0-rc1', '<')) {
            // intl format changed in 4.13.0
            $this->assertDateInSubject('week', 'week December 21 - 27, 2009');
        } else {
            $this->assertDateInSubject('week', 'week December 21 - 27, 2009');
        }
        $this->assertDateInSubject('day', 'Thursday, December 31, 2009');
        $this->assertDateInSubject('month', 'December 2009');
    }

    private function assertDateInSubject($period, $expectedDate)
    {
        $alerts = $this->getTriggeredAlerts();

        $mail = new Mail();
        $this->notifier->sendAlertsPerEmailToRecipient($alerts, $mail, 'test@example.com', $period, 1);

        $expected = 'New alert for website Piwik test [' . $expectedDate . ']';
        $this->assertEquals($expected, $mail->getSubject());
    }

    public function test_sendNewAlerts()
    {
        $methods = array('sendAlertsPerEmailToRecipient', 'sendAlertsPerSmsToRecipient', 'markAlertAsSent');
        $mock    = $this->getMockBuilder('Piwik\Plugins\CustomAlerts\tests\Integration\CustomNotifier')
            ->onlyMethods($methods)
            ->getMock();

        $alerts = array(
            $this->buildAlert(1, 'Alert1', 'week', 4, 'Test', 'login1'),
            $this->buildAlert(2, 'Alert2', 'week', 4, 'Test', 'login2'),
            $this->buildAlert(3, 'Alert3', 'week', 4, 'Test', 'login1'),
            $this->buildAlert(4, 'Alert4', 'week', 4, 'Test', 'login3'),
        );

        $alerts[2]['phone_numbers'] = array('232');

        $idSite = 1;
        $period = 'week';

        $mock->setTriggeredAlerts($alerts);

        $mock->expects($this->at(0))
            ->method('sendAlertsPerEmailToRecipient')
            ->with(
                $this->equalTo($alerts),
                $this->isInstanceOf('\Piwik\Mail'),
                $this->equalTo('test5@example.com'),
                $this->equalTo($period),
                $this->equalTo($idSite)
            );

        $mock->expects($this->at(1))
            ->method('sendAlertsPerEmailToRecipient')
            ->with(
                $this->equalTo(array($alerts[0], $alerts[2])),
                $this->isInstanceOf('\Piwik\Mail'),
                $this->equalTo('test1@example.com'),
                $this->equalTo($period),
                $this->equalTo($idSite)
            );

        $mock->expects($this->at(2))
            ->method('sendAlertsPerEmailToRecipient')
            ->with(
                $this->equalTo(array($alerts[1])),
                $this->isInstanceOf('\Piwik\Mail'),
                $this->equalTo('test2@example.com'),
                $this->equalTo($period),
                $this->equalTo($idSite)
            );

        $mock->expects($this->at(3))
            ->method('sendAlertsPerEmailToRecipient')
            ->with(
                $this->equalTo(array($alerts[3])),
                $this->isInstanceOf('\Piwik\Mail'),
                $this->equalTo('test3@example.com'),
                $this->equalTo($period),
                $this->equalTo($idSite)
            );

        $mock->expects($this->at(4))
            ->method('sendAlertsPerSmsToRecipient')
            ->with(
                $this->equalTo(array($alerts[0], $alerts[1], $alerts[3])),
                $this->isInstanceOf('\Piwik\Plugins\MobileMessaging\Model'),
                $this->equalTo('+1234567890')
            );

        $mock->expects($this->at(5))
            ->method('sendAlertsPerSmsToRecipient')
            ->with(
                $this->equalTo($alerts),
                $this->isInstanceOf('\Piwik\Plugins\MobileMessaging\Model'),
                $this->equalTo('232')
            );

        foreach ($alerts as $index => $alert) {
            $mock->expects($this->at(6 + $index))->method('markAlertAsSent')->with($this->equalTo($alert));
        }

        $mock->sendNewAlerts($period, $idSite);
    }

    public function provideContainerConfig()
    {
        return [
            'observers.global' => \Piwik\DI::add([
                [
                    'Test.Mail.send', \Piwik\DI::value(function (PHPMailer $mail) {
                        $this->mail = $mail;
                        $this->mail->preSend();
                    })
                ],
            ]),
        ];
    }
}