2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Search document unit tests.
20 * @package core_search
22 * @copyright 2016 Eric Merrill {@link http://www.merrilldigital.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once(__DIR__
. '/fixtures/testable_core_search.php');
29 require_once(__DIR__
. '/fixtures/mock_search_area.php');
32 * Unit tests for search document.
34 * @package core_search
36 * @copyright 2016 Eric Merrill {@link http://www.merrilldigital.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class search_document_testcase
extends advanced_testcase
{
42 * @var Instace of core_search_generator.
44 protected $generator = null;
46 public function setUp(): void
{
47 $this->resetAfterTest();
48 set_config('enableglobalsearch', true);
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();
53 $this->generator
= self
::getDataGenerator()->get_plugin_generator('core_search');
54 $this->generator
->setup();
58 * Adding this test here as get_areas_user_accesses process is the same, results just depend on the context level.
62 public function test_search_user_accesses() {
65 $area = new \core_mocksearch\search\
mock_search_area();
66 $renderer = $PAGE->get_renderer('core_search');
67 $engine = new \mock_search\
engine();
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');
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);
84 // Convert to a 'doc data' type format.
85 $docdata = $area->convert_record_to_doc_array($record);
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'));
96 // Export for template, and see if it is escaped.
97 $export = $doc->export_for_template($renderer);
98 $this->assertEquals('Escape & Title', $export['title']);
99 $this->assertEquals('Escape & Content', $export['content']);
100 $this->assertEquals('Escape & Description1', $export['description1']);
101 $this->assertEquals('Escape & Description2', $export['description2']);
102 $this->assertEquals('User Escape & Name', $export['userfullname']);
103 $this->assertEquals('Course & Title', $export['coursefullname']);
107 * Test we can set and get document icon.
109 public function test_get_and_set_doc_icon() {
110 $document = $this->getMockBuilder('\core_search\document')
111 ->disableOriginalConstructor()
112 ->getMockForAbstractClass();
114 $this->assertNull($document->get_doc_icon());
116 $docicon = new \core_search\
document_icon('test_name', 'test_component');
117 $document->set_doc_icon($docicon);
119 $this->assertEquals($docicon, $document->get_doc_icon());
122 public function tearDown(): void
{
123 // For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
124 if ($this->generator
) {
125 // Moodle DML freaks out if we don't teardown the temp table after each run.
126 $this->generator
->teardown();
127 $this->generator
= null;