Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 11
Línea 16... Línea 16...
16
 
16
 
Línea 17... Línea 17...
17
namespace core_message;
17
namespace core_message;
Línea 18... Línea -...
18
 
-
 
19
use core_message\tests\helper as testhelper;
-
 
20
 
-
 
21
defined('MOODLE_INTERNAL') || die();
-
 
22
 
-
 
23
global $CFG;
18
 
24
require_once($CFG->dirroot . '/message/lib.php');
19
use core_message\tests\helper as testhelper;
25
 
20
 
26
/**
21
/**
27
 * Test api's in message lib.
22
 * Test api's in message lib.
28
 *
23
 *
29
 * @package core_message
24
 * @package core_message
30
 * @category test
25
 * @category test
31
 * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
26
 * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
32
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
33
 */
27
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
28
 */
34
class messagelib_test extends \advanced_testcase {
29
final class messagelib_test extends \advanced_testcase {
Línea 35... Línea -...
35
 
-
 
36
    /** @var phpunit_message_sink keep track of messages. */
-
 
37
    protected $messagesink = null;
-
 
38
 
-
 
39
    /**
-
 
40
     * Test set up.
-
 
41
     *
-
 
42
     * This is executed before running any test in this file.
-
 
43
     */
-
 
44
    public function setUp(): void {
-
 
45
        $this->preventResetByRollback(); // Messaging is not compatible with transactions.
-
 
46
        $this->messagesink = $this->redirectMessages();
-
 
47
        $this->resetAfterTest();
-
 
48
    }
-
 
49
 
-
 
50
    /**
-
 
51
     * Send a fake message.
-
 
52
     *
-
 
53
     * {@link message_send()} does not support transaction, this function will simulate a message
-
 
54
     * sent from a user to another. We should stop using it once {@link message_send()} will support
-
 
55
     * transactions. This is not clean at all, this is just used to add rows to the table.
-
 
56
     *
-
 
57
     * @param \stdClass $userfrom user object of the one sending the message.
-
 
58
     * @param \stdClass $userto user object of the one receiving the message.
-
 
59
     * @param string $message message to send.
-
 
60
     * @param int $notification if the message is a notification.
-
 
61
     * @param int $time the time the message was sent
-
 
62
     * @return int the id of the message
-
 
63
     */
-
 
64
    protected function send_fake_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) {
-
 
65
        global $DB;
-
 
66
 
-
 
67
        if (empty($time)) {
-
 
68
            $time = time();
-
 
69
        }
-
 
70
 
-
 
71
        if ($notification) {
-
 
72
            $record = new \stdClass();
-
 
73
            $record->useridfrom = $userfrom->id;
-
 
74
            $record->useridto = $userto->id;
-
 
75
            $record->subject = 'No subject';
-
 
76
            $record->fullmessage = $message;
-
 
77
            $record->smallmessage = $message;
-
 
78
            $record->timecreated = $time;
-
 
79
 
-
 
80
            return $DB->insert_record('notifications', $record);
-
 
81
        }
-
 
82
 
-
 
83
        if ($userfrom->id == $userto->id) {
-
 
84
            // It's a self conversation.
-
 
85
            $conversation = \core_message\api::get_self_conversation($userfrom->id);
-
 
86
            if (empty($conversation)) {
-
 
87
                $conversation = \core_message\api::create_conversation(
-
 
88
                    \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
-
 
89
                    [$userfrom->id]
-
 
90
                );
-
 
91
            }
-
 
92
            $conversationid = $conversation->id;
-
 
93
        } else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
-
 
94
            // It's an individual conversation between two different users.
-
 
95
            $conversation = \core_message\api::create_conversation(
-
 
96
                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
97
                [
-
 
98
                    $userfrom->id,
-
 
99
                    $userto->id
-
 
100
                ]
-
 
101
            );
-
 
102
            $conversationid = $conversation->id;
30
    public static function setUpBeforeClass(): void {
103
        }
-
 
104
 
-
 
105
        // Ok, send the message.
-
 
106
        $record = new \stdClass();
-
 
107
        $record->useridfrom = $userfrom->id;
-
 
108
        $record->conversationid = $conversationid;
-
 
109
        $record->subject = 'No subject';
-
 
110
        $record->fullmessage = $message;
-
 
111
        $record->smallmessage = $message;
31
        global $CFG;
Línea 112... Línea 32...
112
        $record->timecreated = $time;
32
        require_once($CFG->dirroot . '/message/lib.php');
113
 
33
 
114
        return $DB->insert_record('messages', $record);
34
        parent::setUpBeforeClass();
115
    }
35
    }
116
 
36
 
117
    /**
37
    /**
118
     * Test message_get_blocked_users throws an exception because has been removed.
38
     * Test message_get_blocked_users throws an exception because has been removed.
119
     */
39
     */
120
    public function test_message_get_blocked_users() {
40
    public function test_message_get_blocked_users(): void {
121
        $this->expectException('coding_exception');
41
        $this->expectException('coding_exception');
Línea 122... Línea 42...
122
        $this->expectExceptionMessage(
42
        $this->expectExceptionMessage(
123
            'message_get_blocked_users() has been removed, please use \core_message\api::get_blocked_users() instead.'
43
            'message_get_blocked_users() has been removed, please use \core_message\api::get_blocked_users() instead.'
124
        );
44
        );
125
        message_get_blocked_users();
45
        message_get_blocked_users();
126
    }
46
    }
127
 
47
 
128
    /**
48
    /**
129
     * Test message_get_contacts throws an exception because has been removed.
49
     * Test message_get_contacts throws an exception because has been removed.
Línea 130... Línea 50...
130
     */
50
     */
131
    public function test_message_get_contacts() {
51
    public function test_message_get_contacts(): void {
132
        $this->expectException('coding_exception');
52
        $this->expectException('coding_exception');
133
        $this->expectExceptionMessage('message_get_contacts() has been removed.');
53
        $this->expectExceptionMessage('message_get_contacts() has been removed.');
134
        message_get_contacts();
54
        message_get_contacts();
Línea -... Línea 55...
-
 
55
    }
-
 
56
 
135
    }
57
    /**
136
 
58
     * Test message_search_users.
Línea 137... Línea 59...
137
    /**
59
     */
138
     * Test message_search_users.
60
    public function test_message_search_users(): void {
Línea 158... Línea 80...
158
    }
80
    }
Línea 159... Línea 81...
159
 
81
 
160
    /**
82
    /**
161
     * Test message_get_messages.
83
     * Test message_get_messages.
162
     */
84
     */
163
    public function test_message_get_messages() {
85
    public function test_message_get_messages(): void {
Línea 164... Línea 86...
164
        global $DB;
86
        global $DB;
Línea 165... Línea 87...
165
 
87
 
166
        $this->resetAfterTest(true);
88
        $this->resetAfterTest();
Línea 167... Línea 89...
167
 
89
 
Línea 232... Línea 154...
232
    }
154
    }
Línea 233... Línea 155...
233
 
155
 
234
    /**
156
    /**
235
     * Test message_get_messages with only group conversations between users.
157
     * Test message_get_messages with only group conversations between users.
236
     */
158
     */
237
    public function test_message_get_messages_only_group_conversations() {
159
    public function test_message_get_messages_only_group_conversations(): void {
Línea 238... Línea 160...
238
        $this->resetAfterTest(true);
160
        $this->resetAfterTest();
239
 
161
 
Línea 240... Línea 162...
240
        // Set this user as the admin.
162
        // Set this user as the admin.