|
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 //TODO: messing with CFG->dirroot is a really bad idea! I am not going to fix this, sorry. (skodak) 00039 // if anybody wants to fix this then filter manager has to be modified so that it uses different dir, sorry 00040 00041 require_once($CFG->libdir . '/filterlib.php'); 00042 00043 class testable_filter_manager extends filter_manager { 00044 00045 public function __construct() { 00046 parent::__construct(); 00047 } 00048 public function make_filter_object($filtername, $context, $courseid, $localconfig) { 00049 return parent::make_filter_object($filtername, $context, $courseid, $localconfig); 00050 } 00051 public function apply_filter_chain($text, $filterchain) { 00052 return parent::apply_filter_chain($text, $filterchain); 00053 } 00054 } 00055 00059 class filter_manager_test extends UnitTestCase { 00060 public static $includecoverage = array('lib/filterlib.php'); 00061 protected $filtermanager; 00062 protected $olddirroot; 00063 00064 public function setUp() { 00065 global $CFG; 00066 $this->filtermanager = new testable_filter_manager(); 00067 $this->olddirroot = $CFG->dirroot; 00068 $CFG->dirroot = $CFG->tempdir . ''; 00069 } 00070 00071 public function tearDown() { 00072 global $CFG; 00073 $CFG->dirroot = $this->olddirroot; 00074 } 00075 00077 protected function write_file($path, $content) { 00078 global $CFG; 00079 make_upload_directory(str_replace($CFG->dataroot . '/', '', dirname($path))); 00080 file_put_contents($path, $content); 00081 } 00082 00083 public function test_make_filter_object_newstyle() { 00084 global $CFG; 00085 $this->write_file($CFG->dirroot . '/filter/makenewstyletest/filter.php', <<<ENDCODE 00086 <?php 00087 class makenewstyletest_filter extends moodle_text_filter { 00088 public function filter(\$text) { 00089 return \$text; 00090 } 00091 } 00092 ENDCODE 00093 ); 00094 $filter = $this->filtermanager->make_filter_object('filter/makenewstyletest', null, 1, array()); 00095 $this->assertIsA($filter, 'moodle_text_filter'); 00096 $this->assertNotA($filter, 'legacy_filter'); 00097 } 00098 00099 public function test_make_filter_object_legacy() { 00100 global $CFG; 00101 $this->write_file($CFG->dirroot . '/filter/makelegacytest/filter.php', <<<ENDCODE 00102 <?php 00103 function makelegacytest_filter(\$courseid, \$text) { 00104 return \$text; 00105 } 00106 ENDCODE 00107 ); 00108 $filter = $this->filtermanager->make_filter_object('filter/makelegacytest', null, 1, array()); 00109 $this->assertIsA($filter, 'legacy_filter'); 00110 } 00111 00112 public function test_make_filter_object_missing() { 00113 $this->assertNull($this->filtermanager->make_filter_object('filter/nonexistant', null, 1, array())); 00114 } 00115 00116 public function test_apply_filter_chain() { 00117 $filterchain = array(new doubleup_test_filter(null, 1, array()), new killfrogs_test_filter(null, 1, array())); 00118 $this->assertEqual('pawn pawn', $this->filtermanager->apply_filter_chain('frogspawn', $filterchain)); 00119 } 00120 } 00121 00122 class doubleup_test_filter extends moodle_text_filter { 00123 public function filter($text) { 00124 return $text . ' ' . $text; 00125 } 00126 } 00127 00128 class killfrogs_test_filter extends moodle_text_filter { 00129 public function filter($text) { 00130 return str_replace('frogs', '', $text); 00131 } 00132 } 00133 00134