Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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