MDL-31989 search: New search areas
[moodle.git] / mod / glossary / tests / search_test.php
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/>.
17 /**
18  * Glossary search unit tests.
19  *
20  * @package     mod_glossary
21  * @category    test
22  * @copyright   2016 David Monllao {@link http://www.davidmonllao.com}
23  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
30 require_once($CFG->dirroot . '/mod/glossary/tests/generator/lib.php');
32 /**
33  * Provides the unit tests for glossary search.
34  *
35  * @package     mod_glossary
36  * @category    test
37  * @copyright   2016 David Monllao {@link http://www.davidmonllao.com}
38  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class mod_glossary_search_testcase extends advanced_testcase {
42     /**
43      * @var string Area id
44      */
45     protected $entryareaid = null;
47     public function setUp() {
48         $this->resetAfterTest(true);
49         set_config('enableglobalsearch', true);
51         // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
52         $search = testable_core_search::instance();
54         $this->entryareaid = \core_search\manager::generate_areaid('mod_glossary', 'entry');
55     }
57     /**
58      * Availability.
59      *
60      * @return void
61      */
62     public function test_search_enabled() {
64         $searcharea = \core_search\manager::get_search_area($this->entryareaid);
65         list($componentname, $varname) = $searcharea->get_config_var_name();
67         // Enabled by default once global search is enabled.
68         $this->assertTrue($searcharea->is_enabled());
70         set_config('enable' . $varname, false, $componentname);
71         $this->assertFalse($searcharea->is_enabled());
73         set_config('enable' . $varname, true, $componentname);
74         $this->assertTrue($searcharea->is_enabled());
75     }
77     /**
78      * Indexing contents.
79      *
80      * @return void
81      */
82     public function test_entries_indexing() {
83         global $DB;
85         $searcharea = \core_search\manager::get_search_area($this->entryareaid);
86         $this->assertInstanceOf('\mod_glossary\search\entry', $searcharea);
88         $user1 = self::getDataGenerator()->create_user();
89         $user2 = self::getDataGenerator()->create_user();
91         $course1 = self::getDataGenerator()->create_course();
92         $course2 = self::getDataGenerator()->create_course();
94         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
95         $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student');
97         $record = new stdClass();
98         $record->course = $course1->id;
100         $this->setUser($user1);
102         // Approved entries by default glossary.
103         $glossary1 = self::getDataGenerator()->create_module('glossary', $record);
104         $entry1 = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary1);
105         $entry2 = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary1);
107         // All records.
108         $recordset = $searcharea->get_recordset_by_timestamp(0);
109         $this->assertTrue($recordset->valid());
110         $nrecords = 0;
111         foreach ($recordset as $record) {
112             $this->assertInstanceOf('stdClass', $record);
113             $doc = $searcharea->get_document($record);
114             $this->assertInstanceOf('\core_search\document', $doc);
116             // Static caches are working.
117             $dbreads = $DB->perf_get_reads();
118             $doc = $searcharea->get_document($record);
120             // The +1 is because we are not caching glossary alias (keywords) as they depend on a single entry.
121             $this->assertEquals($dbreads + 1, $DB->perf_get_reads());
122             $this->assertInstanceOf('\core_search\document', $doc);
123             $nrecords++;
124         }
125         // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
126         $recordset->close();
127         $this->assertEquals(2, $nrecords);
129         // The +2 is to prevent race conditions.
130         $recordset = $searcharea->get_recordset_by_timestamp(time() + 2);
132         // No new records.
133         $this->assertFalse($recordset->valid());
134         $recordset->close();
135     }
137     /**
138      * Document contents.
139      *
140      * @return void
141      */
142     public function test_entries_document() {
143         global $DB;
145         $searcharea = \core_search\manager::get_search_area($this->entryareaid);
147         $user = self::getDataGenerator()->create_user();
148         $course1 = self::getDataGenerator()->create_course();
149         $this->getDataGenerator()->enrol_user($user->id, $course1->id, 'teacher');
151         $record = new stdClass();
152         $record->course = $course1->id;
154         $this->setUser($user);
155         $glossary = self::getDataGenerator()->create_module('glossary', $record);
156         $entry = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary);
157         $entry->course = $glossary->course;
159         $doc = $searcharea->get_document($entry);
160         $this->assertInstanceOf('\core_search\document', $doc);
161         $this->assertEquals($entry->id, $doc->get('itemid'));
162         $this->assertEquals($course1->id, $doc->get('courseid'));
163         $this->assertEquals($user->id, $doc->get('userid'));
164         $this->assertEquals($entry->concept, $doc->get('title'));
165         $this->assertEquals($entry->definition, $doc->get('content'));
166     }
168     /**
169      * Document accesses.
170      *
171      * @return void
172      */
173     public function test_entries_access() {
174         global $DB;
176         // Returns the instance as long as the component is supported.
177         $searcharea = \core_search\manager::get_search_area($this->entryareaid);
179         $user1 = self::getDataGenerator()->create_user();
180         $user2 = self::getDataGenerator()->create_user();
182         $course1 = self::getDataGenerator()->create_course();
183         $course2 = self::getDataGenerator()->create_course();
185         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'teacher');
186         $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student');
188         $record = new stdClass();
189         $record->course = $course1->id;
191         // Approved entries by default glossary, created by teacher.
192         $this->setUser($user1);
193         $glossary1 = self::getDataGenerator()->create_module('glossary', $record);
194         $teacherapproved = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary1);
195         $teachernotapproved = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary1, array('approved' => false));
197         // Entries need to be approved and created by student.
198         $glossary2 = self::getDataGenerator()->create_module('glossary', $record);
199         $this->setUser($user2);
200         $studentapproved = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary2);
201         $studentnotapproved = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary2, array('approved' => false));
203         // Activity hidden to students.
204         $this->setUser($user1);
205         $glossary3 = self::getDataGenerator()->create_module('glossary', $record);
206         $hidden = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary3);
207         set_coursemodule_visible($glossary3->cmid, 0);
209         $this->setUser($user2);
210         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($teacherapproved->id));
211         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($teachernotapproved->id));
212         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($studentapproved->id));
213         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($studentnotapproved->id));
214         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($hidden->id));
215     }