[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/search/tests/ -> document_test.php (source)

   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   * Search document unit tests.
  19   *
  20   * @package     core_search
  21   * @category    phpunit
  22   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once (__DIR__ . '/fixtures/testable_core_search.php');
  29  require_once (__DIR__ . '/fixtures/mock_search_area.php');
  30  
  31  /**
  32   * Unit tests for search document.
  33   *
  34   * @package     core_search
  35   * @category    phpunit
  36   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class search_document_testcase extends advanced_testcase {
  40  
  41      /**
  42       * @var Instace of core_search_generator.
  43       */
  44      protected $generator = null;
  45  
  46      public function setUp() {
  47          $this->resetAfterTest();
  48          set_config('enableglobalsearch', true);
  49  
  50          // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
  51          $search = testable_core_search::instance();
  52  
  53          $this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
  54          $this->generator->setup();
  55      }
  56  
  57      /**
  58       * Adding this test here as get_areas_user_accesses process is the same, results just depend on the context level.
  59       *
  60       * @return void
  61       */
  62      public function test_search_user_accesses() {
  63          global $DB, $PAGE;
  64  
  65          $area = new \core_mocksearch\search\mock_search_area();
  66          $renderer = $PAGE->get_renderer('core_search');
  67          $engine = new \mock_search\engine();
  68  
  69          $course = $this->getDataGenerator()->create_course(array('fullname' => 'Course & Title'));
  70          $coursectx = context_course::instance($course->id);
  71          $user = $this->getDataGenerator()->create_user(array('firstname' => 'User', 'lastname' => 'Escape & Name'));
  72          $this->getDataGenerator()->enrol_user($user->id, $course->id, 'teacher');
  73  
  74          // Make a record to enter in the search area.
  75          $record = new \stdClass();
  76          $record->title = 'Escape & Title';
  77          $record->content = 'Escape & Content';
  78          $record->description1 = 'Escape & Description1';
  79          $record->description2 = 'Escape & Description2';
  80          $record->userid = $user->id;
  81          $record->courseid = $course->id;
  82          $record = $this->generator->create_record($record);
  83  
  84          // Convert to a 'doc data' type format.
  85          $docdata = $area->convert_record_to_doc_array($record);
  86  
  87          // First see that the docuemnt has the right information, unescaped.
  88          $doc = $engine->to_document($area, $docdata);
  89          $this->assertEquals('Escape & Title', $doc->get('title'));
  90          $this->assertEquals('Escape & Content', $doc->get('content'));
  91          $this->assertEquals('Escape & Description1', $doc->get('description1'));
  92          $this->assertEquals('Escape & Description2', $doc->get('description2'));
  93          $this->assertEquals('User Escape & Name', $doc->get('userfullname'));
  94          $this->assertEquals('Course & Title', $doc->get('coursefullname'));
  95  
  96          // Export for template, and see if it is escaped.
  97          $export = $doc->export_for_template($renderer);
  98          $this->assertEquals('Escape &amp; Title', $export['title']);
  99          $this->assertEquals('Escape &amp; Content', $export['content']);
 100          $this->assertEquals('Escape &amp; Description1', $export['description1']);
 101          $this->assertEquals('Escape &amp; Description2', $export['description2']);
 102          $this->assertEquals('User Escape &amp; Name', $export['userfullname']);
 103          $this->assertEquals('Course &amp; Title', $export['coursefullname']);
 104      }
 105  
 106      public function tearDown() {
 107          // For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
 108          if ($this->generator) {
 109              // Moodle DML freaks out if we don't teardown the temp table after each run.
 110              $this->generator->teardown();
 111              $this->generator = null;
 112          }
 113      }
 114  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1