| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | namespace message_popup;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | global $CFG;
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
 | 
        
           |  |  | 24 | require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | /**
 | 
        
           |  |  | 27 |  * Test message popup API.
 | 
        
           |  |  | 28 |  *
 | 
        
           |  |  | 29 |  * @package message_popup
 | 
        
           |  |  | 30 |  * @category test
 | 
        
           |  |  | 31 |  * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
 | 
        
           |  |  | 32 |  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 33 |  */
 | 
        
           | 1441 | ariadna | 34 | final class api_test extends \advanced_testcase {
 | 
        
           | 1 | efrain | 35 |     use \message_popup_test_helper;
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |     /** @var \phpunit_message_sink message redirection. */
 | 
        
           |  |  | 38 |     public $messagesink;
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * Test set up.
 | 
        
           |  |  | 42 |      *
 | 
        
           |  |  | 43 |      * This is executed before running any test in this file.
 | 
        
           |  |  | 44 |      */
 | 
        
           |  |  | 45 |     public function setUp(): void {
 | 
        
           | 1441 | ariadna | 46 |         parent::setUp();
 | 
        
           | 1 | efrain | 47 |         $this->preventResetByRollback(); // Messaging is not compatible with transactions.
 | 
        
           |  |  | 48 |         $this->messagesink = $this->redirectMessages();
 | 
        
           |  |  | 49 |         $this->resetAfterTest();
 | 
        
           |  |  | 50 |     }
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |     /**
 | 
        
           |  |  | 53 |      * Test that the get_popup_notifications function will return the correct notifications.
 | 
        
           |  |  | 54 |      */
 | 
        
           | 11 | efrain | 55 |     public function test_message_get_popup_notifications(): void {
 | 
        
           | 1 | efrain | 56 |         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
 | 
        
           |  |  | 57 |         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
 | 
        
           |  |  | 60 |         $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
 | 
        
           |  |  | 61 |         $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
 | 
        
           |  |  | 62 |         $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
 | 
        
           |  |  | 63 |         $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |         $notifications = \message_popup\api::get_popup_notifications($recipient->id);
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |         $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
 | 
        
           |  |  | 68 |         $this->assertEquals($notifications[1]->fullmessage, 'Message 4');
 | 
        
           |  |  | 69 |         $this->assertEquals($notifications[2]->fullmessage, 'Message 3');
 | 
        
           |  |  | 70 |         $this->assertEquals($notifications[3]->fullmessage, 'Message 2');
 | 
        
           |  |  | 71 |         $this->assertEquals($notifications[4]->fullmessage, 'Message 1');
 | 
        
           |  |  | 72 |     }
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |     /**
 | 
        
           |  |  | 75 |      * Test that the get_popup_notifications function works correctly with limiting and offsetting
 | 
        
           |  |  | 76 |      * the result set if requested.
 | 
        
           |  |  | 77 |      */
 | 
        
           | 11 | efrain | 78 |     public function test_message_get_popup_notifications_all_limit_and_offset(): void {
 | 
        
           | 1 | efrain | 79 |         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
 | 
        
           |  |  | 80 |         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 |         $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
 | 
        
           |  |  | 83 |         $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
 | 
        
           |  |  | 84 |         $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
 | 
        
           |  |  | 85 |         $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
 | 
        
           |  |  | 86 |         $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
 | 
        
           |  |  | 87 |         $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5);
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |         $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 0);
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |         $this->assertEquals($notifications[0]->fullmessage, 'Message 6');
 | 
        
           |  |  | 92 |         $this->assertEquals($notifications[1]->fullmessage, 'Message 5');
 | 
        
           |  |  | 93 |   | 
        
           |  |  | 94 |         $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 2);
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 |         $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
 | 
        
           |  |  | 97 |         $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |         $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 0, 3);
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |         $this->assertEquals($notifications[0]->fullmessage, 'Message 3');
 | 
        
           |  |  | 102 |         $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
 | 
        
           |  |  | 103 |         $this->assertEquals($notifications[2]->fullmessage, 'Message 1');
 | 
        
           |  |  | 104 |     }
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 |     /**
 | 
        
           |  |  | 107 |      * Test count_unread_popup_notifications.
 | 
        
           |  |  | 108 |      */
 | 
        
           | 11 | efrain | 109 |     public function test_message_count_unread_popup_notifications(): void {
 | 
        
           | 1 | efrain | 110 |         $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
 | 
        
           |  |  | 111 |         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 | 
        
           |  |  | 112 |         $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
 | 
        
           |  |  | 113 |         $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |         $this->send_fake_unread_popup_notification($sender1, $recipient1);
 | 
        
           |  |  | 116 |         $this->send_fake_unread_popup_notification($sender1, $recipient1);
 | 
        
           |  |  | 117 |         $this->send_fake_unread_popup_notification($sender2, $recipient1);
 | 
        
           |  |  | 118 |         $this->send_fake_unread_popup_notification($sender1, $recipient2);
 | 
        
           |  |  | 119 |         $this->send_fake_unread_popup_notification($sender2, $recipient2);
 | 
        
           |  |  | 120 |         $this->send_fake_unread_popup_notification($sender2, $recipient2);
 | 
        
           |  |  | 121 |         $this->send_fake_unread_popup_notification($sender2, $recipient2);
 | 
        
           |  |  | 122 |         $this->send_fake_unread_popup_notification($sender2, $recipient2);
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |         $this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient1->id), 3);
 | 
        
           |  |  | 125 |         $this->assertEquals(\message_popup\api::count_unread_popup_notifications($recipient2->id), 5);
 | 
        
           |  |  | 126 |     }
 | 
        
           |  |  | 127 | }
 |