ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5
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 
35  protected $backupGlobals = FALSE;
36 
37  protected function setUp() {
38  PHPUnit_Framework_Error_Deprecated::$enabled = FALSE;
39 
40  require_once("./Modules/StudyProgramme/classes/class.ilObjStudyProgramme.php");
41 
42  include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
43  ilUnitUtil::performInitialisation();
44 
45  $this->root_object = ilObjStudyProgramme::createInstance();
46  $this->root_object_obj_id = $this->root_object->getId();
47  $this->root_object_ref_id = $this->root_object->getRefId();
48  $this->root_object->putInTree(ROOT_FOLDER_ID);
49 
50  //
51 
52  global $tree;
53  $this->tree = $tree;
54 
55  global $objDefinition;
56  $this->obj_definition = $objDefinition;
57  }
58 
59  protected function tearDown() {
60  if ($this->root_object) {
61  $this->root_object->delete();
62  }
63  }
64 
68  public function testCreation() {
69  $this->assertNotEmpty($this->root_object_obj_id);
70  $this->assertGreaterThan(0, $this->root_object_obj_id);
71 
72  $this->assertNotEmpty($this->root_object_ref_id);
73  $this->assertGreaterThan(0, $this->root_object_ref_id);
74 
75  $this->assertTrue($this->tree->isInTree($this->root_object_ref_id));
76  }
77 
78  public function testDefaults() {
79  $this->assertEquals($this->root_object->getStatus(), ilStudyProgramme::STATUS_DRAFT);
80  }
81 
87  public function testLoadByObjId() {
88  $loaded = new ilObjStudyProgramme($this->root_object_obj_id, false);
89  $orig = $this->root_object;
90  $load_ref_id = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
91 
92  $this->assertNotNull($loaded);
93  $this->assertGreaterThan(0, $loaded->getId());
94  $this->assertEquals( $orig->getId(), $loaded->getId());
95  $this->assertEquals( $orig->getLastChange()->get(IL_CAL_DATETIME)
96  , $loaded->getLastChange()->get(IL_CAL_DATETIME)
97  );
98  $this->assertEquals( $orig->getPoints(), $loaded->getPoints());
99  $this->assertEquals( $orig->getLPMode(), $loaded->getLPMode());
100  $this->assertEquals( $orig->getStatus(), $loaded->getStatus());
101  }
102 
108  public function testLoadByRefId() {
109  $loaded = new ilObjStudyProgramme($this->root_object_ref_id);
110  $orig = $this->root_object;
111 
112  $this->assertNotNull($loaded);
113  $this->assertGreaterThan(0, $loaded->getId());
114  $this->assertEquals( $orig->getId(), $loaded->getId());
115  $this->assertEquals( $orig->getLastChange()->get(IL_CAL_DATETIME)
116  , $loaded->getLastChange()->get(IL_CAL_DATETIME)
117  );
118  $this->assertEquals( $orig->getPoints(), $loaded->getPoints());
119  $this->assertEquals( $orig->getLPMode(), $loaded->getLPMode());
120  $this->assertEquals( $orig->getStatus(), $loaded->getStatus());
121  }
122 
128  public function testGetInstanceByRefId() {
129  require_once("Modules/StudyProgramme/classes/class.ilObjStudyProgrammeCache.php");
130 
131  ilObjStudyProgrammeCache::singleton()->test_clear();
132  $this->assertTrue(ilObjStudyProgrammeCache::singleton()->test_isEmpty());
133 
134  $loaded = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
135  $orig = $this->root_object;
136 
137  $this->assertNotNull($loaded);
138  $this->assertGreaterThan(0, $loaded->getId());
139  $this->assertEquals( $orig->getId(), $loaded->getId());
140  $this->assertEquals( $orig->getLastChange()->get(IL_CAL_DATETIME)
141  , $loaded->getLastChange()->get(IL_CAL_DATETIME)
142  );
143  $this->assertEquals( $orig->getPoints(), $loaded->getPoints());
144  $this->assertEquals( $orig->getLPMode(), $loaded->getLPMode());
145  $this->assertEquals( $orig->getStatus(), $loaded->getStatus());
146  }
147 
153  public function testSettings() {
154  $obj = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
155 
156  $obj->setPoints(10);
157  $obj->setStatus(ilStudyProgramme::STATUS_ACTIVE);
158  $obj->update();
159 
160  $obj = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
161 
162  $this->assertEquals(10, $obj->getPoints());
163  $this->assertEquals(ilStudyProgramme::STATUS_ACTIVE, $obj->getStatus());
164 
165  $midnight = strtotime("today midnight");
166  $this->assertGreaterThan($midnight, $obj->getLastChange()->getUnixTime());
167  }
168 
174  public function testDelete() {
175  $deleted_object = ilObjStudyProgramme::getInstanceByRefId($this->root_object_ref_id);
176 
177  $this->assertTrue($deleted_object->delete());
178  }
179 
183  protected function createSmallTree() {
184  $first_node = ilObjStudyProgramme::createInstance();
185  $second_node = ilObjStudyProgramme::createInstance();
186  $third_node = ilObjStudyProgramme::createInstance();
187 
188  $this->root_object->addNode($first_node);
189  $this->root_object->addNode($second_node);
190  $this->root_object->addNode($third_node);
191 
192  $third_first_node = ilObjStudyProgramme::createInstance();
193  $third_node->addNode($third_first_node);
194  }
195 
201  public function testTreeCreation() {
202  $this->createSmallTree();
203  $this->assertEquals(3, $this->root_object->getAmountOfChildren());
204  }
205 
212  public function testTreeGetChildren() {
213  $this->createSmallTree();
214 
215  $children = $this->root_object->getChildren();
216  $this->assertEquals(3, count($children), "getChildren()");
217 
218  $children = ilObjStudyProgramme::getAllChildren($this->root_object_ref_id);
219  $this->assertEquals(4, count($children), "ilObjStudyProgramme::getAllChildren(".$this->root_object_ref_id.")");
220 
221  $this->assertTrue($this->root_object->hasChildren(), "hasChildren()");
222  $this->assertEquals(3, $this->root_object->getAmountOfChildren(), "getAmountOfChildren()");
223 
224  $this->assertFalse($children[0]->hasChildren(), "hasChildren()");
225  $this->assertEquals(0, $children[0]->getAmountOfChildren(), "getAmountOfChildren()");
226  $this->assertEquals(0, count($children[0]->getChildren()));
227  }
228 
234  public function testTreeGetParent() {
235  $this->createSmallTree();
236  $children = $this->root_object->getChildren();
237 
238  $child = $children[0];
239  $this->assertNotNull($child->getParent());
240  $this->assertNull($this->root_object->getParent());
241  }
242 
246  public function testTreeGetParents() {
247  $this->createSmallTree();
249  $children = $this->root_object->getChildren();
250  $children[0]->addNode($node3);
251 
252  $parents = $node3->getParents();
253  $parent_ids = array_map(function($node) {
254  return $node->getId();
255  }, $parents);
256  $parent_ids_expected = array( $this->root_object->getId()
257  , $children[0]->getId()
258  );
259 
260  $this->assertEquals($parent_ids_expected, $parent_ids);
261  }
262 
268  public function testTreeDepth() {
269  $this->createSmallTree();
270  $children = $this->root_object->getChildren();
271 
272  $child = $children[0];
273 
274  $this->assertEquals(1, $child->getDepth());
275  }
276 
282  public function testTreeGetRoot() {
283  $this->createSmallTree();
284  $children = $this->root_object->getChildren();
285  $child = $children[0];
286 
287  $this->assertEquals($this->root_object->getId(), $child->getRoot()->getId());
288  }
289 
295  public function testApplyToSubTreeNodes() {
296  $this->createSmallTree();
297  $children = $this->root_object->getChildren();
298 
299  $val = 0;
300  $this->root_object->applyToSubTreeNodes(function($node) use (&$val) {
301  $val += $node->getPoints();
302  });
303 
304  // We didn't make modification on the points of the nodes.
305  $this->assertEquals($val, 5 * ilStudyProgramme::DEFAULT_POINTS);
306 
307 
308  $this->root_object->setPoints(1);
309  $children[0]->setPoints(2);
310  $children[1]->setPoints(4);
311  $children[2]->setPoints(1);
312 
313  $third_level = $children[2]->getChildren();
314  $third_level[0]->setPoints(2);
315 
316  $val = 0;
317  $this->root_object->applyToSubTreeNodes(function($node) use (&$val) {
318  $val += $node->getPoints();
319  });
320 
321  $this->assertEquals($val, 10);
322  }
323 
329  public function testAddNode() {
330  $this->createSmallTree();
331 
332  $children = $this->root_object->getChildren();
333  $child = $children[0];
334  $grandchild = new ilObjStudyProgramme();
335  $grandchild->create();
336  $child->addNode($grandchild);
337 
338  $this->assertEquals($child->getId(), $grandchild->getParent()->getId());
339  $this->assertEquals($this->root_object->getId(), $grandchild->getRoot()->getId(),
340  "Root of grandchild is root of tree.");
341  $this->assertEquals(1, $child->getAmountOfChildren());
342  $this->assertEquals(2, $grandchild->getDepth());
343  $this->assertEquals($child->getLPMode(), ilStudyProgramme::MODE_POINTS);
344  }
345 
351  public function testRemoveNode() {
352  $this->createSmallTree();
353 
354  $children = $this->root_object->getChildren();
355  $child = $children[0];
356  $this->root_object->removeNode($child);
357 
358  // Is not in tree anymore...
359  $raised = false;
360  try {
361  $child->getParent();
362  }
363  catch (ilStudyProgrammeTreeException $e) {
364  $raised = true;
365  }
366  $this->assertTrue($raised, "Child does not raise on getParent after it is removed.");
367 
368  $this->assertEquals(2, $this->root_object->getAmountOfChildren());
369 
370  // Can't be removed a second time...
371  $raised = false;
372  try {
373  $this->root_object->removeNode($child);
374  }
375  catch (ilStudyProgrammeTreeException $e) {
376  $raised = true;
377  }
378  $this->assertTrue($raised, "Child can be removed two times.");
379  }
380 
386  public function testAddLeaf() {
387  $this->createSmallTree();
388  $mock_leaf = new ilStudyProgrammeLeafMock();
389 
390  $children = $this->root_object->getChildren();
391  $first_child = $children[0];
392 
393  $first_child->addLeaf($mock_leaf);
394 
395  // We use our mock factory, since the original factory won't know how
396  // to create our mock leaf.
397  $first_child->object_factory = new ilObjectFactoryWrapperMock();
398 
399  $this->assertEquals(3, $this->root_object->getAmountOfChildren(), "getAmountOfChildren()");
400  // Check if StudyProgrammes are not counted as LP-Children
401  $this->assertEquals(0, $this->root_object->getAmountOfLPChildren(), "getAmountOfLPChildren() on root");
402  $this->assertEquals(false, $this->root_object->hasLPChildren(), "hasLPChildren() on root");
403 
404  $this->assertEquals(1, $first_child->getAmountOfLPChildren(), "getAmountOfLPChildren() on first child");
405  $this->assertEquals(true, $first_child->hasLPChildren(), "hasLPChildren() on first child");
406  $this->assertEquals($first_child->getLPMode(), ilStudyProgramme::MODE_LP_COMPLETED);
407 
408  $lp_children = $first_child->getLPChildren();
409  $this->assertEquals(1, count($lp_children));
410  $this->assertEquals($mock_leaf->getId(), $lp_children[0]->getId());
411  }
412 
418  public function testRemoveLeaf() {
419  $mock_leaf = new ilStudyProgrammeLeafMock();
420  $this->root_object->addLeaf($mock_leaf);
421 
422  $this->root_object->removeLeaf($mock_leaf);
423  $this->assertEquals(0, $this->root_object->getAmountOfChildren(), "getAmountOfChildren()");
424  $this->assertEquals(0, $this->root_object->getAmountOfLPChildren(), "getAmountOfLPChildren()");
425 
426  $lp_children = $this->root_object->getLPChildren();
427  $this->assertEquals(0, count($lp_children));
428  }
429 
434  public function testAddWrongChildType() {
435  $this->createSmallTree();
436  $children = $this->root_object->getChildren();
437  $child_n = $children[0];
438  $child_l = $children[1];
439 
440  $mock_leaf1 = new ilStudyProgrammeLeafMock();
441  $mock_leaf2 = new ilStudyProgrammeLeafMock();
442  $node1 = new ilObjStudyProgramme();
443  $node2 = new ilObjStudyProgramme();
444  $node1->create();
445  $node2->create();
446 
447  $child_n->addNode($node1);
448  $child_l->addLeaf($mock_leaf1);
449 
450  $raised = false;
451  try {
452  $child_n->addLeaf($mock_leaf2);
453  }
454  catch (ilStudyProgrammeTreeException $e) {
455  $raised = true;
456  }
457  $this->assertTrue($raised, "Could add leaf to program containing node.");
458 
459  $raised = false;
460  try {
461  $child_n->addLeaf($mock_leaf2);
462  }
463  catch (ilStudyProgrammeTreeException $e) {
464  $raised = true;
465  }
466  $this->assertTrue($raised, "Could add node to program containing leaf.");
467  }
468 
472  public function testMoveTo() {
473  $this->createSmallTree();
474  $children = $this->root_object->getChildren();
475  $child_l = $children[0];
476  $child_r = $children[1];
477  $child_m = $children[2];
478 
479  $child_r->moveTo($child_l);
480 
481  $this->assertEquals(2, $child_r->getDepth());
482  $this->assertEquals($child_l->getId(), $child_r->getParent()->getId());
483  $this->assertEquals(2, $this->root_object->getAmountOfChildren());
484  $this->assertEquals(1, $child_l->getAmountOfChildren());
485 
486  // test recursive moving
487  $this->assertEquals(1, $child_m->getAmountOfChildren());
488 
489  $child_m->moveTo($child_r);
490 
491  $m_children = $child_m->getChildren();
492  $first_third_node = $m_children[0];
493 
494  $this->assertEquals(3, $child_m->getDepth());
495  $this->assertEquals(1, $child_m->getAmountOfChildren());
496  $this->assertNotNull($first_third_node);
497  $this->assertEquals(4, $first_third_node->getDepth());
498  $this->assertEquals($child_m->getId(), $first_third_node->getParent()->getId());
499 
500  $this->assertEquals(1, $this->root_object->getAmountOfChildren());
501  $this->assertEquals(3, count(ilObjStudyProgramme::getAllChildren($child_l->getRefId())));
502  }
503 
508  $this->createSmallTree();
509  $children = $this->root_object->getChildren();
510  $child_l = $children[0];
511  $child_r = $children[1];
512  $this->root_object->setStatus(ilStudyProgramme::STATUS_ACTIVE);
513  $child_l->setStatus(ilStudyProgramme::STATUS_ACTIVE);
514  $child_r->setStatus(ilStudyProgramme::STATUS_ACTIVE);
515 
516  $user = new ilObjUser();
517  $user->create();
518 
519  $child_l->assignUser($user->getId());
520  $this->root_object->removeNode($child_l);
521  }
522 
524  $this->createSmallTree();
525  $children = $this->root_object->getChildren();
526  $child_l = $children[0];
527  $child_r = $children[1];
528  $this->root_object->setStatus(ilStudyProgramme::STATUS_ACTIVE);
529  $child_l->setStatus(ilStudyProgramme::STATUS_ACTIVE);
530  $child_r->setStatus(ilStudyProgramme::STATUS_OUTDATED);
531 
532  $user = new ilObjUser();
533  $user->create();
534 
535  $this->root_object->assignUser($user->getId());
536  $this->root_object->removeNode($child_r);
537  }
538 
539  public function testCreateableSubObjects() {
540  $this->createSmallTree();
541  $children = $this->root_object->getChildren();
542  $child_l = $children[0];
543 
544  $all_possible_subobjects = $this->root_object->getPossibleSubObjects();
545  // don't take rolfs into account, we don't need rolf anymore
546  unset($all_possible_subobjects["rolf"]);
547 
548  // this is course reference and training programme
549  $this->assertCount(2, $all_possible_subobjects);
550  $this->assertArrayHasKey("prg", $all_possible_subobjects);
551  $this->assertArrayHasKey("crsr", $all_possible_subobjects);
552 
553  // root already contains program nodes, so course ref is forbidden
554  $subobjs = ilObjStudyProgramme::getCreatableSubObjects($all_possible_subobjects, $this->root_object->getRefId());
555  $this->assertCount(1, $subobjs);
556  $this->assertArrayHasKey("prg", $subobjs);
557 
558  // first node contains nothing, so course ref and program node are allowed
559  $subobjs = ilObjStudyProgramme::getCreatableSubObjects($all_possible_subobjects, $child_l->getRefId());
560  $this->assertCount(2, $subobjs);
561  $this->assertArrayHasKey("prg", $subobjs);
562  $this->assertArrayHasKey("crsr", $subobjs);
563 
564  $mock_leaf = new ilStudyProgrammeLeafMock();
565  $children = $this->root_object->getChildren();
566  $child_l->object_factory = new ilObjectFactoryWrapperMock();
567  $child_l->addLeaf($mock_leaf);
568 
569  // Now we added a leaf, so no program nodes are allowed anymore.
570  $subobjs = ilObjStudyProgramme::getCreatableSubObjects($all_possible_subobjects, $child_l->getRefId());
571  $this->assertCount(1, $subobjs);
572  $this->assertArrayHasKey("crsr", $subobjs);
573  }
574 
576  $all_possible_subobjects = $this->obj_definition->getSubObjects("prg");
577  // don't take rolfs into account, we don't need rolf anymore
578  unset($all_possible_subobjects["rolf"]);
579  $this->assertEquals( $all_possible_subobjects
580  , ilObjStudyProgramme::getCreatableSubObjects($all_possible_subobjects, null)
581  );
582  }
583 
589  }
590 
592  $this->root_object->delete();
593  $this->root_object = null;
594 
595  global $ilDB;
596  $res = $ilDB->query( "SELECT COUNT(*) cnt "
598  ." WHERE obj_id = ".$this->root_object_obj_id
599  );
600  $rec = $ilDB->fetchAssoc($res);
601  $this->assertEquals(0, $rec["cnt"]);
602  }
603 
604  public function testCreatePermissionExists() {
605  // Ask for permission id for creation of "foobar" to check assumption
606  // that lookupCreateOperationIds just drops unknown object types.
607  $op_ids = ilRbacReview::lookupCreateOperationIds(array("prg", "foobar"));
608  $this->assertCount(1, $op_ids);
609  }
610 }
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.
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.
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.
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:65
testAddWrongChildType()
Test whether nodes can only be added when there is no leaf in the parent and vice versa...