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
+126
View File
@@ -0,0 +1,126 @@
<?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/>.
namespace message_popup;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
/**
* Test message popup API.
*
* @package message_popup
* @category test
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api_test extends \advanced_testcase {
use \message_popup_test_helper;
/** @var \phpunit_message_sink message redirection. */
public $messagesink;
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp(): void {
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$this->messagesink = $this->redirectMessages();
$this->resetAfterTest();
}
/**
* Test that the get_popup_notifications function will return the correct notifications.
*/
public function test_message_get_popup_notifications(): void {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
$notifications = \message_popup\api::get_popup_notifications($recipient->id);
$this->assertEquals($notifications[0]->fullmessage, 'Message 5');
$this->assertEquals($notifications[1]->fullmessage, 'Message 4');
$this->assertEquals($notifications[2]->fullmessage, 'Message 3');
$this->assertEquals($notifications[3]->fullmessage, 'Message 2');
$this->assertEquals($notifications[4]->fullmessage, 'Message 1');
}
/**
* Test that the get_popup_notifications function works correctly with limiting and offsetting
* the result set if requested.
*/
public function test_message_get_popup_notifications_all_limit_and_offset(): void {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
$this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
$this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5);
$notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 0);
$this->assertEquals($notifications[0]->fullmessage, 'Message 6');
$this->assertEquals($notifications[1]->fullmessage, 'Message 5');
$notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 2);
$this->assertEquals($notifications[0]->fullmessage, 'Message 4');
$this->assertEquals($notifications[1]->fullmessage, 'Message 3');
$notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 0, 3);
$this->assertEquals($notifications[0]->fullmessage, 'Message 3');
$this->assertEquals($notifications[1]->fullmessage, 'Message 2');
$this->assertEquals($notifications[2]->fullmessage, 'Message 1');
}
/**
* Test count_unread_popup_notifications.
*/
public function test_message_count_unread_popup_notifications(): void {
$sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
$recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
$this->send_fake_unread_popup_notification($sender1, $recipient1);
$this->send_fake_unread_popup_notification($sender1, $recipient1);
$this->send_fake_unread_popup_notification($sender2, $recipient1);
$this->send_fake_unread_popup_notification($sender1, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->send_fake_unread_popup_notification($sender2, $recipient2);
$this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient1->id), 3);
$this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient2->id), 5);
}
}
+84
View File
@@ -0,0 +1,84 @@
<?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/>.
/**
* Base trait for message popup tests.
*
* @package message_popup
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use \core_message\tests\helper as testhelper;
trait message_popup_test_helper {
/**
* Send a fake unread popup notification.
*
* {@link message_send()} does not support transaction, this function will simulate a message
* sent from a user to another. We should stop using it once {@link message_send()} will support
* transactions. This is not clean at all, this is just used to add rows to the table.
*
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $timecreated time the message was created.
* @return int the id of the message
*/
protected function send_fake_unread_popup_notification(\stdClass $userfrom, \stdClass $userto,
string $message = 'Hello world!', int $timecreated = 0): int {
global $DB;
$id = testhelper::send_fake_unread_notification($userfrom, $userto, $message, $timecreated);
$popup = new stdClass();
$popup->notificationid = $id;
$DB->insert_record('message_popup_notifications', $popup);
return $id;
}
/**
* Send a fake read popup notification.
*
* {@link message_send()} does not support transaction, this function will simulate a message
* sent from a user to another. We should stop using it once {@link message_send()} will support
* transactions. This is not clean at all, this is just used to add rows to the table.
*
* @param stdClass $userfrom user object of the one sending the message.
* @param stdClass $userto user object of the one receiving the message.
* @param string $message message to send.
* @param int $timecreated time the message was created.
* @param int $timeread the the message was read
* @return int the id of the message
*/
protected function send_fake_read_popup_notification(\stdClass $userfrom, \stdClass $userto, string $message = 'Hello world!',
int $timecreated = 0, int $timeread = 0): int {
global $DB;
$id = testhelper::send_fake_read_notification($userfrom, $userto, $message, $timecreated, $timeread);
$popup = new stdClass();
$popup->notificationid = $id;
$DB->insert_record('message_popup_notifications', $popup);
return $id;
}
}
@@ -0,0 +1,66 @@
<?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/>.
/**
* Behat message popup related steps definitions.
*
* @package message_popup
* @category test
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
/**
* Message popup steps definitions.
*
* @package message_popup
* @category test
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_message_popup extends behat_base {
/**
* Open the notification popover in the nav bar.
*
* @Given /^I open the notification popover$/
*/
public function i_open_the_notification_popover() {
$this->execute('behat_general::i_click_on',
array("#nav-notification-popover-container [data-region='popover-region-toggle']", 'css_element'));
$node = $this->get_selected_node('css_element',
'#nav-notification-popover-container [data-region="popover-region-content"]');
$this->ensure_node_is_visible($node);
}
/**
* Open the message popover in the nav bar.
*
* @Given /^I open the message popover$/
*/
public function i_open_the_message_popover() {
$this->execute('behat_general::i_click_on',
array("#nav-message-popover-container [data-region='popover-region-toggle']", 'css_element'));
$node = $this->get_selected_node('css_element', '#nav-message-popover-container [data-region="popover-region-content"]');
$this->ensure_node_is_visible($node);
}
}
@@ -0,0 +1,17 @@
@core_message @message_popup
Feature: Notification popover preferences
In order to modify my notification preferences
As a user
I can navigate to the preferences page from the popover
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| user1 | User | 1 | user1@example.com |
@javascript
Scenario: User navigates to preferences page
Given I log in as "user1"
And I open the notification popover
When I follow "Notification preferences"
Then I should see "Notification preferences"
@@ -0,0 +1,47 @@
@core_message @message_popup @javascript
Feature: Notification popover unread notifications
In order to be kept informed
As a user
I am notified about relevant events in Moodle
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
| 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
Given I log in as "student1"
# Confirm the popover is saying 1 unread notifications.
And I should see "2" in the "#nav-notification-popover-container [data-region='count-container']" "css_element"
# Open the popover.
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
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
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
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
# 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
@@ -0,0 +1,198 @@
<?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/>.
namespace message_popup;
use message_popup_external;
use message_popup_test_helper;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/message/output/popup/externallib.php');
require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
/**
* Class for external message popup functions unit tests.
*
* @package message_popup
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class externallib_test extends \advanced_testcase {
use message_popup_test_helper;
/** @var \phpunit_message_sink message redirection. */
public $messagesink;
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp(): void {
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$this->messagesink = $this->redirectMessages();
$this->resetAfterTest();
}
/**
* Test that get_popup_notifications throws an exception if the user
* doesn't exist.
*/
public function test_get_popup_notifications_no_user_exception(): void {
$this->resetAfterTest(true);
$this->expectException('moodle_exception');
$result = message_popup_external::get_popup_notifications(-2132131, false, 0, 0);
}
/**
* get_popup_notifications should throw exception if user isn't logged in
* user.
*/
public function test_get_popup_notifications_access_denied_exception(): void {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException('moodle_exception');
$result = message_popup_external::get_popup_notifications($sender->id, false, 0, 0);
}
/**
* get_popup_notifications should return notifications for the recipient.
*/
public function test_get_popup_notifications_as_recipient(): void {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
$notificationids = array(
$this->send_fake_unread_popup_notification($sender, $recipient),
$this->send_fake_unread_popup_notification($sender, $recipient),
$this->send_fake_read_popup_notification($sender, $recipient),
$this->send_fake_read_popup_notification($sender, $recipient),
);
// Confirm that admin has super powers to retrieve any notifications.
$this->setAdminUser();
$result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
$this->assertCount(4, $result['notifications']);
// Check we receive custom data as a unserialisable json.
$found = 0;
foreach ($result['notifications'] as $notification) {
if (!empty($notification->customdata)) {
$this->assertObjectHasProperty('datakey', json_decode($notification->customdata));
$found++;
}
}
$this->assertEquals(2, $found);
$this->setUser($recipient);
$result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
$this->assertCount(4, $result['notifications']);
}
/**
* get_popup_notifications result set should work with limit and offset.
*/
public function test_get_popup_notification_limit_offset(): void {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
$this->setUser($recipient);
$notificationids = array(
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 1', 1),
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 2', 2),
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 3', 3),
$this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 4', 4),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 5', 5),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 6', 6),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 7', 7),
$this->send_fake_read_popup_notification($sender, $recipient, 'Notification 8', 8),
);
$result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 0);
$this->assertEquals($result['notifications'][0]->id, $notificationids[7]);
$this->assertEquals($result['notifications'][1]->id, $notificationids[6]);
$result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 2);
$this->assertEquals($result['notifications'][0]->id, $notificationids[5]);
$this->assertEquals($result['notifications'][1]->id, $notificationids[4]);
}
/**
* get_unread_popup_notification should throw an exception for an invalid user.
*/
public function test_get_unread_popup_notification_count_invalid_user_exception(): void {
$this->resetAfterTest(true);
$this->expectException('moodle_exception');
$result = message_popup_external::get_unread_popup_notification_count(-2132131, 0);
}
/**
* get_unread_popup_notification_count should throw exception if being requested for
* non-logged in user.
*/
public function test_get_unread_popup_notification_count_access_denied_exception(): void {
$this->resetAfterTest(true);
$sender = $this->getDataGenerator()->create_user();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$this->expectException('moodle_exception');
$result = message_popup_external::get_unread_popup_notification_count($sender->id, 0);
}
/**
* Test get_unread_popup_notification_count.
*/
public function test_get_unread_popup_notification_count(): void {
$this->resetAfterTest(true);
$sender1 = $this->getDataGenerator()->create_user();
$sender2 = $this->getDataGenerator()->create_user();
$sender3 = $this->getDataGenerator()->create_user();
$recipient = $this->getDataGenerator()->create_user();
$this->setUser($recipient);
$notificationids = array(
$this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
$this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
$this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
$this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
$this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
$this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
);
$count = message_popup_external::get_unread_popup_notification_count($recipient->id);
$this->assertEquals($count, 6);
}
}
@@ -0,0 +1,101 @@
<?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/>.
namespace message_popup;
use core\task\messaging_cleanup_task;
use message_popup_test_helper;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
/**
* Test class
*
* @package message_popup
* @category test
* @copyright 2020 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class messaging_cleanup_test extends \advanced_testcase {
// Helper trait for sending fake popup notifications.
use message_popup_test_helper;
/**
* Test that all popup notifications are cleaned up
*
* @return void
*/
public function test_cleanup_all_notifications(): void {
global $DB;
$this->resetAfterTest();
$userfrom = $this->getDataGenerator()->create_user();
$userto = $this->getDataGenerator()->create_user();
$now = time();
$this->send_fake_unread_popup_notification($userfrom, $userto, 'Message 1', $now - 10);
$notificationid = $this->send_fake_unread_popup_notification($userfrom, $userto, 'Message 2', $now);
// Sanity check.
$this->assertEquals(2, $DB->count_records('message_popup_notifications'));
// Delete all notifications >5 seconds old.
set_config('messagingdeleteallnotificationsdelay', 5);
(new messaging_cleanup_task())->execute();
// We should have just one record now, matching the second notification we sent.
$records = $DB->get_records('message_popup_notifications');
$this->assertCount(1, $records);
$this->assertEquals($notificationid, reset($records)->notificationid);
}
/**
* Test that read popup notifications are cleaned up
*
* @return void
*/
public function test_cleanup_read_notifications(): void {
global $DB;
$this->resetAfterTest();
$userfrom = $this->getDataGenerator()->create_user();
$userto = $this->getDataGenerator()->create_user();
$now = time();
$this->send_fake_read_popup_notification($userfrom, $userto, 'Message 1', $now - 20, $now - 10);
$notificationid = $this->send_fake_read_popup_notification($userfrom, $userto, 'Message 2', $now - 15, $now);
// Sanity check.
$this->assertEquals(2, $DB->count_records('message_popup_notifications'));
// Delete read notifications >5 seconds old.
set_config('messagingdeletereadnotificationsdelay', 5);
(new messaging_cleanup_task())->execute();
// We should have just one record now, matching the second notification we sent.
$records = $DB->get_records('message_popup_notifications');
$this->assertCount(1, $records);
$this->assertEquals($notificationid, reset($records)->notificationid);
}
}