ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilObjStudyProgrammeTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2009 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once(__DIR__."/mocks.php");
25 
36  protected $backupGlobals = FALSE;
37 
38  protected function setUp() {
39  PHPUnit_Framework_Error_Deprecated::$enabled = FALSE;
40 
41  require_once("./Modules/StudyProgramme/classes/class.ilObjStudyProgramme.php");
42 
43  include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
44  ilUnitUtil::performInitialisation();
45 
46  $this->root_object = ilObjStudyProgramme::createInstance();
47  $this->root_object_obj_id = $this->root_object->getId();
48  $this->root_object_ref_id = $this->root_object->getRefId();
49  $this->root_object->putInTree(ROOT_FOLDER_ID);
50 
51  //
52 
53  global $DIC;
54  $tree = $DIC['tree'];
55  $this->tree = $tree;
56 
57  global $DIC;
58  $objDefinition = $DIC['objDefinition'];
59  $this->obj_definition = $objDefinition;
60  }
61 
62  protected function tearDown() {
63  if ($this->root_object) {
64  $this->root_object->delete();
65  }
66  }
67 
71  public function testCreation() {
72  $this->assertNotEmpty($this->root_object_obj_id);
73  $this->assertGreaterThan(0, $this->root_object_obj_id);
74 
75  $this->assertNotEmpty($this->root_object_ref_id);
76  $this->assertGreaterThan(0, $this->root_object_ref_id);
77 
78  $this->assertTrue($this->tree->isInTree($this->root_object_ref_id));
79  }
80 
81  public function testDefaults() {
82  $this->assertEquals($this->root_object->getStatus(), ilStudyProgramme::STATUS_DRAFT);
83  }
84 
90  public function testLoadByObjId() {
91  $loaded = new ilObjStudyProgramme($this->root_object_obj_id, false);
92  $orig = $this->root_object;
93  $load_ref_id = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
94 
95  $this->assertNotNull($loaded);
96  $this->assertGreaterThan(0, $loaded->getId());
97  $this->assertEquals( $orig->getId(), $loaded->getId());
98  $this->assertEquals( $orig->getLastChange()->get(IL_CAL_DATETIME)
99  , $loaded->getLastChange()->get(IL_CAL_DATETIME)
100  );
101  $this->assertEquals( $orig->getPoints(), $loaded->getPoints());
102  $this->assertEquals( $orig->getLPMode(), $loaded->getLPMode());
103  $this->assertEquals( $orig->getStatus(), $loaded->getStatus());
104  }
105 
111  public function testLoadByRefId() {
112  $loaded = new ilObjStudyProgramme($this->root_object_ref_id);
113  $orig = $this->root_object;
114 
115  $this->assertNotNull($loaded);
116  $this->assertGreaterThan(0, $loaded->getId());
117  $this->assertEquals( $orig->getId(), $loaded->getId());
118  $this->assertEquals( $orig->getLastChange()->get(IL_CAL_DATETIME)
119  , $loaded->getLastChange()->get(IL_CAL_DATETIME)
120  );
121  $this->assertEquals( $orig->getPoints(), $loaded->getPoints());
122  $this->assertEquals( $orig->getLPMode(), $loaded->getLPMode());
123  $this->assertEquals( $orig->getStatus(), $loaded->getStatus());
124  }
125 
131  public function testGetInstanceByRefId() {
132  require_once("Modules/StudyProgramme/classes/class.ilObjStudyProgrammeCache.php");
133 
134  ilObjStudyProgrammeCache::singleton()->test_clear();
135  $this->assertTrue(ilObjStudyProgrammeCache::singleton()->test_isEmpty());
136 
137  $loaded = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
138  $orig = $this->root_object;
139 
140  $this->assertNotNull($loaded);
141  $this->assertGreaterThan(0, $loaded->getId());
142  $this->assertEquals( $orig->getId(), $loaded->getId());
143  $this->assertEquals( $orig->getLastChange()->get(IL_CAL_DATETIME)
144  , $loaded->getLastChange()->get(IL_CAL_DATETIME)
145  );
146  $this->assertEquals( $orig->getPoints(), $loaded->getPoints());
147  $this->assertEquals( $orig->getLPMode(), $loaded->getLPMode());
148  $this->assertEquals( $orig->getStatus(), $loaded->getStatus());
149  }
150 
156  public function testSettings() {
157  $obj = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
158 
159  $obj->setPoints(10);
160  $obj->setStatus(ilStudyProgramme::STATUS_ACTIVE);
161  $obj->update();
162 
163  $obj = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
164 
165  $this->assertEquals(10, $obj->getPoints());
166  $this->assertEquals(ilStudyProgramme::STATUS_ACTIVE, $obj->getStatus());
167 
168  $midnight = strtotime("today midnight");
169  $this->assertGreaterThan($midnight, $obj->getLastChange()->getUnixTime());
170  }
171 
177  public function testDelete() {
178  $deleted_object = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
179 
180  $this->assertTrue($deleted_object->delete());
181  }
182 
186  protected function createSmallTree() {
187  $first_node = ilObjStudyProgramme::createInstance();
188  $second_node = ilObjStudyProgramme::createInstance();
189  $third_node = ilObjStudyProgramme::createInstance();
190 
191  $this->root_object->addNode($first_node);
192  $this->root_object->addNode($second_node);
193  $this->root_object->addNode($third_node);
194 
195  $third_first_node = ilObjStudyProgramme::createInstance();
196  $third_node->addNode($third_first_node);
197  }
198 
204  public function testTreeCreation() {
205  $this->createSmallTree();
206  $this->assertEquals(3, $this->root_object->getAmountOfChildren());
207  }
208 
215  public function testTreeGetChildren() {
216  $this->createSmallTree();
217 
218  $children = $this->root_object->getChildren();
219  $this->assertEquals(3, count($children), "getChildren()");
220 
221  $children = ilObjStudyProgramme::getAllChildren($this->root_object_ref_id);
222  $this->assertEquals(4, count($children), "ilObjStudyProgramme::getAllChildren(".$this->root_object_ref_id.")");
223 
224  $this->assertTrue($this->root_object->hasChildren(), "hasChildren()");
225  $this->assertEquals(3, $this->root_object->getAmountOfChildren(), "getAmountOfChildren()");
226 
227  $this->assertFalse($children[0]->hasChildren(), "hasChildren()");
228  $this->assertEquals(0, $children[0]->getAmountOfChildren(), "getAmountOfChildren()");
229  $this->assertEquals(0, count($children[0]->getChildren()));
230  }
231 
237  public function testTreeGetParent() {
238  $this->createSmallTree();
239  $children = $this->root_object->getChildren();
240 
241  $child = $children[0];
242  $this->assertNotNull($child->getParent());
243  $this->assertNull($this->root_object->getParent());
244  }
245 
249  public function testTreeGetParents() {
250  $this->createSmallTree();
252  $children = $this->root_object->getChildren();
253  $children[0]->addNode($node3);
254 
255  $parents = $node3->getParents();
256  $parent_ids = array_map(function($node) {
257  return $node->getId();
258  }, $parents);
259  $parent_ids_expected = array( $this->root_object->getId()
260  , $children[0]->getId()
261  );
262 
263  $this->assertEquals($parent_ids_expected, $parent_ids);
264  }
265 
271  public function testTreeDepth() {
272  $this->createSmallTree();
273  $children = $this->root_object->getChildren();
274 
275  $child = $children[0];
276 
277  $this->assertEquals(1, $child->getDepth());
278  }
279 
285  public function testTreeGetRoot() {
286  $this->createSmallTree();
287  $children = $this->root_object->getChildren();
288  $child = $children[0];
289 
290  $this->assertEquals($this->root_object->getId(), $child->getRoot()->getId());
291  }
292 
298  public function testApplyToSubTreeNodes() {
299  $this->createSmallTree();
300  $children = $this->root_object->getChildren();
301 
302  $val = 0;
303  $this->root_object->applyToSubTreeNodes(function($node) use (&$val) {
304  $val += $node->getPoints();
305  });
306 
307  // We didn't make modification on the points of the nodes.
308  $this->assertEquals($val, 5 * ilStudyProgramme::DEFAULT_POINTS);
309 
310 
311  $this->root_object->setPoints(1);
312  $children[0]->setPoints(2);
313  $children[1]->setPoints(4);
314  $children[2]->setPoints(1);
315 
316  $third_level = $children[2]->getChildren();
317  $third_level[0]->setPoints(2);
318 
319  $val = 0;
320  $this->root_object->applyToSubTreeNodes(function($node) use (&$val) {
321  $val += $node->getPoints();
322  });
323 
324  $this->assertEquals($val, 10);
325  }
326 
332  public function testAddNode() {
333  $this->createSmallTree();
334 
335  $children = $this->root_object->getChildren();
336  $child = $children[0];
337  $grandchild = new ilObjStudyProgramme();
338  $grandchild->create();
339  $child->addNode($grandchild);
340 
341  $this->assertEquals($child->getId(), $grandchild->getParent()->getId());
342  $this->assertEquals($this->root_object->getId(), $grandchild->getRoot()->getId(),
343  "Root of grandchild is root of tree.");
344  $this->assertEquals(1, $child->getAmountOfChildren());
345  $this->assertEquals(2, $grandchild->getDepth());
346  $this->assertEquals($child->getLPMode(), ilStudyProgramme::MODE_POINTS);
347  }
348 
354  public function testRemoveNode() {
355  $this->createSmallTree();
356 
357  $children = $this->root_object->getChildren();
358  $child = $children[0];
359  $this->root_object->removeNode($child);
360 
361  // Is not in tree anymore...
362  $raised = false;
363  try {
364  $child->getParent();
365  }
366  catch (ilStudyProgrammeTreeException $e) {
367  $raised = true;
368  }
369  $this->assertTrue($raised, "Child does not raise on getParent after it is removed.");
370 
371  $this->assertEquals(2, $this->root_object->getAmountOfChildren());
372 
373  // Can't be removed a second time...
374  $raised = false;
375  try {
376  $this->root_object->removeNode($child);
377  }
378  catch (ilStudyProgrammeTreeException $e) {
379  $raised = true;
380  }
381  $this->assertTrue($raised, "Child can be removed two times.");
382  }
383 
389  public function testAddLeaf() {
390  $this->createSmallTree();
391  $mock_leaf = new ilStudyProgrammeLeafMock();
392 
393  $children = $this->root_object->getChildren();
394  $first_child = $children[0];
395 
396  $first_child->addLeaf($mock_leaf);
397 
398  // We use our mock factory, since the original factory won't know how
399  // to create our mock leaf.
400  $first_child->object_factory = new ilObjectFactoryWrapperMock();
401 
402  $this->assertEquals(3, $this->root_object->getAmountOfChildren(), "getAmountOfChildren()");
403  // Check if StudyProgrammes are not counted as LP-Children
404  $this->assertEquals(0, $this->root_object->getAmountOfLPChildren(), "getAmountOfLPChildren() on root");
405  $this->assertEquals(false, $this->root_object->hasLPChildren(), "hasLPChildren() on root");
406 
407  $this->assertEquals(1, $first_child->getAmountOfLPChildren(), "getAmountOfLPChildren() on first child");
408  $this->assertEquals(true, $first_child->hasLPChildren(), "hasLPChildren() on first child");
409  $this->assertEquals($first_child->getLPMode(), ilStudyProgramme::MODE_LP_COMPLETED);
410 
411  $lp_children = $first_child->getLPChildren();
412  $this->assertEquals(1, count($lp_children));
413  $this->assertEquals($mock_leaf->getId(), $lp_children[0]->getId());
414  }
415 
421  public function testRemoveLeaf() {
422  $mock_leaf = new ilStudyProgrammeLeafMock();
423  $this->root_object->addLeaf($mock_leaf);
424 
425  $this->root_object->removeLeaf($mock_leaf);
426  $this->assertEquals(0, $this->root_object->getAmountOfChildren(), "getAmountOfChildren()");
427  $this->assertEquals(0, $this->root_object->getAmountOfLPChildren(), "getAmountOfLPChildren()");
428 
429  $lp_children = $this->root_object->getLPChildren();
430  $this->assertEquals(0, count($lp_children));
431  }
432 
437  public function testAddWrongChildType() {
438  $this->createSmallTree();
439  $children = $this->root_object->getChildren();
440  $child_n = $children[0];
441  $child_l = $children[1];
442 
443  $mock_leaf1 = new ilStudyProgrammeLeafMock();
444  $mock_leaf2 = new ilStudyProgrammeLeafMock();
445  $node1 = new ilObjStudyProgramme();
446  $node2 = new ilObjStudyProgramme();
447  $node1->create();
448  $node2->create();
449 
450  $child_n->addNode($node1);
451  $child_l->addLeaf($mock_leaf1);
452 
453  $raised = false;
454  try {
455  $child_n->addLeaf($mock_leaf2);
456  }
457  catch (ilStudyProgrammeTreeException $e) {
458  $raised = true;
459  }
460  $this->assertTrue($raised, "Could add leaf to program containing node.");
461 
462  $raised = false;
463  try {
464  $child_n->addLeaf($mock_leaf2);
465  }
466  catch (ilStudyProgrammeTreeException $e) {
467  $raised = true;
468  }
469  $this->assertTrue($raised, "Could add node to program containing leaf.");
470  }
471 
475  public function testMoveTo() {
476  $this->createSmallTree();
477  $children = $this->root_object->getChildren();
478  $child_l = $children[0];
479  $child_r = $children[1];
480  $child_m = $children[2];
481 
482  $child_r->moveTo($child_l);
483 
484  $this->assertEquals(2, $child_r->getDepth());
485  $this->assertEquals($child_l->getId(), $child_r->getParent()->getId());
486  $this->assertEquals(2, $this->root_object->getAmountOfChildren());
487  $this->assertEquals(1, $child_l->getAmountOfChildren());
488 
489  // test recursive moving
490  $this->assertEquals(1, $child_m->getAmountOfChildren());
491 
492  $child_m->moveTo($child_r);
493 
494  $m_children = $child_m->getChildren();
495  $first_third_node = $m_children[0];
496 
497  $this->assertEquals(3, $child_m->getDepth());
498  $this->assertEquals(1, $child_m->getAmountOfChildren());
499  $this->assertNotNull($first_third_node);
500  $this->assertEquals(4, $first_third_node->getDepth());
501  $this->assertEquals($child_m->getId(), $first_third_node->getParent()->getId());
502 
503  $this->assertEquals(1, $this->root_object->getAmountOfChildren());
504  $this->assertEquals(3, count(ilObjStudyProgramme::getAllChildren($child_l->getRefId())));
505  }
506 
511  $this->createSmallTree();
512  $children = $this->root_object->getChildren();
513  $child_l = $children[0];
514  $child_r = $children[1];
515  $this->root_object->setStatus(ilStudyProgramme::STATUS_ACTIVE);
516  $child_l->setStatus(ilStudyProgramme::STATUS_ACTIVE);
517  $child_r->setStatus(ilStudyProgramme::STATUS_ACTIVE);
518 
519  $user = new ilObjUser();
520  $user->create();
521 
522  $child_l->assignUser($user->getId());
523  $this->root_object->removeNode($child_l);
524  }
525 
527  $this->createSmallTree();
528  $children = $this->root_object->getChildren();
529  $child_l = $children[0];
530  $child_r = $children[1];
531  $this->root_object->setStatus(ilStudyProgramme::STATUS_ACTIVE);
532  $child_l->setStatus(ilStudyProgramme::STATUS_ACTIVE);
533  $child_r->setStatus(ilStudyProgramme::STATUS_OUTDATED);
534 
535  $user = new ilObjUser();
536  $user->create();
537 
538  $this->root_object->assignUser($user->getId());
539  $this->root_object->removeNode($child_r);
540  }
541 
542  public function testCreateableSubObjects() {
543  $this->createSmallTree();
544  $children = $this->root_object->getChildren();
545  $child_l = $children[0];
546 
547  $all_possible_subobjects = $this->root_object->getPossibleSubObjects();
548  // don't take rolfs into account, we don't need rolf anymore
549  unset($all_possible_subobjects["rolf"]);
550 
551  // this is course reference and training programme
552  $this->assertCount(2, $all_possible_subobjects);
553  $this->assertArrayHasKey("prg", $all_possible_subobjects);
554  $this->assertArrayHasKey("crsr", $all_possible_subobjects);
555 
556  // root already contains program nodes, so course ref is forbidden
557  $subobjs = ilObjStudyProgramme::getCreatableSubObjects($all_possible_subobjects, $this->root_object->getRefId());
558  $this->assertCount(1, $subobjs);
559  $this->assertArrayHasKey("prg", $subobjs);
560 
561  // first node contains nothing, so course ref and program node are allowed
562  $subobjs = ilObjStudyProgramme::getCreatableSubObjects($all_possible_subobjects, $child_l->getRefId());
563  $this->assertCount(2, $subobjs);
564  $this->assertArrayHasKey("prg", $subobjs);
565  $this->assertArrayHasKey("crsr", $subobjs);
566 
567  $mock_leaf = new ilStudyProgrammeLeafMock();
568  $children = $this->root_object->getChildren();
569  $child_l->object_factory = new ilObjectFactoryWrapperMock();
570  $child_l->addLeaf($mock_leaf);
571 
572  // Now we added a leaf, so no program nodes are allowed anymore.
573  $subobjs = ilObjStudyProgramme::getCreatableSubObjects($all_possible_subobjects, $child_l->getRefId());
574  $this->assertCount(1, $subobjs);
575  $this->assertArrayHasKey("crsr", $subobjs);
576  }
577 
579  $all_possible_subobjects = $this->obj_definition->getSubObjects("prg");
580  // don't take rolfs into account, we don't need rolf anymore
581  unset($all_possible_subobjects["rolf"]);
582  $this->assertEquals( $all_possible_subobjects
583  , ilObjStudyProgramme::getCreatableSubObjects($all_possible_subobjects, null)
584  );
585  }
586 
592  }
593 
595  $this->root_object->delete();
596  $this->root_object = null;
597 
598  global $DIC;
599  $ilDB = $DIC['ilDB'];
600  $res = $ilDB->query( "SELECT COUNT(*) cnt "
602  ." WHERE obj_id = ".$this->root_object_obj_id
603  );
604  $rec = $ilDB->fetchAssoc($res);
605  $this->assertEquals(0, $rec["cnt"]);
606  }
607 
608  public function testCreatePermissionExists() {
609  // Ask for permission id for creation of "foobar" to check assumption
610  // that lookupCreateOperationIds just drops unknown object types.
611  $op_ids = ilRbacReview::lookupCreateOperationIds(array("prg", "foobar"));
612  $this->assertCount(1, $op_ids);
613  }
614 }
testTreeGetChildren()
Test function to get children or information about them.
testTreeGetParent()
Test getParent on ilObjStudyProgramme.
testTreeDepth()
Test getDepth on ilObjStudyProgramme.
static getAllChildren($a_ref_id)
Get a list of all ilObjStudyProgrammes in the subtree starting at $a_ref_id.
testLoadByRefId()
Test loading of ilObjStudyProgramme with ref_id.
testRemoveNode()
Test on removeNode.
const IL_CAL_DATETIME
testApplyToSubTreeNodes()
Test applyToSubTreeNodes on ilObjStudyProgramme.
Mock classes for tests.
Definition: mocks.php:20
testSettings()
Test tings on ilObjStudyProgramme.
testGetInstanceByRefId()
Test loading over getInstance.
testCreatableSubObjectsRaisesOnNonProgramRef()
ilException
static lookupCreateOperationIds($a_type_arr)
Lookup operation ids.
static getInstanceByRefId($a_ref_id)
Get an instance of ilObjStudyProgramme, use cache.
testTreeCreation()
Test creating a small tree.
createSmallTree()
Creates a small tree, used by various tests.
Class ilObjStudyProgramme.
Create styles array
The data for the language used.
testTreeGetRoot()
Test getRoot on ilObjStudyProgramme.
static getCreatableSubObjects($a_subobjects, $a_ref_id)
Filter the list of possible subobjects for the objects that actually could be created on a concrete n...
testCreation()
Test creation of ilObjStudyProgramme.
TestCase for the ilObjStudyProgramme needsInstalledILIAS.
testDelete()
Test deletion of a ilObjStudyProgramme.
testCantRemoveNodeWithRelevantProgress()
ilStudyProgrammeTreeException
global $ilDB
testLoadByObjId()
Test loading of ilObjStudyProgramme with obj_id.
static createInstance()
Create an instance of ilObjStudyProgramme, put in cache.
global $DIC
testRemoveLeaf()
Test on removeLead.
Exception is thrown when invariants on the program tree would be violated by manipulation of tree...
testTreeGetParents()
testTreeCreation
Mock for object factory.
Definition: mocks.php:68
testAddWrongChildType()
Test whether nodes can only be added when there is no leaf in the parent and vice versa...