Completed
Push — master ( 418b5a...9bf62d )
by Andras
02:31
created

testIsEnabledForUserEnabledNotInGroup()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace Tests\Richdocuments;
23
24
use OCA\Richdocuments\PermissionManager;
25
use OCP\IConfig;
26
use OCP\IGroupManager;
27
use OCP\IUser;
28
use Test\TestCase;
29
30
class PermissionManagerTest extends TestCase {
31
	/** @var IConfig|\PHPUnit_Framework_MockObject_MockBuilder */
32
	private $config;
33
	/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockBuilder */
34
	private $groupManager;
35
	/** @var PermissionManager */
36
	private $permissionManager;
37
38
	public function setUp() {
39
		parent::setUp();
40
		$this->config = $this->createMock(IConfig::class);
41
		$this->groupManager = $this->createMock(IGroupManager::class);
42
		$this->permissionManager = new PermissionManager($this->config, $this->groupManager);
43
	}
44
45
	public function testIsEnabledForUserEnabledNoRestrictions() {
46
		/** @var IUser|\PHPUnit_Framework_MockObject_MockBuilder $user */
47
		$user = $this->createMock(IUser::class);
48
49
		$this->config
50
			->expects($this->once())
51
			->method('getAppValue')
52
			->with('richdocuments', 'use_groups', '')
53
			->willReturn('');
54
55
		$this->assertTrue($this->permissionManager->isEnabledForUser($user));
56
	}
57
58
	public function testIsEnabledForUserEnabledNotInGroup() {
59
		/** @var IUser|\PHPUnit_Framework_MockObject_MockBuilder $user */
60
		$user = $this->createMock(IUser::class);
61
		$user
62
			->expects($this->once())
63
			->method('getUID')
64
			->willReturn('TestUser');
65
66
		$this->config
67
			->expects($this->once())
68
			->method('getAppValue')
69
			->with('richdocuments', 'use_groups', '')
70
			->willReturn('Enabled1|Enabled2|Enabled3');
71
72
		$this->groupManager
73
			->expects($this->at(0))
74
			->method('isInGroup')
75
			->with('TestUser', 'Enabled1')
76
			->willReturn(false);
77
		$this->groupManager
78
			->expects($this->at(1))
79
			->method('isInGroup')
80
			->with('TestUser', 'Enabled2')
81
			->willReturn(false);
82
		$this->groupManager
83
			->expects($this->at(2))
84
			->method('isInGroup')
85
			->with('TestUser', 'Enabled3')
86
			->willReturn(false);
87
88
		$this->assertFalse($this->permissionManager->isEnabledForUser($user));
89
	}
90
91
	public function testIsEnabledForUserEnabledInGroup() {
92
		/** @var IUser|\PHPUnit_Framework_MockObject_MockBuilder $user */
93
		$user = $this->createMock(IUser::class);
94
		$user
95
			->expects($this->once())
96
			->method('getUID')
97
			->willReturn('TestUser');
98
99
		$this->config
100
			->expects($this->once())
101
			->method('getAppValue')
102
			->with('richdocuments', 'use_groups', '')
103
			->willReturn('Enabled1|Enabled2|Enabled3');
104
105
		$this->groupManager
106
			->expects($this->at(0))
107
			->method('isInGroup')
108
			->with('TestUser', 'Enabled1')
109
			->willReturn(false);
110
		$this->groupManager
111
			->expects($this->at(1))
112
			->method('isInGroup')
113
			->with('TestUser', 'Enabled2')
114
			->willReturn(true);
115
116
		$this->assertTrue($this->permissionManager->isEnabledForUser($user));
117
	}
118
}
119