Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
 */
1441 ariadna 37
final class externallib_test extends \advanced_testcase {
1 efrain 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 {
1441 ariadna 49
        parent::setUp();
1 efrain 50
        $this->preventResetByRollback(); // Messaging is not compatible with transactions.
51
        $this->messagesink = $this->redirectMessages();
52
        $this->resetAfterTest();
53
    }
54
 
55
    /**
56
     * Test that get_popup_notifications throws an exception if the user
57
     * doesn't exist.
58
     */
11 efrain 59
    public function test_get_popup_notifications_no_user_exception(): void {
1 efrain 60
        $this->resetAfterTest(true);
61
 
62
        $this->expectException('moodle_exception');
63
        $result = message_popup_external::get_popup_notifications(-2132131, false, 0, 0);
64
    }
65
 
66
    /**
67
     * get_popup_notifications should throw exception if user isn't logged in
68
     * user.
69
     */
11 efrain 70
    public function test_get_popup_notifications_access_denied_exception(): void {
1 efrain 71
        $this->resetAfterTest(true);
72
 
73
        $sender = $this->getDataGenerator()->create_user();
74
        $user = $this->getDataGenerator()->create_user();
75
 
76
        $this->setUser($user);
77
        $this->expectException('moodle_exception');
78
        $result = message_popup_external::get_popup_notifications($sender->id, false, 0, 0);
79
    }
80
 
81
    /**
82
     * get_popup_notifications should return notifications for the recipient.
83
     */
11 efrain 84
    public function test_get_popup_notifications_as_recipient(): void {
1 efrain 85
        $this->resetAfterTest(true);
86
 
87
        $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
88
        $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
89
 
90
        $notificationids = array(
91
            $this->send_fake_unread_popup_notification($sender, $recipient),
92
            $this->send_fake_unread_popup_notification($sender, $recipient),
93
            $this->send_fake_read_popup_notification($sender, $recipient),
94
            $this->send_fake_read_popup_notification($sender, $recipient),
95
        );
96
 
97
        // Confirm that admin has super powers to retrieve any notifications.
98
        $this->setAdminUser();
99
        $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
100
        $this->assertCount(4, $result['notifications']);
101
        // Check we receive custom data as a unserialisable json.
102
        $found = 0;
103
        foreach ($result['notifications'] as $notification) {
104
            if (!empty($notification->customdata)) {
105
                $this->assertObjectHasProperty('datakey', json_decode($notification->customdata));
106
                $found++;
107
            }
108
        }
109
        $this->assertEquals(2, $found);
110
 
111
        $this->setUser($recipient);
112
        $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0);
113
        $this->assertCount(4, $result['notifications']);
114
    }
115
 
116
    /**
117
     * get_popup_notifications result set should work with limit and offset.
118
     */
11 efrain 119
    public function test_get_popup_notification_limit_offset(): void {
1 efrain 120
        $this->resetAfterTest(true);
121
 
122
        $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
123
        $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
124
 
125
        $this->setUser($recipient);
126
 
127
        $notificationids = array(
128
            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 1', 1),
129
            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 2', 2),
130
            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 3', 3),
131
            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 4', 4),
132
            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 5', 5),
133
            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 6', 6),
134
            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 7', 7),
135
            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 8', 8),
136
        );
137
 
138
        $result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 0);
139
 
140
        $this->assertEquals($result['notifications'][0]->id, $notificationids[7]);
141
        $this->assertEquals($result['notifications'][1]->id, $notificationids[6]);
142
 
143
        $result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 2);
144
 
145
        $this->assertEquals($result['notifications'][0]->id, $notificationids[5]);
146
        $this->assertEquals($result['notifications'][1]->id, $notificationids[4]);
147
    }
148
 
149
    /**
150
     * get_unread_popup_notification should throw an exception for an invalid user.
151
     */
11 efrain 152
    public function test_get_unread_popup_notification_count_invalid_user_exception(): void {
1 efrain 153
        $this->resetAfterTest(true);
154
 
155
        $this->expectException('moodle_exception');
156
        $result = message_popup_external::get_unread_popup_notification_count(-2132131, 0);
157
    }
158
 
159
    /**
160
     * get_unread_popup_notification_count should throw exception if being requested for
161
     * non-logged in user.
162
     */
11 efrain 163
    public function test_get_unread_popup_notification_count_access_denied_exception(): void {
1 efrain 164
        $this->resetAfterTest(true);
165
 
166
        $sender = $this->getDataGenerator()->create_user();
167
        $user = $this->getDataGenerator()->create_user();
168
 
169
        $this->setUser($user);
170
        $this->expectException('moodle_exception');
171
        $result = message_popup_external::get_unread_popup_notification_count($sender->id, 0);
172
    }
173
 
174
    /**
175
     * Test get_unread_popup_notification_count.
176
     */
11 efrain 177
    public function test_get_unread_popup_notification_count(): void {
1 efrain 178
        $this->resetAfterTest(true);
179
 
180
        $sender1 = $this->getDataGenerator()->create_user();
181
        $sender2 = $this->getDataGenerator()->create_user();
182
        $sender3 = $this->getDataGenerator()->create_user();
183
        $recipient = $this->getDataGenerator()->create_user();
184
 
185
        $this->setUser($recipient);
186
 
187
        $notificationids = array(
188
            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
189
            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
190
            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
191
            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
192
            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
193
            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
194
        );
195
 
196
        $count = message_popup_external::get_unread_popup_notification_count($recipient->id);
197
        $this->assertEquals($count, 6);
198
    }
199
}