MDL-23979 switching CFG->dirroot is a really bad idea, I borked this test when adding...
[moodle.git] / lib / simpletest / broken_testfiltermanager.php
CommitLineData
8926f844 1<?php
ccc161f8 2
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///////////////////////////////////////////////////////////////////////////
25
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 */
33
34if (!defined('MOODLE_INTERNAL')) {
35 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
36}
37
1dadfe15
PS
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
40
ccc161f8 41require_once($CFG->libdir . '/filterlib.php');
42
43class testable_filter_manager extends filter_manager {
081a63a9 44
ccc161f8 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}
55
56/**
57 * Test functions that affect filter_active table with contextid = $syscontextid.
58 */
59class filter_manager_test extends UnitTestCase {
081a63a9 60 public static $includecoverage = array('lib/filterlib.php');
ccc161f8 61 protected $filtermanager;
62 protected $olddirroot;
63
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 }
70
71 public function tearDown() {
72 global $CFG;
73 $CFG->dirroot = $this->olddirroot;
74 }
75
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 }
82
83 public function test_make_filter_object_newstyle() {
84 global $CFG;
85 $this->write_file($CFG->dirroot . '/filter/makenewstyletest/filter.php', <<<ENDCODE
86<?php
87class makenewstyletest_filter extends moodle_text_filter {
88 public function filter(\$text) {
89 return \$text;
90 }
91}
92ENDCODE
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 }
98
99 public function test_make_filter_object_legacy() {
100 global $CFG;
101 $this->write_file($CFG->dirroot . '/filter/makelegacytest/filter.php', <<<ENDCODE
102<?php
103function makelegacytest_filter(\$courseid, \$text) {
104 return \$text;
105}
106ENDCODE
107 );
108 $filter = $this->filtermanager->make_filter_object('filter/makelegacytest', null, 1, array());
109 $this->assertIsA($filter, 'legacy_filter');
110 }
111
112 public function test_make_filter_object_missing() {
113 $this->assertNull($this->filtermanager->make_filter_object('filter/nonexistant', null, 1, array()));
114 }
115
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 }
120}
121
122class doubleup_test_filter extends moodle_text_filter {
123 public function filter($text) {
124 return $text . ' ' . $text;
125 }
126}
127
128class killfrogs_test_filter extends moodle_text_filter {
129 public function filter($text) {
130 return str_replace('frogs', '', $text);
131 }
132}
133
8926f844 134