Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * Book search unit tests.
  19   *
  20   * @package     mod_book
  21   * @category    test
  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  namespace mod_book\search;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
  32  
  33  /**
  34   * Provides the unit tests for book search.
  35   *
  36   * @package     mod_book
  37   * @category    test
  38   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class search_test extends \advanced_testcase {
  42  
  43      /**
  44       * @var string Area id
  45       */
  46      protected $bookchapterareaid = null;
  47  
  48      public function setUp(): void {
  49          $this->resetAfterTest(true);
  50          set_config('enableglobalsearch', true);
  51  
  52          $this->bookchapterareaid = \core_search\manager::generate_areaid('mod_book', 'chapter');
  53  
  54          // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
  55          $search = \testable_core_search::instance();
  56      }
  57  
  58      /**
  59       * Availability.
  60       *
  61       * @return void
  62       */
  63      public function test_search_enabled() {
  64  
  65          $searcharea = \core_search\manager::get_search_area($this->bookchapterareaid);
  66          list($componentname, $varname) = $searcharea->get_config_var_name();
  67  
  68          // Enabled by default once global search is enabled.
  69          $this->assertTrue($searcharea->is_enabled());
  70  
  71          set_config($varname . '_enabled', 0, $componentname);
  72          $this->assertFalse($searcharea->is_enabled());
  73  
  74          set_config($varname . '_enabled', 1, $componentname);
  75          $this->assertTrue($searcharea->is_enabled());
  76      }
  77  
  78      /**
  79       * Indexing chapter contents.
  80       *
  81       * @return void
  82       */
  83      public function test_chapters_indexing() {
  84          global $DB;
  85  
  86          // Returns the instance as long as the area is supported.
  87          $searcharea = \core_search\manager::get_search_area($this->bookchapterareaid);
  88          $this->assertInstanceOf('\mod_book\search\chapter', $searcharea);
  89  
  90          $course1 = self::getDataGenerator()->create_course();
  91          $book = $this->getDataGenerator()->create_module('book', array('course' => $course1->id));
  92  
  93          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
  94          $chapter1 = $bookgenerator->create_chapter(array('bookid' => $book->id, 'content' => 'Chapter1', 'title' => 'Title1'));
  95          $chapter2 = $bookgenerator->create_chapter(array('bookid' => $book->id, 'content' => 'Chapter2', 'title' => 'Title2'));
  96  
  97          // All records.
  98          $recordset = $searcharea->get_recordset_by_timestamp(0);
  99          $this->assertTrue($recordset->valid());
 100          $nrecords = 0;
 101          foreach ($recordset as $record) {
 102              $this->assertInstanceOf('stdClass', $record);
 103              $doc = $searcharea->get_document($record);
 104              $this->assertInstanceOf('\core_search\document', $doc);
 105  
 106              // Static caches are working.
 107              $dbreads = $DB->perf_get_reads();
 108              $doc = $searcharea->get_document($record);
 109              $this->assertEquals($dbreads, $DB->perf_get_reads());
 110              $this->assertInstanceOf('\core_search\document', $doc);
 111              $nrecords++;
 112          }
 113          // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
 114          $recordset->close();
 115          $this->assertEquals(2, $nrecords);
 116  
 117          // The +2 is to prevent race conditions.
 118          $recordset = $searcharea->get_recordset_by_timestamp(time() + 2);
 119  
 120          // No new records.
 121          $this->assertFalse($recordset->valid());
 122          $recordset->close();
 123  
 124          // Create another book and chapter.
 125          $book2 = $this->getDataGenerator()->create_module('book', array('course' => $course1->id));
 126          $bookgenerator->create_chapter(array('bookid' => $book2->id,
 127                  'content' => 'Chapter3', 'title' => 'Title3'));
 128  
 129          // Query by context, first book.
 130          $recordset = $searcharea->get_document_recordset(0, \context_module::instance($book->cmid));
 131          $this->assertEquals(2, iterator_count($recordset));
 132          $recordset->close();
 133  
 134          // Second book.
 135          $recordset = $searcharea->get_document_recordset(0, \context_module::instance($book2->cmid));
 136          $this->assertEquals(1, iterator_count($recordset));
 137          $recordset->close();
 138  
 139          // Course.
 140          $recordset = $searcharea->get_document_recordset(0, \context_course::instance($course1->id));
 141          $this->assertEquals(3, iterator_count($recordset));
 142          $recordset->close();
 143      }
 144  
 145      /**
 146       * Document contents.
 147       *
 148       * @return void
 149       */
 150      public function test_check_access() {
 151          global $DB;
 152  
 153          // Returns the instance as long as the area is supported.
 154          $searcharea = \core_search\manager::get_search_area($this->bookchapterareaid);
 155          $this->assertInstanceOf('\mod_book\search\chapter', $searcharea);
 156  
 157          $user1 = self::getDataGenerator()->create_user();
 158          $course1 = self::getDataGenerator()->create_course();
 159          $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
 160  
 161          $book = $this->getDataGenerator()->create_module('book', array('course' => $course1->id));
 162          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
 163  
 164          $chapter = array('bookid' => $book->id, 'content' => 'Chapter1', 'title' => 'Title1');
 165          $chapter1 = $bookgenerator->create_chapter($chapter);
 166          $chapter['content'] = 'Chapter2';
 167          $chapter['title'] = 'Title2';
 168          $chapter['hidden'] = 1;
 169          $chapter2 = $bookgenerator->create_chapter($chapter);
 170  
 171          $this->setAdminUser();
 172          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($chapter1->id));
 173          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($chapter2->id));
 174  
 175          $this->setUser($user1);
 176  
 177          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($chapter1->id));
 178          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($chapter2->id));
 179  
 180          $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($chapter2->id + 10));
 181      }
 182  }