first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+210
View File
@@ -0,0 +1,210 @@
<?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/>.
/**
* Unit tests for core_table\external\dynamic\get;
*
* @package core_table
* @category test
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
declare(strict_types = 1);
namespace core_table\external\dynamic;
use core_table\local\filter\filter;
use advanced_testcase;
/**
* Unit tests for core_table\external\dynamic\get;
*
* @package core_table
* @category test
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_test extends advanced_testcase {
/**
* Test execute invalid component format.
*/
public function test_execute_invalid_component_format(): void {
$this->resetAfterTest();
$this->expectException(\invalid_parameter_exception::class);
get::execute(
"core-user",
"participants",
"",
$this->get_sort_array(['email' => SORT_ASC]),
[],
(string) filter::JOINTYPE_ANY,
null,
null,
null,
null,
[],
null
);
}
/**
* Test execute invalid component.
*/
public function test_execute_invalid_component(): void {
$this->resetAfterTest();
$this->expectException(\UnexpectedValueException::class);
get::execute(
"core_users",
"participants",
"",
$this->get_sort_array(['email' => SORT_ASC]),
[],
(string) filter::JOINTYPE_ANY,
null,
null,
null,
null,
[],
null
);
}
/**
* Test execute invalid handler.
*/
public function test_execute_invalid_handler(): void {
$this->resetAfterTest();
$this->expectException('UnexpectedValueException');
$handler = "\\core_user\\table\\users_participants_table";
$this->expectExceptionMessage("Table handler class {$handler} not found. Please make sure that your table handler class is under the \\core_user\\table namespace.");
// Tests that invalid users_participants_table class gets an exception.
get::execute(
"core_user",
"users_participants_table",
"",
$this->get_sort_array(['email' => SORT_ASC]),
[],
(string) filter::JOINTYPE_ANY,
null,
null,
null,
null,
[],
null
);
}
/**
* Test execute invalid filter.
*/
public function test_execute_invalid_filter(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
// Filter with an invalid name.
$filter = [
[
'fullname' => 'courseid',
'jointype' => filter::JOINTYPE_ANY,
'values' => [(int) $course->id]
]
];
$this->expectException(\invalid_parameter_exception::class);
$this->expectExceptionMessage("Invalid parameter value detected (filters => Invalid parameter value detected " .
"(Missing required key in single structure: name): Missing required key in single structure: name");
get::execute(
"core_user",
"participants", "user-index-participants-{$course->id}",
$this->get_sort_array(['firstname' => SORT_ASC]),
$filter,
(string) filter::JOINTYPE_ANY
);
}
/**
* Test execute method.
*/
public function test_table_get_execute(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$user1 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['email' => 's1@moodle.com']);
$user2 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['email' => 's2@moodle.com']);
$user3 = $this->getDataGenerator()->create_user(['email' => 's3@moodle.com']);
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher', ['email' => 't1@moodle.com']);
$this->setUser($teacher);
$this->get_sort_array(['email' => SORT_ASC]);
$filter = [
[
'name' => 'courseid',
'jointype' => filter::JOINTYPE_ANY,
'values' => [(int) $course->id]
]
];
$participantstable = get::execute(
"core_user",
"participants",
"user-index-participants-{$course->id}",
$this->get_sort_array(['firstname' => SORT_ASC]),
$filter,
(string) filter::JOINTYPE_ANY,
null,
null,
null,
null,
[],
null
);
$html = $participantstable['html'];
$this->assertStringContainsString($user1->email, $html);
$this->assertStringContainsString($user2->email, $html);
$this->assertStringContainsString($teacher->email, $html);
$this->assertStringNotContainsString($user3->email, $html);
}
/**
* Convert a traditional sort order into a sortorder for the web service.
*
* @param array $sortdata
* @return array
*/
protected function get_sort_array(array $sortdata): array {
$newsortorder = [];
foreach ($sortdata as $sortby => $sortorder) {
$newsortorder[] = [
'sortby' => $sortby,
'sortorder' => $sortorder,
];
}
return $newsortorder;
}
}
@@ -0,0 +1,344 @@
<?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/>.
declare(strict_types=1);
namespace core_table\local\filter;
use advanced_testcase;
use InvalidArgumentException;
/**
* Unit tests for core_table\local\filter\filter.
*
* @package core_table
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core_table\local\filter\filter
*/
class filter_test extends advanced_testcase {
/**
* Test that the constructor correctly handles a number of conditions.
*
* @dataProvider constructor_provider
* @param array $args
* @param int $jointype
* @param array $values
*/
public function test_constructor(array $args, int $jointype, array $values): void {
$filter = new filter(...$args);
// We should always get a filter.
$this->assertInstanceOf(filter::class, $filter);
// We should always get the correct join type.
$this->assertEquals($jointype, $filter->get_join_type());
// The values should be the expected ones.
$this->assertSame($values, $filter->get_filter_values());
}
/**
* Data provider for the constructor providing a range of valid constructor arguments.
*
* @return array
*/
public function constructor_provider(): array {
return [
'Name without values' => [['keyword'], filter::JOINTYPE_DEFAULT, []],
'Name with valid join type ANY' => [[
'keyword',
filter::JOINTYPE_ANY,
], filter::JOINTYPE_ANY, []],
'Name with valid join type ALL' => [[
'keyword',
filter::JOINTYPE_ALL,
], filter::JOINTYPE_ALL, []],
'Name with valid join type NONE' => [[
'keyword',
filter::JOINTYPE_NONE,
], filter::JOINTYPE_NONE, []],
'Name, no join type, with set of values' => [
[
'keyword',
null,
[
's1',
'janine',
],
],
filter::JOINTYPE_DEFAULT,
[
'janine',
's1',
],
],
'Name, and ANY, with set of values' => [
[
'keyword',
filter::JOINTYPE_ANY,
[
's1',
'kevin',
'james',
'janine',
],
],
filter::JOINTYPE_ANY,
[
'james',
'janine',
'kevin',
's1',
],
],
'Name, and ANY, with set of values which contains duplicates' => [
[
'keyword',
filter::JOINTYPE_ANY,
[
's1',
'kevin',
'james',
'janine',
'kevin',
],
],
filter::JOINTYPE_ANY,
[
'james',
'janine',
'kevin',
's1',
],
],
];
}
/**
* Test that the constructor throws a relevant exception when passed an invalid join.
*
* @dataProvider constructor_invalid_join_provider
* @param mixed $jointype
*/
public function test_constructor_invalid_joins($jointype): void {
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid join type specified');
new filter('invalid', $jointype);
}
/**
* Data provider for the constructor providing a range of invalid join types to the constructor.
*
* @return array
*/
public function constructor_invalid_join_provider(): array {
return [
'Too low' => [-1],
'Too high' => [4],
];
}
/**
* Enusre that adding filter values works as expected.
*/
public function test_add_filter_value(): void {
$filter = new filter('example');
// Initially an empty list.
$this->assertEmpty($filter->get_filter_values());
// Adding null should do nothing.
$filter->add_filter_value(null);
$this->assertEmpty($filter->get_filter_values());
// Adding empty string should do nothing.
$filter->add_filter_value('');
$this->assertEmpty($filter->get_filter_values());
// Adding a value should return that value.
$filter->add_filter_value('rosie');
$this->assertSame([
'rosie',
], $filter->get_filter_values());
// Adding a second value should add that value.
// The values should sorted.
$filter->add_filter_value('arthur');
$this->assertSame([
'arthur',
'rosie',
], $filter->get_filter_values());
// Adding a duplicate value should not lead to that value being added again.
$filter->add_filter_value('arthur');
$this->assertSame([
'arthur',
'rosie',
], $filter->get_filter_values());
}
/**
* Ensure that it is possibly to set the join type.
*/
public function test_set_join_type(): void {
$filter = new filter('example');
// Initial set with the default type should just work.
// The setter should be chainable.
$this->assertEquals($filter, $filter->set_join_type(filter::JOINTYPE_DEFAULT));
$this->assertEquals(filter::JOINTYPE_DEFAULT, $filter->get_join_type());
// It should be possible to update the join type later.
$this->assertEquals($filter, $filter->set_join_type(filter::JOINTYPE_NONE));
$this->assertEquals(filter::JOINTYPE_NONE, $filter->get_join_type());
$this->assertEquals($filter, $filter->set_join_type(filter::JOINTYPE_ANY));
$this->assertEquals(filter::JOINTYPE_ANY, $filter->get_join_type());
$this->assertEquals($filter, $filter->set_join_type(filter::JOINTYPE_ALL));
$this->assertEquals(filter::JOINTYPE_ALL, $filter->get_join_type());
}
/**
* Ensure that it is not possible to provide a value out of bounds when setting the join type.
*/
public function test_set_join_type_invalid_low(): void {
$filter = new filter('example');
// Valid join types are current 0, 1, or 2.
// A value too low should be rejected.
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid join type specified");
$filter->set_join_type(-1);
}
/**
* Ensure that it is not possible to provide a value out of bounds when setting the join type.
*/
public function test_set_join_type_invalid_high(): void {
$filter = new filter('example');
// Valid join types are current 0, 1, or 2.
// A value too low should be rejected.
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid join type specified");
$filter->set_join_type(4);
}
/**
* Ensure that the name getter is callable.
*/
public function test_get_name(): void {
$filter = new filter('examplename');
$this->assertEquals('examplename', $filter->get_name());
}
/**
* Data provider for the countable tests.
*
* @return array
*/
public function filter_value_provider(): array {
return [
'Empty' => [[], 0],
'Single value' => [[10], 1],
'Single repeated value' => [[10, 10, 10, 10], 1],
'Multiple values, no repeats' => [[1, 2, 3, 4, 5], 5],
'Multiple values, including repeats' => [[1, 2, 1, 3, 1, 3, 4, 1, 5], 5],
];
}
/**
* Ensure that the filter is countable.
*
* @dataProvider filter_value_provider
* @param array $values List of context IDs
* @param int $count Expected count
*/
public function test_countable($values, $count): void {
$filter = new filter('example', null, $values);
$this->assertCount($count, $filter);
}
/**
* Ensure that the contextlist_base iterates over the set of contexts.
*/
public function test_filter_iteration(): void {
$filter = new filter('example');
// The iterator position should be at the start.
$this->assertEquals(0, $filter->key());
foreach ($filter as $filtervalue) {
// This should not be called.
$this->assertFalse(true);
}
// The iterator position should still be at the start.
$this->assertEquals(0, $filter->key());
// Adding filter values should cause the values in the Iterator to be sorted.
$filter = new filter('example');
$filter->add_filter_value(6);
$filter->add_filter_value(5);
$filter->add_filter_value(4);
$filter->add_filter_value(3);
$filter->add_filter_value(2);
// The iterator position should be at the start after adding values.
$this->assertEquals(0, $filter->key());
$foundvalues = [];
foreach ($filter as $filtervalue) {
$foundvalues[] = $filtervalue;
}
$this->assertEquals([2, 3, 4, 5, 6], $foundvalues);
// The iterator position should now be at position 5.
// The position is automatically updated prior to moving.
$this->assertEquals(5, $filter->key());
// Adding another value shoudl cause the Iterator to be re-sorted.
$filter->add_filter_value(1);
// The iterator position should be at the start after adding values.
$this->assertEquals(0, $filter->key());
$foundvalues = [];
foreach ($filter as $filtervalue) {
$foundvalues[] = $filtervalue;
}
$this->assertEquals([1, 2, 3, 4, 5, 6], $foundvalues);
// The iterator position should now be at position 6.
$this->assertEquals(6, $filter->key());
}
/**
* Tests for the count function of a filter.
*/
public function test_filter_current(): void {
$filter = new filter('example', null, [42]);
$this->assertEquals(42, $filter->current());
}
}
@@ -0,0 +1,545 @@
<?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/>.
/**
* Unit tests for core_table\local\filter\filterset.
*
* @package core_table
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
declare(strict_types=1);
namespace core_table\local\filter;
use InvalidArgumentException;
use UnexpectedValueException;
use advanced_testcase;
use moodle_exception;
/**
* Unit tests for core_table\local\filter\filterset.
*
* @package core_table
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filterset_test extends advanced_testcase {
/**
* Ensure that it is possibly to set the join type.
*/
public function test_set_join_type(): void {
$filterset = $this->get_mocked_filterset();
// Initial set with the default type should just work.
// The setter should be chainable.
$this->assertEquals($filterset, $filterset->set_join_type(filterset::JOINTYPE_DEFAULT));
$this->assertEquals(filterset::JOINTYPE_DEFAULT, $filterset->get_join_type());
// It should be possible to update the join type later.
$this->assertEquals($filterset, $filterset->set_join_type(filterset::JOINTYPE_NONE));
$this->assertEquals(filterset::JOINTYPE_NONE, $filterset->get_join_type());
$this->assertEquals($filterset, $filterset->set_join_type(filterset::JOINTYPE_ANY));
$this->assertEquals(filterset::JOINTYPE_ANY, $filterset->get_join_type());
$this->assertEquals($filterset, $filterset->set_join_type(filterset::JOINTYPE_ALL));
$this->assertEquals(filterset::JOINTYPE_ALL, $filterset->get_join_type());
}
/**
* Ensure that it is not possible to provide a value out of bounds when setting the join type.
*/
public function test_set_join_type_invalid_low(): void {
$filterset = $this->get_mocked_filterset();
// Valid join types are current 0, 1, or 2.
// A value too low should be rejected.
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid join type specified");
$filterset->set_join_type(-1);
}
/**
* Ensure that it is not possible to provide a value out of bounds when setting the join type.
*/
public function test_set_join_type_invalid_high(): void {
$filterset = $this->get_mocked_filterset();
// Valid join types are current 0, 1, or 2.
// A value too low should be rejected.
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid join type specified");
$filterset->set_join_type(4);
}
/**
* Ensure that adding filter values works as expected.
*/
public function test_add_filter_value(): void {
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
'name' => filter::class,
'species' => filter::class,
]));
// Initially an empty list.
$this->assertEmpty($filterset->get_filters());
// Test data.
$speciesfilter = new filter('species', null, ['canine']);
$namefilter = new filter('name', null, ['rosie']);
// Add a filter to the list.
$filterset->add_filter($speciesfilter);
$this->assertSame([
$speciesfilter,
], array_values($filterset->get_filters()));
// Adding a second value should add that value.
// The values should sorted.
$filterset->add_filter($namefilter);
$this->assertSame([
$namefilter,
$speciesfilter,
], array_values($filterset->get_filters()));
// Adding an existing filter again should be ignored.
$filterset->add_filter($speciesfilter);
$this->assertSame([
$namefilter,
$speciesfilter,
], array_values($filterset->get_filters()));
}
/**
* Ensure that it is possible to add a filter of a validated filter type.
*/
public function test_add_filter_validated_type(): void {
$namefilter = $this->getMockBuilder(filter::class)
->setConstructorArgs(['name'])
->onlyMethods([])
->getMock();
$namefilter->add_filter_value('rosie');
// Mock the get_optional_filters function.
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
'name' => get_class($namefilter),
]));
// Add a filter to the list.
// This is the 'name' filter.
$filterset->add_filter($namefilter);
$this->assertNull($filterset->check_validity());
}
/**
* Ensure that it is not possible to add a type which is not expected.
*/
public function test_add_filter_unexpected_key(): void {
// Mock the get_optional_filters function.
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([]));
// Add a filter to the list.
// This is the 'name' filter.
$namefilter = new filter('name');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("The filter 'name' was not recognised.");
$filterset->add_filter($namefilter);
}
/**
* Ensure that it is not possible to add a validated type where the type is incorrect.
*/
public function test_add_filter_validated_type_incorrect(): void {
$filtername = "name";
$otherfilter = $this->createMock(filter::class);
// Mock the get_optional_filters function.
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
$filtername => get_class($otherfilter),
]));
// Add a filter to the list.
// This is the 'name' filter.
$namefilter = $this->getMockBuilder(filter::class)
->onlyMethods([])
->setConstructorArgs([$filtername])
->getMock();
$actualtype = get_class($namefilter);
$requiredtype = get_class($otherfilter);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
"The filter '{$filtername}' was incorrectly specified as a {$actualtype}. It must be a {$requiredtype}."
);
$filterset->add_filter($namefilter);
}
/**
* Ensure that a filter can be added from parameters provided to a web service.
*/
public function test_add_filter_from_params(): void {
$filtername = "name";
$otherfilter = $this->getMockBuilder(filter::class)
->onlyMethods([])
->setConstructorArgs([$filtername])
->getMock();
// Mock the get_optional_filters function.
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
$filtername => get_class($otherfilter),
]));
$result = $filterset->add_filter_from_params($filtername, filter::JOINTYPE_DEFAULT, ['kevin']);
// The function is chainable.
$this->assertEquals($filterset, $result);
// Get the filter back.
$filter = $filterset->get_filter($filtername);
$this->assertEquals($filtername, $filter->get_name());
$this->assertEquals(filter::JOINTYPE_DEFAULT, $filter->get_join_type());
$this->assertEquals(['kevin'], $filter->get_filter_values());
}
/**
* Ensure that an unknown filter is not added.
*/
public function test_add_filter_from_params_unable_to_autoload(): void {
// Mock the get_optional_filters function.
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
'name' => '\\moodle\\this\\is\\a\\fake\\class\\name',
]));
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
"The filter class '\\moodle\\this\\is\\a\\fake\\class\\name' for filter 'name' could not be found."
);
$filterset->add_filter_from_params('name', filter::JOINTYPE_DEFAULT, ['kevin']);
}
/**
* Ensure that an unknown filter is not added.
*/
public function test_add_filter_from_params_invalid(): void {
$filtername = "name";
$otherfilter = $this->getMockBuilder(filter::class)
->onlyMethods([])
->setConstructorArgs([$filtername])
->getMock();
// Mock the get_optional_filters function.
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
$filtername => get_class($otherfilter),
]));
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("The filter 'unknownfilter' was not recognised.");
$filterset->add_filter_from_params('unknownfilter', filter::JOINTYPE_DEFAULT, ['kevin']);
}
/**
* Ensure that adding a different filter with a different object throws an Exception.
*/
public function test_duplicate_filter_value(): void {
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
'name' => filter::class,
'species' => filter::class,
]));
// Add a filter to the list.
// This is the 'name' filter.
$namefilter = new filter('name', null, ['rosie']);
$filterset->add_filter($namefilter);
// Add another filter to the list.
// This one has been incorrectly called the 'name' filter when it should be 'species'.
$this->expectException(UnexpectedValueException::Class);
$this->expectExceptionMessage("A filter of type 'name' has already been added. Check that you have the correct filter.");
$speciesfilter = new filter('name', null, ['canine']);
$filterset->add_filter($speciesfilter);
}
/**
* Ensure that validating a filterset correctly compares filter types.
*/
public function test_check_validity_optional_filters_not_specified(): void {
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
'name' => filter::class,
'species' => filter::class,
]));
$this->assertNull($filterset->check_validity());
}
/**
* Ensure that validating a filterset correctly requires required filters.
*/
public function test_check_validity_required_filter(): void {
$filterset = $this->get_mocked_filterset(['get_required_filters']);
$filterset->expects($this->any())
->method('get_required_filters')
->willReturn([
'name' => filter::class
]);
// Add a filter to the list.
// This is the 'name' filter.
$filterset->add_filter(new filter('name'));
$this->assertNull($filterset->check_validity());
}
/**
* Ensure that validating a filterset excepts correctly when a required fieldset is missing.
*/
public function test_check_validity_filter_missing_required(): void {
$filterset = $this->get_mocked_filterset(['get_required_filters']);
$filterset->expects($this->any())
->method('get_required_filters')
->willReturn([
'name' => filter::class,
'species' => filter::class,
]);
$this->expectException(moodle_exception::Class);
$this->expectExceptionMessage("One or more required filters were missing (name, species)");
$filterset->check_validity();
}
/**
* Ensure that getting the filters returns a sorted list of filters.
*/
public function test_get_filters(): void {
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
// Filters are not defined lexically.
'd' => filter::class,
'b' => filter::class,
'a' => filter::class,
'c' => filter::class,
]));
// Filters are added in a different non-lexical order.
$c = new filter('c');
$filterset->add_filter($c);
$b = new filter('b');
$filterset->add_filter($b);
$d = new filter('d');
$filterset->add_filter($d);
$a = new filter('a');
$filterset->add_filter($a);
// But they are returned lexically sorted.
$this->assertEquals([
'a' => $a,
'b' => $b,
'c' => $c,
'd' => $d,
], $filterset->get_filters());
}
/**
* Ensure that getting a singlel filter returns the correct filter.
*/
public function test_get_filter(): void {
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
// Filters are not defined lexically.
'd' => filter::class,
'b' => filter::class,
'a' => filter::class,
'c' => filter::class,
]));
// Filters are added in a different non-lexical order.
$c = new filter('c');
$filterset->add_filter($c);
$b = new filter('b');
$filterset->add_filter($b);
$d = new filter('d');
$filterset->add_filter($d);
$a = new filter('a');
$filterset->add_filter($a);
// Filters can be individually retrieved in any order.
$this->assertEquals($d, $filterset->get_filter('d'));
$this->assertEquals($a, $filterset->get_filter('a'));
$this->assertEquals($b, $filterset->get_filter('b'));
$this->assertEquals($c, $filterset->get_filter('c'));
}
/**
* Ensure that it is not possible to retrieve an unknown filter.
*/
public function test_get_filter_unknown(): void {
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
'a' => filter::class,
]));
$a = new filter('a');
$filterset->add_filter($a);
$this->expectException(UnexpectedValueException::Class);
$this->expectExceptionMessage("The filter specified (d) is invalid.");
$filterset->get_filter('d');
}
/**
* Ensure that it is not possible to retrieve a valid filter before it is created.
*/
public function test_get_filter_not_yet_added(): void {
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
'a' => filter::class,
]));
$this->expectException(UnexpectedValueException::Class);
$this->expectExceptionMessage("The filter specified (a) has not been created.");
$filterset->get_filter('a');
}
/**
* Ensure that the get_all_filtertypes function correctly returns the combined filterset.
*/
public function test_get_all_filtertypes(): void {
$otherfilter = $this->createMock(filter::class);
$filterset = $this->get_mocked_filterset([
'get_optional_filters',
'get_required_filters',
]);
$filterset->method('get_optional_filters')
->will($this->returnValue([
'a' => filter::class,
'c' => get_class($otherfilter),
]));
$filterset->method('get_required_filters')
->will($this->returnValue([
'b' => get_class($otherfilter),
'd' => filter::class,
]));
$this->assertEquals([
'a' => filter::class,
'b' => get_class($otherfilter),
'c' => get_class($otherfilter),
'd' => filter::class,
], $filterset->get_all_filtertypes());
}
/**
* Ensure that the get_all_filtertypes function correctly returns the combined filterset.
*/
public function test_get_all_filtertypes_conflict(): void {
$otherfilter = $this->createMock(filter::class);
$filterset = $this->get_mocked_filterset([
'get_optional_filters',
'get_required_filters',
]);
$filterset->method('get_optional_filters')
->will($this->returnValue([
'a' => filter::class,
'b' => get_class($otherfilter),
'd' => filter::class,
]));
$filterset->method('get_required_filters')
->will($this->returnValue([
'b' => get_class($otherfilter),
'c' => filter::class,
'd' => filter::class,
]));
$this->expectException(InvalidArgumentException::Class);
$this->expectExceptionMessage("Some filter types are both required, and optional: b, d");
$filterset->get_all_filtertypes();
}
/**
* Ensure that the has_filter function works as expected.
*/
public function test_has_filter(): void {
$filterset = $this->get_mocked_filterset(['get_optional_filters']);
$filterset->method('get_optional_filters')
->will($this->returnValue([
// Define filters 'a', and 'b'.
'a' => filter::class,
'b' => filter::class,
]));
// Only add filter 'a'.
$a = new filter('a');
$filterset->add_filter($a);
// Filter 'a' should exist.
$this->assertTrue($filterset->has_filter('a'));
// Filter 'b' is defined, but has not been added.
$this->assertFalse($filterset->has_filter('b'));
// Filter 'c' is not defined.
// No need to throw any kind of exception - this is an existence check.
$this->assertFalse($filterset->has_filter('c'));
}
/**
* Get a mocked copy of the filterset, mocking the specified methods.
*
* @param array $mockedmethods anonymous array containing the list of mocked methods
* @return filterset Mock of the filterset
*/
protected function get_mocked_filterset(array $mockedmethods = []): filterset {
return $this->getMockForAbstractClass(filterset::class, [], '', true, true, true, $mockedmethods);
}
}
@@ -0,0 +1,109 @@
<?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/>.
/**
* Unit tests for core_table\local\filter\filter.
*
* @package core_table
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
declare(strict_types=1);
namespace core_table\local\filter;
use advanced_testcase;
use TypeError;
/**
* Unit tests for core_table\local\filter\integer_filter.
*
* @package core_table
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class integer_filter_test extends advanced_testcase {
/**
* Ensure that the add_filter_value function works as expected with valid values.
*/
public function test_add_filter_value_int(): void {
$filter = new integer_filter('example');
// Initially an empty list.
$this->assertEmpty($filter->get_filter_values());
// Adding a value should return that value.
$filter->add_filter_value(10);
$this->assertSame([
10,
], $filter->get_filter_values());
// Adding a second value should add that value.
// The values should sorted.
$filter->add_filter_value(2);
$this->assertSame([
2,
10,
], $filter->get_filter_values());
// Adding a duplicate value should not lead to that value being added again.
$filter->add_filter_value(10);
$this->assertSame([
2,
10,
], $filter->get_filter_values());
}
/**
* Ensure that the add_filter_value function rejects invalid types.
*
* @dataProvider add_filter_value_invalid_types_provider
* @param mixed $value
* @param string $type
*/
public function test_add_filter_value_type_invalid($value, string $type): void {
$filter = new integer_filter('example');
// Adding empty string is not supported.
$this->expectException(TypeError::class);
$this->expectExceptionMessage("The value supplied was of type '{$type}'. An integer was expected.");
$filter->add_filter_value($value);
}
/**
* Data provider for add_filter_value tests with invalid types.
*
* @return array
*/
public function add_filter_value_invalid_types_provider(): array {
return [
'Null' => [null, 'NULL'],
'Empty string' => ['', 'string'],
'Filled string' => ['example', 'string'],
'Float 1.0' => [1.0, 'double'],
'Float 1.1' => [1.1, 'double'],
'bool' => [false, 'boolean'],
'array' => [[], 'array'],
'stdClass' => [(object) [], 'stdClass'],
// Note: The comparison value will be a fully-qualfied class name.
'Class' => [new filter('example'), filter::class],
];
}
}
@@ -0,0 +1,280 @@
<?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/>.
/**
* Unit tests for core_table\local\filter\numeric_comparison_filter.
*
* @package core_table
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
declare(strict_types=1);
namespace core_table\local\filter;
use advanced_testcase;
use InvalidArgumentException;
use TypeError;
/**
* Unit tests for core_table\local\filter\numeric_comparison_filter.
*
* @package core_table
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class numeric_comparison_filter_test extends advanced_testcase {
/**
* Ensure that the add_filter_value function works as expected with valid values.
*/
public function test_add_filter_value_valid(): void {
$filter = new numeric_comparison_filter('example');
// Initially an empty list.
$this->assertEmpty($filter->get_filter_values());
// Adding a value should return that value.
$filter->add_filter_value(json_encode((object) [
'direction' => '>',
'value' => 100,
]));
$this->assertEquals([
(object) [
'direction' => '>',
'value' => 100,
],
], $filter->get_filter_values());
// Adding a second value should add that value.
// The values should sorted.
$filter->add_filter_value(json_encode((object) [
'direction' => '<=',
'value' => 1000,
]));
$this->assertEquals([
(object) [
'direction' => '<=',
'value' => 1000,
],
(object) [
'direction' => '>',
'value' => 100,
],
], $filter->get_filter_values());
// Adding a duplicate value should not lead to that value being added again.
$filter->add_filter_value(json_encode((object) [
'direction' => '>',
'value' => 100,
]));
$this->assertEquals([
(object) [
'direction' => '<=',
'value' => 1000,
],
(object) [
'direction' => '>',
'value' => 100,
],
], $filter->get_filter_values());
}
/**
* Ensure that the add_filter_value function rejects invalid types.
*
* @dataProvider add_filter_value_invalid_types_provider
* @param mixed $values
* @param string $exceptiontype
* @param string $exceptionmessage
*/
public function test_add_filter_value_type_invalid($values, string $exceptiontype, string $exceptionmessage): void {
$filter = new numeric_comparison_filter('example');
// Adding empty string is not supported.
$this->expectException($exceptiontype);
$this->expectExceptionMessage($exceptionmessage);
call_user_func_array([$filter, 'add_filter_value'], $values);
}
/**
* Data provider for add_filter_value tests with invalid types.
*
* @return array
*/
public function add_filter_value_invalid_types_provider(): array {
return [
'Null' => [
[null],
TypeError::class,
"The value supplied was of type 'NULL'. A string representing a json-encoded value was expected.",
],
'Single value string' => [
[''],
InvalidArgumentException::class,
"A json-encoded object containing both a direction, and comparison value was expected.",
],
'Single value integer' => [
[42],
TypeError::class,
"The value supplied was of type 'integer'. A string representing a json-encoded value was expected.",
],
'Single value float' => [
[4.2],
TypeError::class,
"The value supplied was of type 'double'. A string representing a json-encoded value was expected.",
],
'Single value bool' => [
[false],
TypeError::class,
"The value supplied was of type 'boolean'. A string representing a json-encoded value was expected.",
],
'Single value array' => [
[[]],
TypeError::class,
"The value supplied was of type 'array'. A string representing a json-encoded value was expected.",
],
'Single value object' => [
[(object) []],
TypeError::class,
"The value supplied was of type 'stdClass'. A string representing a json-encoded value was expected.",
],
'Single value class' => [
[new filter('example')],
TypeError::class,
"The value supplied was of type '" . filter::class . "'. A string representing a json-encoded value was expected.",
],
'json-encoded single value null' => [
// Note a json-encoded null is the stringy 'null'.
[json_encode(null)],
InvalidArgumentException::class,
"A json-encoded object containing both a direction, and comparison value was expected.",
],
'json-encoded single value string' => [
[json_encode('')],
InvalidArgumentException::class,
"The value supplied was a json encoded 'string'. " .
"An object containing both a direction, and comparison value was expected.",
],
'json-encoded single value integer' => [
[json_encode(42)],
InvalidArgumentException::class,
"The value supplied was a json encoded 'string'. " .
"An object containing both a direction, and comparison value was expected.",
],
'json-encoded single value double' => [
[json_encode(4.2)],
InvalidArgumentException::class,
"The value supplied was a json encoded 'string'. " .
"An object containing both a direction, and comparison value was expected.",
],
'json-encoded single value bool' => [
[json_encode(false)],
InvalidArgumentException::class,
"The value supplied was a json encoded 'string'. " .
"An object containing both a direction, and comparison value was expected.",
],
'json-encoded single value array' => [
[json_encode([])],
InvalidArgumentException::class,
"The value supplied was a json encoded 'string'. " .
"An object containing both a direction, and comparison value was expected.",
],
'json-encoded empty object' => [
[json_encode((object) [])],
InvalidArgumentException::class,
"A 'direction' must be provided.",
],
'json-encoded single value class' => [
// A class will contain any public properties when json-encoded. It is treated in the same was a stdClass.
[json_encode(new filter('example'))],
InvalidArgumentException::class,
"A 'direction' must be provided.",
],
'Direction provided, value missing' => [
[json_encode([
'direction' => '>',
])],
InvalidArgumentException::class,
"A 'value' must be provided.",
],
'Direction invalid +' => [
[json_encode([
'direction' => '+',
'value' => 100,
])],
InvalidArgumentException::class,
"Invalid direction specified '+'."
],
'Direction invalid -' => [
[json_encode([
'direction' => '-',
'value' => 100,
])],
InvalidArgumentException::class,
"Invalid direction specified '-'."
],
'Value string' => [
[json_encode([
'direction' => '>',
'value' => "example",
])],
TypeError::class,
"The value supplied was of type 'string'. A numeric value was expected."
],
'Value bool' => [
[json_encode([
'direction' => '>',
'value' => false,
])],
TypeError::class,
"The value supplied was of type 'boolean'. A numeric value was expected."
],
'Value array' => [
[json_encode([
'direction' => '>',
'value' => [],
])],
TypeError::class,
"The value supplied was of type 'array'. A numeric value was expected."
],
'Value stdClass' => [
[json_encode([
'direction' => '>',
'value' => (object) [],
])],
TypeError::class,
"The value supplied was of type 'stdClass'. A numeric value was expected."
],
'Value class' => [
// A class will contain any public properties when json-encoded. It is treated in the same was a stdClass.
[json_encode([
'direction' => '>',
'value' => new filter('example'),
])],
TypeError::class,
"The value supplied was of type 'stdClass'. A numeric value was expected."
],
];
}
}
@@ -0,0 +1,109 @@
<?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/>.
/**
* Unit tests for core_table\local\filter\string_filter.
*
* @package core_table
* @category test
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
declare(strict_types=1);
namespace core_table\local\filter;
use advanced_testcase;
use TypeError;
/**
* Unit tests for core_table\local\filter\string_filter.
*
* @package core_table
* @category test
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class string_filter_test extends advanced_testcase {
/**
* Ensure that the add_filter_value function works as expected with valid values.
*/
public function test_add_filter_value_string(): void {
$filter = new string_filter('example');
// Initially an empty list.
$this->assertEmpty($filter->get_filter_values());
// Adding a value should return that value.
$filter->add_filter_value('apple');
$this->assertSame([
'apple',
], $filter->get_filter_values());
// Adding a second value should add that value.
// The values should sorted.
$filter->add_filter_value('pear');
$this->assertSame([
'apple',
'pear',
], $filter->get_filter_values());
// Adding a duplicate value should not lead to that value being added again.
$filter->add_filter_value('apple');
$this->assertSame([
'apple',
'pear',
], $filter->get_filter_values());
}
/**
* Ensure that the add_filter_value function rejects invalid types.
*
* @dataProvider add_filter_value_invalid_types_provider
* @param mixed $value
* @param string $type
*/
public function test_add_filter_value_type_invalid($value, string $type): void {
$filter = new string_filter('example');
// Adding empty string is not supported.
$this->expectException(TypeError::class);
$this->expectExceptionMessage("The value supplied was of type '{$type}'. A string was expected.");
$filter->add_filter_value($value);
}
/**
* Data provider for add_filter_value tests with invalid types.
*
* @return array
*/
public function add_filter_value_invalid_types_provider(): array {
return [
'Null' => [null, 'NULL'],
'1' => [1, 'integer'],
'2' => [2, 'integer'],
'Float 1.0' => [1.0, 'double'],
'Float 1.1' => [1.1, 'double'],
'bool' => [false, 'boolean'],
'array' => [[], 'array'],
'stdClass' => [(object) [], 'stdClass'],
// Note: The comparison value will be a fully-qualified class name.
'Class' => [new filter('example'), filter::class],
];
}
}