|
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 00034 if (!defined('MOODLE_INTERNAL')) { 00035 die('Direct access to this script is forbidden.'); 00036 } 00037 00038 require_once($CFG->libdir . '/filterlib.php'); 00039 00043 class filter_active_global_test extends UnitTestCaseUsingDatabase { 00044 private $syscontextid; 00045 public static $includecoverage = array('lib/filterlib.php'); 00046 00047 public function setUp() { 00048 parent::setUp(); 00049 00050 // Make sure accesslib has cached a sensible system context object 00051 // before we switch to the test DB. 00052 $this->syscontextid = get_context_instance(CONTEXT_SYSTEM)->id; 00053 00054 // Create the table we need and switch to test DB. 00055 $this->create_test_table('filter_active', 'lib'); 00056 $this->switch_to_test_db(); 00057 } 00058 00059 private function assert_only_one_filter_globally($filter, $state) { 00060 $recs = $this->testdb->get_records('filter_active'); 00061 $this->assertEqual(1, count($recs), 'More than one record returned %s.'); 00062 $rec = reset($recs); 00063 $expectedrec = new stdClass; 00064 $expectedrec->filter = $filter; 00065 $expectedrec->contextid = $this->syscontextid; 00066 $expectedrec->active = $state; 00067 $expectedrec->sortorder = 1; 00068 $this->assert(new CheckSpecifiedFieldsExpectation($expectedrec), $rec); 00069 } 00070 00071 private function assert_global_sort_order($filters) { 00072 $sortedfilters = $this->testdb->get_records_menu('filter_active', 00073 array('contextid' => $this->syscontextid), 'sortorder', 'sortorder,filter'); 00074 $testarray = array(); 00075 $index = 1; 00076 foreach($filters as $filter) { 00077 $testarray[$index++] = $filter; 00078 } 00079 $this->assertEqual($testarray, $sortedfilters); 00080 } 00081 00082 public function test_set_filter_globally_on() { 00083 // Setup fixture. 00084 // Exercise SUT. 00085 filter_set_global_state('filter/name', TEXTFILTER_ON, 1); 00086 // Validate. 00087 $this->assert_only_one_filter_globally('filter/name', TEXTFILTER_ON); 00088 } 00089 00090 public function test_set_filter_globally_off() { 00091 // Setup fixture. 00092 // Exercise SUT. 00093 filter_set_global_state('filter/name', TEXTFILTER_OFF, 1); 00094 // Validate. 00095 $this->assert_only_one_filter_globally('filter/name', TEXTFILTER_OFF); 00096 } 00097 00098 public function test_set_filter_globally_disabled() { 00099 // Setup fixture. 00100 // Exercise SUT. 00101 filter_set_global_state('filter/name', TEXTFILTER_DISABLED, 1); 00102 // Validate. 00103 $this->assert_only_one_filter_globally('filter/name', TEXTFILTER_DISABLED); 00104 } 00105 00106 public function test_global_config_exception_on_invalid_state() { 00107 // Setup fixture. 00108 // Set expectation. 00109 $this->expectException(); 00110 // Exercise SUT. 00111 filter_set_global_state('filter/name', 0, 1); 00112 } 00113 00114 public function test_set_no_sortorder_clash() { 00115 // Setup fixture. 00116 // Exercise SUT. 00117 filter_set_global_state('filter/one', TEXTFILTER_DISABLED, 1); 00118 filter_set_global_state('filter/two', TEXTFILTER_DISABLED, 1); 00119 // Validate - should have pushed other filters down. 00120 $this->assert_global_sort_order(array('filter/two', 'filter/one')); 00121 } 00122 00123 public function test_auto_sort_order_disabled() { 00124 // Setup fixture. 00125 // Exercise SUT. 00126 filter_set_global_state('filter/one', TEXTFILTER_DISABLED); 00127 filter_set_global_state('filter/two', TEXTFILTER_DISABLED); 00128 // Validate. 00129 $this->assert_global_sort_order(array('filter/one', 'filter/two')); 00130 } 00131 00132 public function test_auto_sort_order_enabled() { 00133 // Setup fixture. 00134 // Exercise SUT. 00135 filter_set_global_state('filter/one', TEXTFILTER_ON); 00136 filter_set_global_state('filter/two', TEXTFILTER_OFF); 00137 // Validate. 00138 $this->assert_global_sort_order(array('filter/one', 'filter/two')); 00139 } 00140 00141 public function test_auto_sort_order_mixed() { 00142 // Setup fixture. 00143 // Exercise SUT. 00144 filter_set_global_state('filter/0', TEXTFILTER_DISABLED); 00145 filter_set_global_state('filter/1', TEXTFILTER_ON); 00146 filter_set_global_state('filter/2', TEXTFILTER_DISABLED); 00147 filter_set_global_state('filter/3', TEXTFILTER_OFF); 00148 // Validate. 00149 $this->assert_global_sort_order(array('filter/1', 'filter/3', 'filter/0', 'filter/2')); 00150 } 00151 00152 public function test_update_existing_dont_duplicate() { 00153 // Setup fixture. 00154 // Exercise SUT. 00155 filter_set_global_state('filter/name', TEXTFILTER_ON); 00156 filter_set_global_state('filter/name', TEXTFILTER_OFF); 00157 // Validate. 00158 $this->assert_only_one_filter_globally('filter/name', TEXTFILTER_OFF); 00159 } 00160 00161 public function test_sort_order_not_too_low() { 00162 // Setup fixture. 00163 filter_set_global_state('filter/1', TEXTFILTER_ON); 00164 // Set expectation. 00165 $this->expectException(); 00166 // Exercise SUT. 00167 filter_set_global_state('filter/2', TEXTFILTER_ON, 0); 00168 } 00169 00170 public function test_sort_order_not_too_high() { 00171 // Setup fixture. 00172 filter_set_global_state('filter/1', TEXTFILTER_ON); 00173 // Set expectation. 00174 $this->expectException(); 00175 // Exercise SUT. 00176 filter_set_global_state('filter/2', TEXTFILTER_ON, 3); 00177 } 00178 00179 public function test_update_reorder_down() { 00180 // Setup fixture. 00181 filter_set_global_state('filter/1', TEXTFILTER_ON); 00182 filter_set_global_state('filter/2', TEXTFILTER_ON); 00183 filter_set_global_state('filter/3', TEXTFILTER_ON); 00184 // Exercise SUT. 00185 filter_set_global_state('filter/2', TEXTFILTER_ON, 1); 00186 // Validate. 00187 $this->assert_global_sort_order(array('filter/2', 'filter/1', 'filter/3')); 00188 } 00189 00190 public function test_update_reorder_up() { 00191 // Setup fixture. 00192 filter_set_global_state('filter/1', TEXTFILTER_ON); 00193 filter_set_global_state('filter/2', TEXTFILTER_ON); 00194 filter_set_global_state('filter/3', TEXTFILTER_ON); 00195 filter_set_global_state('filter/4', TEXTFILTER_ON); 00196 // Exercise SUT. 00197 filter_set_global_state('filter/2', TEXTFILTER_ON, 3); 00198 // Validate. 00199 $this->assert_global_sort_order(array('filter/1', 'filter/3', 'filter/2', 'filter/4')); 00200 } 00201 00202 public function test_auto_sort_order_change_to_enabled() { 00203 // Setup fixture. 00204 filter_set_global_state('filter/1', TEXTFILTER_ON); 00205 filter_set_global_state('filter/2', TEXTFILTER_DISABLED); 00206 filter_set_global_state('filter/3', TEXTFILTER_DISABLED); 00207 // Exercise SUT. 00208 filter_set_global_state('filter/3', TEXTFILTER_ON); 00209 // Validate. 00210 $this->assert_global_sort_order(array('filter/1', 'filter/3', 'filter/2')); 00211 } 00212 00213 public function test_auto_sort_order_change_to_disabled() { 00214 // Setup fixture. 00215 filter_set_global_state('filter/1', TEXTFILTER_ON); 00216 filter_set_global_state('filter/2', TEXTFILTER_ON); 00217 filter_set_global_state('filter/3', TEXTFILTER_DISABLED); 00218 // Exercise SUT. 00219 filter_set_global_state('filter/1', TEXTFILTER_DISABLED); 00220 // Validate. 00221 $this->assert_global_sort_order(array('filter/2', 'filter/1', 'filter/3')); 00222 } 00223 00224 public function test_filter_get_global_states() { 00225 // Setup fixture. 00226 filter_set_global_state('filter/1', TEXTFILTER_ON); 00227 filter_set_global_state('filter/2', TEXTFILTER_OFF); 00228 filter_set_global_state('filter/3', TEXTFILTER_DISABLED); 00229 // Exercise SUT. 00230 $filters = filter_get_global_states(); 00231 // Validate. 00232 $this->assertEqual(array( 00233 'filter/1' => (object) array('filter' => 'filter/1', 'active' => TEXTFILTER_ON, 'sortorder' => 1), 00234 'filter/2' => (object) array('filter' => 'filter/2', 'active' => TEXTFILTER_OFF, 'sortorder' => 2), 00235 'filter/3' => (object) array('filter' => 'filter/3', 'active' => TEXTFILTER_DISABLED, 'sortorder' => 3) 00236 ), $filters); 00237 } 00238 } 00239 00243 class filter_active_local_test extends UnitTestCaseUsingDatabase { 00244 public function setUp() { 00245 parent::setUp(); 00246 00247 // Create the table we need and switch to test DB. 00248 $this->create_test_table('filter_active', 'lib'); 00249 $this->switch_to_test_db(); 00250 } 00251 00252 private function assert_only_one_local_setting($filter, $contextid, $state) { 00253 $recs = $this->testdb->get_records('filter_active'); 00254 $this->assertEqual(1, count($recs), 'More than one record returned %s.'); 00255 $rec = reset($recs); 00256 $expectedrec = new stdClass; 00257 $expectedrec->filter = $filter; 00258 $expectedrec->contextid = $contextid; 00259 $expectedrec->active = $state; 00260 $this->assert(new CheckSpecifiedFieldsExpectation($expectedrec), $rec); 00261 } 00262 00263 private function assert_no_local_setting() { 00264 $this->assertEqual(0, $this->testdb->count_records('filter_active')); 00265 } 00266 00267 public function test_local_on() { 00268 // Exercise SUT. 00269 filter_set_local_state('filter/name', 123, TEXTFILTER_ON); 00270 // Validate. 00271 $this->assert_only_one_local_setting('filter/name', 123, TEXTFILTER_ON); 00272 } 00273 00274 public function test_local_off() { 00275 // Exercise SUT. 00276 filter_set_local_state('filter/name', 123, TEXTFILTER_OFF); 00277 // Validate. 00278 $this->assert_only_one_local_setting('filter/name', 123, TEXTFILTER_OFF); 00279 } 00280 00281 public function test_local_inherit() { 00282 global $DB; 00283 // Exercise SUT. 00284 filter_set_local_state('filter/name', 123, TEXTFILTER_INHERIT); 00285 // Validate. 00286 $this->assert_no_local_setting(); 00287 } 00288 00289 public function test_local_invalid_state_throws_exception() { 00290 // Set expectation. 00291 $this->expectException(); 00292 // Exercise SUT. 00293 filter_set_local_state('filter/name', 123, -9999); 00294 } 00295 00296 public function test_throws_exception_when_setting_global() { 00297 // Set expectation. 00298 $this->expectException(); 00299 // Exercise SUT. 00300 filter_set_local_state('filter/name', get_context_instance(CONTEXT_SYSTEM)->id, TEXTFILTER_INHERIT); 00301 } 00302 00303 public function test_local_inherit_deletes_existing() { 00304 global $DB; 00305 // Setup fixture. 00306 filter_set_local_state('filter/name', 123, TEXTFILTER_INHERIT); 00307 // Exercise SUT. 00308 filter_set_local_state('filter/name', 123, TEXTFILTER_INHERIT); 00309 // Validate. 00310 $this->assert_no_local_setting(); 00311 } 00312 } 00313 00317 class filter_config_test extends UnitTestCaseUsingDatabase { 00318 public function setUp() { 00319 parent::setUp(); 00320 00321 // Create the table we need and switch to test DB. 00322 $this->create_test_table('filter_config', 'lib'); 00323 $this->switch_to_test_db(); 00324 } 00325 00326 private function assert_only_one_config($filter, $context, $name, $value) { 00327 $recs = $this->testdb->get_records('filter_config'); 00328 $this->assertEqual(1, count($recs), 'More than one record returned %s.'); 00329 $rec = reset($recs); 00330 $expectedrec = new stdClass; 00331 $expectedrec->filter = $filter; 00332 $expectedrec->contextid = $context; 00333 $expectedrec->name = $name; 00334 $expectedrec->value = $value; 00335 $this->assert(new CheckSpecifiedFieldsExpectation($expectedrec), $rec); 00336 } 00337 00338 public function test_set_new_config() { 00339 // Exercise SUT. 00340 filter_set_local_config('filter/name', 123, 'settingname', 'An arbitrary value'); 00341 // Validate. 00342 $this->assert_only_one_config('filter/name', 123, 'settingname', 'An arbitrary value'); 00343 } 00344 00345 public function test_update_existing_config() { 00346 // Setup fixture. 00347 filter_set_local_config('filter/name', 123, 'settingname', 'An arbitrary value'); 00348 // Exercise SUT. 00349 filter_set_local_config('filter/name', 123, 'settingname', 'A changed value'); 00350 // Validate. 00351 $this->assert_only_one_config('filter/name', 123, 'settingname', 'A changed value'); 00352 } 00353 00354 public function test_filter_get_local_config() { 00355 // Setup fixture. 00356 filter_set_local_config('filter/name', 123, 'setting1', 'An arbitrary value'); 00357 filter_set_local_config('filter/name', 123, 'setting2', 'Another arbitrary value'); 00358 filter_set_local_config('filter/name', 122, 'settingname', 'Value from another context'); 00359 filter_set_local_config('filter/other', 123, 'settingname', 'Someone else\'s value'); 00360 // Exercise SUT. 00361 $config = filter_get_local_config('filter/name', 123); 00362 // Validate. 00363 $this->assertEqual(array('setting1' => 'An arbitrary value', 'setting2' => 'Another arbitrary value'), $config); 00364 } 00365 } 00366 00367 class filter_get_active_available_in_context_test extends UnitTestCaseUsingDatabase { 00368 private $syscontext; 00369 private $childcontext; 00370 private $childcontext2; 00371 00372 public function setUp() { 00373 parent::setUp(); 00374 00375 // Create the table we need and switch to test DB. 00376 $this->create_test_tables(array('filter_active', 'filter_config', 'context', 'course_categories', 'course'), 'lib'); 00377 $this->switch_to_test_db(); 00378 // Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily) 00379 $this->create_system_context_record(); 00380 // Reset all caches 00381 accesslib_clear_all_caches_for_unit_testing(); 00382 00383 $this->syscontext = get_context_instance(CONTEXT_SYSTEM); 00384 00385 // Set up a child context. 00386 $cat = new stdClass(); 00387 $cat->name = 'testcategory'; 00388 $cat->parent = 0; 00389 $cat->depth = 1; 00390 $cat->sortorder = 100; 00391 $cat->timemodified = time(); 00392 $catid = $this->testdb->insert_record('course_categories', $cat); 00393 $this->testdb->set_field('course_categories', 'path', '/' . $catid, array('id' => $catid)); 00394 $this->childcontext = context_coursecat::instance($catid); 00395 00396 // Set up a grandchild context. 00397 $course = new stdClass(); 00398 $course->fullname = 'testcourse'; 00399 $course->shortname = 'tc'; 00400 $course->summary = 'testcourse summary'; 00401 $course->newsitems = 0; 00402 $course->numsections = 1; 00403 $course->category = $catid; 00404 $course->format = 'topics'; 00405 $course->timecreated = time(); 00406 $course->visible = 1; 00407 $course->timemodified = $course->timecreated; 00408 $courseid = $this->testdb->insert_record('course', $course); 00409 $this->childcontext2 = context_course::instance($courseid); 00410 } 00411 00412 private function assert_filter_list($expectedfilters, $filters) { 00413 $this->assert(new ArraysHaveSameValuesExpectation($expectedfilters), array_keys($filters)); 00414 } 00415 00416 public function test_globally_on_is_returned() { 00417 // Setup fixture. 00418 filter_set_global_state('filter/name', TEXTFILTER_ON); 00419 // Exercise SUT. 00420 $filters = filter_get_active_in_context($this->syscontext); 00421 // Validate. 00422 $this->assert_filter_list(array('filter/name'), $filters); 00423 // Check no config returned correctly. 00424 $this->assertEqual(array(), $filters['filter/name']); 00425 } 00426 00427 public function test_globally_off_not_returned() { 00428 // Setup fixture. 00429 filter_set_global_state('filter/name', TEXTFILTER_OFF); 00430 // Exercise SUT. 00431 $filters = filter_get_active_in_context($this->childcontext2); 00432 // Validate. 00433 $this->assert_filter_list(array(), $filters); 00434 } 00435 00436 public function test_globally_off_overridden() { 00437 // Setup fixture. 00438 filter_set_global_state('filter/name', TEXTFILTER_OFF); 00439 filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_ON); 00440 // Exercise SUT. 00441 $filters = filter_get_active_in_context($this->childcontext2); 00442 // Validate. 00443 $this->assert_filter_list(array('filter/name'), $filters); 00444 } 00445 00446 public function test_globally_on_overridden() { 00447 // Setup fixture. 00448 filter_set_global_state('filter/name', TEXTFILTER_ON); 00449 filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_OFF); 00450 // Exercise SUT. 00451 $filters = filter_get_active_in_context($this->childcontext2); 00452 // Validate. 00453 $this->assert_filter_list(array(), $filters); 00454 } 00455 00456 public function test_globally_disabled_not_overridden() { 00457 // Setup fixture. 00458 filter_set_global_state('filter/name', TEXTFILTER_DISABLED); 00459 filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_ON); 00460 // Exercise SUT. 00461 $filters = filter_get_active_in_context($this->syscontext); 00462 // Validate. 00463 $this->assert_filter_list(array(), $filters); 00464 } 00465 00466 public function test_single_config_returned() { 00467 // Setup fixture. 00468 filter_set_global_state('filter/name', TEXTFILTER_ON); 00469 filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value'); 00470 // Exercise SUT. 00471 $filters = filter_get_active_in_context($this->childcontext); 00472 // Validate. 00473 $this->assertEqual(array('settingname' => 'A value'), $filters['filter/name']); 00474 } 00475 00476 public function test_multi_config_returned() { 00477 // Setup fixture. 00478 filter_set_global_state('filter/name', TEXTFILTER_ON); 00479 filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value'); 00480 filter_set_local_config('filter/name', $this->childcontext->id, 'anothersettingname', 'Another value'); 00481 // Exercise SUT. 00482 $filters = filter_get_active_in_context($this->childcontext); 00483 // Validate. 00484 $this->assertEqual(array('settingname' => 'A value', 'anothersettingname' => 'Another value'), $filters['filter/name']); 00485 } 00486 00487 public function test_config_from_other_context_not_returned() { 00488 // Setup fixture. 00489 filter_set_global_state('filter/name', TEXTFILTER_ON); 00490 filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value'); 00491 filter_set_local_config('filter/name', $this->childcontext2->id, 'anothersettingname', 'Another value'); 00492 // Exercise SUT. 00493 $filters = filter_get_active_in_context($this->childcontext2); 00494 // Validate. 00495 $this->assertEqual(array('anothersettingname' => 'Another value'), $filters['filter/name']); 00496 } 00497 00498 public function test_config_from_other_filter_not_returned() { 00499 // Setup fixture. 00500 filter_set_global_state('filter/name', TEXTFILTER_ON); 00501 filter_set_local_config('filter/name', $this->childcontext->id, 'settingname', 'A value'); 00502 filter_set_local_config('filter/other', $this->childcontext->id, 'anothersettingname', 'Another value'); 00503 // Exercise SUT. 00504 $filters = filter_get_active_in_context($this->childcontext); 00505 // Validate. 00506 $this->assertEqual(array('settingname' => 'A value'), $filters['filter/name']); 00507 } 00508 00509 protected function assert_one_available_filter($filter, $localstate, $inheritedstate, $filters) { 00510 $this->assertEqual(1, count($filters), 'More than one record returned %s.'); 00511 $rec = $filters[$filter]; 00512 $expectedrec = new stdClass; 00513 $expectedrec->filter = $filter; 00514 $expectedrec->localstate = $localstate; 00515 $expectedrec->inheritedstate = $inheritedstate; 00516 $this->assert(new CheckSpecifiedFieldsExpectation($expectedrec), $rec); 00517 } 00518 00519 public function test_available_in_context_localoverride() { 00520 // Setup fixture. 00521 filter_set_global_state('filter/name', TEXTFILTER_ON); 00522 filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_OFF); 00523 // Exercise SUT. 00524 $filters = filter_get_available_in_context($this->childcontext); 00525 // Validate. 00526 $this->assert_one_available_filter('filter/name', TEXTFILTER_OFF, TEXTFILTER_ON, $filters); 00527 } 00528 00529 public function test_available_in_context_nolocaloverride() { 00530 // Setup fixture. 00531 filter_set_global_state('filter/name', TEXTFILTER_ON); 00532 filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_OFF); 00533 // Exercise SUT. 00534 $filters = filter_get_available_in_context($this->childcontext2); 00535 // Validate. 00536 $this->assert_one_available_filter('filter/name', TEXTFILTER_INHERIT, TEXTFILTER_OFF, $filters); 00537 } 00538 00539 public function test_available_in_context_disabled_not_returned() { 00540 // Setup fixture. 00541 filter_set_global_state('filter/name', TEXTFILTER_DISABLED); 00542 filter_set_local_state('filter/name', $this->childcontext->id, TEXTFILTER_ON); 00543 // Exercise SUT. 00544 $filters = filter_get_available_in_context($this->childcontext); 00545 // Validate. 00546 $this->assertEqual(array(), $filters); 00547 } 00548 00549 public function test_available_in_context_exception_with_syscontext() { 00550 // Set expectation. 00551 $this->expectException(); 00552 // Exercise SUT. 00553 filter_get_available_in_context($this->syscontext); 00554 } 00555 } 00556 00557 class filter_preload_activities_test extends UnitTestCaseUsingDatabase { 00558 private $syscontext, $catcontext, $coursecontext, $activity1context, $activity2context; 00559 00560 public function setUp() { 00561 parent::setUp(); 00562 00563 // Make sure accesslib has cached a sensible system context object 00564 // before we switch to the test DB. 00565 $this->syscontext = get_context_instance(CONTEXT_SYSTEM); 00566 00567 // Create the table we need and switch to test DB. 00568 $this->create_test_tables(array('filter_active', 'filter_config', 'context', 00569 'course', 'course_categories', 'course_modules', 'modules', 'course_sections', 00570 'course_modules_availability', 'grade_items', 'cache_text'), 'lib'); 00571 $this->create_test_tables(array('label'), 'mod/label'); 00572 $this->switch_to_test_db(); 00573 // Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily) 00574 $this->create_system_context_record(); 00575 // Reset all caches 00576 accesslib_clear_all_caches_for_unit_testing(); 00577 00578 $this->syscontext = get_context_instance(CONTEXT_SYSTEM); 00579 00580 // Make the category 00581 $cat = new stdClass(); 00582 $cat->name = 'testcategory'; 00583 $cat->parent = 0; 00584 $cat->depth = 1; 00585 $cat->sortorder = 100; 00586 $cat->timemodified = time(); 00587 $catid = $this->testdb->insert_record('course_categories', $cat); 00588 $this->testdb->set_field('course_categories', 'path', '/' . $catid, array('id' => $catid)); 00589 $this->catcontext = context_coursecat::instance($catid); 00590 00591 // Make the course 00592 $course = (object)array( 00593 'shortname' => 'TEST101'); 00594 $courseid = $this->testdb->insert_record('course', $course); 00595 $this->coursecontext = context_course::instance($courseid); 00596 00597 // Set up section 00598 $section = (object)array( 00599 'course' => $courseid); 00600 $section->id = $this->testdb->insert_record('course_sections', $section); 00601 00602 // Make course-modules 00603 $mod = (object)array( 00604 'name' => 'label', 00605 'visible' => 1); 00606 $mod->id = $this->testdb->insert_record('modules', $mod); 00607 $label1 = (object)array( 00608 'course' => $courseid, 00609 'intro' => 'Intro 1', 00610 'name' => 'Label 1'); 00611 $label1->id = $this->testdb->insert_record('label', $label1); 00612 $cm1 = (object)array( 00613 'course' => $courseid, 00614 'section' => $section->id, 00615 'module' => $mod->id, 00616 'instance' => $label1->id); 00617 $cm1->id = $this->testdb->insert_record('course_modules', $cm1); 00618 $this->activity1context = context_module::instance($cm1->id); 00619 00620 $label2 = (object)array( 00621 'course' => $courseid, 00622 'intro' => 'Intro 2', 00623 'name' => 'Label 2'); 00624 $label2->id = $this->testdb->insert_record('label', $label2); 00625 $cm2 = (object)array( 00626 'course' => $courseid, 00627 'section' => $section->id, 00628 'module' => $mod->id, 00629 'instance' => $label2->id); 00630 $cm2->id = $this->testdb->insert_record('course_modules', $cm2); 00631 $this->testdb->set_field('course_sections', 'sequence', 00632 "$cm1->id,$cm2->id", array('id' => $section->id)); 00633 $this->activity2context = context_module::instance($cm2->id); 00634 } 00635 00636 private function assert_matches($modinfo) { 00637 global $FILTERLIB_PRIVATE; 00638 00639 // Use preload cache... 00640 $FILTERLIB_PRIVATE = new stdClass; 00641 filter_preload_activities($modinfo); 00642 00643 // Get data and check no queries are made 00644 $before = $this->testdb->perf_get_reads(); 00645 $plfilters1 = filter_get_active_in_context($this->activity1context); 00646 $plfilters2 = filter_get_active_in_context($this->activity2context); 00647 $after = $this->testdb->perf_get_reads(); 00648 $this->assertEqual($before, $after); 00649 00650 // Repeat without cache and check it makes queries now 00651 $FILTERLIB_PRIVATE = new stdClass; 00652 $before = $this->testdb->perf_get_reads(); 00653 $filters1 = filter_get_active_in_context($this->activity1context); 00654 $filters2 = filter_get_active_in_context($this->activity2context); 00655 $after = $this->testdb->perf_get_reads(); 00656 $this->assertTrue($after > $before); 00657 00658 // Check they match 00659 $this->assertEqual($plfilters1, $filters1); 00660 $this->assertEqual($plfilters2, $filters2); 00661 } 00662 00663 public function test_preload() { 00664 global $FILTERLIB_PRIVATE; 00665 00666 // Get course and modinfo 00667 $course = $this->testdb->get_record('course', array('id'=>1)); 00668 $modinfo = new course_modinfo($course, 1); 00669 00670 // Note: All the tests in this function check that the result from the 00671 // preloaded cache is the same as the result from calling the standard 00672 // function without preloading. 00673 00674 // Initially, check with no filters enabled 00675 $this->assert_matches($modinfo); 00676 00677 // Enable filter globally, check 00678 filter_set_global_state('filter/name', TEXTFILTER_ON); 00679 $this->assert_matches($modinfo); 00680 00681 // Disable for activity 2 00682 filter_set_local_state('filter/name', $this->activity2context->id, TEXTFILTER_OFF); 00683 $this->assert_matches($modinfo); 00684 00685 // Disable at category 00686 filter_set_local_state('filter/name', $this->catcontext->id, TEXTFILTER_OFF); 00687 $this->assert_matches($modinfo); 00688 00689 // Enable for activity 1 00690 filter_set_local_state('filter/name', $this->activity1context->id, TEXTFILTER_ON); 00691 $this->assert_matches($modinfo); 00692 00693 // Disable globally 00694 filter_set_global_state('filter/name', TEXTFILTER_DISABLED); 00695 $this->assert_matches($modinfo); 00696 00697 // Add another 2 filters 00698 filter_set_global_state('filter/frog', TEXTFILTER_ON); 00699 filter_set_global_state('filter/zombie', TEXTFILTER_ON); 00700 $this->assert_matches($modinfo); 00701 00702 // Disable random one of these in each context 00703 filter_set_local_state('filter/zombie', $this->activity1context->id, TEXTFILTER_OFF); 00704 filter_set_local_state('filter/frog', $this->activity2context->id, TEXTFILTER_OFF); 00705 $this->assert_matches($modinfo); 00706 00707 // Now do some filter options 00708 filter_set_local_config('filter/name', $this->activity1context->id, 'a', 'x'); 00709 filter_set_local_config('filter/zombie', $this->activity1context->id, 'a', 'y'); 00710 filter_set_local_config('filter/frog', $this->activity1context->id, 'a', 'z'); 00711 // These last two don't do anything as they are not at final level but I 00712 // thought it would be good to have that verified in test 00713 filter_set_local_config('filter/frog', $this->coursecontext->id, 'q', 'x'); 00714 filter_set_local_config('filter/frog', $this->catcontext->id, 'q', 'z'); 00715 $this->assert_matches($modinfo); 00716 } 00717 } 00718 00719 class filter_delete_config_test extends UnitTestCaseUsingDatabase { 00720 protected $syscontext; 00721 00722 public function setUp() { 00723 parent::setUp(); 00724 00725 $this->syscontext = get_context_instance(CONTEXT_SYSTEM); 00726 00727 // Create the table we need and switch to test DB. 00728 $this->create_test_tables(array('filter_active', 'filter_config', 'config', 'config_plugins'), 'lib'); 00729 $this->switch_to_test_db(); 00730 } 00731 00732 public function test_filter_delete_all_for_filter() { 00733 // Setup fixture. 00734 filter_set_global_state('filter/name', TEXTFILTER_ON); 00735 filter_set_global_state('filter/other', TEXTFILTER_ON); 00736 filter_set_local_config('filter/name', $this->syscontext->id, 'settingname', 'A value'); 00737 filter_set_local_config('filter/other', $this->syscontext->id, 'settingname', 'Other value'); 00738 set_config('configname', 'A config value', 'filter_name'); 00739 set_config('configname', 'Other config value', 'filter_other'); 00740 // Exercise SUT. 00741 filter_delete_all_for_filter('filter/name'); 00742 // Validate. 00743 $this->assertEqual(1, $this->testdb->count_records('filter_active')); 00744 $this->assertTrue($this->testdb->record_exists('filter_active', array('filter' => 'filter/other'))); 00745 $this->assertEqual(1, $this->testdb->count_records('filter_config')); 00746 $this->assertTrue($this->testdb->record_exists('filter_config', array('filter' => 'filter/other'))); 00747 $expectedconfig = new stdClass; 00748 $expectedconfig->configname = 'Other config value'; 00749 $this->assertEqual($expectedconfig, get_config('filter_other')); 00750 $this->assertIdentical(get_config('filter_name'), new stdClass()); 00751 } 00752 00753 public function test_filter_delete_all_for_context() { 00754 // Setup fixture. 00755 filter_set_global_state('filter/name', TEXTFILTER_ON); 00756 filter_set_local_state('filter/name', 123, TEXTFILTER_OFF); 00757 filter_set_local_config('filter/name', 123, 'settingname', 'A value'); 00758 filter_set_local_config('filter/other', 123, 'settingname', 'Other value'); 00759 filter_set_local_config('filter/other', 122, 'settingname', 'Other value'); 00760 // Exercise SUT. 00761 filter_delete_all_for_context(123); 00762 // Validate. 00763 $this->assertEqual(1, $this->testdb->count_records('filter_active')); 00764 $this->assertTrue($this->testdb->record_exists('filter_active', array('contextid' => $this->syscontext->id))); 00765 $this->assertEqual(1, $this->testdb->count_records('filter_config')); 00766 $this->assertTrue($this->testdb->record_exists('filter_config', array('filter' => 'filter/other'))); 00767 } 00768 } 00769 00770 class filter_filter_set_applies_to_strings extends UnitTestCaseUsingDatabase { 00771 protected $origcfgstringfilters; 00772 protected $origcfgfilterall; 00773 00774 public function setUp() { 00775 global $CFG; 00776 parent::setUp(); 00777 00778 // Create the table we need and switch to test DB. 00779 $this->create_test_table('config', 'lib'); 00780 $this->switch_to_test_db(); 00781 00782 // Store original $CFG; 00783 $this->origcfgstringfilters = $CFG->stringfilters; 00784 $this->origcfgfilterall = $CFG->filterall; 00785 } 00786 00787 public function tearDown() { 00788 global $CFG; 00789 $CFG->stringfilters = $this->origcfgstringfilters; 00790 $CFG->filterall = $this->origcfgfilterall; 00791 00792 parent::tearDown(); 00793 } 00794 00795 public function test_set() { 00796 global $CFG; 00797 // Setup fixture. 00798 $CFG->filterall = 0; 00799 $CFG->stringfilters = ''; 00800 // Exercise SUT. 00801 filter_set_applies_to_strings('filter/name', true); 00802 // Validate. 00803 $this->assertEqual('filter/name', $CFG->stringfilters); 00804 $this->assertTrue($CFG->filterall); 00805 } 00806 00807 public function test_unset_to_empty() { 00808 global $CFG; 00809 // Setup fixture. 00810 $CFG->filterall = 1; 00811 $CFG->stringfilters = 'filter/name'; 00812 // Exercise SUT. 00813 filter_set_applies_to_strings('filter/name', false); 00814 // Validate. 00815 $this->assertEqual('', $CFG->stringfilters); 00816 $this->assertFalse($CFG->filterall); 00817 } 00818 00819 public function test_unset_multi() { 00820 global $CFG; 00821 // Setup fixture. 00822 $CFG->filterall = 1; 00823 $CFG->stringfilters = 'filter/name,filter/other'; 00824 // Exercise SUT. 00825 filter_set_applies_to_strings('filter/name', false); 00826 // Validate. 00827 $this->assertEqual('filter/other', $CFG->stringfilters); 00828 $this->assertTrue($CFG->filterall); 00829 } 00830 }