/lib/simpletest/broken_testfiltermanager.php

https://github.com/nigeldaley/moodle · PHP · 134 lines · 80 code · 19 blank · 35 comment · 1 complexity · 44614fa5adbad472a30cb48419edf810 MD5 · raw file

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