[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 123 /** 124 * Document contents. 125 * 126 * @return void 127 */ 128 public function test_check_access() { 129 global $DB; 130 131 // Returns the instance as long as the area is supported. 132 $searcharea = \core_search\manager::get_search_area($this->bookchapterareaid); 133 $this->assertInstanceOf('\mod_book\search\chapter', $searcharea); 134 135 $user1 = self::getDataGenerator()->create_user(); 136 $course1 = self::getDataGenerator()->create_course(); 137 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student'); 138 139 $book = $this->getDataGenerator()->create_module('book', array('course' => $course1->id)); 140 $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); 141 142 $chapter = array('bookid' => $book->id, 'content' => 'Chapter1', 'title' => 'Title1'); 143 $chapter1 = $bookgenerator->create_chapter($chapter); 144 $chapter['content'] = 'Chapter2'; 145 $chapter['title'] = 'Title2'; 146 $chapter['hidden'] = 1; 147 $chapter2 = $bookgenerator->create_chapter($chapter); 148 149 $this->setAdminUser(); 150 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($chapter1->id)); 151 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($chapter2->id)); 152 153 $this->setUser($user1); 154 155 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($chapter1->id)); 156 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($chapter2->id)); 157 158 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($chapter2->id + 10)); 159 } 160 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |