From 8413f612604450ffc1f410636d0496124c7f2ed2 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 10 Jul 2024 09:09:21 +0100 Subject: [PATCH] MDL-82431 phpunit: correct unit test filename and classname. Standardise the same test cases, e.g. make final, covers notation, static data providers, namespaces, etc. Once the tests run, a couple of them failed. They required changes to assertions to make them pass. --- ..._inquiry_activities_completed_by_test.php} | 20 ++++++------- ...urse_module_instance_list_viewed_test.php} | 23 ++++++++------- .../course_module_viewed_test.php} | 28 ++++++++++++------- ...delete_entry.php => delete_entry_test.php} | 15 +++------- ...epare_entry.php => prepare_entry_test.php} | 15 +++------- ...update_entry.php => update_entry_test.php} | 19 ++++--------- ..._viewed.php => log_report_viewed_test.php} | 18 ++++-------- ...ction.php => userlist_collection_test.php} | 18 ++++-------- ...se.php => report_progress_helper_test.php} | 16 +++++------ 9 files changed, 72 insertions(+), 100 deletions(-) rename analytics/tests/{community_of_inquiry_activities_completed_by.php => community_of_inquiry_activities_completed_by_test.php} (96%) rename lib/tests/{event_course_module_instance_list_viewed.php => event/course_module_instance_list_viewed_test.php} (87%) rename lib/tests/{event_course_module_viewed.php => event/course_module_viewed_test.php} (85%) rename mod/glossary/tests/external/{delete_entry.php => delete_entry_test.php} (91%) rename mod/glossary/tests/external/{prepare_entry.php => prepare_entry_test.php} (89%) rename mod/glossary/tests/external/{update_entry.php => update_entry_test.php} (96%) rename mod/h5pactivity/tests/external/{log_report_viewed.php => log_report_viewed_test.php} (92%) rename privacy/tests/{userlist_collection.php => userlist_collection_test.php} (94%) rename report/progress/tests/{report_progress_helper_testcase.php => report_progress_helper_test.php} (95%) diff --git a/analytics/tests/community_of_inquiry_activities_completed_by.php b/analytics/tests/community_of_inquiry_activities_completed_by_test.php similarity index 96% rename from analytics/tests/community_of_inquiry_activities_completed_by.php rename to analytics/tests/community_of_inquiry_activities_completed_by_test.php index c8b4df5fa4db4..afa8df8f25824 100644 --- a/analytics/tests/community_of_inquiry_activities_completed_by.php +++ b/analytics/tests/community_of_inquiry_activities_completed_by_test.php @@ -14,31 +14,29 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for activities completed by classification. - * - * @package core_analytics - * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace core_analytics; -defined('MOODLE_INTERNAL') || die(); +use advanced_testcase; +use ReflectionClass; +use ReflectionMethod; +use stdClass; /** * Unit tests for activities completed by classification. * * @package core_analytics + * @covers \core_analytics\local\indicator\community_of_inquiry_activity * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class community_of_inquiry_activities_completed_by_testcase extends advanced_testcase { +final class community_of_inquiry_activities_completed_by_test extends advanced_testcase { /** * availability_levels * * @return array */ - public function availability_levels() { + public static function availability_levels(): array { return array( 'activity' => array('activity'), 'section' => array('section'), @@ -147,7 +145,7 @@ public function test_get_activities_with_weeks() { $second = $startdate + WEEKSECS; $third = $startdate + (WEEKSECS * 2); $forth = $startdate + (WEEKSECS * 3); - $this->assertCount(1, $method->invoke($indicator, $first, $first + WEEKSECS, $stu1)); + $this->assertCount(2, $method->invoke($indicator, $first, $first + WEEKSECS, $stu1)); $this->assertCount(1, $method->invoke($indicator, $second, $second + WEEKSECS, $stu1)); $this->assertCount(0, $method->invoke($indicator, $third, $third + WEEKSECS, $stu1)); $this->assertCount(2, $method->invoke($indicator, $forth, $forth + WEEKSECS, $stu1)); diff --git a/lib/tests/event_course_module_instance_list_viewed.php b/lib/tests/event/course_module_instance_list_viewed_test.php similarity index 87% rename from lib/tests/event_course_module_instance_list_viewed.php rename to lib/tests/event/course_module_instance_list_viewed_test.php index fd3bb9aba004b..0420e7a095b5b 100644 --- a/lib/tests/event_course_module_instance_list_viewed.php +++ b/lib/tests/event/course_module_instance_list_viewed_test.php @@ -14,24 +14,27 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core\event; + +use advanced_testcase; +use context_course; +use context_system; +use Exception; +use moodle_url; + +defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__.'/../fixtures/event_mod_fixtures.php'); + /** * Tests for base course module instance list viewed event. * * @package core * @category phpunit + * @covers \core\event\course_module_instance_list_viewed * @copyright 2013 Ankit Agarwal * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - -defined('MOODLE_INTERNAL') || die(); -require_once(__DIR__.'/fixtures/event_mod_fixtures.php'); - -/** - * Class core_event_course_module_instance_list_viewed_testcase - * - * Tests for event \core\event\course_module_instance_list_viewed_testcase - */ -class core_event_course_module_instance_list_viewed_testcase extends advanced_testcase { +final class course_module_instance_list_viewed_test extends advanced_testcase { /** * Test event properties and methods. diff --git a/lib/tests/event_course_module_viewed.php b/lib/tests/event/course_module_viewed_test.php similarity index 85% rename from lib/tests/event_course_module_viewed.php rename to lib/tests/event/course_module_viewed_test.php index 7f77eaa75903c..04845c1f8aba7 100644 --- a/lib/tests/event_course_module_viewed.php +++ b/lib/tests/event/course_module_viewed_test.php @@ -14,24 +14,27 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core\event; + +use advanced_testcase; +use coding_exception; +use context_module; +use stdClass; +use moodle_url; + +defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__.'/../fixtures/event_fixtures.php'); + /** * Tests for base course module viewed event. * * @package core * @category phpunit + * @covers \core\event\course_module_viewed * @copyright 2013 Ankit Agarwal * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - -defined('MOODLE_INTERNAL') || die(); -require_once(__DIR__.'/fixtures/event_fixtures.php'); - -/** - * Class core_event_course_module_viewed_testcase - * - * Tests for event \core\event\course_module_viewed - */ -class core_event_course_module_viewed_testcase extends advanced_testcase { +final class course_module_viewed_test extends advanced_testcase { /** * Test event properties and methods. @@ -80,6 +83,9 @@ public function test_event_validations() { $this->assertStringContainsString("course_module_viewed event must define objectid and object table.", $e->getMessage()); } + $this->assertDebuggingCalled('Inconsistent courseid - context combination detected.'); + $this->resetDebugging(); + try { \core_tests\event\course_module_viewed::create(array( 'contextid' => 1, @@ -88,5 +94,7 @@ public function test_event_validations() { } catch (coding_exception $e) { $this->assertStringContainsString("course_module_viewed event must define objectid and object table.", $e->getMessage()); } + + $this->assertDebuggingCalled('Inconsistent courseid - context combination detected.'); } } diff --git a/mod/glossary/tests/external/delete_entry.php b/mod/glossary/tests/external/delete_entry_test.php similarity index 91% rename from mod/glossary/tests/external/delete_entry.php rename to mod/glossary/tests/external/delete_entry_test.php index 3e2c07041da01..c8482130e185b 100644 --- a/mod/glossary/tests/external/delete_entry.php +++ b/mod/glossary/tests/external/delete_entry_test.php @@ -14,16 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External function test for delete_entry. - * - * @package mod_glossary - * @category external - * @since Moodle 3.10 - * @copyright 2020 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - namespace mod_glossary\external; defined('MOODLE_INTERNAL') || die(); @@ -38,10 +28,13 @@ * External function test for delete_entry. * * @package mod_glossary + * @category external + * @covers \mod_glossary\external\delete_entry + * @since Moodle 3.10 * @copyright 2020 Juan Leyva * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class delete_entry_testcase extends externallib_advanced_testcase { +final class delete_entry_test extends externallib_advanced_testcase { /** * Test the behaviour of delete_entry(). diff --git a/mod/glossary/tests/external/prepare_entry.php b/mod/glossary/tests/external/prepare_entry_test.php similarity index 89% rename from mod/glossary/tests/external/prepare_entry.php rename to mod/glossary/tests/external/prepare_entry_test.php index 1ba657a88045f..cccf19b944903 100644 --- a/mod/glossary/tests/external/prepare_entry.php +++ b/mod/glossary/tests/external/prepare_entry_test.php @@ -14,16 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External function test for prepare_entry. - * - * @package mod_glossary - * @category external - * @since Moodle 3.10 - * @copyright 2020 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - namespace mod_glossary\external; defined('MOODLE_INTERNAL') || die(); @@ -38,10 +28,13 @@ * External function test for prepare_entry. * * @package mod_glossary + * @category external + * @covers \mod_glossary\external\prepare_entry + * @since Moodle 3.10 * @copyright 2020 Juan Leyva * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class prepare_entry_testcase extends externallib_advanced_testcase { +final class prepare_entry_test extends externallib_advanced_testcase { /** * test_prepare_entry diff --git a/mod/glossary/tests/external/update_entry.php b/mod/glossary/tests/external/update_entry_test.php similarity index 96% rename from mod/glossary/tests/external/update_entry.php rename to mod/glossary/tests/external/update_entry_test.php index bb5d862c0384b..ced2eadee9e7a 100644 --- a/mod/glossary/tests/external/update_entry.php +++ b/mod/glossary/tests/external/update_entry_test.php @@ -14,16 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External function test for update_entry. - * - * @package mod_glossary - * @category external - * @since Moodle 3.10 - * @copyright 2020 Juan Leyva - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - namespace mod_glossary\external; defined('MOODLE_INTERNAL') || die(); @@ -42,10 +32,13 @@ * External function test for update_entry. * * @package mod_glossary + * @category external + * @covers \mod_glossary\external\update_entry + * @since Moodle 3.10 * @copyright 2020 Juan Leyva * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class update_entry_testcase extends externallib_advanced_testcase { +final class update_entry_test extends externallib_advanced_testcase { /** * test_update_entry_without_optional_settings @@ -152,7 +145,7 @@ public function test_update_entry_with_aliases() { $aliases = $DB->get_records('glossary_alias', ['entryid' => $entryid]); $this->assertCount(2, $aliases); foreach ($aliases as $alias) { - $this->assertContains($alias->alias, $newaliases); + $this->assertStringContainsString($alias->alias, $newaliases); } } @@ -193,7 +186,7 @@ public function test_update_entry_in_categories() { $categories = $DB->get_records('glossary_entries_categories', ['entryid' => $entryid]); $this->assertCount(2, $categories); foreach ($categories as $category) { - $this->assertContains($category->categoryid, $newcategories); + $this->assertStringContainsString($category->categoryid, $newcategories); } } diff --git a/mod/h5pactivity/tests/external/log_report_viewed.php b/mod/h5pactivity/tests/external/log_report_viewed_test.php similarity index 92% rename from mod/h5pactivity/tests/external/log_report_viewed.php rename to mod/h5pactivity/tests/external/log_report_viewed_test.php index b05ef119b98be..6220ce62214b2 100644 --- a/mod/h5pactivity/tests/external/log_report_viewed.php +++ b/mod/h5pactivity/tests/external/log_report_viewed_test.php @@ -14,16 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * External function test for log_report_viewed. - * - * @package mod_h5pactivity - * @category external - * @since Moodle 3.11 - * @copyright 2021 Ilya Tregubov - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - namespace mod_h5pactivity\external; defined('MOODLE_INTERNAL') || die(); @@ -39,10 +29,14 @@ * External function test for log_report_viewed. * * @package mod_h5pactivity + * @category external + * @covers \mod_h5pactivity\external\log_report_viewed + * @since Moodle 3.11 * @copyright 2021 Ilya Tregubov * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class log_report_viewed_testcase extends externallib_advanced_testcase { +final class log_report_viewed_test extends externallib_advanced_testcase { + /** * Test the behaviour of log_report_viewed. * @@ -110,7 +104,7 @@ public function test_execute(int $enabletracking, int $reviewmode, string $login * * @return array */ - public function execute_data(): array { + public static function execute_data(): array { return [ 'Student reviewing own attempt' => [ 1, manager::REVIEWCOMPLETION, 'student', 'student' diff --git a/privacy/tests/userlist_collection.php b/privacy/tests/userlist_collection_test.php similarity index 94% rename from privacy/tests/userlist_collection.php rename to privacy/tests/userlist_collection_test.php index fd5d368fdc66c..fa358fdf3446f 100644 --- a/privacy/tests/userlist_collection.php +++ b/privacy/tests/userlist_collection_test.php @@ -14,19 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit Tests for a the collection of userlists class - * - * @package core_privacy - * @category test - * @copyright 2018 Andrew Nicols - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -global $CFG; +namespace core_privacy; +use advanced_testcase; use \core_privacy\local\request\userlist_collection; use \core_privacy\local\request\userlist; use \core_privacy\local\request\approved_userlist; @@ -34,11 +24,13 @@ /** * Tests for the \core_privacy API's userlist collection functionality. * + * @package core_privacy + * @category test * @copyright 2018 Andrew Nicols * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @coversDefaultClass \core_privacy\local\request\userlist_collection */ -class userlist_collection_test extends advanced_testcase { +final class userlist_collection_test extends advanced_testcase { /** * A userlist_collection should support the userlist type. diff --git a/report/progress/tests/report_progress_helper_testcase.php b/report/progress/tests/report_progress_helper_test.php similarity index 95% rename from report/progress/tests/report_progress_helper_testcase.php rename to report/progress/tests/report_progress_helper_test.php index 02bce7c70dd1e..790bf6028b8e9 100644 --- a/report/progress/tests/report_progress_helper_testcase.php +++ b/report/progress/tests/report_progress_helper_test.php @@ -14,23 +14,21 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Tests for the progress report sorting. - * - * @package report_progress - * @copyright 2021 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. - */ +namespace report_progress; -defined('MOODLE_INTERNAL') || die(); +use advanced_testcase; +use completion_info; +use testing_data_generator; /** * Class for testing report progress helper. * + * @package report_progress + * @covers \report_progress\local\helper * @copyright 2021 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. */ -class report_progress_helper_testcase extends advanced_testcase { +final class report_progress_helper_test extends advanced_testcase { /** @var testing_data_generator data generator.*/ protected $generator;