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 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_search_users.
39
     */
11 efrain 40
    public function test_message_search_users(): void {
1 efrain 41
        global $USER;
42
 
11 efrain 43
        $this->resetAfterTest();
44
 
1 efrain 45
        // Set this user as the admin.
46
        $this->setAdminUser();
47
 
48
        // Create a user to add to the admin's contact list.
1441 ariadna 49
        $user1 = $this->getDataGenerator()->create_user(['firstname' => 'Test1', 'lastname' => 'user1']);
50
        $user2 = $this->getDataGenerator()->create_user(['firstname' => 'Test2', 'lastname' => 'user2']);
1 efrain 51
 
52
        // Add users to the admin's contact list.
1441 ariadna 53
        api::add_contact($USER->id, $user1->id);
54
        api::add_contact($USER->id, $user2->id);
1 efrain 55
 
56
        $this->assertCount(1, message_search_users(0, 'Test1'));
57
        $this->assertCount(2, message_search_users(0, 'Test'));
58
        $this->assertCount(1, message_search_users(0, 'user1'));
59
        $this->assertCount(2, message_search_users(0, 'user'));
60
    }
61
 
62
    /**
63
     * Test message_get_messages.
64
     */
11 efrain 65
    public function test_message_get_messages(): void {
1 efrain 66
        global $DB;
67
 
11 efrain 68
        $this->resetAfterTest();
1 efrain 69
 
70
        // Set this user as the admin.
71
        $this->setAdminUser();
72
 
73
        $user1 = self::getDataGenerator()->create_user();
74
        $user2 = self::getDataGenerator()->create_user();
75
        $user3 = self::getDataGenerator()->create_user();
76
 
1441 ariadna 77
        api::add_contact($user1->id, $user2->id);
78
        api::add_contact($user1->id, $user3->id);
1 efrain 79
 
80
        // Create some individual conversations.
1441 ariadna 81
        $ic1 = api::create_conversation(
82
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
83
            [$user1->id, $user2->id]
84
        );
85
        $ic2 = api::create_conversation(
86
            api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
87
            [$user1->id, $user3->id]
88
        );
1 efrain 89
 
90
        // Send some messages to individual conversations.
91
        $im1 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 1');
92
        $im2 = testhelper::send_fake_message_to_conversation($user2, $ic1->id, 'Message 2');
93
        $im3 = testhelper::send_fake_message_to_conversation($user1, $ic1->id, 'Message 3');
94
        $im4 = testhelper::send_fake_message_to_conversation($user1, $ic2->id, 'Message 4');
95
 
96
        // Mark a message as read by user2.
97
        $message = $DB->get_record('messages', ['id' => $im1]);
1441 ariadna 98
        api::mark_message_as_read($user2->id, $message);
1 efrain 99
 
100
        // Retrieve unread messages sent from user1 to user2.
101
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_UNREAD);
102
        $this->assertCount(1, $lastmessages);
103
        $this->assertArrayHasKey($im3, $lastmessages);
104
 
105
        // Get only read messages.
106
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ);
107
        $this->assertCount(1, $lastmessages);
108
        $this->assertArrayHasKey($im1, $lastmessages);
109
 
110
        // Get both read and unread.
111
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ_AND_UNREAD);
112
        $this->assertCount(2, $lastmessages);
113
        $this->assertArrayHasKey($im1, $lastmessages);
114
        $this->assertArrayHasKey($im3, $lastmessages);
115
 
116
        // Repeat retrieve read/unread messages but using a bool to test backwards compatibility.
117
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, false);
118
        $this->assertCount(1, $lastmessages);
119
        $this->assertArrayHasKey($im3, $lastmessages);
120
 
121
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, true);
122
        $this->assertCount(1, $lastmessages);
123
        $this->assertArrayHasKey($im1, $lastmessages);
124
 
125
        // Create some group conversations.
1441 ariadna 126
        $gc1 = api::create_conversation(
127
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
128
            [$user1->id, $user2->id, $user3->id],
129
            'Group chat'
130
        );
1 efrain 131
 
132
        // Send some messages to group conversations.
133
        $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1');
134
 
135
        // Retrieve all messages sent from user1 to user2 (the result should be the same as before, because only individual
136
        // conversations should be considered by the message_get_messages function).
137
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ_AND_UNREAD);
138
        $this->assertCount(2, $lastmessages);
139
        $this->assertArrayHasKey($im1, $lastmessages);
140
        $this->assertArrayHasKey($im3, $lastmessages);
141
    }
142
 
143
    /**
144
     * Test message_get_messages with only group conversations between users.
145
     */
11 efrain 146
    public function test_message_get_messages_only_group_conversations(): void {
147
        $this->resetAfterTest();
1 efrain 148
 
149
        // Set this user as the admin.
150
        $this->setAdminUser();
151
 
152
        $user1 = self::getDataGenerator()->create_user();
153
        $user2 = self::getDataGenerator()->create_user();
154
        $user3 = self::getDataGenerator()->create_user();
155
 
156
        // Create some group conversations.
1441 ariadna 157
        $gc1 = api::create_conversation(
158
            api::MESSAGE_CONVERSATION_TYPE_GROUP,
159
            [$user1->id, $user2->id, $user3->id],
160
            'Group chat'
161
        );
1 efrain 162
 
163
        // Send some messages to group conversations.
164
        $gm1 = testhelper::send_fake_message_to_conversation($user1, $gc1->id, 'Group message 1');
165
        $gm2 = testhelper::send_fake_message_to_conversation($user2, $gc1->id, 'Group message 2');
166
 
167
        // Retrieve all messages sent from user1 to user2. There shouldn't be messages, because only individual
168
        // conversations should be considered by the message_get_messages function.
169
        $lastmessages = message_get_messages($user2->id, $user1->id, 0, MESSAGE_GET_READ_AND_UNREAD);
170
        $this->assertCount(0, $lastmessages);
171
    }
172
}