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

Database.test.php « core « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b0e37b28460b28fb11d8b08f78f6635f7489824 (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
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
<?php
if(!defined("PIWIK_PATH_TEST_TO_ROOT")) {
	define('PIWIK_PATH_TEST_TO_ROOT', getcwd().'/../..');
}
if(!defined('PIWIK_CONFIG_TEST_INCLUDED'))
{
	require_once PIWIK_PATH_TEST_TO_ROOT . "/tests/config_test.php";
}
Mock::generate('Piwik_Access');

class Test_Database extends UnitTestCase
{
	function __construct( $title = '')
	{
		parent::__construct( $title );
		print("The test class extends Test_Database: the test Piwik database is created once in the constructor, and all tables are truncated at the end of EACH unit test method.<br>");
		
		Piwik::createConfigObject();
		Zend_Registry::get('config')->setTestEnvironment();	
		Zend_Registry::get('config')->disableSavingConfigurationFileUpdates();
		Piwik::createDatabaseObject();
		Piwik::createLogObject();

		Piwik::dropDatabase();
		Piwik::createDatabase();
		Piwik::disconnectDatabase();
		Piwik::createDatabaseObject();
		Piwik::createTables();
	}
	public function __destruct()
	{
	}
	public function setUp()
	{
	}
	
	public function tearDown()
	{
		Piwik::truncateAllTables();
	}
	
	public function testHelloWorld()
	{
		$this->assertTrue(true);
	}
	
	public function testHelloWorld2()
	{
		$this->assertTrue(true);
	}
	
}


class FakeAccess
{
	static public $superUser = false;
	static public $idSitesAdmin = array();
	static public $idSitesView = array();
	static public $identity = 'superUserLogin';
	
	static public function setIdSitesAdmin($ids)
	{
		self::$superUser = false;
		self::$idSitesAdmin = $ids;
	}
	static public function setIdSitesView($ids)
	{
		self::$superUser = false;
		self::$idSitesView = $ids;
	}
	
	static public function checkUserIsSuperUser()
	{
		if(!self::$superUser)
		{
			throw new Exception("checkUserIsSuperUser Fake exception // string not to be tested");
		}
	}
	
	static public function setSuperUser()
	{
		self::$superUser = true;
	}

	static public function reloadAccess()
	{}

	static public function checkUserHasAdminAccess( $idSites )
	{
		if(!self::$superUser)
		{
			$websitesAccess=self::$idSitesAdmin;
		}
		else
		{
			$websitesAccess=Piwik_SitesManager_API::getAllSitesId();
		}
		
		if(!is_array($idSites))
		{
			$idSites = Piwik_Site::getIdSitesFromIdSitesString($idSites);
		}
		foreach($idSites as $idsite)
		{
			if(!in_array($idsite, $websitesAccess))
			{
				throw new Exception("checkUserHasAdminAccess Fake exception // string not to be tested");
			}
		}
	}
	
	//means at least view access
	static public function checkUserHasViewAccess( $idSites )
	{
		if(!self::$superUser)
		{
			$websitesAccess=array_merge(self::$idSitesView,self::$idSitesAdmin);
		}
		else
		{
			$websitesAccess=Piwik_SitesManager_API::getAllSitesId();
		}
		
		if(!is_array($idSites))
		{
			$idSites=array($idSites);
		}
		foreach($idSites as $idsite)
		{
			if(!in_array($idsite, $websitesAccess))
			{
				throw new Exception("checkUserHasViewAccess Fake exception // string not to be tested");
			}
		}
	}

	static public function checkUserHasSomeViewAccess()
	{
		if(!self::$superUser)
		{
			if( count(self::$idSitesView) == 0 )
			{
				throw new Exception("checkUserHasSomeViewAccess Fake exception // string not to be tested");
			}
		}
		else
		{
			return;
		}
	}

	//means at least view access
	static public function checkUserHasSomeAdminAccess()
	{
		if(!self::$superUser)
		{
			if( count(self::$idSitesAdmin) == 0 )
			{
				throw new Exception("checkUserHasSomeAdminAccess Fake exception // string not to be tested");
			}
		}
		else
		{
			return; //super user has some admin rights
		}
	}
	
	static public function getLogin()
	{
		return self::$identity;
	}
	
	static public function getSitesIdWithAdminAccess()
	{
		if(self::$superUser)
		{
			return Piwik_SitesManager_API::getAllSitesId();
		}
		return  self::$idSitesAdmin;
	}
	
	static public function getSitesIdWithViewAccess()
	{
		if(self::$superUser)
		{
			return Piwik_SitesManager_API::getAllSitesId();
		}
		return  self::$idSitesView;
	}
	static public function getSitesIdWithAtLeastViewAccess()
	{
		if(self::$superUser)
		{
			return Piwik_SitesManager_API::getAllSitesId();
		}
		return  array_merge(self::$idSitesView,self::$idSitesAdmin);
	}
}