Search moodle.org's
Developer Documentation

Long Term Support Release

  • Bug fixes for general core bugs in 3.5.x ended May 2019 (12 months).
  • Bug fixes for security issues in 3.5.x will end 10 May 2021 (36 months) - Support has ended.
  • minimum PHP 7.0.0 Note: minimum PHP version has increased since Moodle 3.3. PHP 7.1.x and 7.2.x are supported too. PHP 7.x could have some engine limitations.
  • Differences Between: [Versions 35 and 311]

       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   * Unit tests.
      19   *
      20   * @package filter_data
      21   * @category test
      22   * @copyright 2015 David Monllao
      23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      24   */
      25  
      26  defined('MOODLE_INTERNAL') || die();
      27  
      28  global $CFG;
      29  require_once($CFG->dirroot . '/filter/data/filter.php');
      30  
      31  /**
      32   * Tests for filter_data.
      33   *
      34   * @package filter_data
      35   * @copyright 2015 David Monllao
      36   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      37   */
      38  class filter_data_filter_testcase extends advanced_testcase {
      39  
      40      /**
      41       * Tests that the filter applies the required changes.
      42       *
      43       * @return void
      44       */
      45      public function test_filter() {
      46  
      47          $this->resetAfterTest(true);
      48          $this->setAdminUser();
      49          filter_manager::reset_caches();
      50  
      51          filter_set_global_state('data', TEXTFILTER_ON);
      52  
      53          $course1 = $this->getDataGenerator()->create_course();
      54          $coursecontext1 = context_course::instance($course1->id);
      55  
      56          $course2 = $this->getDataGenerator()->create_course();
      57          $coursecontext2 = context_course::instance($course2->id);
      58  
      59          $sitecontext = context_course::instance(SITEID);
      60  
      61          $site = get_site();
      62          $this->add_simple_database_instance($site, array('SiteEntry'));
      63          $this->add_simple_database_instance($course1, array('CourseEntry'));
      64  
      65          $html = '<p>I like CourseEntry and SiteEntry</p>';
      66  
      67          // Testing at course level (both site and course).
      68          $filtered = format_text($html, FORMAT_HTML, array('context' => $coursecontext1));
      69          $this->assertRegExp('/title=(\'|")CourseEntry(\'|")/', $filtered);
      70          $this->assertRegExp('/title=(\'|")SiteEntry(\'|")/', $filtered);
      71  
      72          // Testing at site level (only site).
      73          $filtered = format_text($html, FORMAT_HTML, array('context' => $sitecontext));
      74          $this->assertNotRegExp('/title=(\'|")CourseEntry(\'|")/', $filtered);
      75          $this->assertRegExp('/title=(\'|")SiteEntry(\'|")/', $filtered);
      76  
      77          // Changing to another course to test the caches invalidation (only site).
      78          $filtered = format_text($html, FORMAT_HTML, array('context' => $coursecontext2));
      79          $this->assertNotRegExp('/title=(\'|")CourseEntry(\'|")/', $filtered);
      80          $this->assertRegExp('/title=(\'|")SiteEntry(\'|")/', $filtered);
      81      }
      82  
      83      /**
      84       * Adds a database instance to the provided course + a text field + adds all attached entries.
      85       *
      86       * @param stdClass $course
      87       * @param array $entries A list of entry names.
      88       * @return void
      89       */
      90      protected function add_simple_database_instance($course, $entries = false) {
      91          global $DB;
      92  
      93          $database = $this->getDataGenerator()->create_module('data',
      94                  array('course' => $course->id));
      95  
      96          // A database field.
      97          $field = data_get_field_new('text', $database);
      98          $fielddetail = new stdClass();
      99          $fielddetail->d = $database->id;
     100          $fielddetail->mode = 'add';
     101          $fielddetail->type = 'text';
     102          $fielddetail->sesskey = sesskey();
     103          $fielddetail->name = 'Name';
     104          $fielddetail->description = 'Some name';
     105          $fielddetail->param1 = '1';
     106          $field->define_field($fielddetail);
     107          $field->insert_field();
     108          $recordid = data_add_record($database);
     109  
     110          // Database entries.
     111          foreach ($entries as $entrytext) {
     112              $datacontent = array();
     113              $datacontent['fieldid'] = $field->field->id;
     114              $datacontent['recordid'] = $recordid;
     115              $datacontent['content'] = $entrytext;
     116              $contentid = $DB->insert_record('data_content', $datacontent);
     117          }
     118      }
     119  }