From d4a6aa40c91fc1627c1b274ef91922d7240e3dce Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 17 Apr 2020 11:49:43 +0800 Subject: [PATCH 2/2] MDL-68246 keyword tests --- user/tests/participants_search_test.php | 235 ++++++++++++++++++++++++ 1 file changed, 235 insertions(+) diff --git a/user/tests/participants_search_test.php b/user/tests/participants_search_test.php index d6701515940..688edfef21c 100644 --- a/user/tests/participants_search_test.php +++ b/user/tests/participants_search_test.php @@ -32,6 +32,7 @@ use context_course; use context_coursecat; use core_table\local\filter\filter; use core_table\local\filter\integer_filter; +use core_table\local\filter\string_filter; use core_user\table\participants_filterset; use moodle_recordset; use stdClass; @@ -591,4 +592,238 @@ class participants_search_test extends advanced_testcase { return $finaltests; } + /** + * Ensure that the keyword filter works as expected with the provided test cases. + * + * @param array $usersdata The list of users to create + * @param array $keyweords The list of keyweords to filter by + * @param int $jointype The join type to use when combining filter values + * @param int $count The expected count + * @param array $expectedusers + * @dataProvider keyword_provider + */ + public function test_keyword_filter(array $usersdata, array $keywords, int $jointype, int $count, array $expectedusers): void { + global $DB; + + $course = $this->getDataGenerator()->create_course(); + $coursecontext = context_course::instance($course->id); + + $category = $DB->get_record('course_categories', ['id' => $course->category]); + $categorycontext = context_coursecat::instance($category->id); + + $users = []; + + foreach ($usersdata as $username => $userdata) { + $user = $this->getDataGenerator()->create_user($userdata); + $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student'); + $users[$username] = $user; + } + + // Create a secondary course with users. We should not see these users. + $this->create_course_with_users(10, 10, 10, 10); + + // Create the basic filter. + $filterset = new participants_filterset(); + $filterset->add_filter(new integer_filter('courseid', null, [(int) $course->id])); + + // Create the keyword filter. + $keywordfilter = new string_filter('keywords'); + $filterset->add_filter($keywordfilter); + + // Configure the filter. + foreach ($keywords as $keyword) { + $keywordfilter->add_filter_value($keyword); + } + $keywordfilter->set_join_type($jointype); + + // Run the search. + $search = new participants_search($filterset); + $rs = $search->get_participants(); + $this->assertInstanceOf(moodle_recordset::class, $rs); + $records = $this->convert_recordset_to_array($rs); + + $this->assertCount($count, $records); + $this->assertEquals($count, $search->get_total_participants_count()); + + foreach ($expectedusers as $expecteduser) { + $this->assertArrayHasKey($users[$expecteduser]->id, $records); + } + + } + + /** + * Data provider for keyword tests. + * + * @return array + */ + public function keyword_provider(): array { + $tests = [ + // Users where the keyword matches firstname, lastname, or username. + 'Users with basic names' => (object) [ + 'users' => [ + 'adam.ant' => [ + 'firstname' => 'Adam', + 'lastname' => 'Ant', + ], + 'barbara.bennett' => [ + 'firstname' => 'Barbara', + 'lastname' => 'Bennett', + ], + 'colin.carnforth' => [ + 'firstname' => 'Colin', + 'lastname' => 'Carnforth', + ], + 'tony.rogers' => [ + 'firstname' => 'Anthony', + 'lastname' => 'Rogers', + ], + 'sarah.rester' => [ + 'firstname' => 'Sarah', + 'lastname' => 'Rester', + 'email' => 'zazu@example.com', + ], + ], + 'expect' => [ + 'ANY: No filter' => (object) [ + 'keywords' => [], + 'jointype' => filter::JOINTYPE_ANY, + 'count' => 5, + 'expectedusers' => [ + 'adam.ant', + 'barbara.bennett', + 'colin.carnforth', + 'tony.rogers', + 'sarah.rester', + ], + ], + 'ANY: First name only' => (object) [ + 'keywords' => ['adam'], + 'jointype' => filter::JOINTYPE_ANY, + 'count' => 1, + 'expectedusers' => [ + 'adam.ant', + ], + ], + 'ANY: Last name only' => (object) [ + 'keywords' => ['BeNNeTt'], + 'jointype' => filter::JOINTYPE_ANY, + 'count' => 1, + 'expectedusers' => [ + 'barbara.bennett', + ], + ], + 'ANY: First/Last name' => (object) [ + 'keywords' => ['ant'], + 'jointype' => filter::JOINTYPE_ANY, + 'count' => 2, + 'expectedusers' => [ + 'adam.ant', + 'tony.rogers', + ], + ], + 'ANY: Username (not searched)' => (object) [ + 'keywords' => ['sara.rester'], + 'jointype' => filter::JOINTYPE_ANY, + 'count' => 0, + 'expectedusers' => [], + ], + 'ANY: Email' => (object) [ + 'keywords' => ['zazu'], + 'jointype' => filter::JOINTYPE_ANY, + 'count' => 1, + 'expectedusers' => [ + 'sarah.rester', + ], + ], + /* + 'ANY: Multiple filters' => (object) [ + 'keywords' => ['ant', 'rog'], + 'jointype' => filter::JOINTYPE_ANY, + 'count' => 2, + 'expectedusers' => [ + 'adam.ant', + 'tony.rogers', + ], + ], + */ + + // Filter: ALL. + 'ALL: No filter' => (object) [ + 'keywords' => [], + 'jointype' => filter::JOINTYPE_ALL, + 'count' => 5, + 'expectedusers' => [ + 'adam.ant', + 'barbara.bennett', + 'colin.carnforth', + 'tony.rogers', + 'sarah.rester', + ], + ], + 'ALL: First name only' => (object) [ + 'keywords' => ['adam'], + 'jointype' => filter::JOINTYPE_ALL, + 'count' => 1, + 'expectedusers' => [ + 'adam.ant', + ], + ], + 'ALL: Last name only' => (object) [ + 'keywords' => ['BeNNeTt'], + 'jointype' => filter::JOINTYPE_ALL, + 'count' => 1, + 'expectedusers' => [ + 'barbara.bennett', + ], + ], + 'ALL: First/Last name' => (object) [ + 'keywords' => ['ant'], + 'jointype' => filter::JOINTYPE_ALL, + 'count' => 2, + 'expectedusers' => [ + 'adam.ant', + 'tony.rogers', + ], + ], + 'ALL: Username (not searched)' => (object) [ + 'keywords' => ['sara.rester'], + 'jointype' => filter::JOINTYPE_ALL, + 'count' => 0, + 'expectedusers' => [], + ], + 'ALL: Email' => (object) [ + 'keywords' => ['zazu'], + 'jointype' => filter::JOINTYPE_ALL, + 'count' => 1, + 'expectedusers' => [ + 'sarah.rester', + ], + ], + 'ALL: Multiple filters' => (object) [ + 'keywords' => ['ant', 'rog'], + 'jointype' => filter::JOINTYPE_ALL, + 'count' => 1, + 'expectedusers' => [ + 'tony.rogers', + ], + ], + ], + ], + ]; + + $finaltests = []; + foreach ($tests as $testname => $testdata) { + foreach ($testdata->expect as $expectname => $expectdata) { + $finaltests["$testname => {$expectname}"] = [ + 'users' => $testdata->users, + 'keywords' => $expectdata->keywords, + 'jointype' => $expectdata->jointype, + 'count' => $expectdata->count, + 'expectedusers' => $expectdata->expectedusers, + ]; + } + } + + return $finaltests; + } } -- 2.21.0