Skip to content

Commit

Permalink
MDL-74674 core: create notification generators
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonso-salces committed Jun 29, 2022
1 parent f4df13f commit a950ed0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 41 deletions.
39 changes: 39 additions & 0 deletions lib/behat/classes/behat_core_generator.php
Expand Up @@ -275,6 +275,12 @@ protected function get_creatable_entities(): array {
'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;
Expand Down Expand Up @@ -1009,6 +1015,39 @@ protected function process_setup_backpack_connected(array $data) {
$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.
*
Expand Down
30 changes: 30 additions & 0 deletions lib/behat/classes/behat_generator_base.php
Expand Up @@ -316,6 +316,36 @@ protected function get_user_id($username) {
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
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit a950ed0

Please sign in to comment.