unittests: NOBUG fix pagelib unit tests

This commit is contained in:
tjhunt 2009-09-30 16:24:05 +00:00
parent ec26b38806
commit 7d87587449
3 changed files with 37 additions and 12 deletions

View File

@ -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)) {

View File

@ -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('');
}
}

View File

@ -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);