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
namespace core_search;
18
 
19
use advanced_testcase;
20
use context_course;
21
use core_mocksearch\search\mock_search_area;
22
use mock_search\engine;
23
use testable_core_search;
24
use stdClass;
25
 
26
/**
27
 * Unit tests for search document.
28
 *
29
 * @package     core_search
30
 * @category    test
31
 * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @coversDefaultClass \core_search\document
34
 */
1441 ariadna 35
final class document_test extends \advanced_testcase {
1 efrain 36
 
37
    /**
38
     * Setup to ensure that fixtures are loaded.
39
     */
40
    public static function setupBeforeClass(): void {
41
        global $CFG;
42
        require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
43
        require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
44
    }
45
 
46
    /**
47
     * @var Instace of core_search_generator.
48
     */
49
    protected $generator = null;
50
 
51
    public function setUp(): void {
1441 ariadna 52
        parent::setUp();
1 efrain 53
        $this->resetAfterTest();
54
        set_config('enableglobalsearch', true);
55
 
56
        // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
57
        $search = \testable_core_search::instance();
58
 
59
        $this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
60
        $this->generator->setup();
61
    }
62
 
63
    /**
64
     * Adding this test here as get_areas_user_accesses process is the same, results just depend on the context level.
65
     *
66
     * @covers ::export_for_template
67
     * @return void
68
     */
11 efrain 69
    public function test_search_user_accesses(): void {
1 efrain 70
        global $PAGE;
71
 
72
        $area = new mock_search_area();
73
        $renderer = $PAGE->get_renderer('core_search');
74
        $engine = new engine();
75
 
76
        $course = $this->getDataGenerator()->create_course(['fullname' => 'Course & Title']);
77
        $user = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => 'Escape & Name']);
78
        $this->getDataGenerator()->enrol_user($user->id, $course->id, 'teacher');
79
        $this->setAdminUser();
80
 
81
        // Make a record to enter in the search area.
82
        $record = new stdClass();
83
        $record->title = 'Escape & Title';
84
        $record->content = 'Escape & Content';
85
        $record->description1 = 'Escape & Description1';
86
        $record->description2 = 'Escape & Description2';
87
        $record->userid = $user->id;
88
        $record->courseid = $course->id;
89
        $record = $this->generator->create_record($record);
90
 
91
        // Convert to a 'doc data' type format.
92
        $docdata = $area->convert_record_to_doc_array($record);
93
 
94
        // First see that the docuemnt has the right information, unescaped.
95
        $doc = $engine->to_document($area, $docdata);
96
        $this->assertEquals('Escape & Title', $doc->get('title'));
97
        $this->assertEquals('Escape & Content', $doc->get('content'));
98
        $this->assertEquals('Escape & Description1', $doc->get('description1'));
99
        $this->assertEquals('Escape & Description2', $doc->get('description2'));
100
        $this->assertEquals('User Escape & Name', $doc->get('userfullname'));
101
        $this->assertEquals('Course & Title', $doc->get('coursefullname'));
102
 
103
        // Export for template, and see if it is escaped.
104
        $export = $doc->export_for_template($renderer);
105
        $this->assertEquals('Escape &amp; Title', $export['title']);
106
        $this->assertEquals('Escape &amp; Content', $export['content']);
107
        $this->assertEquals('Escape &amp; Description1', $export['description1']);
108
        $this->assertEquals('Escape &amp; Description2', $export['description2']);
109
        $this->assertEquals('User Escape &amp; Name', $export['userfullname']);
110
        $this->assertEquals('Course &amp; Title', $export['coursefullname']);
111
    }
112
 
113
    /**
114
     * Test we can set and get document icon.
115
     *
116
     * @covers ::set_doc_icon
117
     */
11 efrain 118
    public function test_get_and_set_doc_icon(): void {
1 efrain 119
        $document = $this->getMockBuilder('\core_search\document')
120
            ->disableOriginalConstructor()
121
            ->getMockForAbstractClass();
122
 
123
        $this->assertNull($document->get_doc_icon());
124
 
125
        $docicon = new \core_search\document_icon('test_name', 'test_component');
126
        $document->set_doc_icon($docicon);
127
 
128
        $this->assertEquals($docicon, $document->get_doc_icon());
129
    }
130
 
131
    public function tearDown(): void {
132
        // For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
133
        if ($this->generator) {
134
            // Moodle DML freaks out if we don't teardown the temp table after each run.
135
            $this->generator->teardown();
136
            $this->generator = null;
137
        }
1441 ariadna 138
        parent::tearDown();
1 efrain 139
    }
140
 
141
    /**
142
     * Test the document author visibility depending on the user capabilities.
143
     *
144
     * @covers ::export_for_template
145
     * @dataProvider document_author_visibility_provider
146
     * @param string $rolename the role name
147
     * @param array $capexceptions the capabilities exceptions
148
     * @param bool $expected the expected author visibility
149
     * @param bool $owndocument if the resulting document belongs to the current user
150
     */
