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

ManyUsers.php « Fixtures « tests « UsersManager « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1382a55fd2ce95f452a545f966b366ea574e6181 (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link    http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
namespace Piwik\Plugins\UsersManager\tests\Fixtures;

use Piwik\Plugins\UsersManager\API;
use Piwik\Plugins\UsersManager\Model;
use Piwik\Plugins\UsersManager\UserUpdater;
use Piwik\Tests\Framework\Fixture;

/**
 * Generates tracker testing data for our APITest
 *
 * This Simple fixture adds one website and tracks one visit with couple pageviews and an ecommerce conversion
 */
class ManyUsers extends Fixture
{
    const SITE_COUNT = 100;
    const USER_COUNT = 100;

    public $dateTime = '2013-01-23 01:23:45';
    public $idSite = 1;
    public $siteCopyCount;
    public $userCopyCount;
    public $addTextSuffixes;

    public $baseUsers = array(
        'login1' => array('superuser' => 1),
        'login2' => array('view' => array(3,5),   'admin' => array(1,2,6)),
        'login3' => array('view' => array(),        'admin' => array()), // no access to any site
        'login4' => array('view' => array(6),       'admin' => array()), // only access to one with view
        'login5' => array('view' => array(),        'admin' => array(3)), // only access to one with admin
        'login6' => array('view' => array(),        'admin' => array(6,3)), // access to a couple of sites with admin
        'login7' => array('view' => array(2,1,6,3), 'admin' => array()), // access to a couple of sites with view
        'login8' => array('view' => array(4,7),     'admin' => array(2,5)), // access to a couple of sites with admin and view
        'login9' => array('view' => array(5,6),     'admin' => array(8,9)),
        'login10' => array('superuser' => 1)
    );

    public $baseSites = [
        'sleep',
        'escapesequence',
        'hunter',
        'transistor',
        'wicket',
        'relentless',
        'scarecrow',
        'nova',
        'resilience',
        'tricks',
    ];

    public $textAdditions = [
        'life',
        'light',
        'flight',
        'conchords',
    ];

    public function __construct($userCopyCount = self::USER_COUNT, $siteCopyCount = self::SITE_COUNT, $addTextSuffixes = true)
    {
        $this->userCopyCount = $userCopyCount;
        $this->siteCopyCount = $siteCopyCount;
        $this->addTextSuffixes = $addTextSuffixes;
    }

    public function setUp()
    {
        $this->setUpWebsite();
        $this->setUpUsers();
    }

    public function tearDown()
    {
        // empty
    }

    private function setUpWebsite()
    {
        for ($i = 0; $i < self::SITE_COUNT; $i++) {
            $siteName = $this->baseSites[$i % count($this->baseSites)];
            if ($i != 0) {
                $siteName .= $i;
            }
            Fixture::createWebsite('2010-01-01 00:00:00', $ecommerce = 0, $siteName);
        }
    }

    protected function setUpUsers()
    {
        $totalUserCount = 0;

        $model = new Model();
        $api = API::getInstance();
        for ($i = 0; $i != $this->userCopyCount; ++$i) {
            $addToEmail = $i % 2 == 0;

            foreach ($this->baseUsers as $baseLogin => $permissions) {
                ++$totalUserCount;

                $textAddition = $this->textAdditions[$totalUserCount % count($this->textAdditions)];

                $login = $this->addTextSuffixes ? ($i . '_' . $baseLogin) : $baseLogin;
                if ($this->addTextSuffixes && !$addToEmail) {
                    $login .= $textAddition;
                }

                $email = $login . '@example.com';
                if ($this->addTextSuffixes &&$addToEmail) {
                    $email = $login . $textAddition . '@example.com';
                }

                $api->addUser($login, 'password', $email);

                foreach ($permissions as $access => $idSites) {
                    if (empty($idSites)) {
                        continue;
                    }

                    if ($access == 'superuser') {
                        $userUpdater = new UserUpdater();
                        $userUpdater->setSuperUserAccessWithoutCurrentPassword($login, true);
                    } else {
                        $api->setUserAccess($login, $access, $idSites);
                    }
                }

                $user = $model->getUser($login);
                $this->users[$login]['token'] = $user['token_auth'];
            }
        }
    }
}