Spaces:
Paused

File size: 2,890 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
<?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\Fixtures;

use Piwik\Piwik;
use Piwik\Plugins\CustomAlerts\Model;
use Piwik\Tests\Framework\Fixture;
use Piwik\Plugins\Goals\API;

/**
 * Tracks custom events
 */
class CustomAlerts extends Fixture
{
    public $dateTime = '2010-01-03 11:22:33';
    public $idSite = 1;

    public function setUp(): void
    {
        $this->setUpWebsites();
        $this->addAlerts();
    }

    private function setUpWebsites()
    {
        // tests run in UTC, the Tracker in UTC
        if (!self::siteCreated($idSite = 1)) {
            self::createWebsite($this->dateTime);
        }

        // tests run in UTC, the Tracker in UTC
        if (!self::siteCreated($idSite = 2)) {
            self::createWebsite($this->dateTime);
        }

        API::getInstance()->addGoal($idSite, 'Goal for idSite 2', 'title', 'normal title', 'exact', true, 50, true);
    }

    private function addAlerts()
    {
        $this->createAlert('Test Alert 1', 'day', array($this->idSite), 'nb_visits', 'VisitsSummary_get');
        $this->createAlert('Test Alert 2', 'week', array($this->idSite), 'nb_uniq_visitors', 'Referrers_getSearchEngines');
        $this->createAlert('Test Alert 3', 'month', array($this->idSite), 'nb_hits', 'Actions_getPageUrls');

        // should not show up as different login
        $this->createAlert('Test Alert 4', 'month', array($this->idSite), 'nb_hits', 'Actions_getPageUrls', 'loginnotmatching');

        $this->triggerAlert(1, 10, 5, '2014-01-16 03:21:17');
        $this->triggerAlert(1, 2999, 10, '2014-01-15 03:21:17');
        $this->triggerAlert(2, 1004, 1, '2013-01-16 03:21:17');
        $this->triggerAlert(2, 10, 5, '2013-10-16 03:21:17');

        // should not show up as belongs to different login
        $this->triggerAlert(4, 10, 5, '2013-10-16 03:21:17');
    }

    private function createAlert($name, $period, $idSites, $metric, $report, $login = false)
    {
        if (false === $login) {
            $login = Piwik::getCurrentUserLogin();
        }

        $emails       = array('test1@example.com', 'test2@example.com');
        $phoneNumbers = array('0123456789');

        $reportMatched = '';
        if ('VisitsSummary_get' != $report) {
            $reportMatched = 'Piwik';
        }

        $model = new Model();
        $model->createAlert($name, $idSites, $login, $period, 0, $emails, $phoneNumbers, $metric, 'less_than', 5, $comparedTo = 1, $report, 'matches_exactly', $reportMatched, ['email', 'mobile'], '', '');
    }

    private function triggerAlert($idAlert, $valueNew, $valueOld, $datetime)
    {
        $model = new Model();
        $model->triggerAlert($idAlert, $this->idSite, $valueNew, $valueOld, $datetime);
    }
}