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