MDL-23979 switching CFG->dirroot is a really bad idea, I borked this test when adding...
[moodle.git] / lib / simpletest / broken_testfiltermanager.php
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 //                                                                       //
5 // NOTICE OF COPYRIGHT                                                   //
6 //                                                                       //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
8 //          http://moodle.org                                            //
9 //                                                                       //
10 // Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
11 //                                                                       //
12 // This program is free software; you can redistribute it and/or modify  //
13 // it under the terms of the GNU General Public License as published by  //
14 // the Free Software Foundation; either version 2 of the License, or     //
15 // (at your option) any later version.                                   //
16 //                                                                       //
17 // This program is distributed in the hope that it will be useful,       //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
20 // GNU General Public License for more details:                          //
21 //                                                                       //
22 //          http://www.gnu.org/copyleft/gpl.html                         //
23 //                                                                       //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27  * Tests for the parts of ../filterlib.php that handle creating filter objects,
28  * and using them to filter strings.
29  *
30  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31  * @package moodlecore
32  */
34 if (!defined('MOODLE_INTERNAL')) {
35     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
36 }
38 //TODO: messing with CFG->dirroot is a really bad idea! I am not going to fix this, sorry. (skodak)
39 //      if anybody wants to fix this then filter manager has to be modified so that it uses different dir, sorry
41 require_once($CFG->libdir . '/filterlib.php');
43 class testable_filter_manager extends filter_manager {
45     public function __construct() {
46         parent::__construct();
47     }
48     public function make_filter_object($filtername, $context, $courseid, $localconfig) {
49         return parent::make_filter_object($filtername, $context, $courseid, $localconfig);
50     }
51     public function apply_filter_chain($text, $filterchain) {
52         return parent::apply_filter_chain($text, $filterchain);
53     }
54 }
56 /**
57  * Test functions that affect filter_active table with contextid = $syscontextid.
58  */
59 class filter_manager_test extends UnitTestCase {
60     public static $includecoverage = array('lib/filterlib.php');
61     protected $filtermanager;
62     protected $olddirroot;
64     public function setUp() {
65         global $CFG;
66         $this->filtermanager = new testable_filter_manager();
67         $this->olddirroot = $CFG->dirroot;
68         $CFG->dirroot = $CFG->dataroot . '/temp';
69     }
71     public function tearDown() {
72         global $CFG;
73         $CFG->dirroot = $this->olddirroot;
74     }
76     /** Basically does file_put_contents, but ensures the directory exists first. */
77     protected function write_file($path, $content) {
78         global $CFG;
79         make_upload_directory(str_replace($CFG->dataroot . '/', '', dirname($path)));
80         file_put_contents($path, $content);
81     }
83     public function test_make_filter_object_newstyle() {
84         global $CFG;
85         $this->write_file($CFG->dirroot . '/filter/makenewstyletest/filter.php', <<<ENDCODE
86 <?php
87 class makenewstyletest_filter extends moodle_text_filter {
88     public function filter(\$text) {
89         return \$text;
90     }
91 }
92 ENDCODE
93         );
94         $filter = $this->filtermanager->make_filter_object('filter/makenewstyletest', null, 1, array());
95         $this->assertIsA($filter, 'moodle_text_filter');
96         $this->assertNotA($filter, 'legacy_filter');
97     }
99     public function test_make_filter_object_legacy() {
100         global $CFG;
101         $this->write_file($CFG->dirroot . '/filter/makelegacytest/filter.php', <<<ENDCODE
102 <?php
103 function makelegacytest_filter(\$courseid, \$text) {
104     return \$text;
106 ENDCODE
107         );
108         $filter = $this->filtermanager->make_filter_object('filter/makelegacytest', null, 1, array());
109         $this->assertIsA($filter, 'legacy_filter');
110     }
112     public function test_make_filter_object_missing() {
113         $this->assertNull($this->filtermanager->make_filter_object('filter/nonexistant', null, 1, array()));
114     }
116     public function test_apply_filter_chain() {
117         $filterchain = array(new doubleup_test_filter(null, 1, array()), new killfrogs_test_filter(null, 1, array()));
118         $this->assertEqual('pawn pawn', $this->filtermanager->apply_filter_chain('frogspawn', $filterchain));
119     }
122 class doubleup_test_filter extends moodle_text_filter {
123     public function filter($text) {
124         return $text . ' ' . $text;
125     }
128 class killfrogs_test_filter extends moodle_text_filter {
129     public function filter($text) {
130         return str_replace('frogs', '', $text);
131     }