ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
6 
7 require_once(__DIR__ . "/mocks.php");
8 
18 {
19  protected $backupGlobals = false;
20 
21  protected function setUp() : void
22  {
23  require_once("./Modules/StudyProgramme/classes/class.ilObjStudyProgramme.php");
24  PHPUnit\Framework\Error\Deprecated::$enabled = false;
25 
26  global $DIC;
27  if (!$DIC) {
28  include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
29  try {
30  ilUnitUtil::performInitialisation();
31  } catch (Exception $e) {
32  }
33  }
34 
36  $this->root->putInTree(ROOT_FOLDER_ID);
37  $this->root->object_factory = new ilObjectFactoryWrapperMock();
38 
39  $this->node1 = ilObjStudyProgramme::createInstance();
40  $this->node2 = ilObjStudyProgramme::createInstance();
41 
42  $this->leaf1 = new ilStudyProgrammeLeafMock();
43  $this->leaf2 = new ilStudyProgrammeLeafMock();
44 
45  $this->root->addNode($this->node1);
46  $this->root->addNode($this->node2);
47  $this->node1->addLeaf($this->leaf1);
48  $this->node2->addLeaf($this->leaf2);
49 
50  global $DIC;
51  $tree = $DIC['tree'];
52  $this->tree = $tree;
53  }
54 
55  protected function tearDown() : void
56  {
57  if ($this->root) {
58  $this->root->delete();
59  }
60  }
61 
62 
63  protected function newUser()
64  {
65  $user = new ilObjUser();
66  $user->create();
67  return $user;
68  }
69 
73  public function testNoAssignmentWhenDraft()
74  {
75  $user = $this->newUser();
76  $this->assertEquals(ilStudyProgrammeSettings::STATUS_DRAFT, $this->root->getStatus());
77  $this->root->assignUser($user->getId(), 6);
78  }
79 
83  public function testNoAssignmentWhenOutdated()
84  {
85  $user = $this->newUser();
86 
87  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_OUTDATED);
88  $this->assertEquals(ilStudyProgrammeSettings::STATUS_OUTDATED, $this->root->getStatus());
89  $this->root->assignUser($user->getId(), 6);
90  }
91 
96  {
97  $user = $this->newUser();
98  $prg = new ilObjStudyProgramme();
99  $prg->assignUser($user->getId(), 6);
100  }
101 
102  public function testUserId()
103  {
104  $user1 = $this->newUser();
105  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
106  $ass = $this->root->assignUser($user1->getId(), 6);
107  $this->assertEquals($user1->getId(), $ass->getUserId());
108  }
109 
110  public function testHasAssignmentOf()
111  {
112  $user1 = $this->newUser();
113  $user2 = $this->newUser();
114 
115  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
116 
117  $this->root->assignUser($user1->getId(), 6);
118  $this->assertTrue($this->root->hasAssignmentOf($user1->getId()));
119  $this->assertTrue($this->node1->hasAssignmentOf($user1->getId()));
120  $this->assertTrue($this->node2->hasAssignmentOf($user1->getId()));
121 
122  $this->assertFalse($this->root->hasAssignmentOf($user2->getId()));
123  $this->assertFalse($this->node1->hasAssignmentOf($user2->getId()));
124  $this->assertFalse($this->node2->hasAssignmentOf($user2->getId()));
125  }
126 
127  public function testGetAmountOfAssignments()
128  {
129  $user1 = $this->newUser();
130  $user2 = $this->newUser();
131  $user3 = $this->newUser();
132  $user4 = $this->newUser();
133 
134  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
135  $this->node1->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
136  $this->node2->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
137 
138  $this->assertEquals(0, $this->root->getAmountOfAssignmentsOf($user1->getId()));
139 
140  $this->root->assignUser($user2->getId(), 6);
141  $this->assertEquals(1, $this->root->getAmountOfAssignmentsOf($user2->getId()));
142  $this->assertEquals(1, $this->node1->getAmountOfAssignmentsOf($user2->getId()));
143  $this->assertEquals(1, $this->node2->getAmountOfAssignmentsOf($user2->getId()));
144 
145  $this->root->assignUser($user3->getId(), 6);
146  $this->root->assignUser($user3->getId(), 6);
147  $this->assertEquals(2, $this->root->getAmountOfAssignmentsOf($user3->getId()));
148  $this->assertEquals(2, $this->node1->getAmountOfAssignmentsOf($user3->getId()));
149  $this->assertEquals(2, $this->node2->getAmountOfAssignmentsOf($user3->getId()));
150 
151  $this->root->assignUser($user4->getId(), 6);
152  $this->node1->assignUser($user4->getId(), 6);
153  $this->assertEquals(1, $this->root->getAmountOfAssignmentsOf($user4->getId()));
154  $this->assertEquals(2, $this->node1->getAmountOfAssignmentsOf($user4->getId()));
155  $this->assertEquals(1, $this->node2->getAmountOfAssignmentsOf($user4->getId()));
156  }
157 
158  public function testGetAssignmentsOf()
159  {
160  $user1 = $this->newUser();
161  $user2 = $this->newUser();
162 
163  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
164  $this->node1->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
165 
166  $this->assertEquals(0, count($this->root->getAssignmentsOf($user1->getId())));
167  $this->assertEquals(0, count($this->node1->getAssignmentsOf($user1->getId())));
168  $this->assertEquals(0, count($this->node2->getAssignmentsOf($user1->getId())));
169 
170  $this->root->assignUser($user2->getId(), 6);
171  $this->node1->assignUser($user2->getId(), 6);
172 
173  $root_ass = $this->root->getAssignmentsOf($user2->getId());
174  $node1_ass = $this->node1->getAssignmentsOf($user2->getId());
175  $node2_ass = $this->node2->getAssignmentsOf($user2->getId());
176 
177  $this->assertEquals(1, count($root_ass));
178  $this->assertEquals(2, count($node1_ass));
179  $this->assertEquals(1, count($node2_ass));
180 
181  $this->assertEquals($this->root->getId(), $root_ass[0]->getStudyProgramme()->getId());
182  $this->assertEquals($this->root->getId(), $node2_ass[0]->getStudyProgramme()->getId());
183 
184  $node1_ass_prg_ids = array_map(function ($ass) {
185  return $ass->getStudyProgramme()->getId();
186  }, $node1_ass);
187  $this->assertContains($this->root->getId(), $node1_ass_prg_ids);
188  $this->assertContains($this->node1->getId(), $node1_ass_prg_ids);
189  }
190 
194  public function testRemoveOnRootNodeOnly1()
195  {
196  $user = $this->newUser();
197 
198  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
199 
200  $ass1 = $this->root->assignUser($user->getId(), 6);
201  $this->node1->removeAssignment($ass1);
202  }
203 
207  public function testRemoveOnRootNodeOnly2()
208  {
209  $user = $this->newUser();
210 
211  $this->node1->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
212 
213  $ass1 = $this->node1->assignUser($user->getId(), 6);
214  $this->root->removeAssignment($ass1);
215  }
216 
217  public function testRemoveAssignment1()
218  {
219  $user = $this->newUser();
220 
221  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
222 
223  $ass1 = $this->root->assignUser($user->getId(), 6);
224  $this->root->removeAssignment($ass1);
225  $this->assertFalse($this->root->hasAssignmentOf($user->getId()));
226  }
227 
228  public function testRemoveAssignment2()
229  {
230  $user = $this->newUser();
231 
232  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
233 
234  $ass1 = $this->root->assignUser($user->getId(), 6);
235  $ass1->deassign();
236  $this->assertFalse($this->root->hasAssignmentOf($user->getId()));
237  }
238 
239  public function testGetAssignments()
240  {
241  $user1 = $this->newUser();
242  $user2 = $this->newUser();
243 
244  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
245  $this->node1->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
246 
247  $ass1 = $this->root->assignUser($user1->getId(), 6);
248  $ass2 = $this->node1->assignUser($user2->getId(), 6);
249 
250  $asses = $this->node1->getAssignments();
251  $ass_ids = array_map(function ($ass) {
252  return $ass->getId();
253  }, $asses);
254  //$this->assertContains($ass1->getId(), $ass_ids);
255  $this->assertContains($ass2->getId(), $ass_ids);
256  }
257 
258  public function testNoRestartDate()
259  {
260  $user1 = $this->newUser();
261 
264 
265  $prg1->putInTree(ROOT_FOLDER_ID);
266  $prg1->addNode($prg2);
267  $prg1->setValidityOfQualificationPeriod(110);
268  $prg1->update();
269 
270  $prg1->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
271  $prg2->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
272 
273  $ass1 = $prg1->assignUser($user1->getId(), 6);
274  $progress2 = $prg2->getProgressForAssignment($ass1->getId());
275  $progress2->markAccredited(6);
276  $progress1 = $prg1->getProgressForAssignment($ass1->getId());
277  $this->assertTrue($progress1->isSuccessful());
278 
279  $ass1_new = array_shift($prg1->getAssignmentsOf($user1->getId()));
280  $this->assertNull($ass1_new->getRestartDate());
281  }
282 
283  public function testRestartDate()
284  {
285  $user1 = $this->newUser();
286 
289 
290  $prg1->putInTree(ROOT_FOLDER_ID);
291  $prg1->addNode($prg2);
292  $prg1->setValidityOfQualificationPeriod(110);
293  $prg1->setRestartPeriod(10);
294  $prg1->update();
295 
296  $prg1->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
297  $prg2->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
298 
299  $ass1 = $prg1->assignUser($user1->getId(), 6);
300  $progress2 = $prg2->getProgressForAssignment($ass1->getId());
301  $progress2->markAccredited(6);
302  $progress1 = $prg1->getProgressForAssignment($ass1->getId());
303  $this->assertTrue($progress1->isSuccessful());
304 
305  $ass1_new = array_shift($prg1->getAssignmentsOf($user1->getId()));
306  $val_date = new DateTime();
307  $val_date->add(new DateInterval('P100D'));
308  $this->assertEquals(
309  $val_date->format('Ymd'),
310  $ass1_new->getRestartDate()->format('Ymd')
311  );
312  }
313 
314 
316  {
317  $user1 = $this->newUser();
318  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
319  $ass = $this->root->assignUser($user1->getId(), 6);
320 
321  $root_id = $this->root->getId();
322  $this->root->delete();
323  $this->root = null;
324 
325  global $DIC;
326  $ilDB = $DIC['ilDB'];
327  $res = $ilDB->query(
328  "SELECT COUNT(*) cnt "
330  . " WHERE root_prg_id = " . $root_id
331  );
332  $rec = $ilDB->fetchAssoc($res);
333  $this->assertEquals(0, $rec["cnt"]);
334  }
335 
337  {
338  $user = $this->newUser();
339  $this->root->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
340  $ass1 = $this->root->assignUser($user->getId(), 6);
341  $ass1->deassign();
342 
343  global $DIC;
344  $ilDB = $DIC['ilDB'];
345  $res = $ilDB->query(
346  "SELECT COUNT(*) cnt "
348  . " WHERE id = " . $ass1->getId()
349  );
350  $rec = $ilDB->fetchAssoc($res);
351  $this->assertEquals(0, $rec["cnt"]);
352  }
353 
354  public function testRstartAssignment()
355  {
356  $user1 = $this->newUser();
357 
360 
361  $prg1->putInTree(ROOT_FOLDER_ID);
362  $prg1->addNode($prg2);
363 
364  $prg1->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
365  $prg2->setStatus(ilStudyProgrammeSettings::STATUS_ACTIVE);
366 
367  $ass1 = $prg1->assignUser($user1->getId(), 6);
368 
369  $progress1 = $prg1->getProgressForAssignment($ass1->getId());
370  $progress1->markFailed(6);
371 
372  $this->assertEquals(
373  $prg1->getProgressForAssignment($ass1->getId())->getStatus(),
375  );
376  $this->assertEquals(
377  $prg2->getProgressForAssignment($ass1->getId())->getStatus(),
379  );
380 
381  $ass2 = $ass1->restartAssignment();
382  $this->assertEquals(
383  $prg1->getProgressForAssignment($ass2->getId())->getStatus(),
385  );
386  $this->assertEquals(
387  $prg2->getProgressForAssignment($ass2->getId())->getStatus(),
389  );
390 
391  $this->assertNotEquals($ass2->getId(), $ass1->getId());
392  $this->assertNotEquals(
393  $prg1->getProgressForAssignment($ass2->getId()),
394  $prg1->getProgressForAssignment($ass1->getId())
395  );
396  $this->assertNotEquals(
397  $prg2->getProgressForAssignment($ass2->getId()),
398  $prg2->getProgressForAssignment($ass1->getId())
399  );
400 
401  $assignments = $prg1->getAssignmentsOf($user1->getId());
402  $this->assertCount(2, $assignments);
403  foreach ($assignments as $ass) {
404  if ($ass->getId() === $ass1->getId()) {
405  $this->assertEquals(
406  $ass->getRestartedAssignmentId(),
407  $ass2->getId()
408  );
409  } elseif ($ass->getId() === $ass2->getId()) {
410  $this->assertEquals(
411  $ass->getRestartedAssignmentId(),
413  );
414  } else {
415  $this->assertFalse('there are more assignments than expected');
416  }
417  }
418  }
419 }
Mock classes for tests.
Definition: mocks.php:20
const ROOT_FOLDER_ID
Definition: constants.php:30
foreach($_POST as $key=> $value) $res
global $DIC
Definition: goto.php:24
TestCase for the assignment of users to a programme.
global $ilDB
static createInstance()
Create an instance of ilObjStudyProgramme, put in cache.
Mock for object factory.
Definition: mocks.php:68