From 2afe21eea392dc46f45675f8223cf5420a45c195 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 6 May 2009 08:30:25 +0000 Subject: [PATCH] moodle_page: MDL-12212 implement ->context --- lib/pagelib.php | 26 +++ lib/simpletest/testpagelib_moodlepage.php | 223 ++++++++++++++++++++++ 2 files changed, 249 insertions(+) create mode 100644 lib/simpletest/testpagelib_moodlepage.php diff --git a/lib/pagelib.php b/lib/pagelib.php index 6e4ef01e03..eafd37a44d 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -52,6 +52,8 @@ class moodle_page { protected $_course = null; + protected $_context = null; + /** * @return integer one of the STATE_... constants. You should not normally need * to use this in your code. It is indended for internal use by this class @@ -84,6 +86,16 @@ class moodle_page { return $this->_course; } + /** + * @return object the main context to which this page belongs. + */ + public function get_context() { + if (is_null($this->_context)) { + throw new coding_exception('$PAGE->context accessed before it was known.'); + } + return $this->_context; + } + /** * Set the state. The state must be one of that STATE_... constants, and * the state is only allowed to advance one step at a time. @@ -111,6 +123,8 @@ class moodle_page { * call it for you if you pass a $course to it. You can use this function * on pages that do need to call require_login(). * + * Sets $PAGE->context to the course context, if it is not already set. + * * @param object the course to set as the global course. */ public function set_course($course) { @@ -127,10 +141,22 @@ class moodle_page { $this->_course = clone($course); $COURSE = $this->_course; + if (!$this->_context) { + $this->set_context(get_context_instance(CONTEXT_COURSE, $this->_course->id)); + } + moodle_setlocale(); theme_setup(); } + /** + * Set the main context to which this page belongs. + * @param object $context a context object, normally obtained with get_context_instance. + */ + public function set_context($context) { + $this->_context = $context; + } + /** * PHP overloading magic to make the $PAGE->course syntax work. */ diff --git a/lib/simpletest/testpagelib_moodlepage.php b/lib/simpletest/testpagelib_moodlepage.php new file mode 100644 index 0000000000..f7de99017f --- /dev/null +++ b/lib/simpletest/testpagelib_moodlepage.php @@ -0,0 +1,223 @@ +libdir . '/pagelib.php'); + +/** + * Test functions that affect filter_active table with contextid = $syscontextid. + */ +class moodle_page_test extends UnitTestCase { + protected $testpage; + protected $originalcourse; + + public function setUp() { + global $COURSE; + $this->originalcourse = $COURSE; + $this->testpage = new moodle_page(); + } + + public function tearDown() { + global $COURSE; + $this->testpage = NULL; + $COURSE = $this->originalcourse; + } + + /** Creates an object with all the fields you would expect a $course object to have. */ + protected function create_a_course() { + $course = new stdClass; + $course->id = -1; + $course->category = 2; + $course->fullname = 'Anonymous test course'; + $course->shortname = 'ANON'; + $course->summary = ''; + return $course; + } + + /** Creates an object with all the fields you would expect a $course object to have. */ + protected function create_a_context() { + $context = new stdClass; + $context->id = 2; + $context->contextlevel = CONTEXT_COURSECAT; + $context->instanceid = 1; + $context->path = '/1/2'; + $context->depth = '2'; + return $context; + } + + public function test_course_returns_site_before_set() { + global $SITE; + // Validate + $this->assertIdentical($SITE, $this->testpage->course); + } + + public function test_setting_course_works() { + // Setup fixture + $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->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course); + } + + public function test_global_course_and_page_course_are_same() { + global $COURSE; + // Setup fixture + $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($this->testpage->course, $COURSE); + } + + public function test_cannot_set_course_once_output_started() { + // Setup fixture + $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER); + $course = $this->create_a_course(); + // Set expectation. + $this->expectException(); + // Exercise SUT + $this->testpage->set_course($course); + } + + public function test_set_state_normal_path() { + $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); + + $this->testpage->set_state(moodle_page::STATE_IN_BODY); + $this->assertEqual(moodle_page::STATE_IN_BODY, $this->testpage->state); + + $this->testpage->set_state(moodle_page::STATE_PRINTING_FOOTER); + $this->assertEqual(moodle_page::STATE_PRINTING_FOOTER, $this->testpage->state); + + $this->testpage->set_state(moodle_page::STATE_DONE); + $this->assertEqual(moodle_page::STATE_DONE, $this->testpage->state); + } + + public function test_set_state_cannot_skip_one() { + // Set expectation. + $this->expectException(); + // Exercise SUT + $this->testpage->set_state(moodle_page::STATE_IN_BODY); + } + + public function test_header_printed_false_initially() { + // Validate + $this->assertFalse($this->testpage->headerprinted); + } + + public function test_header_printed_becomes_true() { + // Exercise SUT + $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER); + $this->testpage->set_state(moodle_page::STATE_IN_BODY); + // Validate + $this->assertTrue($this->testpage->headerprinted); + } + + public function test_cant_get_context_before_set() { + // Set expectation. + $this->expectException(); + // Exercise SUT + $this->testpage->context; + } + + public function test_set_context() { + // Setup fixture + $context = $this->create_a_context(); + // Exercise SUT + $this->testpage->set_context($context); + // Validate + $this->assert(new CheckSpecifiedFieldsExpectation($context), $this->testpage->context); + } +} + +/** + * Test functions that affect filter_active table with contextid = $syscontextid. + */ +class moodle_page_with_db_test extends UnitTestCaseUsingDatabase { + protected $testpage; + protected $originalcourse; + + public function setUp() { + global $COURSE; + $this->originalcourse = $COURSE; + $this->testpage = new moodle_page(); + $this->create_test_table('context', 'lib'); + $this->switch_to_test_db(); + } + + public function tearDown() { + global $COURSE; + $this->testpage = NULL; + $COURSE = $this->originalcourse; + } + + /** Creates an object with all the fields you would expect a $course object to have. */ + protected function create_a_course_with_context() { + $course = new stdClass; + $course->id = -1; + $course->category = 2; + $course->fullname = 'Anonymous test course'; + $course->shortname = 'ANON'; + $course->summary = ''; + + $context = new stdClass; + $context->contextlevel = CONTEXT_COURSE; + $context->instanceid = $course->id; + $context->path = 'not initialised'; + $context->depth = '-1'; + $this->testdb->insert_record('context', $context); + + return $course; + } + + public function test_setting_course_sets_context() { + // Setup fixture + $course = $this->create_a_course_with_context(); + // Exercise SUT + $this->testpage->set_course($course); + // Validate + $expectedcontext = new stdClass; + $expectedcontext->contextlevel = CONTEXT_COURSE; + $expectedcontext->instanceid = $course->id; + $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context); + } +} +?> -- 2.39.5