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