mirror of
https://github.com/moodle/moodle.git
synced 2025-08-10 03:16:42 +02:00
MDL-61028 core_search: Allow filtering search by user (front-end)
Creates a new 'Users' field in the search filters form. This field requires new JavaScript and, to implement this, a new AJAX-callable web service to search for users by name, with detailed restrictions based on the current user's access to view profiles.
This commit is contained in:
parent
71d985ab7d
commit
a6cacdd266
9 changed files with 417 additions and 1 deletions
67
search/tests/behat/search_by_user.feature
Normal file
67
search/tests/behat/search_by_user.feature
Normal file
|
@ -0,0 +1,67 @@
|
|||
@core @core_search
|
||||
Feature: Select users when searching for user-created content
|
||||
In order to search for content by specific users
|
||||
As a user
|
||||
I need to be able to add users to the select list in the search form
|
||||
|
||||
Background:
|
||||
Given the following config values are set as admin:
|
||||
| enableglobalsearch | 1 |
|
||||
| searchengine | solr |
|
||||
And the following "courses" exist:
|
||||
| shortname | fullname |
|
||||
| C1 | Frogs |
|
||||
| C2 | Zombies |
|
||||
And the following "activities" exist:
|
||||
| activity | name | intro | course | idnumber |
|
||||
| page | PageName1 | PageDesc1 | C1 | PAGE1 |
|
||||
And the following "users" exist:
|
||||
| username | firstname | lastname |
|
||||
| s1 | Anne | Other |
|
||||
| s2 | Anne | Additional |
|
||||
| t | Anne | Ditin |
|
||||
And the following "course enrolments" exist:
|
||||
| user | course | role |
|
||||
| s1 | C1 | student |
|
||||
| s2 | C2 | student |
|
||||
| t | C1 | teacher |
|
||||
|
||||
@javascript
|
||||
Scenario: As administrator, search for users from home page
|
||||
Given I log in as "admin"
|
||||
And global search expects the query "frogs" and will return:
|
||||
| type | idnumber |
|
||||
| activity | PAGE1 |
|
||||
And I search for "frogs" using the header global search box
|
||||
And I expand all fieldsets
|
||||
And I set the field with xpath "//select[@id='id_userids']/../input[@type='text']" to "Anne"
|
||||
# Alphabetical surname order.
|
||||
Then "Anne Additional" "text" should appear before "Anne Ditin" "text"
|
||||
And "Anne Ditin" "text" should appear before "Anne Other" "text"
|
||||
|
||||
@javascript
|
||||
Scenario: As administrator, search for users within course
|
||||
Given I log in as "admin"
|
||||
And I am on "Frogs" course homepage
|
||||
And global search expects the query "frogs" and will return:
|
||||
| type | idnumber |
|
||||
| activity | PAGE1 |
|
||||
And I search for "frogs" using the header global search box
|
||||
And I expand all fieldsets
|
||||
And I select "Course: Frogs" from the "Search within" singleselect
|
||||
And I set the field with xpath "//select[@id='id_userids']/../input[@type='text']" to "Anne"
|
||||
# Users in selected course appear first.
|
||||
And "Anne Additional" "text" should appear after "Anne Other" "text"
|
||||
|
||||
@javascript
|
||||
Scenario: As student, cannot see users on other courses
|
||||
Given I log in as "s1"
|
||||
And I am on "Frogs" course homepage
|
||||
And global search expects the query "frogs" and will return:
|
||||
| type | idnumber |
|
||||
| activity | PAGE1 |
|
||||
And I search for "frogs" using the header global search box
|
||||
And I expand all fieldsets
|
||||
And I set the field with xpath "//select[@id='id_userids']/../input[@type='text']" to "A"
|
||||
Then "Anne Ditin" "text" should appear before "Anne Other" "text"
|
||||
And "Anne Additional" "text" should not exist
|
72
search/tests/external_test.php
Normal file
72
search/tests/external_test.php
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* External function unit tests.
|
||||
*
|
||||
* @package core_search
|
||||
* @copyright 2017 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core_search;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* External function unit tests.
|
||||
*
|
||||
* @package core_search
|
||||
* @copyright 2017 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class external_testcase extends \advanced_testcase {
|
||||
|
||||
public function setUp() {
|
||||
$this->resetAfterTest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the get_relevant_users function used when selecting users in search filter.
|
||||
*/
|
||||
public function test_get_relevant_users() {
|
||||
// Set up two users to search for and one to do the searching.
|
||||
$generator = $this->getDataGenerator();
|
||||
$student1 = $generator->create_user(['firstname' => 'Amelia', 'lastname' => 'Aardvark']);
|
||||
$student2 = $generator->create_user(['firstname' => 'Amelia', 'lastname' => 'Beetle']);
|
||||
$student3 = $generator->create_user(['firstname' => 'Zebedee', 'lastname' => 'Boing']);
|
||||
$course = $generator->create_course();
|
||||
$generator->enrol_user($student1->id, $course->id, 'student');
|
||||
$generator->enrol_user($student2->id, $course->id, 'student');
|
||||
$generator->enrol_user($student3->id, $course->id, 'student');
|
||||
|
||||
// As student 3, search for the other two.
|
||||
$this->setUser($student3);
|
||||
$result = external::get_relevant_users('Amelia', 0);
|
||||
$this->assertCount(2, $result);
|
||||
|
||||
// Check that the result contains all the expected fields.
|
||||
$this->assertEquals($student1->id, $result[0]->id);
|
||||
$this->assertEquals('Amelia Aardvark', $result[0]->fullname);
|
||||
$this->assertContains('/u/f2', $result[0]->profileimageurlsmall);
|
||||
|
||||
// Check we aren't leaking information about user email address (for instance).
|
||||
$this->assertObjectNotHasAttribute('email', $result[0]);
|
||||
|
||||
// Note: We are not checking search permissions, search by different fields, etc. as these
|
||||
// are covered by the core_user::search unit test.
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue