Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       1  <?php
       2  // This file is part of Moodle - http://moodle.org/
       3  //
       4  // Moodle is free software: you can redistribute it and/or modify
       5  // it under the terms of the GNU General Public License as published by
       6  // the Free Software Foundation, either version 3 of the License, or
       7  // (at your option) any later version.
       8  //
       9  // Moodle is distributed in the hope that it will be useful,
      10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12  // GNU General Public License for more details.
      13  //
      14  // You should have received a copy of the GNU General Public License
      15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
      16  
      17  /**
      18   * Tests for the parts of ../filterlib.php that involve loading the configuration
      19   * from, and saving the configuration to, the database.
      20   *
      21   * @package   core_filter
      22   * @category  phpunit
      23   * @copyright 2009 Tim Hunt
      24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      25   */
      26  
      27  defined('MOODLE_INTERNAL') || die();
      28  
      29  global $CFG;
      30  require_once($CFG->libdir . '/filterlib.php');
      31  
      32  /**
      33   * Test filters.
      34   */
      35  class core_filterlib_testcase extends advanced_testcase {
      36      private $syscontext;
      37      private $childcontext;
      38      private $childcontext2;
      39      private $catcontext;
      40      private $coursecontext;
      41      private $course;
      42      private $activity1context;
      43      private $activity2context;
      44  
      45      protected function setUp() {
      46          global $DB;
      47          parent::setUp();
      48  
      49          $this->resetAfterTest();
      50          $DB->delete_records('filter_active', array());
      51          $DB->delete_records('filter_config', array());
      52      }
      53  
      54      private function assert_only_one_filter_globally($filter, $state) {
      55          global $DB;
      56          $recs = $DB->get_records('filter_active');
      57          $this->assertCount(1, $recs);
      58          $rec = reset($recs);
      59          unset($rec->id);
      60          $expectedrec = new stdClass();
      61          $expectedrec->filter = $filter;
      62          $expectedrec->contextid = context_system::instance()->id;
      63          $expectedrec->active = $state;
      64          $expectedrec->sortorder = 1;
      65          $this->assertEquals($expectedrec, $rec);
      66      }
      67  
      68      private function assert_global_sort_order($filters) {
      69          global $DB;
      70  
      71          $sortedfilters = $DB->get_records_menu('filter_active',
      72              array('contextid' => context_system::instance()->id), 'sortorder', 'sortorder,filter');
      73          $testarray = array();
      74          $index = 1;
      75          foreach ($filters as $filter) {
      76              $testarray[$index++] = $filter;
      77          }
      78          $this->assertEquals($testarray, $sortedfilters);
      79      }
      80  
      81      public function test_set_filter_globally_on() {
      82          // Setup fixture.
      83          // Exercise SUT.
      84          filter_set_global_state('name', TEXTFILTER_ON);
      85          // Validate.
      86          $this->assert_only_one_filter_globally('name', TEXTFILTER_ON);
      87      }
      88  
      89      public function test_set_filter_globally_off() {
      90          // Setup fixture.
      91          // Exercise SUT.
      92          filter_set_global_state('name', TEXTFILTER_OFF);
      93          // Validate.
      94          $this->assert_only_one_filter_globally('name', TEXTFILTER_OFF);
      95      }
      96  
      97      public function test_set_filter_globally_disabled() {
      98          // Setup fixture.
      99          // Exercise SUT.
     100          filter_set_global_state('name', TEXTFILTER_DISABLED);
     101          // Validate.
     102          $this->assert_only_one_filter_globally('name', TEXTFILTER_DISABLED);
     103      }
     104  
     105      /**
     106       * @expectedException coding_exception
     107       */
     108      public function test_global_config_exception_on_invalid_state() {
     109          filter_set_global_state('name', 0);
     110      }
     111  
     112      public function test_auto_sort_order() {
     113          // Setup fixture.
     114          // Exercise SUT.
     115          filter_set_global_state('one', TEXTFILTER_DISABLED);
     116          filter_set_global_state('two', TEXTFILTER_DISABLED);
     117          // Validate.
     118          $this->assert_global_sort_order(array('one', 'two'));
     119      }
     120  
     121      public function test_auto_sort_order_enabled() {
     122          // Setup fixture.
     123          // Exercise SUT.
     124          filter_set_global_state('one', TEXTFILTER_ON);
     125          filter_set_global_state('two', TEXTFILTER_OFF);
     126          // Validate.
     127          $this->assert_global_sort_order(array('one', 'two'));
     128      }
     129  
     130      public function test_update_existing_dont_duplicate() {
     131          // Setup fixture.
     132          // Exercise SUT.
     133          filter_set_global_state('name', TEXTFILTER_ON);
     134          filter_set_global_state('name', TEXTFILTER_OFF);
     135          // Validate.
     136          $this->assert_only_one_filter_globally('name', TEXTFILTER_OFF);
     137      }
     138  
     139      public function test_update_reorder_down() {
     140          // Setup fixture.
     141          filter_set_global_state('one', TEXTFILTER_ON);
     142          filter_set_global_state('two', TEXTFILTER_ON);
     143          filter_set_global_state('three', TEXTFILTER_ON);
     144          // Exercise SUT.
     145          filter_set_global_state('two', TEXTFILTER_ON, -1);
     146          // Validate.
     147          $this->assert_global_sort_order(array('two', 'one', 'three'));
     148      }
     149  
     150      public function test_update_reorder_up() {
     151          // Setup fixture.
     152          filter_set_global_state('one', TEXTFILTER_ON);
     153          filter_set_global_state('two', TEXTFILTER_ON);
     154          filter_set_global_state('three', TEXTFILTER_ON);
     155          filter_set_global_state('four', TEXTFILTER_ON);
     156          // Exercise SUT.
     157          filter_set_global_state('two', TEXTFILTER_ON, 1);
     158          // Validate.
     159          $this->assert_global_sort_order(array('one', 'three', 'two', 'four'));
     160      }
     161  
     162      public function test_auto_sort_order_change_to_enabled() {
     163          // Setup fixture.
     164          filter_set_global_state('one', TEXTFILTER_ON);
     165          filter_set_global_state('two', TEXTFILTER_DISABLED);
     166          filter_set_global_state('three', TEXTFILTER_DISABLED);
     167          // Exercise SUT.
     168          filter_set_global_state('three', TEXTFILTER_ON);
     169          // Validate.
     170          $this->assert_global_sort_order(array('one', 'three', 'two'));
     171      }
     172  
     173      public function test_auto_sort_order_change_to_disabled() {
     174          // Setup fixture.
     175          filter_set_global_state('one', TEXTFILTER_ON);
     176          filter_set_global_state('two', TEXTFILTER_ON);
     177          filter_set_global_state('three', TEXTFILTER_DISABLED);
     178          // Exercise SUT.
     179          filter_set_global_state('one', TEXTFILTER_DISABLED);
     180          // Validate.
     181          $this->assert_global_sort_order(array('two', 'one', 'three'));
     182      }
     183  
     184      public function test_filter_get_global_states() {
     185          // Setup fixture.
     186          filter_set_global_state('one', TEXTFILTER_ON);
     187          filter_set_global_state('two', TEXTFILTER_OFF);
     188          filter_set_global_state('three', TEXTFILTER_DISABLED);
     189          // Exercise SUT.
     190          $filters = filter_get_global_states();
     191          // Validate.
     192          $this->assertEquals(array(
     193              'one' => (object) array('filter' => 'one', 'active' => TEXTFILTER_ON, 'sortorder' => 1),
     194              'two' => (object) array('filter' => 'two', 'active' => TEXTFILTER_OFF, 'sortorder' => 2),
     195              'three' => (object) array('filter' => 'three', 'active' => TEXTFILTER_DISABLED, 'sortorder' => 3)
     196          ), $filters);
     197      }
     198  
     199      private function assert_only_one_local_setting($filter, $contextid, $state) {
     200          global $DB;
     201          $recs = $DB->get_records('filter_active');
     202          $this->assertEquals(1, count($recs), 'More than one record returned %s.');
     203          $rec = reset($recs);
     204          unset($rec->id);
     205          unset($rec->sortorder);
     206          $expectedrec = new stdClass();
     207          $expectedrec->filter = $filter;
     208          $expectedrec->contextid = $contextid;
     209          $expectedrec->active = $state;
     210          $this->assertEquals($expectedrec, $rec);
     211      }
     212  
     213      private function assert_no_local_setting() {
     214          global $DB;
     215          $this->assertEquals(0, $DB->count_records('filter_active'));
     216      }
     217  
     218      public function test_local_on() {
     219          // Exercise SUT.
     220          filter_set_local_state('name', 123, TEXTFILTER_ON);
     221          // Validate.
     222          $this->assert_only_one_local_setting('name', 123, TEXTFILTER_ON);
     223      }
     224  
     225      public function test_local_off() {
     226          // Exercise SUT.
     227          filter_set_local_state('name', 123, TEXTFILTER_OFF);
     228          // Validate.
     229          $this->assert_only_one_local_setting('name', 123, TEXTFILTER_OFF);
     230      }
     231  
     232      public function test_local_inherit() {
     233          // Exercise SUT.
     234          filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
     235          // Validate.
     236          $this->assert_no_local_setting();
     237      }
     238  
     239      /**
     240       * @expectedException coding_exception
     241       */
     242      public function test_local_invalid_state_throws_exception() {
     243          // Exercise SUT.
     244          filter_set_local_state('name', 123, -9999);
     245      }
     246  
     247      /**
     248       * @expectedException coding_exception
     249       */
     250      public function test_throws_exception_when_setting_global() {
     251          // Exercise SUT.
     252          filter_set_local_state('name', context_system::instance()->id, TEXTFILTER_INHERIT);
     253      }
     254  
     255      public function test_local_inherit_deletes_existing() {
     256          // Setup fixture.
     257          filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
     258          // Exercise SUT.
     259          filter_set_local_state('name', 123, TEXTFILTER_INHERIT);
     260          // Validate.
     261          $this->assert_no_local_setting();
     262      }
     263  
     264      private function assert_only_one_config($filter, $context, $name, $value) {
     265          global $DB;
     266          $recs = $DB->get_records('filter_config');
     267          $this->assertEquals(1, count($recs), 'More than one record returned %s.');
     268          $rec = reset($recs);
     269          unset($rec->id);
     270          $expectedrec = new stdClass();
     271          $expectedrec->filter = $filter;
     272          $expectedrec->contextid = $context;
     273          $expectedrec->name = $name;
     274          $expectedrec->value = $value;
     275          $this->assertEquals($expectedrec, $rec);
     276      }
     277  
     278      public function test_set_new_config() {
     279          // Exercise SUT.
     280          filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
     281          // Validate.
     282          $this->assert_only_one_config('name', 123, 'settingname', 'An arbitrary value');
     283      }
     284  
     285      public function test_update_existing_config() {
     286          // Setup fixture.
     287          filter_set_local_config('name', 123, 'settingname', 'An arbitrary value');
     288          // Exercise SUT.
     289          filter_set_local_config('name', 123, 'settingname', 'A changed value');
     290          // Validate.
     291          $this->assert_only_one_config('name', 123, 'settingname', 'A changed value');
     292      }
     293  
     294      public function test_filter_get_local_config() {
     295          // Setup fixture.
     296          filter_set_local_config('name', 123, 'setting1', 'An arbitrary value');
     297          filter_set_local_config('name', 123, 'setting2', 'Another arbitrary value');
     298          filter_set_local_config('name', 122, 'settingname', 'Value from another context');
     299          filter_set_local_config('other', 123, 'settingname', 'Someone else\'s value');
     300          // Exercise SUT.
     301          $config = filter_get_local_config('name', 123);
     302          // Validate.
     303          $this->assertEquals(array('setting1' => 'An arbitrary value', 'setting2' => 'Another arbitrary value'), $config);
     304      }
     305  
     306      protected function setup_available_in_context_tests() {
     307          $course = $this->getDataGenerator()->create_course(array('category'=>1));
     308  
     309          $this->childcontext = context_coursecat::instance(1);
     310          $this->childcontext2 = context_course::instance($course->id);
     311          $this->syscontext = context_system::instance();
     312      }
     313  
     314      private function assert_filter_list($expectedfilters, $filters) {
     315          $this->setup_available_in_context_tests();
     316          $this->assertEquals($expectedfilters, array_keys($filters), '', 0, 10, true);
     317      }
     318  
     319      public function test_globally_on_is_returned() {
     320          $this->setup_available_in_context_tests();
     321          // Setup fixture.
     322          filter_set_global_state('name', TEXTFILTER_ON);
     323          // Exercise SUT.
     324          $filters = filter_get_active_in_context($this->syscontext);
     325          // Validate.
     326          $this->assert_filter_list(array('name'), $filters);
     327          // Check no config returned correctly.
     328          $this->assertEquals(array(), $filters['name']);
     329      }
     330  
     331      public function test_globally_off_not_returned() {
     332          $this->setup_available_in_context_tests();
     333          // Setup fixture.
     334          filter_set_global_state('name', TEXTFILTER_OFF);
     335          // Exercise SUT.
     336          $filters = filter_get_active_in_context($this->childcontext2);
     337          // Validate.
     338          $this->assert_filter_list(array(), $filters);
     339      }
     340  
     341      public function test_globally_off_overridden() {
     342          $this->setup_available_in_context_tests();
     343          // Setup fixture.
     344          filter_set_global_state('name', TEXTFILTER_OFF);
     345          filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_ON);
     346          // Exercise SUT.
     347          $filters = filter_get_active_in_context($this->childcontext2);
     348          // Validate.
     349          $this->assert_filter_list(array('name'), $filters);
     350      }
     351  
     352      public function test_globally_on_overridden() {
     353          $this->setup_available_in_context_tests();
     354          // Setup fixture.
     355          filter_set_global_state('name', TEXTFILTER_ON);
     356          filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_OFF);
     357          // Exercise SUT.
     358          $filters = filter_get_active_in_context($this->childcontext2);
     359          // Validate.
     360          $this->assert_filter_list(array(), $filters);
     361      }
     362  
     363      public function test_globally_disabled_not_overridden() {
     364          $this->setup_available_in_context_tests();
     365          // Setup fixture.
     366          filter_set_global_state('name', TEXTFILTER_DISABLED);
     367          filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_ON);
     368          // Exercise SUT.
     369          $filters = filter_get_active_in_context($this->syscontext);
     370          // Validate.
     371          $this->assert_filter_list(array(), $filters);
     372      }
     373  
     374      public function test_single_config_returned() {
     375          $this->setup_available_in_context_tests();
     376          // Setup fixture.
     377          filter_set_global_state('name', TEXTFILTER_ON);
     378          filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
     379          // Exercise SUT.
     380          $filters = filter_get_active_in_context($this->childcontext);
     381          // Validate.
     382          $this->assertEquals(array('settingname' => 'A value'), $filters['name']);
     383      }
     384  
     385      public function test_multi_config_returned() {
     386          $this->setup_available_in_context_tests();
     387          // Setup fixture.
     388          filter_set_global_state('name', TEXTFILTER_ON);
     389          filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
     390          filter_set_local_config('name', $this->childcontext->id, 'anothersettingname', 'Another value');
     391          // Exercise SUT.
     392          $filters = filter_get_active_in_context($this->childcontext);
     393          // Validate.
     394          $this->assertEquals(array('settingname' => 'A value', 'anothersettingname' => 'Another value'), $filters['name']);
     395      }
     396  
     397      public function test_config_from_other_context_not_returned() {
     398          $this->setup_available_in_context_tests();
     399          // Setup fixture.
     400          filter_set_global_state('name', TEXTFILTER_ON);
     401          filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
     402          filter_set_local_config('name', $this->childcontext2->id, 'anothersettingname', 'Another value');
     403          // Exercise SUT.
     404          $filters = filter_get_active_in_context($this->childcontext2);
     405          // Validate.
     406          $this->assertEquals(array('anothersettingname' => 'Another value'), $filters['name']);
     407      }
     408  
     409      public function test_config_from_other_filter_not_returned() {
     410          $this->setup_available_in_context_tests();
     411          // Setup fixture.
     412          filter_set_global_state('name', TEXTFILTER_ON);
     413          filter_set_local_config('name', $this->childcontext->id, 'settingname', 'A value');
     414          filter_set_local_config('other', $this->childcontext->id, 'anothersettingname', 'Another value');
     415          // Exercise SUT.
     416          $filters = filter_get_active_in_context($this->childcontext);
     417          // Validate.
     418          $this->assertEquals(array('settingname' => 'A value'), $filters['name']);
     419      }
     420  
     421      protected function assert_one_available_filter($filter, $localstate, $inheritedstate, $filters) {
     422          $this->setup_available_in_context_tests();
     423          $this->assertEquals(1, count($filters), 'More than one record returned %s.');
     424          $rec = $filters[$filter];
     425          unset($rec->id);
     426          $expectedrec = new stdClass();
     427          $expectedrec->filter = $filter;
     428          $expectedrec->localstate = $localstate;
     429          $expectedrec->inheritedstate = $inheritedstate;
     430          $this->assertEquals($expectedrec, $rec);
     431      }
     432  
     433      public function test_available_in_context_localoverride() {
     434          $this->setup_available_in_context_tests();
     435          // Setup fixture.
     436          filter_set_global_state('name', TEXTFILTER_ON);
     437          filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_OFF);
     438          // Exercise SUT.
     439          $filters = filter_get_available_in_context($this->childcontext);
     440          // Validate.
     441          $this->assert_one_available_filter('name', TEXTFILTER_OFF, TEXTFILTER_ON, $filters);
     442      }
     443  
     444      public function test_available_in_context_nolocaloverride() {
     445          $this->setup_available_in_context_tests();
     446          // Setup fixture.
     447          filter_set_global_state('name', TEXTFILTER_ON);
     448          filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_OFF);
     449          // Exercise SUT.
     450          $filters = filter_get_available_in_context($this->childcontext2);
     451          // Validate.
     452          $this->assert_one_available_filter('name', TEXTFILTER_INHERIT, TEXTFILTER_OFF, $filters);
     453      }
     454  
     455      public function test_available_in_context_disabled_not_returned() {
     456          $this->setup_available_in_context_tests();
     457          // Setup fixture.
     458          filter_set_global_state('name', TEXTFILTER_DISABLED);
     459          filter_set_local_state('name', $this->childcontext->id, TEXTFILTER_ON);
     460          // Exercise SUT.
     461          $filters = filter_get_available_in_context($this->childcontext);
     462          // Validate.
     463          $this->assertEquals(array(), $filters);
     464      }
     465  
     466      /**
     467       * @expectedException coding_exception
     468       */
     469      public function test_available_in_context_exception_with_syscontext() {
     470          $this->setup_available_in_context_tests();
     471          // Exercise SUT.
     472          filter_get_available_in_context($this->syscontext);
     473      }
     474  
     475      protected function setup_preload_activities_test() {
     476          $this->syscontext = context_system::instance();
     477          $this->catcontext = context_coursecat::instance(1);
     478          $this->course = $this->getDataGenerator()->create_course(array('category'=>1));
     479          $this->coursecontext = context_course::instance($this->course->id);
     480          $page1 =  $this->getDataGenerator()->create_module('page', array('course'=>$this->course->id));
     481          $this->activity1context = context_module::instance($page1->cmid);
     482          $page2 =  $this->getDataGenerator()->create_module('page', array('course'=>$this->course->id));
     483          $this->activity2context = context_module::instance($page2->cmid);
     484      }
     485  
     486      private function assert_matches($modinfo) {
     487          global $FILTERLIB_PRIVATE, $DB;
     488  
     489          // Use preload cache...
     490          $FILTERLIB_PRIVATE = new stdClass();
     491          filter_preload_activities($modinfo);
     492  
     493          // Get data and check no queries are made.
     494          $before = $DB->perf_get_reads();
     495          $plfilters1 = filter_get_active_in_context($this->activity1context);
     496          $plfilters2 = filter_get_active_in_context($this->activity2context);
     497          $after = $DB->perf_get_reads();
     498          $this->assertEquals($before, $after);
     499  
     500          // Repeat without cache and check it makes queries now.
     501          $FILTERLIB_PRIVATE = new stdClass;
     502          $before = $DB->perf_get_reads();
     503          $filters1 = filter_get_active_in_context($this->activity1context);
     504          $filters2 = filter_get_active_in_context($this->activity2context);
     505          $after = $DB->perf_get_reads();
     506          $this->assertTrue($after > $before);
     507  
     508          // Check they match.
     509          $this->assertEquals($plfilters1, $filters1);
     510          $this->assertEquals($plfilters2, $filters2);
     511      }
     512  
     513      public function test_preload() {
     514          $this->setup_preload_activities_test();
     515          // Get course and modinfo.
     516          $modinfo = new course_modinfo($this->course, 2);
     517  
     518          // Note: All the tests in this function check that the result from the
     519          // preloaded cache is the same as the result from calling the standard
     520          // function without preloading.
     521  
     522          // Initially, check with no filters enabled.
     523          $this->assert_matches($modinfo);
     524  
     525          // Enable filter globally, check.
     526          filter_set_global_state('name', TEXTFILTER_ON);
     527          $this->assert_matches($modinfo);
     528  
     529          // Disable for activity 2.
     530          filter_set_local_state('name', $this->activity2context->id, TEXTFILTER_OFF);
     531          $this->assert_matches($modinfo);
     532  
     533          // Disable at category.
     534          filter_set_local_state('name', $this->catcontext->id, TEXTFILTER_OFF);
     535          $this->assert_matches($modinfo);
     536  
     537          // Enable for activity 1.
     538          filter_set_local_state('name', $this->activity1context->id, TEXTFILTER_ON);
     539          $this->assert_matches($modinfo);
     540  
     541          // Disable globally.
     542          filter_set_global_state('name', TEXTFILTER_DISABLED);
     543          $this->assert_matches($modinfo);
     544  
     545          // Add another 2 filters.
     546          filter_set_global_state('frog', TEXTFILTER_ON);
     547          filter_set_global_state('zombie', TEXTFILTER_ON);
     548          $this->assert_matches($modinfo);
     549  
     550          // Disable random one of these in each context.
     551          filter_set_local_state('zombie', $this->activity1context->id, TEXTFILTER_OFF);
     552          filter_set_local_state('frog', $this->activity2context->id, TEXTFILTER_OFF);
     553          $this->assert_matches($modinfo);
     554  
     555          // Now do some filter options.
     556          filter_set_local_config('name', $this->activity1context->id, 'a', 'x');
     557          filter_set_local_config('zombie', $this->activity1context->id, 'a', 'y');
     558          filter_set_local_config('frog', $this->activity1context->id, 'a', 'z');
     559          // These last two don't do anything as they are not at final level but I
     560          // thought it would be good to have that verified in test.
     561          filter_set_local_config('frog', $this->coursecontext->id, 'q', 'x');
     562          filter_set_local_config('frog', $this->catcontext->id, 'q', 'z');
     563          $this->assert_matches($modinfo);
     564      }
     565  
     566      public function test_filter_delete_all_for_filter() {
     567          global $DB;
     568  
     569          // Setup fixture.
     570          filter_set_global_state('name', TEXTFILTER_ON);
     571          filter_set_global_state('other', TEXTFILTER_ON);
     572          filter_set_local_config('name', context_system::instance()->id, 'settingname', 'A value');
     573          filter_set_local_config('other', context_system::instance()->id, 'settingname', 'Other value');
     574          set_config('configname', 'A config value', 'filter_name');
     575          set_config('configname', 'Other config value', 'filter_other');
     576          // Exercise SUT.
     577          filter_delete_all_for_filter('name');
     578          // Validate.
     579          $this->assertEquals(1, $DB->count_records('filter_active'));
     580          $this->assertTrue($DB->record_exists('filter_active', array('filter' => 'other')));
     581          $this->assertEquals(1, $DB->count_records('filter_config'));
     582          $this->assertTrue($DB->record_exists('filter_config', array('filter' => 'other')));
     583          $expectedconfig = new stdClass;
     584          $expectedconfig->configname = 'Other config value';
     585          $this->assertEquals($expectedconfig, get_config('filter_other'));
     586          $this->assertEquals(get_config('filter_name'), new stdClass());
     587      }
     588  
     589      public function test_filter_delete_all_for_context() {
     590          global $DB;
     591  
     592          // Setup fixture.
     593          filter_set_global_state('name', TEXTFILTER_ON);
     594          filter_set_local_state('name', 123, TEXTFILTER_OFF);
     595          filter_set_local_config('name', 123, 'settingname', 'A value');
     596          filter_set_local_config('other', 123, 'settingname', 'Other value');
     597          filter_set_local_config('other', 122, 'settingname', 'Other value');
     598          // Exercise SUT.
     599          filter_delete_all_for_context(123);
     600          // Validate.
     601          $this->assertEquals(1, $DB->count_records('filter_active'));
     602          $this->assertTrue($DB->record_exists('filter_active', array('contextid' => context_system::instance()->id)));
     603          $this->assertEquals(1, $DB->count_records('filter_config'));
     604          $this->assertTrue($DB->record_exists('filter_config', array('filter' => 'other')));
     605      }
     606  
     607      public function test_set() {
     608          global $CFG;
     609  
     610          $this->assertFileExists("$CFG->dirroot/filter/emailprotect"); // Any standard filter.
     611          $this->assertFileExists("$CFG->dirroot/filter/tidy");         // Any standard filter.
     612          $this->assertFileNotExists("$CFG->dirroot/filter/grgrggr");   // Any non-existent filter
     613  
     614          // Setup fixture.
     615          set_config('filterall', 0);
     616          set_config('stringfilters', '');
     617          // Exercise SUT.
     618          filter_set_applies_to_strings('tidy', true);
     619          // Validate.
     620          $this->assertEquals('tidy', $CFG->stringfilters);
     621          $this->assertEquals(1, $CFG->filterall);
     622  
     623          filter_set_applies_to_strings('grgrggr', true);
     624          $this->assertEquals('tidy', $CFG->stringfilters);
     625          $this->assertEquals(1, $CFG->filterall);
     626  
     627          filter_set_applies_to_strings('emailprotect', true);
     628          $this->assertEquals('tidy,emailprotect', $CFG->stringfilters);
     629          $this->assertEquals(1, $CFG->filterall);
     630      }
     631  
     632      public function test_unset_to_empty() {
     633          global $CFG;
     634  
     635          $this->assertFileExists("$CFG->dirroot/filter/tidy"); // Any standard filter.
     636  
     637          // Setup fixture.
     638          set_config('filterall', 1);
     639          set_config('stringfilters', 'tidy');
     640          // Exercise SUT.
     641          filter_set_applies_to_strings('tidy', false);
     642          // Validate.
     643          $this->assertEquals('', $CFG->stringfilters);
     644          $this->assertEquals('', $CFG->filterall);
     645      }
     646  
     647      public function test_unset_multi() {
     648          global $CFG;
     649  
     650          $this->assertFileExists("$CFG->dirroot/filter/emailprotect"); // Any standard filter.
     651          $this->assertFileExists("$CFG->dirroot/filter/tidy");         // Any standard filter.
     652          $this->assertFileExists("$CFG->dirroot/filter/multilang");    // Any standard filter.
     653  
     654          // Setup fixture.
     655          set_config('filterall', 1);
     656          set_config('stringfilters', 'emailprotect,tidy,multilang');
     657          // Exercise SUT.
     658          filter_set_applies_to_strings('tidy', false);
     659          // Validate.
     660          $this->assertEquals('emailprotect,multilang', $CFG->stringfilters);
     661          $this->assertEquals(1, $CFG->filterall);
     662      }
     663  
     664      public function test_filter_manager_instance() {
     665  
     666          set_config('perfdebug', 7);
     667          filter_manager::reset_caches();
     668          $filterman = filter_manager::instance();
     669          $this->assertInstanceOf('filter_manager', $filterman);
     670          $this->assertNotInstanceOf('performance_measuring_filter_manager', $filterman);
     671  
     672          set_config('perfdebug', 15);
     673          filter_manager::reset_caches();
     674          $filterman = filter_manager::instance();
     675          $this->assertInstanceOf('filter_manager', $filterman);
     676          $this->assertInstanceOf('performance_measuring_filter_manager', $filterman);
     677      }
     678  }
    

    Search This Site: