From abaece1e26d02409bcf4964a7c216013dee99495 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Mon, 30 Aug 2010 02:50:22 +0000 Subject: [PATCH] navigation MDL-23981 Fixed up simpletests for the navigation --- lib/navigationlib.php | 12 +++++- lib/simpletest/testnavigationlib.php | 55 +++++++++++++++++----------- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 30269038052..6914e506b8e 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -1342,8 +1342,16 @@ class global_navigation extends navigation_node { require_once($CFG->dirroot.'/course/lib.php'); - $modinfo = get_fast_modinfo($course); - $sections = array_slice(get_all_sections($course->id), 0, $course->numsections+1, true); + if (!$this->cache->cached('modinfo'.$course->id)) { + $this->cache->set('modinfo'.$course->id, get_fast_modinfo($course)); + } + $modinfo = $this->cache->{'modinfo'.$course->id}; + + if (!$this->cache->cached('coursesections'.$course->id)) { + $this->cache->set('coursesections'.$course->id, array_slice(get_all_sections($course->id), 0, $course->numsections+1, true)); + } + $sections = $this->cache->{'coursesections'.$course->id}; + $viewhiddensections = has_capability('moodle/course:viewhiddensections', $this->page->context); if (isloggedin() && !isguestuser()) { diff --git a/lib/simpletest/testnavigationlib.php b/lib/simpletest/testnavigationlib.php index cd753e6faa0..28c5b1e58fe 100644 --- a/lib/simpletest/testnavigationlib.php +++ b/lib/simpletest/testnavigationlib.php @@ -165,14 +165,13 @@ class navigation_node_test extends UnitTestCase { public function test_find_expandable() { $expandable = array(); $this->node->find_expandable($expandable); - $this->assertEqual(count($expandable), 5); - if (count($expandable) === 5) { + $this->assertEqual(count($expandable), 4); + if (count($expandable) === 4) { $name = $expandable[0]['branchid']; $name .= $expandable[1]['branchid']; $name .= $expandable[2]['branchid']; $name .= $expandable[3]['branchid']; - $name .= $expandable[4]['branchid']; - $this->assertEqual($name, 'demo1demo2demo4hiddendemo2hiddendemo3'); + $this->assertEqual($name, 'demo1demo2demo4hiddendemo2'); } } @@ -239,12 +238,15 @@ class navigation_node_test extends UnitTestCase { */ class exposed_global_navigation extends global_navigation { protected $exposedkey = 'exposed_'; - function __construct() { + public function __construct(moodle_page $page=null) { global $PAGE; - parent::__construct($PAGE); + if ($page === null) { + $page = $PAGE; + } + parent::__construct($page); $this->cache = new navigation_cache('simpletest_nav'); } - function __call($method, $arguments) { + public function __call($method, $arguments) { if (strpos($method,$this->exposedkey) !== false) { $method = substr($method, strlen($this->exposedkey)); } @@ -253,6 +255,9 @@ class exposed_global_navigation extends global_navigation { } throw new coding_exception('You have attempted to access a method that does not exist for the given object '.$method, DEBUG_DEVELOPER); } + public function set_initialised() { + $this->initialised = true; + } } class mock_initialise_global_navigation extends global_navigation { @@ -333,7 +338,7 @@ class global_navigation_test extends UnitTestCase { $course->numsections = 10; $course->modinfo = $this->modinfo5; $this->node->load_generic_course_sections($course, $coursenode, 'topic', 'topic'); - $this->assertEqual($coursenode->children->count(),4); + $this->assertEqual($coursenode->children->count(),1); } public function test_format_display_course_content() { $this->assertTrue($this->node->exposed_format_display_course_content('topic')); @@ -389,9 +394,8 @@ class global_navigation_test extends UnitTestCase { */ class exposed_navbar extends navbar { protected $exposedkey = 'exposed_'; - function __construct() { - global $PAGE; - parent::__construct($PAGE); + public function __construct(moodle_page $page) { + parent::__construct($page); $this->cache = new navigation_cache('simpletest_nav'); } function __call($method, $arguments) { @@ -405,6 +409,12 @@ class exposed_navbar extends navbar { } } +class navigation_exposed_moodle_page extends moodle_page { + public function set_navigation(navigation_node $node) { + $this->_navigation = $node; + } +} + class navbar_test extends UnitTestCase { protected $node; protected $oldnav; @@ -414,20 +424,24 @@ class navbar_test extends UnitTestCase { public function setUp() { global $PAGE; - $this->oldnav = $PAGE->navigation; - $this->cache = new navigation_cache('simpletest_nav'); - $this->node = new exposed_navbar(); + $temptree = new global_navigation_test(); $temptree->setUp(); $temptree->node->find('course2', navigation_node::TYPE_COURSE)->make_active(); - $PAGE->navigation = $temptree->node; - } - public function tearDown() { - global $PAGE; - $PAGE->navigation = $this->oldnav; + + $page = new navigation_exposed_moodle_page(); + $page->set_url($PAGE->url); + $page->set_context($PAGE->context); + + $navigation = new exposed_global_navigation($page); + $navigation->children = $temptree->node->children; + $navigation->set_initialised(); + $page->set_navigation($navigation); + + $this->cache = new navigation_cache('simpletest_nav'); + $this->node = new exposed_navbar($page); } public function test_add() { - global $CFG; // Add a node with all args set $this->node->add('test_add_1','http://www.moodle.org/',navigation_node::TYPE_COURSE,'testadd1','testadd1',new pix_icon('i/course', '')); // Add a node with the minimum args required @@ -436,7 +450,6 @@ class navbar_test extends UnitTestCase { $this->assertIsA($this->node->get('testadd2'), 'navigation_node'); } public function test_has_items() { - global $PAGE; $this->assertTrue($this->node->has_items()); } }