MDL-23979 adding missing context init code
[moodle.git] / lib / simpletest / 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 require_once($CFG->libdir . '/filterlib.php');
40 class testable_filter_manager extends filter_manager {
42     public function __construct() {
43         parent::__construct();
44     }
45     public function make_filter_object($filtername, $context, $courseid, $localconfig) {
46         return parent::make_filter_object($filtername, $context, $courseid, $localconfig);
47     }
48     public function apply_filter_chain($text, $filterchain) {
49         return parent::apply_filter_chain($text, $filterchain);
50     }
51 }
53 /**
54  * Test functions that affect filter_active table with contextid = $syscontextid.
55  */
56 class filter_manager_test extends UnitTestCase {
57     public static $includecoverage = array('lib/filterlib.php');
58     protected $filtermanager;
59     protected $olddirroot;
61     public function setUp() {
62         global $CFG;
63         $this->filtermanager = new testable_filter_manager();
64         $this->olddirroot = $CFG->dirroot;
65         $CFG->dirroot = $CFG->dataroot . '/temp';
66     }
68     public function tearDown() {
69         global $CFG;
70         $CFG->dirroot = $this->olddirroot;
71     }
73     /** Basically does file_put_contents, but ensures the directory exists first. */
74     protected function write_file($path, $content) {
75         global $CFG;
76         make_upload_directory(str_replace($CFG->dataroot . '/', '', dirname($path)));
77         file_put_contents($path, $content);
78     }
80     public function test_make_filter_object_newstyle() {
81         global $CFG;
82         $this->write_file($CFG->dirroot . '/filter/makenewstyletest/filter.php', <<<ENDCODE
83 <?php
84 class makenewstyletest_filter extends moodle_text_filter {
85     public function filter(\$text) {
86         return \$text;
87     }
88 }
89 ENDCODE
90         );
91         $filter = $this->filtermanager->make_filter_object('filter/makenewstyletest', null, 1, array());
92         $this->assertIsA($filter, 'moodle_text_filter');
93         $this->assertNotA($filter, 'legacy_filter');
94     }
96     public function test_make_filter_object_legacy() {
97         global $CFG;
98         $this->write_file($CFG->dirroot . '/filter/makelegacytest/filter.php', <<<ENDCODE
99 <?php
100 function makelegacytest_filter(\$courseid, \$text) {
101     return \$text;
103 ENDCODE
104         );
105         $filter = $this->filtermanager->make_filter_object('filter/makelegacytest', null, 1, array());
106         $this->assertIsA($filter, 'legacy_filter');
107     }
109     public function test_make_filter_object_missing() {
110         $this->assertNull($this->filtermanager->make_filter_object('filter/nonexistant', null, 1, array()));
111     }
113     public function test_apply_filter_chain() {
114         $filterchain = array(new doubleup_test_filter(null, 1, array()), new killfrogs_test_filter(null, 1, array()));
115         $this->assertEqual('pawn pawn', $this->filtermanager->apply_filter_chain('frogspawn', $filterchain));
116     }
119 class doubleup_test_filter extends moodle_text_filter {
120     public function filter($text) {
121         return $text . ' ' . $text;
122     }
125 class killfrogs_test_filter extends moodle_text_filter {
126     public function filter($text) {
127         return str_replace('frogs', '', $text);
128     }