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
 * External function unit tests.
19
 *
20
 * @package core_search
21
 * @copyright 2017 The Open University
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_search;
26
 
27
/**
28
 * External function unit tests.
29
 *
30
 * @package core_search
31
 * @copyright 2017 The Open University
32
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
1441 ariadna 34
final class external_test extends \advanced_testcase {
1 efrain 35
 
36
    public function setUp(): void {
1441 ariadna 37
        parent::setUp();
1 efrain 38
        $this->resetAfterTest();
39
    }
40
 
41
    /**
42
     * Checks the get_relevant_users function used when selecting users in search filter.
43
     */
11 efrain 44
    public function test_get_relevant_users(): void {
1 efrain 45
        // Set up two users to search for and one to do the searching.
46
        $generator = $this->getDataGenerator();
47
        $student1 = $generator->create_user(['firstname' => 'Amelia', 'lastname' => 'Aardvark']);
48
        $student2 = $generator->create_user(['firstname' => 'Amelia', 'lastname' => 'Beetle']);
49
        $student3 = $generator->create_user(['firstname' => 'Zebedee', 'lastname' => 'Boing']);
50
        $course = $generator->create_course();
51
        $generator->enrol_user($student1->id, $course->id, 'student');
52
        $generator->enrol_user($student2->id, $course->id, 'student');
53
        $generator->enrol_user($student3->id, $course->id, 'student');
54
 
55
        // As student 3, search for the other two.
56
        $this->setUser($student3);
57
        $result = external::clean_returnvalue(
58
            external::get_relevant_users_returns(),
59
            external::get_relevant_users('Amelia', 0)
60
        );
61
 
62
        // Check we got the two expected users back.
63
        $this->assertEquals([
64
            $student1->id,
65
            $student2->id,
66
        ], array_column($result, 'id'));
67
 
68
        // Check that the result contains all the expected fields.
69
        $this->assertEquals($student1->id, $result[0]['id']);
70
        $this->assertEquals('Amelia Aardvark', $result[0]['fullname']);
71
        $this->assertStringContainsString('/u/f2', $result[0]['profileimageurlsmall']);
72
 
73
        // Check we aren't leaking information about user email address (for instance).
74
        $this->assertArrayNotHasKey('email', $result[0]);
75
 
76
        // Note: We are not checking search permissions, search by different fields, etc. as these
77
        // are covered by the core_user::search unit test.
78
    }
79
}