Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
use message_popup_external;
20
use message_popup_test_helper;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
 
26
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
27
require_once($CFG->dirroot . '/message/output/popup/externallib.php');
28
require_once($CFG->dirroot . '/message/output/popup/tests/base.php');
29
 
30
/**
31
 * Class for external message popup functions unit tests.
32
 *
33
 * @package    message_popup
34
 * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class externallib_test extends \advanced_testcase {
38
    use message_popup_test_helper;
39
 
40
    /** @var \phpunit_message_sink message redirection. */
41
    public $messagesink;
42
 
43
    /**
44
     * Test set up.
45
     *
46
     * This is executed before running any test in this file.
47
     */
48
    public function setUp(): void {
49
        $this->preventResetByRollback(); // Messaging is not compatible with transactions.
50
        $this->messagesink = $this->redirectMessages();
51
        $this->resetAfterTest();
52
    }
53
 
54
    /**
55
     * Test that get_popup_notifications throws an exception if the user
56
     * doesn't exist.
57
     */
11 efrain 58
    public function test_get_popup_notifications_no_user_exception(): void {
1 efrain 59
        $this->resetAfterTest(true);
60
 
61
        $this->expectException('moodle_exception');
62
        $result = message_popup_external::get_popup_notifications(-2132131, false, 0, 0);
63
    }
64
 
65
    /**
66
     * get_popup_notifications should throw exception if user isn't logged in
67
     * user.
68
     */
11 efrain 69
    public function test_get_popup_notifications_access_denied_exception(): void {
1 efrain 70
        $this->resetAfterTest(true);
71
 
72
        $sender = $this->getDataGenerator()->create_user();
73
        $user = $this->getDataGenerator()->create_user();
74
 
75
        $this->setUser($user);
76
        $this->expectException('moodle_exception');
77
        $result = message_popup_external::get_popup_notifications($sender->id, false, 0, 0);
78
    }
79
 
80
    /**
81
     * get_popup_notifications should return notifications for the recipient.
82
     */
11 efrain 83
    public function test_get_popup_notifications_as_recipient(): void {
1 efrain 84
        $this->resetAfterTest(true);
85
 
86
        $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
87
        $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
88
 
89
        $notificationids = array(
90
            $this->send_fake_unread_popup_notification($sender, $recipient),
91
            $this->send_fake_unread_popup_notification($sender, $recipient),
92
            $this->send_fake_read_popup_notification($sender, $recipient),
93
            $this->send_fake_read_popup_notification($sender, $recipient),
94
        );
95
 
96
        // Confirm that admin has super powers to retrieve any notifications.
97
        $this->setAdminUser();
98
        $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
99
        $this->assertCount(4, $result['notifications']);
100
        // Check we receive custom data as a unserialisable json.
101
        $found = 0;
102
        foreach ($result['notifications'] as $notification) {
103
            if (!empty($notification->customdata)) {
104
                $this->assertObjectHasProperty('datakey', json_decode($notification->customdata));
105
                $found++;
106
            }
107
        }
108
        $this->assertEquals(2, $found);
109
 
110
        $this->setUser($recipient);
111
        $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
112
        $this->assertCount(4, $result['notifications']);
113
    }
114
 
115
    /**
116
     * get_popup_notifications result set should work with limit and offset.
117
     */
11 efrain 118
    public function test_get_popup_notification_limit_offset(): void {
1 efrain 119
        $this->resetAfterTest(true);
120
 
121
        $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
122
        $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
123
 
124
        $this->setUser($recipient);
125
 
126
        $notificationids = array(
127
            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 1', 1),
128
            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 2', 2),
129
            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 3', 3),
130
            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 4', 4),
131
            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 5', 5),
132
            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 6', 6),
133
            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 7', 7),
134
            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 8', 8),
135
        );
136
 
137
        $result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 0);
138
 
139
        $this->assertEquals($result['notifications'][0]->id, $notificationids[7]);
140
        $this->assertEquals($result['notifications'][1]->id, $notificationids[6]);
141
 
142
        $result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 2);
143
 
144
        $this->assertEquals($result['notifications'][0]->id, $notificationids[5]);
145
        $this->assertEquals($result['notifications'][1]->id, $notificationids[4]);
146
    }
147
 
148
    /**
149
     * get_unread_popup_notification should throw an exception for an invalid user.
150
     */
11 efrain 151
    public function test_get_unread_popup_notification_count_invalid_user_exception(): void {
1 efrain 152
        $this->resetAfterTest(true);
153
 
154
        $this->expectException('moodle_exception');
155
        $result = message_popup_external::get_unread_popup_notification_count(-2132131, 0);
156
    }
157
 
158
    /**
159
     * get_unread_popup_notification_count should throw exception if being requested for
160
     * non-logged in user.
161
     */
11 efrain 162
    public function test_get_unread_popup_notification_count_access_denied_exception(): void {
1 efrain 163
        $this->resetAfterTest(true);
164
 
165
        $sender = $this->getDataGenerator()->create_user();
166
        $user = $this->getDataGenerator()->create_user();
167
 
168
        $this->setUser($user);
169
        $this->expectException('moodle_exception');
170
        $result = message_popup_external::get_unread_popup_notification_count($sender->id, 0);
171
    }
172
 
173
    /**
174
     * Test get_unread_popup_notification_count.
175
     */
11 efrain 176
    public function test_get_unread_popup_notification_count(): void {
1 efrain 177
        $this->resetAfterTest(true);
178
 
179
        $sender1 = $this->getDataGenerator()->create_user();
180
        $sender2 = $this->getDataGenerator()->create_user();
181
        $sender3 = $this->getDataGenerator()->create_user();
182
        $recipient = $this->getDataGenerator()->create_user();
183
 
184
        $this->setUser($recipient);
185
 
186
        $notificationids = array(
187
            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
188
            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
189
            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
190
            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
191
            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
192
            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
193
        );
194
 
195
        $count = message_popup_external::get_unread_popup_notification_count($recipient->id);
196
        $this->assertEquals($count, 6);
197
    }
198
}