From 96b7a7d06690c04947aab9b64747d9b760acc0a9 Mon Sep 17 00:00:00 2001 From: Alfonso Salces Date: Tue, 12 Apr 2022 12:26:59 +0200 Subject: [PATCH] MDL-74674 core: create notification generators --- lib/behat/classes/behat_core_generator.php | 39 ++++++++++++ lib/behat/classes/behat_generator_base.php | 30 +++++++++ .../behat/notification_popover_unread.feature | 62 +++++++------------ 3 files changed, 90 insertions(+), 41 deletions(-) diff --git a/lib/behat/classes/behat_core_generator.php b/lib/behat/classes/behat_core_generator.php index 02ad43e3730..2b4a8c3293e 100644 --- a/lib/behat/classes/behat_core_generator.php +++ b/lib/behat/classes/behat_core_generator.php @@ -287,6 +287,12 @@ class behat_core_generator extends behat_generator_base { 'required' => ['user', 'course', 'lastaccess'], 'switchids' => ['user' => 'userid', 'course' => 'courseid'], ], + 'notifications' => [ + 'singular' => 'notification', + 'datagenerator' => 'notification', + 'required' => ['subject', 'userfrom', 'userto'], + 'switchids' => ['userfrom' => 'userfromid', 'userto' => 'usertoid'], + ], ]; return $entities; @@ -1040,6 +1046,39 @@ class behat_core_generator extends behat_generator_base { $DB->insert_record('badge_backpack', $backpack); } + /** + * Creates notifications to specific user. + * + * @param array $data + * @return void + */ + protected function process_notification(array $data) { + global $DB; + + $notification = new stdClass(); + $notification->useridfrom = $data['userfromid']; + $notification->useridto = $data['usertoid']; + $notification->subject = $data['subject']; + $notification->fullmessage = $data['subject'] . ' description'; + $notification->smallmessage = $data['subject'] . ' description'; + $notification->fullmessagehtml = $data['subject'] . ' description'; + + if ($data['timecreated'] !== 'null') { + $notification->timecreated = $data['timecreated']; + } + + if ($data['timeread'] !== 'null') { + $notification->timeread = $data['timeread']; + } + + if (!empty($data)) { + $popupnotification = new stdClass(); + $popupnotification->notificationid = $DB->insert_record('notifications', $notification); + $DB->insert_record('message_popup_notifications', $popupnotification); + } + + } + /** * Creates user last access data within given courses. * diff --git a/lib/behat/classes/behat_generator_base.php b/lib/behat/classes/behat_generator_base.php index 4cfa6382bd2..ffeb22b6d99 100644 --- a/lib/behat/classes/behat_generator_base.php +++ b/lib/behat/classes/behat_generator_base.php @@ -316,6 +316,36 @@ abstract class behat_generator_base { return $id; } + /** + * Gets the user id from it's username. + * @throws Exception + * @param string $username + * @return int + */ + protected function get_userfrom_id(string $username) { + global $DB; + + if (!$id = $DB->get_field('user', 'id', ['username' => $username])) { + throw new Exception('The specified user with username "' . $username . '" does not exist'); + } + return $id; + } + + /** + * Gets the user id from it's username. + * @throws Exception + * @param string $username + * @return int + */ + protected function get_userto_id(string $username) { + global $DB; + + if (!$id = $DB->get_field('user', 'id', ['username' => $username])) { + throw new Exception('The specified user with username "' . $username . '" does not exist'); + } + return $id; + } + /** * Gets the role id from it's shortname. * @throws Exception diff --git a/message/output/popup/tests/behat/notification_popover_unread.feature b/message/output/popup/tests/behat/notification_popover_unread.feature index 0fbec12e492..3f986903a2c 100644 --- a/message/output/popup/tests/behat/notification_popover_unread.feature +++ b/message/output/popup/tests/behat/notification_popover_unread.feature @@ -5,61 +5,41 @@ Feature: Notification popover unread notifications I am notified about relevant events in Moodle Background: - # This will make sure popup notifications are enabled and create - # two assignment notifications. One for the student submitting their - # assignment and another for the teacher grading it. - Given the following "courses" exist: - | fullname | shortname | category | groupmode | - | Course 1 | C1 | 0 | 1 | - # Make sure the popup notifications are enabled for assignments. - And the following config values are set as admin: - | popup_provider_mod_assign_assign_notification_permitted | permitted | message | - | message_provider_mod_assign_assign_notification_loggedin | popup | message | - | message_provider_mod_assign_assign_notification_loggedoff | popup | message | - And the following "users" exist: + Given the following "users" exist: | username | firstname | lastname | email | - | teacher1 | Teacher | 1 | teacher1@example.com | | student1 | Student | 1 | student1@example.com | - And the following "course enrolments" exist: - | user | course | role | - | teacher1 | C1 | editingteacher | - | student1 | C1 | student | - And the following "activity" exists: - | activity | assign | - | course | C1 | - | name | Test assignment name | - | assignsubmission_onlinetext_enabled | 1 | - | assignsubmission_file_enabled | 0 | - | submissiondrafts | 0 | - # This should generate a notification. - And the following "mod_assign > submissions" exist: - | assign | user | onlinetext | - | Test assignment name | student1 | I'm the student1 submission | + | student2 | Student | 2 | student2@example.com | + # This should generate some notifications + And the following "notifications" exist: + | subject | userfrom | userto | timecreated | timeread | + | Test 01 | student2 | student1 | 1654587996 | null | + | Test 02 | student2 | student1 | 1654587997 | null | Scenario: Notification popover shows correct unread count - When I log in as "student1" + Given I log in as "student1" # Confirm the popover is saying 1 unread notifications. - Then I should see "1" in the "#nav-notification-popover-container [data-region='count-container']" "css_element" + And I should see "2" in the "#nav-notification-popover-container [data-region='count-container']" "css_element" # Open the popover. - And I open the notification popover - # Confirm the submission notification is visible. - And I should see "You have submitted your assignment submission for Test assignment name" in the "#nav-notification-popover-container" "css_element" + When I open the notification popover + # Confirm the notifications are visible. + Then I should see "Test 01" in the "#nav-notification-popover-container" "css_element" + And I should see "Test 02" in the "#nav-notification-popover-container" "css_element" @_bug_phantomjs Scenario: Clicking a notification marks it as read - When I log in as "student1" - # Open the popover. + Given I log in as "student1" + # Open the notifications. + When I open the notification popover + And I follow "Test 01" And I open the notification popover - # Click on the submission notification. - And I follow "You have submitted your assignment submission for Test assignment name" + And I follow "Test 02" + # Confirm the count element is hidden (i.e. there are no unread notifications). Then "[data-region='count-container']" "css_element" in the "#nav-notification-popover-container" "css_element" should not be visible Scenario: Mark all notifications as read - When I log in as "student1" - # Open the popover. - And I open the notification popover - # Click the mark all as read button. + Given I log in as "student1" + When I open the notification popover And I click on "Mark all as read" "link" in the "#nav-notification-popover-container" "css_element" # Refresh the page to make sure we send a new request for the unread count. And I reload the page