From da16718e8b2a05ebbd0d40babe82f6374f9ef3a0 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Mon, 29 May 2023 18:04:36 +0200 Subject: [PATCH] MDL-78207 course: Add tests to cover activitybadge feature --- .../tests/output/activitybadge_test.php | 208 ++++++++++++++++++ lib/tests/modinfolib_test.php | 33 +++ 2 files changed, 241 insertions(+) create mode 100644 course/format/tests/output/activitybadge_test.php diff --git a/course/format/tests/output/activitybadge_test.php b/course/format/tests/output/activitybadge_test.php new file mode 100644 index 00000000000..3fbd428cc3b --- /dev/null +++ b/course/format/tests/output/activitybadge_test.php @@ -0,0 +1,208 @@ +. + +namespace core_courseformat\output; + +use stdClass; + +/** + * Tests for activitybadge class. + * + * @package core_courseformat + * @copyright 2023 Sara Arjona + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \core_courseformat\output\activitybadge + */ +class activitybadge_test extends \advanced_testcase { + + /** + * Test the behaviour of create_instance() and export_for_template() attributes. + * @runInSeparateProcess + * + * @covers ::export_for_template + * @covers ::create_instance + */ + public function test_activitybadge_export_for_template() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $data = $this->setup_scenario(); + + $user = $this->getDataGenerator()->create_user(['trackforums' => 1]); + $this->getDataGenerator()->enrol_user( + $user->id, + $data->course->id, + 'student' + ); + $this->setUser($user); + + $renderer = $data->renderer; + + // The activitybadge for a file with all options enabled shouldn't be empty. + $class = activitybadge::create_instance($data->fileshowtype); + $result = $class->export_for_template($renderer); + $this->check_activitybadge($result, 'TXT', 'badge-none'); + + // The activitybadge for a file with Show type option disabled should be empty. + $class = activitybadge::create_instance($data->filehidetype); + $result = $class->export_for_template($renderer); + $this->check_activitybadge($result); + + // The activitybadge for a forum with unread messages shouldn't be empty. + $class = activitybadge::create_instance($data->forumunread); + $result = $class->export_for_template($renderer); + $this->check_activitybadge($result, '1 unread post', 'badge-dark'); + + // The activitybadge for a forum without unread messages should be empty. + $class = activitybadge::create_instance($data->forumread); + $result = $class->export_for_template($renderer); + $this->check_activitybadge($result); + + // The activitybadge for an assignment should be empty. + $class = activitybadge::create_instance($data->assign); + $this->assertNull($class); + + // The activitybadge for a label should be empty. + $class = activitybadge::create_instance($data->label); + $this->assertNull($class); + } + + /** + * Setup the default scenario, creating some activities: + * - A forum with one unread message from the teacher. + * - Another forum without unread messages. + * - A file with all the appearance options enabled. + * - A file with the "Show type" option disabled. + * - An assignment. + * - A label. + * + * @return stdClass the scenario instances. + */ + private function setup_scenario(): stdClass { + global $PAGE; + + $course = $this->getDataGenerator()->create_course(['numsections' => 1]); + + // Enrol editing teacher to the course. + $teacher = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user( + $teacher->id, + $course->id, + 'editingteacher' + ); + $this->setUser($teacher); + + // Create a forum with tracking forced and add a discussion. + $record = new stdClass(); + $record->introformat = FORMAT_HTML; + $record->course = $course->id; + $record->trackingtype = FORUM_TRACKING_FORCED; + $forumread = $this->getDataGenerator()->create_module('forum', $record); + $forumunread = $this->getDataGenerator()->create_module('forum', $record); + $record = new stdClass(); + $record->course = $course->id; + $record->userid = $teacher->id; + $record->forum = $forumunread->id; + $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); + + // Create a file with all the options enabled. + $record = (object)[ + 'course' => $course->id, + 'showsize' => 1, + 'showtype' => 1, + 'showdate' => 1, + ]; + $fileshowtype = self::getDataGenerator()->create_module('resource', $record); + + // Create a file with Show type disabled. + $record = (object)[ + 'course' => $course->id, + 'showsize' => 1, + 'showtype' => 0, + 'showdate' => 1, + ]; + $filehidetype = self::getDataGenerator()->create_module('resource', $record); + + // Create an assignment and a label. + $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]); + $label = $this->getDataGenerator()->create_module('label', ['course' => $course->id]); + + rebuild_course_cache($course->id, true); + $renderer = course_get_format($course->id)->get_renderer($PAGE); + $modinfo = get_fast_modinfo($course->id); + + return (object)[ + 'course' => $course, + 'forumunread' => $modinfo->get_cm($forumunread->cmid), + 'discussion' => $discussion, + 'forumread' => $modinfo->get_cm($forumread->cmid), + 'fileshowtype' => $modinfo->get_cm($fileshowtype->cmid), + 'filehidetype' => $modinfo->get_cm($filehidetype->cmid), + 'assign' => $modinfo->get_cm($assign->cmid), + 'label' => $modinfo->get_cm($label->cmid), + 'renderer' => $renderer, + ]; + } + + /** + * Method to check if the result of the export_from_template is the expected. + * + * @param stdClass $result The result of the export_from_template() call. + * @param string|null $content The expected activitybadge content. + * @param string|null $style The expected activitybadge style. + * @param string|null $url The expected activitybadge url. + * @param string|null $elementid The expected activitybadge element id. + * @param array|null $extra The expected activitybadge extra attributes. + */ + private function check_activitybadge( + stdClass $result, + ?string $content = null, + ?string $style = null, + ?string $url = null, + ?string $elementid = null, + ?array $extra = null + ): void { + if (is_null($content)) { + $this->assertObjectNotHasAttribute('badgecontent', $result); + } else { + $this->assertEquals($content, $result->badgecontent); + } + + if (is_null($style)) { + $this->assertObjectNotHasAttribute('badgestyle', $result); + } else { + $this->assertEquals($style, $result->badgestyle); + } + + if (is_null($url)) { + $this->assertObjectNotHasAttribute('badgeurl', $result); + } else { + $this->assertEquals($url, $result->badgeurl); + } + + if (is_null($elementid)) { + $this->assertObjectNotHasAttribute('badgeelementid', $result); + } else { + $this->assertEquals($elementid, $result->badgeelementid); + } + + if (is_null($extra)) { + $this->assertObjectNotHasAttribute('badgeextraattributes', $result); + } else { + $this->assertEquals($extra, $result->badgeextraattributes); + } + } +} diff --git a/lib/tests/modinfolib_test.php b/lib/tests/modinfolib_test.php index f99049a1d1f..a0c20cc5c56 100644 --- a/lib/tests/modinfolib_test.php +++ b/lib/tests/modinfolib_test.php @@ -583,6 +583,39 @@ class modinfolib_test extends advanced_testcase { } + /** + * Tests for function cm_info::get_activitybadge(). + * + * @covers \cm_info::get_activitybadge + */ + public function test_cm_info_get_activitybadge(): void { + global $PAGE; + + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); + $resource = $this->getDataGenerator()->create_module('resource', ['course' => $course->id]); + $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]); + $label = $this->getDataGenerator()->create_module('label', ['course' => $course->id]); + + $renderer = $PAGE->get_renderer('core'); + $modinfo = get_fast_modinfo($course->id); + + // Forum and resource implements the activitybadge feature. + $cminfo = $modinfo->get_cm($forum->cmid); + $this->assertNotNull($cminfo->get_activitybadge($renderer)); + $cminfo = $modinfo->get_cm($resource->cmid); + $this->assertNotNull($cminfo->get_activitybadge($renderer)); + + // Assign and label don't implement the activitybadge feature (at least for now). + $cminfo = $modinfo->get_cm($assign->cmid); + $this->assertNull($cminfo->get_activitybadge($renderer)); + $cminfo = $modinfo->get_cm($label->cmid); + $this->assertNull($cminfo->get_activitybadge($renderer)); + } + /** * Tests the availability property that has been added to course modules * and sections (just to see that it is correctly saved and accessed).