Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ScheduledReportsTest.php « Integration « tests « ScheduledReports « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63e9c302e5bfd103752e480ecf09d2e62736948f (plain)
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
<?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\ScheduledReports\tests;

use Piwik\Db;
use Piwik\Piwik;
use Piwik\Plugins\ScheduledReports\API;
use Piwik\Plugins\ScheduledReports\ScheduledReports;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group ScheduledReports
 * @group ScheduledReportsTest
 * @group Plugins
 */
class ScheduledReportsTest extends IntegrationTestCase
{
    /**
     * @var ScheduledReports
     */
    private $reports;
    private $reportIds = array();

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

        $this->reports = new ScheduledReports();
        $this->setIdentity('userlogin');

        for ($i = 1; $i <= 4; $i++) {
            Fixture::createWebsite('2014-01-01 00:00:00');
            $this->addReport('userlogin', $i);
        }

        $this->addReport('otherUser', 1);
        $this->addReport('anotherUser', 2);
    }

    public function test_deleteUserReportForSites_shouldNotRemoveAnythingIfNoSitesOrNoLogin()
    {
        $this->reports->deleteUserReportForSites('userLogin', array());

        $this->assertHasReport('userlogin', 1);
        $this->assertHasReport('userlogin', 2);
        $this->assertHasReport('userlogin', 3);
        $this->assertHasReport('userlogin', 4);
        $this->assertHasReport('otherUser', 1);
        $this->assertHasReport('anotherUser', 2);
        
        $this->reports->deleteUserReportForSites('', array(1, 2, 3, 4));

        $this->assertHasReport('userlogin', 1);
        $this->assertHasReport('userlogin', 2);
        $this->assertHasReport('userlogin', 3);
        $this->assertHasReport('userlogin', 4);
        $this->assertHasReport('otherUser', 1);
        $this->assertHasReport('anotherUser', 2);
    }

    public function test_deleteUserReportForSites_shouldNotFailIfUserHasNoReports()
    {
        $this->reports->deleteUserReportForSites('unk', array());

        $this->assertHasReport('userlogin', 1);
        $this->assertHasReport('userlogin', 2);
        $this->assertHasReport('userlogin', 3);
        $this->assertHasReport('userlogin', 4);
        $this->assertHasReport('otherUser', 1);
        $this->assertHasReport('anotherUser', 2);
    }

    public function test_deleteUserReportForSites_shouldRemoveOnlyReportsForGivenSitesAndLogin()
    {
        $this->reports->deleteUserReportForSites('userLogin', array(1, 2));

        $this->assertHasNotReport('userlogin', 1);
        $this->assertHasNotReport('userlogin', 2);

        $this->assertHasReport('userlogin', 3);
        $this->assertHasReport('userlogin', 4);
        $this->assertHasReport('otherUser', 1);
        $this->assertHasReport('anotherUser', 2);
    }

    public function test_ScheduledReports_shouldRemoveOnlyReportsForGivenSitesAndLogin_IfEventIsTriggered()
    {
        Piwik::postEvent('UsersManager.removeSiteAccess', array('userLogin', array(1, 2)));

        $this->assertHasNotReport('userlogin', 1);
        $this->assertHasNotReport('userlogin', 2);

        $this->assertHasReport('userlogin', 3);
        $this->assertHasReport('userlogin', 4);
        $this->assertHasReport('otherUser', 1);
        $this->assertHasReport('anotherUser', 2);
    }

    public function test_deleteUserReport_shouldRemoveAllReportsOfASpecificUser()
    {
        $this->reports->deleteUserReport('userLogin');

        $this->assertHasNotReport('userlogin', 1);
        $this->assertHasNotReport('userlogin', 2);
        $this->assertHasNotReport('userlogin', 3);
        $this->assertHasNotReport('userlogin', 4);

        $this->assertHasReport('otherUser', 1);
        $this->assertHasReport('anotherUser', 2);
    }

    private function assertHasReport($login, $idSite)
    {
        $report = $this->getReport($login, $idSite);

        $this->assertNotEmpty($report, "Report for $login, $idSite should exist but does not");
    }

    private function assertHasNotReport($login, $idSite)
    {
        try {
            $this->getReport($login, $idSite);
            $this->fail("Report for $login, $idSite should not exist but does");
        } catch (\Exception $e) {
            self::assertStringContainsString("Requested report couldn't be found", $e->getMessage());
        }
    }

    private function getReport($login, $idSite)
    {
        $this->setIdentity($login);

        return API::getInstance()->getReports($idSite, 'day', $this->reportIds[$login . '_' . $idSite]);
    }

    private function addReport($login, $idSite)
    {
        $this->setIdentity($login);

        $reportType   = 'email';
        $reportFormat = 'pdf';
        $reports      = array();
        $parameters   = array(ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_TABLES_ONLY);

        $reportId = API::getInstance()->addReport($idSite, 'description', 'day', 3, $reportType, $reportFormat, $reports, $parameters);
        $this->reportIds[$login . '_' . $idSite] = $reportId;
    }

    private function setIdentity($login)
    {
        FakeAccess::$identity  = $login;
        FakeAccess::$superUser = true;
    }

    public function provideContainerConfig()
    {
        return array(
            'Piwik\Access' => new FakeAccess()
        );
    }
}