From 7d875874496f92ff2807e7810b4e0f45f1fde043 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 30 Sep 2009 16:24:05 +0000 Subject: [PATCH] unittests: NOBUG fix pagelib unit tests --- lib/blocklib.php | 7 ++-- lib/outputlib.php | 2 ++ lib/simpletest/testpagelib_moodlepage.php | 40 +++++++++++++++++------ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/lib/blocklib.php b/lib/blocklib.php index 3d203ec49e..9c3a0f6465 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -167,7 +167,9 @@ class block_manager { * @return array the internal names of the regions on this page where block may appear. */ public function get_regions() { - $this->page->initialise_theme_and_output(); + if (is_null($this->defaultregion)) { + $this->page->initialise_theme_and_output(); + } return array_keys($this->regions); } @@ -399,6 +401,7 @@ class block_manager { */ public function load_blocks($includeinvisible = null) { global $DB, $CFG; + if (!is_null($this->birecordsbyregion)) { // Already done. return; @@ -411,7 +414,7 @@ class block_manager { } // Ensure we have been initialised. - if (!isset($this->defaultregion)) { + if (is_null($this->defaultregion)) { $this->page->initialise_theme_and_output(); // If there are still no block regions, then there are no blocks on this page. if (empty($this->regions)) { diff --git a/lib/outputlib.php b/lib/outputlib.php index 6a8e05fb89..6a92853052 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -611,6 +611,8 @@ class theme_config { if (!empty($layoutinfo['regions'])) { $blockmanager->add_regions($layoutinfo['regions']); $blockmanager->set_default_region($layoutinfo['defaultregion']); + } else { + $blockmanager->set_default_region(''); } } diff --git a/lib/simpletest/testpagelib_moodlepage.php b/lib/simpletest/testpagelib_moodlepage.php index 4620fa22ca..affa5ff6c5 100644 --- a/lib/simpletest/testpagelib_moodlepage.php +++ b/lib/simpletest/testpagelib_moodlepage.php @@ -56,20 +56,21 @@ class testable_moodle_page extends moodle_page { class moodle_page_test extends UnitTestCase { protected $testpage; protected $originalcourse; + protected $originalpage; public static $includecoverage = array('lib/pagelib.php', 'lib/blocklib.php'); public function setUp() { - global $COURSE; + global $COURSE, $PAGE; $this->originalcourse = $COURSE; + $this->originalpage = $PAGE; $this->testpage = new testable_moodle_page(); - $this->testpage->blocks->add_regions(array('side-pre', 'side-post')); - $this->testpage->blocks->set_default_region('side-post'); } public function tearDown() { global $COURSE; $this->testpage = NULL; $COURSE = $this->originalcourse; + $PAGE = $this->originalpage; } /** Creates an object with all the fields you would expect a $course object to have. */ @@ -110,20 +111,33 @@ class moodle_page_test extends UnitTestCase { $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course); } - public function test_global_course_and_page_course_are_same() { - global $COURSE; + public function test_global_course_and_page_course_are_same_with_global_page() { + global $COURSE, $PAGE; // Setup fixture $course = $this->create_a_course(); $this->testpage->set_context(new stdClass); // Avoid trying to set the context. + $PAGE = $this->testpage; // Exercise SUT $this->testpage->set_course($course); // Validate $this->assertIdentical($this->testpage->course, $COURSE); } - public function test_cannot_set_course_once_output_started() { + public function test_global_course_not_changed_with_non_global_page() { + global $COURSE; + $originalcourse = $COURSE; // Setup fixture - $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER); + $course = $this->create_a_course(); + $this->testpage->set_context(new stdClass); // Avoid trying to set the context. + // Exercise SUT + $this->testpage->set_course($course); + // Validate + $this->assertIdentical($originalcourse, $COURSE); + } + + public function test_cannot_set_course_once_theme_set() { + // Setup fixture + $this->testpage->force_theme('standard'); $course = $this->create_a_course(); // Set expectation. $this->expectException(); @@ -131,9 +145,9 @@ class moodle_page_test extends UnitTestCase { $this->testpage->set_course($course); } - public function test_cannot_set_category_once_output_started() { + public function test_cannot_set_category_once_theme_set() { // Setup fixture - $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER); + $this->testpage->force_theme('standard'); // Set expectation. $this->expectException(); // Exercise SUT @@ -162,7 +176,10 @@ class moodle_page_test extends UnitTestCase { } public function test_set_state_normal_path() { - $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state); + $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); + $this->testpage->set_course($this->create_a_course()); + + $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state); $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER); $this->assertEqual(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state); @@ -187,6 +204,9 @@ class moodle_page_test extends UnitTestCase { } public function test_header_printed_becomes_true() { + $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); + $this->testpage->set_course($this->create_a_course()); + // Exercise SUT $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER); $this->testpage->set_state(moodle_page::STATE_IN_BODY); -- 2.39.5