Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletest/testnavigationlib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 if (!defined('MOODLE_INTERNAL')) {
00027     die('Direct access to this script is forbidden.');    
00028 }
00029 require_once($CFG->libdir . '/navigationlib.php');
00030 
00031 class navigation_node_test extends UnitTestCase {
00032     protected $tree;
00033     public static $includecoverage = array('./lib/navigationlib.php');
00034     public static $excludecoverage = array();
00035     protected $fakeproperties = array(
00036         'text' => 'text',
00037         'shorttext' => 'A very silly extra long short text string, more than 25 characters',
00038         'key' => 'key',
00039         'type' => 'navigation_node::TYPE_COURSE',
00040         'action' => 'http://www.moodle.org/');
00041     protected $activeurl = null;
00042     protected $inactivenode = null;
00043 
00047     public $node;
00048 
00049     public function setUp() {
00050         global $CFG, $PAGE;
00051         parent::setUp();
00052 
00053         $this->activeurl = $PAGE->url;
00054         navigation_node::override_active_url($this->activeurl);
00055         
00056         $this->inactiveurl = new moodle_url('http://www.moodle.com/');
00057         $this->fakeproperties['action'] = $this->inactiveurl;
00058 
00059         $this->node = new navigation_node('Test Node');
00060         $this->node->type = navigation_node::TYPE_SYSTEM;
00061         $demo1 = $this->node->add('demo1', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo1', new pix_icon('i/course', ''));
00062         $demo2 = $this->node->add('demo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'demo2', new pix_icon('i/course', ''));
00063         $demo3 = $this->node->add('demo3', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'demo3',new pix_icon('i/course', ''));
00064         $demo4 = $demo3->add('demo4', $this->inactiveurl,navigation_node::TYPE_COURSE,  null, 'demo4', new pix_icon('i/course', ''));
00065         $demo5 = $demo3->add('demo5', $this->activeurl, navigation_node::TYPE_COURSE, null, 'demo5',new pix_icon('i/course', ''));
00066         $demo5->add('activity1', null, navigation_node::TYPE_ACTIVITY, null, 'activity1')->make_active();
00067         $hiddendemo1 = $this->node->add('hiddendemo1', $this->inactiveurl, navigation_node::TYPE_CATEGORY, null, 'hiddendemo1', new pix_icon('i/course', ''));
00068         $hiddendemo1->hidden = true;
00069         $hiddendemo1->add('hiddendemo2', $this->inactiveurl, navigation_node::TYPE_COURSE, null, 'hiddendemo2', new pix_icon('i/course', ''))->helpbutton = 'Here is a help button';;
00070         $hiddendemo1->add('hiddendemo3', $this->inactiveurl, navigation_node::TYPE_COURSE,null, 'hiddendemo3', new pix_icon('i/course', ''))->display = false;
00071     }
00072 
00073     public function test___construct() {
00074         global $CFG;
00075         $node = new navigation_node($this->fakeproperties);
00076         $this->assertEqual($node->text, $this->fakeproperties['text']);
00077         $this->assertEqual($node->title, $this->fakeproperties['text']);
00078         $this->assertTrue(strpos($this->fakeproperties['shorttext'], substr($node->shorttext,0, -3))===0);
00079         $this->assertEqual($node->key, $this->fakeproperties['key']);
00080         $this->assertEqual($node->type, $this->fakeproperties['type']);
00081         $this->assertEqual($node->action, $this->fakeproperties['action']);
00082     }
00083     public function test_add() {
00084         global $CFG;
00085         // Add a node with all args set
00086         $node1 = $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','key',new pix_icon('i/course', ''));
00087         // Add a node with the minimum args required
00088         $node2 = $this->node->add('test_add_2',null, navigation_node::TYPE_CUSTOM,'testadd2');
00089         $node3 = $this->node->add(str_repeat('moodle ', 15),str_repeat('moodle', 15));
00090 
00091         $this->assertIsA($node1, 'navigation_node');
00092         $this->assertIsA($node2, 'navigation_node');
00093         $this->assertIsA($node3, 'navigation_node');
00094 
00095         $this->assertReference($node1, $this->node->get('key'));
00096         $this->assertReference($node2, $this->node->get($node2->key));
00097         $this->assertReference($node2, $this->node->get($node2->key, $node2->type));
00098         $this->assertReference($node3, $this->node->get($node3->key, $node3->type));
00099     }
00100 
00101     public function test_add_before() {
00102         global $CFG;
00103         // Create 3 nodes
00104         $node1 = navigation_node::create('test_add_1', null, navigation_node::TYPE_CUSTOM,
00105                 'test 1', 'testadd1');
00106         $node2 = navigation_node::create('test_add_2', null, navigation_node::TYPE_CUSTOM,
00107                 'test 2', 'testadd2');
00108         $node3 = navigation_node::create('test_add_3', null, navigation_node::TYPE_CUSTOM,
00109                 'test 3', 'testadd3');
00110         // Add node 2, then node 1 before 2, then node 3 at end
00111         $this->node->add_node($node2);
00112         $this->node->add_node($node1, 'testadd2');
00113         $this->node->add_node($node3);
00114         // Check the last 3 nodes are in 1, 2, 3 order and have those indexes
00115         foreach($this->node->children as $child) {
00116             $keys[] = $child->key;
00117         }
00118         $this->assertEqual('testadd1', $keys[count($keys)-3]);
00119         $this->assertEqual('testadd2', $keys[count($keys)-2]);
00120         $this->assertEqual('testadd3', $keys[count($keys)-1]);
00121     }
00122 
00123     public function test_add_class() {
00124         $node = $this->node->get('demo1');
00125         $this->assertIsA($node, 'navigation_node');
00126         if ($node !== false) {
00127             $node->add_class('myclass');
00128             $classes = $node->classes;
00129             $this->assertTrue(in_array('myclass', $classes));
00130         }
00131     }
00132 
00133 
00134     public function test_check_if_active() {
00135         // First test the string urls
00136         // demo1 -> action is http://www.moodle.org/, thus should be true
00137         $demo5 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
00138         if ($this->assertIsA($demo5, 'navigation_node')) {
00139             $this->assertTrue($demo5->check_if_active());
00140         }
00141 
00142         // demo2 -> action is http://www.moodle.com/, thus should be false
00143         $demo2 = $this->node->get('demo2');
00144         if ($this->assertIsA($demo2, 'navigation_node')) {
00145             $this->assertFalse($demo2->check_if_active());
00146         }
00147     }
00148 
00149     public function test_contains_active_node() {
00150         // demo5, and activity1 were set to active during setup
00151         // Should be true as it contains all nodes
00152         $this->assertTrue($this->node->contains_active_node());
00153         // Should be true as demo5 is a child of demo3
00154         $this->assertTrue($this->node->get('demo3')->contains_active_node());
00155         // Obviously duff
00156         $this->assertFalse($this->node->get('demo1')->contains_active_node());
00157         // Should be true as demo5 contains activity1
00158         $this->assertTrue($this->node->get('demo3')->get('demo5')->contains_active_node());
00159         // Should be true activity1 is the active node
00160         $this->assertTrue($this->node->get('demo3')->get('demo5')->get('activity1')->contains_active_node());
00161         // Obviously duff
00162         $this->assertFalse($this->node->get('demo3')->get('demo4')->contains_active_node());
00163     }
00164 
00165     public function test_find_active_node() {
00166         $activenode1 = $this->node->find_active_node();
00167         $activenode2 = $this->node->get('demo1')->find_active_node();
00168         
00169         if ($this->assertIsA($activenode1, 'navigation_node')) {
00170             $this->assertReference($activenode1, $this->node->get('demo3')->get('demo5')->get('activity1'));
00171         }
00172         
00173         $this->assertNotA($activenode2, 'navigation_node');
00174     }
00175 
00176     public function test_find() {
00177         $node1 = $this->node->find('demo1', navigation_node::TYPE_COURSE);
00178         $node2 = $this->node->find('demo5', navigation_node::TYPE_COURSE);
00179         $node3 = $this->node->find('demo5', navigation_node::TYPE_CATEGORY);
00180         $node4 = $this->node->find('demo0', navigation_node::TYPE_COURSE);
00181         $this->assertIsA($node1, 'navigation_node');
00182         $this->assertIsA($node2, 'navigation_node');
00183         $this->assertNotA($node3, 'navigation_node');
00184         $this->assertNotA($node4, 'navigation_node');
00185     }
00186 
00187     public function test_find_expandable() {
00188         $expandable = array();
00189         $this->node->find_expandable($expandable);
00190         $this->assertEqual(count($expandable), 4);
00191         if (count($expandable) === 4) {
00192             $name = $expandable[0]['key'];
00193             $name .= $expandable[1]['key'];
00194             $name .= $expandable[2]['key'];
00195             $name .= $expandable[3]['key'];
00196             $this->assertEqual($name, 'demo1demo2demo4hiddendemo2');
00197         }
00198     }
00199 
00200     public function test_get() {
00201         $node1 = $this->node->get('demo1'); // Exists
00202         $node2 = $this->node->get('demo4'); // Doesn't exist for this node
00203         $node3 = $this->node->get('demo0'); // Doesn't exist at all
00204         $node4 = $this->node->get(false);   // Sometimes occurs in nature code
00205         $this->assertIsA($node1, 'navigation_node');
00206         $this->assertFalse($node2);
00207         $this->assertFalse($node3);
00208         $this->assertFalse($node4);
00209     }
00210 
00211     public function test_get_css_type() {
00212         $csstype1 = $this->node->get('demo3')->get_css_type();
00213         $csstype2 = $this->node->get('demo3')->get('demo5')->get_css_type();
00214         $this->node->get('demo3')->get('demo5')->type = 1000;
00215         $csstype3 = $this->node->get('demo3')->get('demo5')->get_css_type();
00216         $this->assertEqual($csstype1, 'type_category');
00217         $this->assertEqual($csstype2, 'type_course');
00218         $this->assertEqual($csstype3, 'type_unknown');
00219     }
00220 
00221     public function test_make_active() {
00222         global $CFG;
00223         $node1 = $this->node->add('active node 1', null, navigation_node::TYPE_CUSTOM, null, 'anode1');
00224         $node2 = $this->node->add('active node 2', new moodle_url($CFG->wwwroot), navigation_node::TYPE_COURSE, null, 'anode2');
00225         $node1->make_active();
00226         $this->node->get('anode2')->make_active();
00227         $this->assertTrue($node1->isactive);
00228         $this->assertTrue($this->node->get('anode2')->isactive);
00229     }
00230     public function test_remove() {
00231         $remove1 = $this->node->add('child to remove 1', null, navigation_node::TYPE_CUSTOM, null, 'remove1');
00232         $remove2 = $this->node->add('child to remove 2', null, navigation_node::TYPE_CUSTOM, null, 'remove2');
00233         $remove3 = $remove2->add('child to remove 3', null, navigation_node::TYPE_CUSTOM, null, 'remove3');
00234 
00235         $this->assertIsA($remove1, 'navigation_node');
00236         $this->assertIsA($remove2, 'navigation_node');
00237         $this->assertIsA($remove3, 'navigation_node');
00238 
00239         $this->assertIsA($this->node->get('remove1'), 'navigation_node');
00240         $this->assertIsA($this->node->get('remove2'), 'navigation_node');
00241         $this->assertIsA($remove2->get('remove3'), 'navigation_node');
00242 
00243         $this->assertTrue($remove1->remove());
00244         $this->assertTrue($this->node->get('remove2')->remove());
00245         $this->assertTrue($remove2->get('remove3')->remove());
00246 
00247         $this->assertFalse($this->node->get('remove1'));
00248         $this->assertFalse($this->node->get('remove2'));
00249     }
00250     public function test_remove_class() {
00251         $this->node->add_class('testclass');
00252         $this->assertTrue($this->node->remove_class('testclass'));
00253         $this->assertFalse(in_array('testclass', $this->node->classes));
00254     }
00255 }
00256 
00261 class exposed_global_navigation extends global_navigation {
00262     protected $exposedkey = 'exposed_';
00263     public function __construct(moodle_page $page=null) {
00264         global $PAGE;
00265         if ($page === null) {
00266             $page = $PAGE;
00267         }
00268         parent::__construct($page);
00269         $this->cache = new navigation_cache('simpletest_nav');
00270     }
00271     public function __call($method, $arguments) {
00272         if (strpos($method,$this->exposedkey) !== false) {
00273             $method = substr($method, strlen($this->exposedkey));
00274         }
00275         if (method_exists($this, $method)) {
00276             return call_user_func_array(array($this, $method), $arguments);
00277         }
00278         throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
00279     }
00280     public function set_initialised() {
00281         $this->initialised = true;
00282     }
00283 }
00284 
00285 class mock_initialise_global_navigation extends global_navigation {
00286 
00287     static $count = 1;
00288 
00289     public function load_for_category() {
00290         $this->add('load_for_category', null, null, null, 'initcall'.self::$count);
00291         self::$count++;
00292         return 0;
00293     }
00294 
00295     public function load_for_course() {
00296         $this->add('load_for_course', null, null, null, 'initcall'.self::$count);
00297         self::$count++;
00298         return 0;
00299     }
00300 
00301     public function load_for_activity() {
00302         $this->add('load_for_activity', null, null, null, 'initcall'.self::$count);
00303         self::$count++;
00304         return 0;
00305     }
00306 
00307     public function load_for_user() {
00308         $this->add('load_for_user', null, null, null, 'initcall'.self::$count);
00309         self::$count++;
00310         return 0;
00311     }
00312 }
00313 
00314 class global_navigation_test extends UnitTestCase {
00318     public $node;
00319     public static $includecoverage = array('./lib/navigationlib.php');
00320     public static $excludecoverage = array();
00321 
00322     public function setUp() {
00323         $this->node = new exposed_global_navigation();
00324         // Create an initial tree structure to work with
00325         $cat1 = $this->node->add('category 1', null, navigation_node::TYPE_CATEGORY, null, 'cat1');
00326         $cat2 = $this->node->add('category 2', null, navigation_node::TYPE_CATEGORY, null, 'cat2');
00327         $cat3 = $this->node->add('category 3', null, navigation_node::TYPE_CATEGORY, null, 'cat3');
00328         $sub1 = $cat2->add('sub category 1', null, navigation_node::TYPE_CATEGORY, null, 'sub1');
00329         $sub2 = $cat2->add('sub category 2', null, navigation_node::TYPE_CATEGORY, null, 'sub2');
00330         $sub3 = $cat2->add('sub category 3', null, navigation_node::TYPE_CATEGORY, null, 'sub3');
00331         $course1 = $sub2->add('course 1', null, navigation_node::TYPE_COURSE, null, 'course1');
00332         $course2 = $sub2->add('course 2', null, navigation_node::TYPE_COURSE, null, 'course2');
00333         $course3 = $sub2->add('course 3', null, navigation_node::TYPE_COURSE, null, 'course3');
00334         $section1 = $course2->add('section 1', null, navigation_node::TYPE_SECTION, null, 'sec1');
00335         $section2 = $course2->add('section 2', null, navigation_node::TYPE_SECTION, null, 'sec2');
00336         $section3 = $course2->add('section 3', null, navigation_node::TYPE_SECTION, null, 'sec3');
00337         $act1 = $section2->add('activity 1', null, navigation_node::TYPE_ACTIVITY, null, 'act1');
00338         $act2 = $section2->add('activity 2', null, navigation_node::TYPE_ACTIVITY, null, 'act2');
00339         $act3 = $section2->add('activity 3', null, navigation_node::TYPE_ACTIVITY, null, 'act3');
00340         $res1 = $section2->add('resource 1', null, navigation_node::TYPE_RESOURCE, null, 'res1');
00341         $res2 = $section2->add('resource 2', null, navigation_node::TYPE_RESOURCE, null, 'res2');
00342         $res3 = $section2->add('resource 3', null, navigation_node::TYPE_RESOURCE, null, 'res3');
00343     }
00344 
00345     public function test_format_display_course_content() {
00346         $this->assertTrue($this->node->exposed_format_display_course_content('topic'));
00347         $this->assertFalse($this->node->exposed_format_display_course_content('scorm'));
00348         $this->assertTrue($this->node->exposed_format_display_course_content('dummy'));
00349     }
00350     public function test_module_extends_navigation() {
00351         $this->assertTrue($this->node->exposed_module_extends_navigation('data'));
00352         $this->assertFalse($this->node->exposed_module_extends_navigation('test1'));
00353     }
00354 }
00355 
00360 class exposed_navbar extends navbar {
00361     protected $exposedkey = 'exposed_';
00362     public function __construct(moodle_page $page) {
00363         parent::__construct($page);
00364         $this->cache = new navigation_cache('simpletest_nav');
00365     }
00366     function __call($method, $arguments) {
00367         if (strpos($method,$this->exposedkey) !== false) {
00368             $method = substr($method, strlen($this->exposedkey));
00369         }
00370         if (method_exists($this, $method)) {
00371             return call_user_func_array(array($this, $method), $arguments);
00372         }
00373         throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
00374     }
00375 }
00376 
00377 class navigation_exposed_moodle_page extends moodle_page {
00378     public function set_navigation(navigation_node $node) {
00379         $this->_navigation = $node;
00380     }
00381 }
00382 
00383 class navbar_test extends UnitTestCase {
00384     protected $node;
00385     protected $oldnav;
00386 
00387     public static $includecoverage = array('./lib/navigationlib.php');
00388     public static $excludecoverage = array();
00389 
00390     public function setUp() {
00391         global $PAGE;
00392 
00393         $temptree = new global_navigation_test();
00394         $temptree->setUp();
00395         $temptree->node->find('course2', navigation_node::TYPE_COURSE)->make_active();
00396 
00397         $page = new navigation_exposed_moodle_page();
00398         $page->set_url($PAGE->url);
00399         $page->set_context($PAGE->context);
00400 
00401         $navigation = new exposed_global_navigation($page);
00402         $navigation->children = $temptree->node->children;
00403         $navigation->set_initialised();
00404         $page->set_navigation($navigation);
00405 
00406         $this->cache = new navigation_cache('simpletest_nav');
00407         $this->node = new exposed_navbar($page);
00408     }
00409     public function test_add() {
00410         // Add a node with all args set
00411         $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',new pix_icon('i/course', ''));
00412         // Add a node with the minimum args required
00413         $this->node->add('test_add_2','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd2','testadd2',new pix_icon('i/course', ''));
00414         $this->assertIsA($this->node->get('testadd1'), 'navigation_node');
00415         $this->assertIsA($this->node->get('testadd2'), 'navigation_node');
00416     }
00417     public function test_has_items() {
00418         $this->assertTrue($this->node->has_items());
00419     }
00420 }
00421 
00422 class navigation_cache_test extends UnitTestCase {
00423     protected $cache;
00424 
00425     public static $includecoverage = array('./lib/navigationlib.php');
00426     public static $excludecoverage = array();
00427 
00428     public function setUp() {
00429         $this->cache = new navigation_cache('simpletest_nav');
00430         $this->cache->anysetvariable = true;
00431     }
00432     public function test___get() {
00433         $this->assertTrue($this->cache->anysetvariable);
00434         $this->assertEqual($this->cache->notasetvariable, null);
00435     }
00436     public function test___set() {
00437         $this->cache->myname = 'Sam Hemelryk';
00438         $this->assertTrue($this->cache->cached('myname'));
00439         $this->assertEqual($this->cache->myname, 'Sam Hemelryk');
00440     }
00441     public function test_cached() {
00442         $this->assertTrue($this->cache->cached('anysetvariable'));
00443         $this->assertFalse($this->cache->cached('notasetvariable'));
00444     }
00445     public function test_clear() {
00446         $cache = clone($this->cache);
00447         $this->assertTrue($cache->cached('anysetvariable'));
00448         $cache->clear();
00449         $this->assertFalse($cache->cached('anysetvariable'));
00450     }
00451     public function test_set() {
00452         $this->cache->set('software', 'Moodle');
00453         $this->assertTrue($this->cache->cached('software'));
00454         $this->assertEqual($this->cache->software, 'Moodle');
00455     }
00456 }
00457 
00462 class exposed_settings_navigation extends settings_navigation {
00463     protected $exposedkey = 'exposed_';
00464     function __construct() {
00465         global $PAGE;
00466         parent::__construct($PAGE);
00467         $this->cache = new navigation_cache('simpletest_nav');
00468     }
00469     function __call($method, $arguments) {
00470         if (strpos($method,$this->exposedkey) !== false) {
00471             $method = substr($method, strlen($this->exposedkey));
00472         }
00473         if (method_exists($this, $method)) {
00474             return call_user_func_array(array($this, $method), $arguments);
00475         }
00476         throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER);
00477     }
00478 }
00479 
00480 class settings_navigation_test extends UnitTestCase {
00481     protected $node;
00482     protected $cache;
00483 
00484     public static $includecoverage = array('./lib/navigationlib.php');
00485     public static $excludecoverage = array();
00486 
00487     public function setUp() {
00488         global $PAGE;
00489         $this->cache = new navigation_cache('simpletest_nav');
00490         $this->node = new exposed_settings_navigation();
00491     }
00492     public function test___construct() {
00493         $this->node = new exposed_settings_navigation();
00494     }
00495     public function test___initialise() {
00496         $this->node->initialise();
00497         $this->assertEqual($this->node->id, 'settingsnav');
00498     }
00499     public function test_in_alternative_role() {
00500         $this->assertFalse($this->node->exposed_in_alternative_role());
00501     }
00502 }
 All Data Structures Namespaces Files Functions Variables Enumerations