ILIAS  release_8 Revision v8.24
class.ilObjChatroomAccessTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\MockObject\MockObject;
22
28{
31 protected ilDBInterface $db;
32
34 {
35 $expected = [
36 ['permission' => 'read', 'cmd' => 'view', 'lang_var' => 'enter', 'default' => true],
37 ['permission' => 'write', 'cmd' => 'settings-general', 'lang_var' => 'settings'],
38 ];
39
40 $commands = $this->access::_getCommands();
41
42 $this->assertIsArray($commands);
43 $this->assertSame($expected, $commands);
44 }
45
46 public function testGotoCheckFails(): void
47 {
48 $user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->onlyMethods(
49 ['getId']
50 )->getMock();
51 $user->method('getId')->willReturn(6);
52
53 $this->setGlobalVariable('ilUser', $user);
54
55 $chatroomSettings = $this->createMock(ilDBStatement::class);
56 $chatroomSettings
57 ->method('fetchAssoc')
58 ->willReturnOnConsecutiveCalls(
59 [
60 'keyword' => 'public_room_ref',
61 'value' => '1',
62 ],
63 null,
64 );
65
66 $this->db
67 ->method('fetchAssoc')
68 ->willReturnCallback(static function (ilDBStatement $statement) {
69 return $statement->fetchAssoc();
70 });
71
72 $this->db
73 ->method('query')
74 ->with($this->stringContains("FROM settings WHERE module='chatroom'", false))
75 ->willReturn($chatroomSettings);
76 $this->setGlobalVariable('ilDB', $this->db);
77
78 $rbacsystem = $this->getMockBuilder(ilRbacSystem::class)->disableOriginalConstructor()->onlyMethods(
79 ['checkAccess', 'checkAccessOfUser']
80 )->getMock();
81 $rbacsystem->method('checkAccess')->with(
82 $this->logicalOr($this->equalTo('read'), $this->equalTo('visible')),
83 $this->equalTo('1')
84 )->willReturn(false);
85 $rbacsystem->method('checkAccessOfUser')->with(
86 $this->equalTo(6),
87 $this->logicalOr($this->equalTo('read'), $this->equalTo('visible')),
88 $this->equalTo('1')
89 )->willReturn(false);
90
91 $this->setGlobalVariable('rbacsystem', $rbacsystem);
92
93 $this->assertFalse($this->access::_checkGoto(''));
94 $this->assertFalse($this->access::_checkGoto('chtr'));
95 $this->assertFalse($this->access::_checkGoto('chtr_'));
96 $this->assertFalse($this->access::_checkGoto('chtr_'));
97 $this->assertFalse($this->access::_checkGoto('chtr_test'));
98 $this->assertFalse($this->access::_checkGoto('chtr_1'));
99 }
100
101 public function testGotoCheckSucceeds(): void
102 {
103 $user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->onlyMethods(
104 ['getId']
105 )->getMock();
106 $user->method('getId')->willReturn(6);
107
108 $this->setGlobalVariable('ilUser', $user);
109
110 $chatroomSettings = $this->createMock(ilDBStatement::class);
111 $chatroomSettings
112 ->method('fetchAssoc')
113 ->willReturnOnConsecutiveCalls(
114 [
115 'keyword' => 'public_room_ref',
116 'value' => '5',
117 ],
118 null
119 );
120
121 $this->db
122 ->method('fetchAssoc')
123 ->willReturnCallback(static function (ilDBStatement $statement) {
124 return $statement->fetchAssoc();
125 });
126
127 $this->db
128 ->method('query')
129 ->with($this->stringContains("FROM settings WHERE module='chatroom'", false))
130 ->willReturn($chatroomSettings);
131 $this->setGlobalVariable('ilDB', $this->db);
132
133 $rbacsystem = $this->getMockBuilder(ilRbacSystem::class)->disableOriginalConstructor()->onlyMethods(
134 ['checkAccess', 'checkAccessOfUser']
135 )->getMock();
136 $rbacsystem->method('checkAccess')->with(
137 $this->logicalOr($this->equalTo('read'), $this->equalTo('visible'), $this->equalTo('write')),
138 $this->equalTo('5')
139 )->willReturn(true);
140 $rbacsystem->method('checkAccessOfUser')->with(
141 $this->equalTo(6),
142 $this->logicalOr($this->equalTo('read'), $this->equalTo('visible'), $this->equalTo('write')),
143 $this->equalTo('5')
144 )->willReturn(true);
145
146 $this->setGlobalVariable('rbacsystem', $rbacsystem);
147
148 $this->assertTrue($this->access::_checkGoto('chtr_5'));
149 }
150
151 public function testAccessChecksFail(): void
152 {
153 $userId = 1;
154 $refId = 99;
155 $objId = 6;
156
157 $user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->onlyMethods(
158 ['getId']
159 )->getMock();
160 $user->expects($this->once())->method('getId')->willReturn($userId);
161
162 $this->setGlobalVariable('ilUser', $user);
163
164 $rbacsystem = $this->getMockBuilder(ilRbacSystem::class)->disableOriginalConstructor()->onlyMethods(
165 ['checkAccessOfUser']
166 )->getMock();
167 $rbacsystem->expects($this->once())->method('checkAccessOfUser')->with(
168 $this->equalTo($userId),
169 $this->equalTo('write'),
170 $this->equalTo($refId)
171 )->willReturn(false);
172
173 $this->setGlobalVariable('rbacsystem', $rbacsystem);
174
175 $this->assertFalse($this->access->_checkAccess('unused', 'write', $refId, $objId));
176 }
177
178 public function testAccessChecksSucceed(): void
179 {
180 $userId = 1;
181 $refId = 99;
182 $objId = 6;
183
184 $user = $this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->onlyMethods(
185 ['getId']
186 )->getMock();
187 $user->expects($this->once())->method('getId')->willReturn($userId);
188
189 $this->setGlobalVariable('ilUser', $user);
190
191 $this->db->method('fetchAssoc')->willReturnOnConsecutiveCalls(
192 ['keyword' => 'chat_enabled', 'value' => '0'],
193 null
194 );
195
196 $rbacsystem = $this->getMockBuilder(ilRbacSystem::class)->disableOriginalConstructor()->onlyMethods(
197 ['checkAccessOfUser']
198 )->getMock();
199 $rbacsystem->expects($this->once())->method('checkAccessOfUser')->with(
200 $this->equalTo($userId),
201 $this->equalTo('write'),
202 $this->equalTo($refId)
203 )->willReturn(true);
204
205 $this->setGlobalVariable('rbacsystem', $rbacsystem);
206
207 $this->assertTrue($this->access->_checkAccess('unused', 'write', $refId, $objId));
208 }
209
210 protected function setUp(): void
211 {
212 parent::setUp();
213
214 $settingsReflection = new ReflectionClass(ilSetting::class);
215 $cache = $settingsReflection->getProperty('settings_cache');
216 $cache->setAccessible(true);
217 $cache->setValue($settingsReflection, []);
218
219 $this->access = new ilObjChatroomAccess();
220 $this->db = $this->createGlobalIlDBMock();
221 }
222}
Class ilChatroomAbstractTest.
setGlobalVariable(string $name, $value)
Class ilObjChatroomAccessTest.
Access class for chatroom objects.
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$objId
Definition: xapitoken.php:57
$refId
Definition: xapitoken.php:58