ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilStudyProgrammeUserAssignmentTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2015 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5require_once(__DIR__ . "/mocks.php");
6
16{
17 protected $backupGlobals = false;
18
19 protected function setUp()
20 {
21 PHPUnit_Framework_Error_Deprecated::$enabled = false;
22
23 require_once("./Modules/StudyProgramme/classes/class.ilObjStudyProgramme.php");
24
25 include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
26 ilUnitUtil::performInitialisation();
27
29 $this->root->putInTree(ROOT_FOLDER_ID);
30 $this->root->object_factory = new ilObjectFactoryWrapperMock();
31
34
35 $this->leaf1 = new ilStudyProgrammeLeafMock();
36 $this->leaf2 = new ilStudyProgrammeLeafMock();
37
38 $this->root->addNode($this->node1);
39 $this->root->addNode($this->node2);
40 $this->node1->addLeaf($this->leaf1);
41 $this->node2->addLeaf($this->leaf2);
42
43 global $DIC;
44 $tree = $DIC['tree'];
45 $this->tree = $tree;
46 }
47
48 protected function tearDown()
49 {
50 if ($this->root) {
51 $this->root->delete();
52 }
53 }
54
55
56 protected function newUser()
57 {
58 $user = new ilObjUser();
59 $user->create();
60 return $user;
61 }
62
66 public function testNoAssignmentWhenDraft()
67 {
68 $user = $this->newUser();
69 $this->assertEquals(ilStudyProgramme::STATUS_DRAFT, $this->root->getStatus());
70 $this->root->assignUser($user->getId());
71 }
72
77 {
78 $user = $this->newUser();
79
80 $this->root->setStatus(ilStudyProgramme::STATUS_OUTDATED);
81 $this->assertEquals(ilStudyProgramme::STATUS_OUTDATED, $this->root->getStatus());
82 $this->root->assignUser($user->getId());
83 }
84
89 {
90 $user = $this->newUser();
91 $prg = new ilObjStudyProgramme();
92 $prg->assignUser($user->getId());
93 }
94
95 public function testUserId()
96 {
97 $user1 = $this->newUser();
98 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
99 $ass = $this->root->assignUser($user1->getId());
100 $this->assertEquals($user1->getId(), $ass->getUserId());
101 }
102
103 public function testHasAssignmentOf()
104 {
105 $user1 = $this->newUser();
106 $user2 = $this->newUser();
107
108 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
109
110 $this->root->assignUser($user1->getId());
111 $this->assertTrue($this->root->hasAssignmentOf($user1->getId()));
112 $this->assertTrue($this->node1->hasAssignmentOf($user1->getId()));
113 $this->assertTrue($this->node2->hasAssignmentOf($user1->getId()));
114
115 $this->assertFalse($this->root->hasAssignmentOf($user2->getId()));
116 $this->assertFalse($this->node1->hasAssignmentOf($user2->getId()));
117 $this->assertFalse($this->node2->hasAssignmentOf($user2->getId()));
118 }
119
121 {
122 $user1 = $this->newUser();
123 $user2 = $this->newUser();
124 $user3 = $this->newUser();
125 $user4 = $this->newUser();
126
127 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
128 $this->node1->setStatus(ilStudyProgramme::STATUS_ACTIVE);
129 $this->node2->setStatus(ilStudyProgramme::STATUS_ACTIVE);
130
131 $this->assertEquals(0, $this->root->getAmountOfAssignmentsOf($user1->getId()));
132
133 $this->root->assignUser($user2->getId());
134 $this->assertEquals(1, $this->root->getAmountOfAssignmentsOf($user2->getId()));
135 $this->assertEquals(1, $this->node1->getAmountOfAssignmentsOf($user2->getId()));
136 $this->assertEquals(1, $this->node2->getAmountOfAssignmentsOf($user2->getId()));
137
138 $this->root->assignUser($user3->getId());
139 $this->root->assignUser($user3->getId());
140 $this->assertEquals(2, $this->root->getAmountOfAssignmentsOf($user3->getId()));
141 $this->assertEquals(2, $this->node1->getAmountOfAssignmentsOf($user3->getId()));
142 $this->assertEquals(2, $this->node2->getAmountOfAssignmentsOf($user3->getId()));
143
144 $this->root->assignUser($user4->getId());
145 $this->node1->assignUser($user4->getId());
146 $this->assertEquals(1, $this->root->getAmountOfAssignmentsOf($user4->getId()));
147 $this->assertEquals(2, $this->node1->getAmountOfAssignmentsOf($user4->getId()));
148 $this->assertEquals(1, $this->node2->getAmountOfAssignmentsOf($user4->getId()));
149 }
150
151 public function testGetAssignmentsOf()
152 {
153 $user1 = $this->newUser();
154 $user2 = $this->newUser();
155
156 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
157 $this->node1->setStatus(ilStudyProgramme::STATUS_ACTIVE);
158
159 $this->assertEquals(0, count($this->root->getAssignmentsOf($user1->getId())));
160 $this->assertEquals(0, count($this->node1->getAssignmentsOf($user1->getId())));
161 $this->assertEquals(0, count($this->node2->getAssignmentsOf($user1->getId())));
162
163 $this->root->assignUser($user2->getId());
164 $this->node1->assignUser($user2->getId());
165
166 $root_ass = $this->root->getAssignmentsOf($user2->getId());
167 $node1_ass = $this->node1->getAssignmentsOf($user2->getId());
168 $node2_ass = $this->node2->getAssignmentsOf($user2->getId());
169
170 $this->assertEquals(1, count($root_ass));
171 $this->assertEquals(2, count($node1_ass));
172 $this->assertEquals(1, count($node2_ass));
173
174 $this->assertEquals($this->root->getId(), $root_ass[0]->getStudyProgramme()->getId());
175 $this->assertEquals($this->root->getId(), $node2_ass[0]->getStudyProgramme()->getId());
176
177 $node1_ass_prg_ids = array_map(function ($ass) {
178 return $ass->getStudyProgramme()->getId();
179 }, $node1_ass);
180 $this->assertContains($this->root->getId(), $node1_ass_prg_ids);
181 $this->assertContains($this->node1->getId(), $node1_ass_prg_ids);
182 }
183
188 {
189 $user = $this->newUser();
190
191 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
192
193 $ass1 = $this->root->assignUser($user->getId());
194 $this->node1->removeAssignment($ass1);
195 }
196
201 {
202 $user = $this->newUser();
203
204 $this->node1->setStatus(ilStudyProgramme::STATUS_ACTIVE);
205
206 $ass1 = $this->node1->assignUser($user->getId());
207 $this->root->removeAssignment($ass1);
208 }
209
210 public function testRemoveAssignment1()
211 {
212 $user = $this->newUser();
213
214 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
215
216 $ass1 = $this->root->assignUser($user->getId());
217 $this->root->removeAssignment($ass1);
218 $this->assertFalse($this->root->hasAssignmentOf($user->getId()));
219 }
220
221 public function testRemoveAssignment2()
222 {
223 $user = $this->newUser();
224
225 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
226
227 $ass1 = $this->root->assignUser($user->getId());
228 $ass1->deassign();
229 $this->assertFalse($this->root->hasAssignmentOf($user->getId()));
230 }
231
232 public function testGetAssignments()
233 {
234 $user1 = $this->newUser();
235 $user2 = $this->newUser();
236
237 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
238 $this->node1->setStatus(ilStudyProgramme::STATUS_ACTIVE);
239
240 $ass1 = $this->root->assignUser($user1->getId());
241 $ass2 = $this->node1->assignUser($user2->getId());
242
243 $asses = $this->node1->getAssignments();
244 $ass_ids = array_map(function ($ass) {
245 return $ass->getId();
246 }, $asses);
247 $this->assertContains($ass1->getId(), $ass_ids);
248 $this->assertContains($ass2->getId(), $ass_ids);
249 }
250
252 {
253 $user1 = $this->newUser();
254 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
255 $ass = $this->root->assignUser($user1->getId());
256
257 $root_id = $this->root->getId();
258 $this->root->delete();
259 $this->root = null;
260
261 global $DIC;
262 $ilDB = $DIC['ilDB'];
263 $res = $ilDB->query(
264 "SELECT COUNT(*) cnt "
266 . " WHERE root_prg_id = " . $root_id
267 );
268 $rec = $ilDB->fetchAssoc($res);
269 $this->assertEquals(0, $rec["cnt"]);
270 }
271
273 {
274 $user = $this->newUser();
275 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
276 $ass1 = $this->root->assignUser($user->getId());
277 $ass1->deassign();
278
279 global $DIC;
280 $ilDB = $DIC['ilDB'];
281 $res = $ilDB->query(
282 "SELECT COUNT(*) cnt "
284 . " WHERE id = " . $ass1->getId()
285 );
286 $rec = $ilDB->fetchAssoc($res);
287 $this->assertEquals(0, $rec["cnt"]);
288 }
289}
An exception for terminatinating execution or to throw for unit testing.
Class ilObjStudyProgramme.
static createInstance()
Create an instance of ilObjStudyProgramme, put in cache.
Mock for object factory.
Definition: mocks.php:69
Mock classes for tests.
Definition: mocks.php:21
TestCase for the assignment of users to a programme.
testRemoveOnRootNodeOnly1()
@expectedException ilException
testNoAssignmentWhenDraft()
@expectedException ilException
testNoAssignmentWhenOutdated()
@expectedException ilException
testNoAssignementWhenNotCreated()
@expectedException ilException
testRemoveOnRootNodeOnly2()
@expectedException ilException
$user
Definition: migrateto20.php:57
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
global $ilDB