Completed
Branchmaster (b375cb)
created

AuthTest::testInit()   B

↳ Parent: AuthTest

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 24
Code Lines 11

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
namespace Redaxscript\Tests;
3
4
use Redaxscript\Auth;
5
use Redaxscript\Db;
6
use Redaxscript\Server;
7
use Redaxscript\Request;
8
9
/**
10
 * AuthTest
11
 *
12
 * @since 3.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Tests
16
 * @author Henry Ruhs
17
 */
18
19
class AuthTest extends TestCaseAbstract
20
{
21
	/**
22
	 * instance of the request class
23
	 *
24
	 * @var object
25
	 */
26
27
	protected $_request;
28
29
	/**
30
	 * setUp
31
	 *
32
	 * @since 3.0.0
33
	 */
34
35
	public function setUp()
36
	{
37
		$this->_request = Request::getInstance();
38
	}
39
40
	/**
41
	 * tearDownAfterClass
42
	 *
43
	 * @since 3.0.0
44
	 */
45
46
	public static function tearDownAfterClass()
47
	{
48
		Db::forTablePrefix('users')->whereIdIs(1)->findOne()->set('groups', '1')->save();
49
	}
50
51
	/**
52
	 * providerGetPermission
53
	 *
54
	 * @since 3.0.0
55
	 *
56
	 * @return array
57
	 */
58
59
	public function providerGetPermission()
60
	{
61
		return $this->getProvider('tests/provider/auth_get_permission.json');
62
	}
63
64
	/**
65
	 * providerGetFilter
66
	 *
67
	 * @since 3.0.0
68
	 *
69
	 * @return array
70
	 */
71
72
	public function providerGetFilter()
73
	{
74
		return $this->getProvider('tests/provider/auth_get_filter.json');
75
	}
76
77
	/**
78
	 * testInit
79
	 *
80
	 * @since 3.0.0
81
	 */
82
83
	public function testInit()
84
	{
85
		/* setup */
86
87
		$root = new Server\Root($this->_request);
88
		$this->_request->setSession($root->getOutput() . '/auth',
89
		[
90
			'user' =>
91
			[
92
				'test' => 'test'
93
			],
94
			'permission' =>
95
			[
96
				'test' => 'test'
97
			]
98
		]);
99
		$auth = new Auth($this->_request);
100
		$auth->init();
101
102
		/* compare */
103
104
		$this->assertEquals('test', $auth->getUser('test'));
105
		$this->assertEquals('test', $auth->getPermission('test'));
106
	}
107
108
	/**
109
	 * testLogin
110
	 *
111
	 * @since 3.0.0
112
	 */
113
114
	public function testLoginAndLogout()
115
	{
116
		/* setup */
117
118
		$auth = new Auth($this->_request);
119
		$auth->logout();
120
121
		/* compare */
122
123
		$this->assertFalse($auth->login());
124
		$this->assertTrue($auth->login(1));
125
		$this->assertTrue($auth->logout());
126
		$this->assertFalse($auth->logout());
127
	}
128
129
	/**
130
	 * testGetUserInvalid
131
	 *
132
	 * @since 3.0.0
133
	 */
134
135
	public function testGetUserInvalid()
136
	{
137
		/* setup */
138
139
		$auth = new Auth($this->_request);
140
		$auth->login(1);
141
142
		/* actual */
143
144
		$actual = $auth->getUser('invalidKey');
145
146
		/* compare */
147
148
		$this->assertFalse($actual);
149
	}
150
151
	/**
152
	 * testGetPermission
153
	 *
154
	 * @since 3.0.0
155
	 *
156
	 * @param string $method
157
	 * @param array $typeArray
158
	 * @param string $groups
159
	 *
160
	 * @dataProvider providerGetPermission
161
	 */
162
163
	public function testGetPermission($method = null, $typeArray = [], $groups = null)
164
	{
165
		/* setup */
166
167
		Db::forTablePrefix('users')->whereIdIs(1)->findOne()->set('groups', $groups)->save();
168
		$auth = new Auth($this->_request);
169
		$auth->login(1);
170
171
		/* compare */
172
173
		foreach ($typeArray as $key => $value)
174
		{
175
			$this->assertEquals($auth->$method($key), $value);
176
		}
177
	}
178
179
	/**
180
	 * testGetFilter
181
	 *
182
	 * @since 3.0.0
183
	 *
184
	 * @param string $groups
185
	 * @param boolean $expect
186
	 *
187
	 * @dataProvider providerGetFilter
188
	 */
189
190
	public function testGetFilter($groups = null, $expect = null)
191
	{
192
		/* setup */
193
194
		Db::forTablePrefix('users')->whereIdIs(1)->findOne()->set('groups', $groups)->save();
195
		$auth = new Auth($this->_request);
196
		$auth->login(1);
197
198
		/* actual */
199
200
		$actual = $auth->getFilter();
201
202
		/* compare */
203
204
		$this->assertEquals($expect, $actual);
205
	}
206
}