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 core_message;
18
 
19
use core_message\tests\helper as testhelper;
20
 
21
/**
22
 * Test api's in message lib.
23
 *
24
 * @package core_message
25
 * @category test
26
 * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
27
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
11 efrain 29
final class messagelib_test extends \advanced_testcase {
30
    public static function setUpBeforeClass(): void {
31
        global $CFG;
32
        require_once($CFG->dirroot . '/message/lib.php');
1 efrain 33
 
11 efrain 34
        parent::setUpBeforeClass();
1 efrain 35
    }
36
 
37
    /**
38
     * Test message_get_blocked_users throws an exception because has been removed.
39
     */
11 efrain 40
    public function test_message_get_blocked_users(): void {
1 efrain 41
        $this->expectException('coding_exception');
42
        $this->expectExceptionMessage(
43
            'message_get_blocked_users() has been removed, please use \core_message\api::get_blocked_users() instead.'
44
        );
45
        message_get_blocked_users();
46
    }
47
 
48
    /**
49
     * Test message_get_contacts throws an exception because has been removed.
50
     */
11 efrain 51
    public function test_message_get_contacts(): void {
1 efrain 52
        $this->expectException('coding_exception');
53
        $this->expectExceptionMessage('message_get_contacts() has been removed.');
54
        message_get_contacts();
55
    }
56
 
57
    /**
58
     * Test message_search_users.
59
     */
11 efrain 60
    public function test_message_search_users(): void {
1 efrain 61
        global $USER;
62
 
11 efrain 63
        $this->resetAfterTest();
64
 
1 efrain 65
        // Set this user as the admin.
66
        $this->setAdminUser();
67
 
68
        // Create a user to add to the admin's contact list.
69
        $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
70
        $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
71
 
72
        // Add users to the admin's contact list.
73
        \core_message\api::add_contact($USER->id, $user1->id);
74
        \core_message\api::add_contact($USER->id, $user2->id);
75
 
76
        $this->assertCount(1, message_search_users(0, 'Test1'));
77
        $this->assertCount(2, message_search_users(0, 'Test'));
78
        $this->assertCount(1, message_search_users(0, 'user1'));
79
        $this->assertCount(2, message_search_users(0, 'user'));
80
    }
81
 
82
    /**
83
     * Test message_get_messages.
84
     */
11 efrain 85
    public function test_message_get_messages(): void {
1 efrain 86
        global $DB;
87
 
11 efrain 88
        $this->resetAfterTest();
1 efrain 89
 
90
        // Set this user as the admin.
91
        $this->setAdminUser();
92
 
93
        $user1 = self::getDataGenerator()->create_user();
94
        $user2 = self::getDataGenerator()->create_user();
95
        $user3 = self::getDataGenerator()->create_user();
96
 
97
        \core_message\api::add_contact($user1->id, $user2->id);
98
        \core_message\api::add_contact($user1->id, $user3->id);
99
 
100
        // Create some individual conversations.
101
        $ic1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
102
            [$user1->id, $user2->id]);
103
        $ic2 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
104
            [$user1->id, $user3->id]);
105
 
106
        // Send some messages to individual conversations.
107
        $im1 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 1');
108
        $im2 = testhelper::send_fake_message_to_conversation($user2, $ic1->id, 'Message 2');
109
        $im3 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 3');
110
        $im4 = testhelper::send_fake_message_to_conversation($user1, $ic2->id, 'Message 4');
111
 
112
        // Mark a message as read by user2.
113
        $message = $DB->get_record('messages', ['id' => $im1]);
114
        \core_message\api::mark_message_as_read($user2->id, $message);
115
 
116
        // Retrieve unread messages sent from user1 to user2.
117
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_UNREAD);
118
        $this->assertCount(1, $lastmessages);
119
        $this->assertArrayHasKey($im3, $lastmessages);
120
 
121
        // Get only read messages.
122
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ);
123
        $this->assertCount(1, $lastmessages);
124
        $this->assertArrayHasKey($im1, $lastmessages);
125
 
126
        // Get both read and unread.
127
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ_AND_UNREAD);
128
        $this->assertCount(2, $lastmessages);
129
        $this->assertArrayHasKey($im1, $lastmessages);
130
        $this->assertArrayHasKey($im3, $lastmessages);
131
 
132
        // Repeat retrieve read/unread messages but using a bool to test backwards compatibility.
133
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, false);
134
        $this->assertCount(1, $lastmessages);
135
        $this->assertArrayHasKey($im3, $lastmessages);
136
 
137
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, true);
138
        $this->assertCount(1, $lastmessages);
139
        $this->assertArrayHasKey($im1, $lastmessages);
140
 
141
        // Create some group conversations.
142
        $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
143
            [$user1->id, $user2->id, $user3->id], 'Group chat');
144
 
145
        // Send some messages to group conversations.
146
        $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1');
147
 
148
        // Retrieve all messages sent from user1 to user2 (the result should be the same as before, because only individual
149
        // conversations should be considered by the message_get_messages function).
150
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ_AND_UNREAD);
151
        $this->assertCount(2, $lastmessages);
152
        $this->assertArrayHasKey($im1, $lastmessages);
153
        $this->assertArrayHasKey($im3, $lastmessages);
154
    }
155
 
156
    /**
157
     * Test message_get_messages with only group conversations between users.
158
     */
11 efrain 159
    public function test_message_get_messages_only_group_conversations(): void {
160
        $this->resetAfterTest();
1 efrain 161
 
162
        // Set this user as the admin.
163
        $this->setAdminUser();
164
 
165
        $user1 = self::getDataGenerator()->create_user();
166
        $user2 = self::getDataGenerator()->create_user();
167
        $user3 = self::getDataGenerator()->create_user();
168
 
169
        // Create some group conversations.
170
        $gc1 = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
171
            [$user1->id, $user2->id, $user3->id], 'Group chat');
172
 
173
        // Send some messages to group conversations.
174
        $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1');
175
        $gm2 = testhelper::send_fake_message_to_conversation($user2, $gc1->id, 'Group message 2');
176
 
177
        // Retrieve all messages sent from user1 to user2. There shouldn't be messages, because only individual
178
        // conversations should be considered by the message_get_messages function.
179
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ_AND_UNREAD);
180
        $this->assertCount(0, $lastmessages);
181
    }
182
 
183
}