151
    public function test_document_author_visibility(
152
        string $rolename = 'editingteacher',
153
        array $capexceptions = [],
154
        bool $expected = true,
155
        bool $owndocument = false
11 efrain 156
    ): void {
1 efrain 157
        global $DB, $PAGE;
158
 
159
        $area = new mock_search_area();
160
        $renderer = $PAGE->get_renderer('core_search');
161
        $engine = new engine();
162
 
163
        $course = $this->getDataGenerator()->create_course(['fullname' => 'Course & Title']);
164
        $context = context_course::instance($course->id);
165
 
166
        $roleid = $DB->get_field('role', 'id', ['shortname' => $rolename]);
167
        foreach ($capexceptions as $capability) {
168
            assign_capability($capability, CAP_PROHIBIT, $roleid, $context->id);
169
        }
170
 
171
        $user = $this->getDataGenerator()->create_user(['firstname' => 'Test', 'lastname' => 'User']);
172
        $this->getDataGenerator()->enrol_user($user->id, $course->id, $rolename);
173
        $this->setUser($user);
174
 
175
        if ($owndocument) {
176
            $author = $user;
177
        } else {
178
            $author = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => 'Escape & Name']);
179
            $this->getDataGenerator()->enrol_user($author->id, $course->id, 'student');
180
        }
181
 
182
        // Make a record to enter in the search area.
183
        $record = new stdClass();
184
        $record->title = 'Escape & Title';
185
        $record->content = 'Escape & Content';
186
        $record->description1 = 'Escape & Description1';
187
        $record->description2 = 'Escape & Description2';
188
        $record->userid = $author->id;
189
        $record->courseid = $course->id;
190
        $record->contextid = $context->id;
191
        $record = $this->generator->create_record($record);
192
 
193
        // Convert to a 'doc data' type format.
194
        $docdata = $area->convert_record_to_doc_array($record);
195
 
196
        // First see that the document has the user information.
197
        $doc = $engine->to_document($area, $docdata);
198
        $this->assertEquals(fullname($author), $doc->get('userfullname'));
199
 
200
        // Export for template, and see if it the user information is exported.
201
        $export = $doc->export_for_template($renderer);
202
 
203
        if ($expected) {
204
            $authorname = htmlentities(fullname($author), ENT_COMPAT);
205
            $this->assertEquals($authorname, $export['userfullname']);
206
        } else {
207
            $this->assertArrayNotHasKey('userfullname', $export);
208
        }
209
    }
210
 
211
    /**
212
     * Data provider for test_document_author_visibility().
213
     *
214
     * @return array
215
     */
1441 ariadna 216
    public static function document_author_visibility_provider(): array {
1 efrain 217
        return [
218
            'Teacher' => [
219
                'rolename' => 'editingteacher',
220
                'capexceptions' => [],
221
                'expected' => true,
222
                'owndocument' => false,
223
            ],
224
            'Non editing teacher' => [
225
                'rolename' => 'teacher',
226
                'capexceptions' => [],
227
                'expected' => true,
228
                'owndocument' => false,
229
            ],
230
            'Student' => [
231
                'rolename' => 'student',
232
                'capexceptions' => [],
233
                'expected' => true,
234
                'owndocument' => false,
235
            ],
236
            // Adding capability exceptions.
237
            'Student without view profiles' => [
238
                'rolename' => 'student',
239
                'capexceptions' => ['moodle/user:viewdetails'],
240
                'expected' => false,
241
                'owndocument' => false,
242
            ],
243
            'Student without view participants' => [
244
                'rolename' => 'student',
245
                'capexceptions' => ['moodle/course:viewparticipants'],
246
                'expected' => false,
247
                'owndocument' => false,
248
            ],
249
            'Student without view participants or profiles' => [
250
                'rolename' => 'student',
251
                'capexceptions' => ['moodle/user:viewdetails', 'moodle/course:viewparticipants'],
252
                'expected' => false,
253
                'owndocument' => false,
254
            ],
255
            // Users should be able to see its own documents.
256
            'Student author without view profiles' => [
257
                'rolename' => 'student',
258
                'capexceptions' => ['moodle/user:viewdetails'],
259
                'expected' => true,
260
                'owndocument' => true,
261
            ],
262
            'Student author without view participants' => [
263
                'rolename' => 'student',
264
                'capexceptions' => ['moodle/course:viewparticipants'],
265
                'expected' => true,
266
                'owndocument' => true,
267
            ],
268
            'Student author without view participants or profiles' => [
269
                'rolename' => 'student',
270
                'capexceptions' => ['moodle/user:viewdetails', 'moodle/course:viewparticipants'],
271
                'expected' => true,
272
                'owndocument' => true,
273
            ],
274
 
275
        ];
276
    }
277
}