|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00004 // // 00005 // NOTICE OF COPYRIGHT // 00006 // // 00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 00008 // http://moodle.org // 00009 // // 00010 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // 00011 // // 00012 // This program is free software; you can redistribute it and/or modify // 00013 // it under the terms of the GNU General Public License as published by // 00014 // the Free Software Foundation; either version 2 of the License, or // 00015 // (at your option) any later version. // 00016 // // 00017 // This program is distributed in the hope that it will be useful, // 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00020 // GNU General Public License for more details: // 00021 // // 00022 // http://www.gnu.org/copyleft/gpl.html // 00023 // // 00025 00033 if (!defined('MOODLE_INTERNAL')) { 00034 die('Direct access to this script is forbidden.'); 00035 } 00036 00037 require_once($CFG->libdir . '/pagelib.php'); 00038 require_once($CFG->libdir . '/blocklib.php'); 00039 00041 class testable_moodle_page extends moodle_page { 00042 public function initialise_default_pagetype($script = null) { 00043 parent::initialise_default_pagetype($script); 00044 } 00045 public function url_to_class_name($url) { 00046 return parent::url_to_class_name($url); 00047 } 00048 public function all_editing_caps() { 00049 return parent::all_editing_caps(); 00050 } 00051 } 00052 00056 class moodle_page_test extends UnitTestCase { 00057 protected $testpage; 00058 protected $originalcourse; 00059 protected $originalpage; 00060 public static $includecoverage = array('lib/pagelib.php', 'lib/blocklib.php'); 00061 00062 public function setUp() { 00063 global $COURSE, $PAGE; 00064 $this->originalcourse = $COURSE; 00065 $this->originalpage = $PAGE; 00066 $this->testpage = new testable_moodle_page(); 00067 } 00068 00069 public function tearDown() { 00070 global $COURSE, $PAGE; 00071 $this->testpage = NULL; 00072 $COURSE = $this->originalcourse; 00073 $PAGE = $this->originalpage; 00074 } 00075 00077 protected function create_a_course() { 00078 $course = new stdClass; 00079 $course->id = 13; // Why 13, good question. MDL-21007 00080 $course->category = 2; 00081 $course->fullname = 'Anonymous test course'; 00082 $course->shortname = 'ANON'; 00083 $course->summary = ''; 00084 return $course; 00085 } 00086 00088 protected function create_a_context() { 00089 $context = new stdClass; 00090 $context->id = 2; 00091 $context->contextlevel = CONTEXT_COURSECAT; 00092 $context->instanceid = 1; 00093 $context->path = '/1/2'; 00094 $context->depth = '2'; 00095 return $context; 00096 } 00097 00098 public function test_course_returns_site_before_set() { 00099 global $SITE; 00100 // Validate 00101 $this->assertIdentical($SITE, $this->testpage->course); 00102 } 00103 00104 public function test_setting_course_works() { 00105 // Setup fixture 00106 $course = $this->create_a_course(); 00107 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context. 00108 // Exercise SUT 00109 $this->testpage->set_course($course); 00110 // Validate 00111 $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course); 00112 } 00113 00114 public function test_global_course_and_page_course_are_same_with_global_page() { 00115 global $COURSE, $PAGE; 00116 // Setup fixture 00117 $course = $this->create_a_course(); 00118 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context. 00119 $PAGE = $this->testpage; 00120 // Exercise SUT 00121 $this->testpage->set_course($course); 00122 // Validate 00123 $this->assertIdentical($this->testpage->course, $COURSE); 00124 } 00125 00126 public function test_global_course_not_changed_with_non_global_page() { 00127 global $COURSE; 00128 $originalcourse = $COURSE; 00129 // Setup fixture 00130 $course = $this->create_a_course(); 00131 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context. 00132 // Exercise SUT 00133 $this->testpage->set_course($course); 00134 // Validate 00135 $this->assertIdentical($originalcourse, $COURSE); 00136 } 00137 00138 public function test_cannot_set_course_once_theme_set() { 00139 // Setup fixture 00140 $this->testpage->force_theme(theme_config::DEFAULT_THEME); 00141 $course = $this->create_a_course(); 00142 // Set expectation. 00143 $this->expectException(); 00144 // Exercise SUT 00145 $this->testpage->set_course($course); 00146 } 00147 00148 public function test_cannot_set_category_once_theme_set() { 00149 // Setup fixture 00150 $this->testpage->force_theme(theme_config::DEFAULT_THEME); 00151 // Set expectation. 00152 $this->expectException(); 00153 // Exercise SUT 00154 $this->testpage->set_category_by_id(123); 00155 } 00156 00157 public function test_cannot_set_category_once_course_set() { 00158 // Setup fixture 00159 $course = $this->create_a_course(); 00160 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context. 00161 $this->testpage->set_course($course); 00162 // Set expectation. 00163 $this->expectException(); 00164 // Exercise SUT 00165 $this->testpage->set_category_by_id(123); 00166 } 00167 00168 public function test_categories_array_empty_for_front_page() { 00169 // Setup fixture 00170 $course = $this->create_a_course(); 00171 $course->category = 0; 00172 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); // Avoid trying to set the context. 00173 $this->testpage->set_course($course); 00174 // Exercise SUT and validate. 00175 $this->assertEqual(array(), $this->testpage->categories); 00176 } 00177 00178 public function test_set_state_normal_path() { 00179 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); 00180 $this->testpage->set_course($this->create_a_course()); 00181 00182 $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state); 00183 00184 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER); 00185 $this->assertEqual(moodle_page::STATE_PRINTING_HEADER, $this->testpage->state); 00186 00187 $this->testpage->set_state(moodle_page::STATE_IN_BODY); 00188 $this->assertEqual(moodle_page::STATE_IN_BODY, $this->testpage->state); 00189 00190 $this->testpage->set_state(moodle_page::STATE_DONE); 00191 $this->assertEqual(moodle_page::STATE_DONE, $this->testpage->state); 00192 } 00193 00194 public function test_set_state_cannot_skip_one() { 00195 // Set expectation. 00196 $this->expectException(); 00197 // Exercise SUT 00198 $this->testpage->set_state(moodle_page::STATE_IN_BODY); 00199 } 00200 00201 public function test_header_printed_false_initially() { 00202 // Validate 00203 $this->assertFalse($this->testpage->headerprinted); 00204 } 00205 00206 public function test_header_printed_becomes_true() { 00207 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); 00208 $this->testpage->set_course($this->create_a_course()); 00209 00210 // Exercise SUT 00211 $this->testpage->set_state(moodle_page::STATE_PRINTING_HEADER); 00212 $this->testpage->set_state(moodle_page::STATE_IN_BODY); 00213 // Validate 00214 $this->assertTrue($this->testpage->headerprinted); 00215 } 00216 00217 public function test_set_context() { 00218 // Setup fixture 00219 $context = $this->create_a_context(); 00220 // Exercise SUT 00221 $this->testpage->set_context($context); 00222 // Validate 00223 $this->assert(new CheckSpecifiedFieldsExpectation($context), $this->testpage->context); 00224 } 00225 00226 public function test_pagetype_defaults_to_script() { 00227 // Exercise SUT and validate 00228 $this->assertEqual('admin-tool-unittest-index', $this->testpage->pagetype); 00229 } 00230 00231 public function test_set_pagetype() { 00232 // Exercise SUT 00233 $this->testpage->set_pagetype('a-page-type'); 00234 // Validate 00235 $this->assertEqual('a-page-type', $this->testpage->pagetype); 00236 } 00237 00238 public function test_initialise_default_pagetype() { 00239 // Exercise SUT 00240 $this->testpage->initialise_default_pagetype('admin/tool/unittest/index.php'); 00241 // Validate 00242 $this->assertEqual('admin-tool-unittest-index', $this->testpage->pagetype); 00243 } 00244 00245 public function test_initialise_default_pagetype_fp() { 00246 // Exercise SUT 00247 $this->testpage->initialise_default_pagetype('index.php'); 00248 // Validate 00249 $this->assertEqual('site-index', $this->testpage->pagetype); 00250 } 00251 00252 public function test_get_body_classes_empty() { 00253 // Validate 00254 $this->assertEqual('', $this->testpage->bodyclasses); 00255 } 00256 00257 public function test_get_body_classes_single() { 00258 // Exercise SUT 00259 $this->testpage->add_body_class('aclassname'); 00260 // Validate 00261 $this->assertEqual('aclassname', $this->testpage->bodyclasses); 00262 } 00263 00264 public function test_get_body_classes() { 00265 // Exercise SUT 00266 $this->testpage->add_body_classes(array('aclassname', 'anotherclassname')); 00267 // Validate 00268 $this->assertEqual('aclassname anotherclassname', $this->testpage->bodyclasses); 00269 } 00270 00271 public function test_url_to_class_name() { 00272 $this->assertEqual('example-com', $this->testpage->url_to_class_name('http://example.com')); 00273 $this->assertEqual('example-com--80', $this->testpage->url_to_class_name('http://example.com:80')); 00274 $this->assertEqual('example-com--moodle', $this->testpage->url_to_class_name('https://example.com/moodle')); 00275 $this->assertEqual('example-com--8080--nested-moodle', $this->testpage->url_to_class_name('https://example.com:8080/nested/moodle')); 00276 } 00277 00278 public function test_set_docs_path() { 00279 // Exercise SUT 00280 $this->testpage->set_docs_path('a/file/path'); 00281 // Validate 00282 $this->assertEqual('a/file/path', $this->testpage->docspath); 00283 } 00284 00285 public function test_docs_path_defaults_from_pagetype() { 00286 // Exercise SUT 00287 $this->testpage->set_pagetype('a-page-type'); 00288 // Validate 00289 $this->assertEqual('a/page/type', $this->testpage->docspath); 00290 } 00291 00292 public function test_set_url_root() { 00293 global $CFG; 00294 // Exercise SUT 00295 $this->testpage->set_url('/'); 00296 // Validate 00297 $this->assertEqual($CFG->wwwroot . '/', $this->testpage->url->out()); 00298 } 00299 00300 public function test_set_url_one_param() { 00301 global $CFG; 00302 // Exercise SUT 00303 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123)); 00304 // Validate 00305 $this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123', $this->testpage->url->out()); 00306 } 00307 00308 public function test_set_url_two_params() { 00309 global $CFG; 00310 // Exercise SUT 00311 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7)); 00312 // Validate 00313 $this->assertEqual($CFG->wwwroot . '/mod/quiz/attempt.php?attempt=123&page=7', $this->testpage->url->out()); 00314 } 00315 00316 public function test_set_url_using_moodle_url() { 00317 global $CFG; 00318 // Fixture setup 00319 $url = new moodle_url('/mod/workshop/allocation.php', array('cmid' => 29, 'method' => 'manual')); 00320 // Exercise SUT 00321 $this->testpage->set_url($url); 00322 // Validate 00323 $this->assertEqual($CFG->wwwroot . '/mod/workshop/allocation.php?cmid=29&method=manual', $this->testpage->url->out()); 00324 } 00325 00326 public function test_set_url_sets_page_type() { 00327 // Exercise SUT 00328 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7)); 00329 // Validate 00330 $this->assertEqual('mod-quiz-attempt', $this->testpage->pagetype); 00331 } 00332 00333 public function test_set_url_does_not_change_explicit_page_type() { 00334 // Setup fixture 00335 $this->testpage->set_pagetype('a-page-type'); 00336 // Exercise SUT 00337 $this->testpage->set_url('/mod/quiz/attempt.php', array('attempt' => 123, 'page' => 7)); 00338 // Validate 00339 $this->assertEqual('a-page-type', $this->testpage->pagetype); 00340 } 00341 00342 public function test_set_subpage() { 00343 // Exercise SUT 00344 $this->testpage->set_subpage('somestring'); 00345 // Validate 00346 $this->assertEqual('somestring', $this->testpage->subpage); 00347 } 00348 00349 public function test_set_heading() { 00350 // Exercise SUT 00351 $this->testpage->set_heading('a heading'); 00352 // Validate 00353 $this->assertEqual('a heading', $this->testpage->heading); 00354 } 00355 00356 public function test_set_title() { 00357 // Exercise SUT 00358 $this->testpage->set_title('a title'); 00359 // Validate 00360 $this->assertEqual('a title', $this->testpage->title); 00361 } 00362 00363 public function test_default_pagelayout() { 00364 // Exercise SUT and Validate 00365 $this->assertEqual('base', $this->testpage->pagelayout); 00366 } 00367 00368 public function test_set_pagelayout() { 00369 // Exercise SUT 00370 $this->testpage->set_pagelayout('type'); 00371 // Validate 00372 $this->assertEqual('type', $this->testpage->pagelayout); 00373 } 00374 } 00375 00379 class moodle_page_with_context_table_test extends UnitTestCaseUsingDatabase { 00380 protected $testpage; 00381 protected $originalcourse; 00382 00383 public function setUp() { 00384 global $COURSE; 00385 parent::setUp(); 00386 $this->originalcourse = $COURSE; 00387 $this->testpage = new moodle_page(); 00388 $this->create_test_table('context', 'lib'); 00389 $this->switch_to_test_db(); 00390 } 00391 00392 public function tearDown() { 00393 global $COURSE; 00394 $this->testpage = NULL; 00395 $COURSE = $this->originalcourse; 00396 parent::tearDown(); 00397 } 00398 00400 protected function create_a_course_with_context() { 00401 $course = new stdClass; 00402 $course->id = 13; 00403 $course->category = 2; 00404 $course->fullname = 'Anonymous test course'; 00405 $course->shortname = 'ANON'; 00406 $course->summary = ''; 00407 00408 $context = new stdClass; 00409 $context->contextlevel = CONTEXT_COURSE; 00410 $context->instanceid = $course->id; 00411 $context->path = 'not initialised'; 00412 $context->depth = '13'; 00413 $this->testdb->insert_record('context', $context); 00414 00415 return $course; 00416 } 00417 00418 public function test_setting_course_sets_context() { 00419 // Setup fixture 00420 $course = $this->create_a_course_with_context(); 00421 // Exercise SUT 00422 $this->testpage->set_course($course); 00423 // Validate 00424 $expectedcontext = new stdClass; 00425 $expectedcontext->contextlevel = CONTEXT_COURSE; 00426 $expectedcontext->instanceid = $course->id; 00427 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context); 00428 } 00429 } 00430 00434 class moodle_page_categories_test extends UnitTestCaseUsingDatabase { 00435 protected $testpage; 00436 protected $originalcourse; 00437 00438 public function setUp() { 00439 global $COURSE, $SITE; 00440 parent::setUp(); 00441 $this->originalcourse = $COURSE; 00442 $this->testpage = new moodle_page(); 00443 $this->create_test_tables(array('course_categories', 'context'), 'lib'); 00444 $this->switch_to_test_db(); 00445 00446 $context = new stdClass; 00447 $context->contextlevel = CONTEXT_COURSE; 00448 $context->instanceid = $SITE->id; 00449 $context->path = 'not initialised'; 00450 $context->depth = '13'; 00451 $this->testdb->insert_record('context', $context); 00452 } 00453 00454 public function tearDown() { 00455 global $COURSE; 00456 $this->testpage = NULL; 00457 $COURSE = $this->originalcourse; 00458 parent::tearDown(); 00459 } 00460 00462 protected function create_a_category_with_context($parentid = 0) { 00463 if ($parentid) { 00464 $parent = $this->testdb->get_record('course_categories', array('id' => $parentid)); 00465 } else { 00466 $parent = new stdClass; 00467 $parent->depth = 0; 00468 $parent->path = ''; 00469 } 00470 $cat = new stdClass; 00471 $cat->name = 'Anonymous test category'; 00472 $cat->description = ''; 00473 $cat->parent = $parentid; 00474 $cat->depth = $parent->depth + 1; 00475 $cat->id = $this->testdb->insert_record('course_categories', $cat); 00476 $cat->path = $parent->path . '/' . $cat->id; 00477 $this->testdb->set_field('course_categories', 'path', $cat->path, array('id' => $cat->id)); 00478 00479 $context = new stdClass; 00480 $context->contextlevel = CONTEXT_COURSECAT; 00481 $context->instanceid = $cat->id; 00482 $context->path = 'not initialised'; 00483 $context->depth = '13'; 00484 $this->testdb->insert_record('context', $context); 00485 00486 return $cat; 00487 } 00488 00489 public function test_set_category_top_level() { 00490 // Setup fixture 00491 $cat = $this->create_a_category_with_context(); 00492 // Exercise SUT 00493 $this->testpage->set_category_by_id($cat->id); 00494 // Validate 00495 $this->assert(new CheckSpecifiedFieldsExpectation($cat), $this->testpage->category); 00496 $expectedcontext = new stdClass; // Test it sets the context. 00497 $expectedcontext->contextlevel = CONTEXT_COURSECAT; 00498 $expectedcontext->instanceid = $cat->id; 00499 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context); 00500 } 00501 00502 public function test_set_nested_categories() { 00503 // Setup fixture 00504 $topcat = $this->create_a_category_with_context(); 00505 $subcat = $this->create_a_category_with_context($topcat->id); 00506 // Exercise SUT 00507 $this->testpage->set_category_by_id($subcat->id); 00508 // Validate 00509 $categories = $this->testpage->categories; 00510 $this->assertEqual(2, count($categories)); 00511 $this->assert(new CheckSpecifiedFieldsExpectation($topcat), array_pop($categories)); 00512 $this->assert(new CheckSpecifiedFieldsExpectation($subcat), array_pop($categories)); 00513 } 00514 } 00515 00519 class moodle_page_cm_test extends UnitTestCaseUsingDatabase { 00520 protected $testpage; 00521 protected $originalcourse; 00522 00523 public function setUp() { 00524 global $COURSE, $SITE; 00525 parent::setUp(); 00526 $this->originalcourse = $COURSE; 00527 $this->testpage = new moodle_page(); 00528 $this->create_test_tables(array('course', 'context', 'modules', 'course_modules', 'course_modules_availability', 'grade_items', 'course_sections'), 'lib'); 00529 $this->create_test_table('forum', 'mod/forum'); 00530 $this->switch_to_test_db(); 00531 00532 $context = new stdClass; 00533 $context->contextlevel = CONTEXT_COURSE; 00534 $context->instanceid = $SITE->id; 00535 $context->path = 'not initialised'; 00536 $context->depth = '13'; 00537 $this->testdb->insert_record('context', $context); 00538 } 00539 00540 public function tearDown() { 00541 global $COURSE; 00542 $this->testpage = NULL; 00543 $COURSE = $this->originalcourse; 00544 parent::tearDown(); 00545 } 00546 00548 protected function create_a_forum_with_context() { 00549 $course = new stdClass; 00550 $course->category = 2; 00551 $course->fullname = 'Anonymous test course'; 00552 $course->shortname = 'ANON'; 00553 $course->summary = ''; 00554 $course->modinfo = null; 00555 $course->id = $this->testdb->insert_record('course', $course); 00556 00557 $forum = new stdClass; 00558 $forum->course = $course->id; 00559 $forum->name = 'Anonymouse test forum'; 00560 $forum->intro = ''; 00561 $forum->id = $this->testdb->insert_record('forum', $forum); 00562 00563 $module = new stdClass; 00564 $module->name = 'forum'; 00565 $module->id = $this->testdb->insert_record('modules', $module); 00566 00567 $cm = new stdClass; 00568 $cm->course = $course->id; 00569 $cm->instance = $forum->id; 00570 $cm->modname = 'forum'; 00571 $cm->module = $module->id; 00572 $cm->name = $forum->name; 00573 $cm->id = $this->testdb->insert_record('course_modules', $cm); 00574 00575 $section = new stdClass; 00576 $section->course = $course->id; 00577 $section->section = 0; 00578 $section->sequence = $cm->id; 00579 $section->id = $this->testdb->insert_record('course_sections', $section); 00580 00581 $context = new stdClass; 00582 $context->contextlevel = CONTEXT_MODULE; 00583 $context->instanceid = $cm->id; 00584 $context->path = 'not initialised'; 00585 $context->depth = '13'; 00586 $this->testdb->insert_record('context', $context); 00587 00588 return array($cm, $course, $forum); 00589 } 00590 00591 public function test_cm_null_initially() { 00592 // Validate 00593 $this->assertNull($this->testpage->cm); 00594 } 00595 00596 public function test_set_cm() { 00597 // Setup fixture 00598 list($cm) = $this->create_a_forum_with_context(); 00599 // Exercise SUT 00600 $this->testpage->set_cm($cm); 00601 // Validate 00602 $this->assert(new CheckSpecifiedFieldsExpectation($cm), $this->testpage->cm); 00603 } 00604 00605 public function test_cannot_set_activity_record_before_cm() { 00606 // Setup fixture 00607 list($cm, $course, $forum) = $this->create_a_forum_with_context(); 00608 // Set expectation 00609 $this->expectException(); 00610 // Exercise SUT 00611 $this->testpage->set_activity_record($forum); 00612 } 00613 00614 public function test_setting_cm_sets_context() { 00615 // Setup fixture 00616 list($cm) = $this->create_a_forum_with_context(); 00617 // Exercise SUT 00618 $this->testpage->set_cm($cm); 00619 // Validate 00620 $expectedcontext = new stdClass; 00621 $expectedcontext->contextlevel = CONTEXT_MODULE; 00622 $expectedcontext->instanceid = $cm->id; 00623 $this->assert(new CheckSpecifiedFieldsExpectation($expectedcontext), $this->testpage->context); 00624 } 00625 00626 public function test_activity_record_loaded_if_not_set() { 00627 // Setup fixture 00628 list($cm, $course, $forum) = $this->create_a_forum_with_context(); 00629 // Exercise SUT 00630 $this->testpage->set_cm($cm); 00631 // Validate 00632 $this->assert(new CheckSpecifiedFieldsExpectation($forum), $this->testpage->activityrecord); 00633 } 00634 00635 public function test_set_activity_record() { 00636 // Setup fixture 00637 list($cm, $course, $forum) = $this->create_a_forum_with_context(); 00638 $this->testpage->set_cm($cm); 00639 // Exercise SUT 00640 $this->testpage->set_activity_record($forum); 00641 // Validate 00642 $this->assert(new CheckSpecifiedFieldsExpectation($forum), $this->testpage->activityrecord); 00643 } 00644 00645 public function test_cannot_set_inconsistent_activity_record_course() { 00646 // Setup fixture 00647 list($cm, $course, $forum) = $this->create_a_forum_with_context(); 00648 $this->testpage->set_cm($cm); 00649 // Set expectation 00650 $this->expectException(); 00651 // Exercise SUT 00652 $forum->course = 13; 00653 $this->testpage->set_activity_record($forum); 00654 } 00655 00656 public function test_cannot_set_inconsistent_activity_record_instance() { 00657 // Setup fixture 00658 list($cm, $course, $forum) = $this->create_a_forum_with_context(); 00659 $this->testpage->set_cm($cm); 00660 // Set expectation 00661 $this->expectException(); 00662 // Exercise SUT 00663 $forum->id = 13; 00664 $this->testpage->set_activity_record($forum); 00665 } 00666 00667 public function test_setting_cm_sets_course() { 00668 // Setup fixture 00669 list($cm, $course) = $this->create_a_forum_with_context(); 00670 // Exercise SUT 00671 $this->testpage->set_cm($cm); 00672 // Validate 00673 unset($course->modinfo); // This changed, but we don't care 00674 $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course); 00675 } 00676 00677 public function test_set_cm_with_course_and_activity_no_db() { 00678 // Setup fixture 00679 list($cm, $course, $forum) = $this->create_a_forum_with_context(); 00680 // This only works without db if we already have modinfo cache 00681 $modinfo = get_fast_modinfo($course); 00682 $this->drop_test_table('forum'); 00683 $this->drop_test_table('course'); 00684 // Exercise SUT 00685 $this->testpage->set_cm($cm, $course, $forum); 00686 // Validate 00687 $this->assert(new CheckSpecifiedFieldsExpectation($cm), $this->testpage->cm); 00688 $this->assert(new CheckSpecifiedFieldsExpectation($course), $this->testpage->course); 00689 $this->assert(new CheckSpecifiedFieldsExpectation($forum), $this->testpage->activityrecord); 00690 } 00691 00692 public function test_cannot_set_cm_with_inconsistent_course() { 00693 // Setup fixture 00694 list($cm, $course, $forum) = $this->create_a_forum_with_context(); 00695 // Set expectation 00696 $this->expectException(); 00697 // Exercise SUT 00698 $cm->course = 13; 00699 $this->testpage->set_cm($cm, $course); 00700 } 00701 00702 public function test_get_activity_name() { 00703 // Setup fixture 00704 list($cm, $course, $forum) = $this->create_a_forum_with_context(); 00705 // Exercise SUT 00706 $this->testpage->set_cm($cm, $course, $forum); 00707 // Validate 00708 $this->assertEqual('forum', $this->testpage->activityname); 00709 } 00710 } 00711 00715 class moodle_page_editing_test extends UnitTestCase { 00716 protected $testpage; 00717 protected $originaluserediting; 00718 00719 public function setUp() { 00720 global $USER; 00721 $this->originaluserediting = !empty($USER->editing); 00722 $this->testpage = new testable_moodle_page(); 00723 $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM)); 00724 } 00725 00726 public function tearDown() { 00727 global $USER; 00728 $this->testpage = NULL; 00729 $USER->editing = $this->originaluserediting; 00730 } 00731 00732 // We are relying on the fact that unit tests are alwyas run by admin, to 00733 // ensure the user_allows_editing call returns true. 00734 public function test_user_is_editing_on() { 00735 // Setup fixture 00736 global $USER; 00737 $USER->editing = true; 00738 // Validate 00739 $this->assertTrue($this->testpage->user_is_editing()); 00740 } 00741 00742 // We are relying on the fact that unit tests are alwyas run by admin, to 00743 // ensure the user_allows_editing call returns true. 00744 public function test_user_is_editing_off() { 00745 // Setup fixture 00746 global $USER; 00747 $USER->editing = false; 00748 // Validate 00749 $this->assertFalse($this->testpage->user_is_editing()); 00750 } 00751 00752 public function test_default_editing_capabilities() { 00753 // Validate 00754 $this->assertEqual(array('moodle/site:manageblocks'), $this->testpage->all_editing_caps()); 00755 } 00756 00757 public function test_other_block_editing_cap() { 00758 // Exercise SUT 00759 $this->testpage->set_blocks_editing_capability('moodle/my:manageblocks'); 00760 // Validate 00761 $this->assertEqual(array('moodle/my:manageblocks'), $this->testpage->all_editing_caps()); 00762 } 00763 00764 public function test_other_editing_cap() { 00765 // Exercise SUT 00766 $this->testpage->set_other_editing_capability('moodle/course:manageactivities'); 00767 // Validate 00768 $actualcaps = $this->testpage->all_editing_caps(); 00769 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:manageblocks'); 00770 $this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps); 00771 } 00772 00773 public function test_other_editing_caps() { 00774 // Exercise SUT 00775 $this->testpage->set_other_editing_capability(array('moodle/course:manageactivities', 'moodle/site:other')); 00776 // Validate 00777 $actualcaps = $this->testpage->all_editing_caps(); 00778 $expectedcaps = array('moodle/course:manageactivities', 'moodle/site:other', 'moodle/site:manageblocks'); 00779 $this->assert(new ArraysHaveSameValuesExpectation($expectedcaps), $actualcaps); 00780 } 00781 } 00782