ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
15 protected $backupGlobals = FALSE;
16
17 protected function setUp() {
18 PHPUnit_Framework_Error_Deprecated::$enabled = FALSE;
19
20 require_once("./Modules/StudyProgramme/classes/class.ilObjStudyProgramme.php");
21
22 include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
23 ilUnitUtil::performInitialisation();
24
26 $this->root->putInTree(ROOT_FOLDER_ID);
27 $this->root->object_factory = new ilObjectFactoryWrapperMock();
28
31
32 $this->leaf1 = new ilStudyProgrammeLeafMock();
33 $this->leaf2 = new ilStudyProgrammeLeafMock();
34
35 $this->root->addNode($this->node1);
36 $this->root->addNode($this->node2);
37 $this->node1->addLeaf($this->leaf1);
38 $this->node2->addLeaf($this->leaf2);
39
40 global $tree;
41 $this->tree = $tree;
42 }
43
44 protected function tearDown() {
45 if ($this->root) {
46 $this->root->delete();
47 }
48 }
49
50
51 protected function newUser() {
52 $user = new ilObjUser();
53 $user->create();
54 return $user;
55 }
56
60 public function testNoAssignmentWhenDraft() {
61 $user = $this->newUser();
62 $this->assertEquals(ilStudyProgramme::STATUS_DRAFT, $this->root->getStatus());
63 $this->root->assignUser($user->getId());
64 }
65
69 public function testNoAssignmentWhenOutdated() {
70 $user = $this->newUser();
71
72 $this->root->setStatus(ilStudyProgramme::STATUS_OUTDATED);
73 $this->assertEquals(ilStudyProgramme::STATUS_OUTDATED, $this->root->getStatus());
74 $this->root->assignUser($user->getId());
75 }
76
81 $user = $this->newUser();
82 $prg = new ilObjStudyProgramme();
83 $prg->assignUser($user->getId());
84 }
85
86 public function testUserId() {
87 $user1 = $this->newUser();
88 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
89 $ass = $this->root->assignUser($user1->getId());
90 $this->assertEquals($user1->getId(), $ass->getUserId());
91 }
92
93 public function testHasAssignmentOf() {
94 $user1 = $this->newUser();
95 $user2 = $this->newUser();
96
97 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
98
99 $this->root->assignUser($user1->getId());
100 $this->assertTrue($this->root->hasAssignmentOf($user1->getId()));
101 $this->assertTrue($this->node1->hasAssignmentOf($user1->getId()));
102 $this->assertTrue($this->node2->hasAssignmentOf($user1->getId()));
103
104 $this->assertFalse($this->root->hasAssignmentOf($user2->getId()));
105 $this->assertFalse($this->node1->hasAssignmentOf($user2->getId()));
106 $this->assertFalse($this->node2->hasAssignmentOf($user2->getId()));
107 }
108
109 public function testGetAmountOfAssignments() {
110 $user1 = $this->newUser();
111 $user2 = $this->newUser();
112 $user3 = $this->newUser();
113 $user4 = $this->newUser();
114
115 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
116 $this->node1->setStatus(ilStudyProgramme::STATUS_ACTIVE);
117 $this->node2->setStatus(ilStudyProgramme::STATUS_ACTIVE);
118
119 $this->assertEquals(0, $this->root->getAmountOfAssignmentsOf($user1->getId()));
120
121 $this->root->assignUser($user2->getId());
122 $this->assertEquals(1, $this->root->getAmountOfAssignmentsOf($user2->getId()));
123 $this->assertEquals(1, $this->node1->getAmountOfAssignmentsOf($user2->getId()));
124 $this->assertEquals(1, $this->node2->getAmountOfAssignmentsOf($user2->getId()));
125
126 $this->root->assignUser($user3->getId());
127 $this->root->assignUser($user3->getId());
128 $this->assertEquals(2, $this->root->getAmountOfAssignmentsOf($user3->getId()));
129 $this->assertEquals(2, $this->node1->getAmountOfAssignmentsOf($user3->getId()));
130 $this->assertEquals(2, $this->node2->getAmountOfAssignmentsOf($user3->getId()));
131
132 $this->root->assignUser($user4->getId());
133 $this->node1->assignUser($user4->getId());
134 $this->assertEquals(1, $this->root->getAmountOfAssignmentsOf($user4->getId()));
135 $this->assertEquals(2, $this->node1->getAmountOfAssignmentsOf($user4->getId()));
136 $this->assertEquals(1, $this->node2->getAmountOfAssignmentsOf($user4->getId()));
137 }
138
139 public function testGetAssignmentsOf() {
140 $user1 = $this->newUser();
141 $user2 = $this->newUser();
142
143 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
144 $this->node1->setStatus(ilStudyProgramme::STATUS_ACTIVE);
145
146 $this->assertEquals(0, count($this->root->getAssignmentsOf($user1->getId())));
147 $this->assertEquals(0, count($this->node1->getAssignmentsOf($user1->getId())));
148 $this->assertEquals(0, count($this->node2->getAssignmentsOf($user1->getId())));
149
150 $this->root->assignUser($user2->getId());
151 $this->node1->assignUser($user2->getId());
152
153 $root_ass = $this->root->getAssignmentsOf($user2->getId());
154 $node1_ass = $this->node1->getAssignmentsOf($user2->getId());
155 $node2_ass = $this->node2->getAssignmentsOf($user2->getId());
156
157 $this->assertEquals(1, count($root_ass));
158 $this->assertEquals(2, count($node1_ass));
159 $this->assertEquals(1, count($node2_ass));
160
161 $this->assertEquals($this->root->getId(), $root_ass[0]->getStudyProgramme()->getId());
162 $this->assertEquals($this->root->getId(), $node2_ass[0]->getStudyProgramme()->getId());
163
164 $node1_ass_prg_ids = array_map(function($ass) {
165 return $ass->getStudyProgramme()->getId();
166 }, $node1_ass);
167 $this->assertContains($this->root->getId(), $node1_ass_prg_ids);
168 $this->assertContains($this->node1->getId(), $node1_ass_prg_ids);
169 }
170
174 public function testRemoveOnRootNodeOnly1() {
175 $user = $this->newUser();
176
177 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
178
179 $ass1 = $this->root->assignUser($user->getId());
180 $this->node1->removeAssignment($ass1);
181 }
182
186 public function testRemoveOnRootNodeOnly2() {
187 $user = $this->newUser();
188
189 $this->node1->setStatus(ilStudyProgramme::STATUS_ACTIVE);
190
191 $ass1 = $this->node1->assignUser($user->getId());
192 $this->root->removeAssignment($ass1);
193 }
194
195 public function testRemoveAssignment1() {
196 $user = $this->newUser();
197
198 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
199
200 $ass1 = $this->root->assignUser($user->getId());
201 $this->root->removeAssignment($ass1);
202 $this->assertFalse($this->root->hasAssignmentOf($user->getId()));
203 }
204
205 public function testRemoveAssignment2() {
206 $user = $this->newUser();
207
208 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
209
210 $ass1 = $this->root->assignUser($user->getId());
211 $ass1->deassign();
212 $this->assertFalse($this->root->hasAssignmentOf($user->getId()));
213 }
214
215 public function testGetAssignments() {
216 $user1 = $this->newUser();
217 $user2 = $this->newUser();
218
219 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
220 $this->node1->setStatus(ilStudyProgramme::STATUS_ACTIVE);
221
222 $ass1 = $this->root->assignUser($user1->getId());
223 $ass2 = $this->node1->assignUser($user2->getId());
224
225 $asses = $this->node1->getAssignments();
226 $ass_ids = array_map(function($ass) {
227 return $ass->getId();
228 }, $asses);
229 $this->assertContains($ass1->getId(), $ass_ids);
230 $this->assertContains($ass2->getId(), $ass_ids);
231 }
232
234 $user1 = $this->newUser();
235 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
236 $ass = $this->root->assignUser($user1->getId());
237
238 $root_id = $this->root->getId();
239 $this->root->delete();
240 $this->root = null;
241
242 global $ilDB;
243 $res = $ilDB->query( "SELECT COUNT(*) cnt "
245 ." WHERE root_prg_id = ".$root_id
246 );
247 $rec = $ilDB->fetchAssoc($res);
248 $this->assertEquals(0, $rec["cnt"]);
249 }
250
252 $user = $this->newUser();
253 $this->root->setStatus(ilStudyProgramme::STATUS_ACTIVE);
254 $ass1 = $this->root->assignUser($user->getId());
255 $ass1->deassign();
256
257 global $ilDB;
258 $res = $ilDB->query( "SELECT COUNT(*) cnt "
260 ." WHERE id = ".$ass1->getId()
261 );
262 $rec = $ilDB->fetchAssoc($res);
263 $this->assertEquals(0, $rec["cnt"]);
264 }
265
266}
Class ilObjStudyProgramme.
static createInstance()
Create an instance of ilObjStudyProgramme, put in cache.
Mock for object factory.
Definition: mocks.php:66
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
global $ilDB