weekly release 3.2dev
[moodle.git] / message / tests / api_test.php
CommitLineData
79f6c36c
MN
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/**
18 * Test message API.
19 *
20 * @package core_message
21 * @category test
22 * @copyright 2016 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
31
32/**
33 * Test message API.
34 *
35 * @package core_message
36 * @category test
37 * @copyright 2016 Mark Nelson <markn@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class core_message_api_testcase extends core_message_messagelib_testcase {
41
42 public function test_message_mark_all_read_for_user_touser() {
43 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
44 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
45
7d69958e
RW
46 $this->send_fake_message($sender, $recipient, 'Notification', 1);
47 $this->send_fake_message($sender, $recipient, 'Notification', 1);
48 $this->send_fake_message($sender, $recipient, 'Notification', 1);
79f6c36c
MN
49 $this->send_fake_message($sender, $recipient);
50 $this->send_fake_message($sender, $recipient);
51 $this->send_fake_message($sender, $recipient);
52
53 \core_message\api::mark_all_read_for_user($recipient->id);
54 $this->assertEquals(message_count_unread_messages($recipient), 0);
55 }
56
57 public function test_message_mark_all_read_for_user_touser_with_fromuser() {
58 $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
59 $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
60 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
61
7d69958e
RW
62 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
63 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
64 $this->send_fake_message($sender1, $recipient, 'Notification', 1);
79f6c36c
MN
65 $this->send_fake_message($sender1, $recipient);
66 $this->send_fake_message($sender1, $recipient);
67 $this->send_fake_message($sender1, $recipient);
7d69958e
RW
68 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
69 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
70 $this->send_fake_message($sender2, $recipient, 'Notification', 1);
79f6c36c
MN
71 $this->send_fake_message($sender2, $recipient);
72 $this->send_fake_message($sender2, $recipient);
73 $this->send_fake_message($sender2, $recipient);
74
75 \core_message\api::mark_all_read_for_user($recipient->id, $sender1->id);
76 $this->assertEquals(message_count_unread_messages($recipient), 6);
77 }
78
79 public function test_message_mark_all_read_for_user_touser_with_type() {
80 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
81 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
82
7d69958e
RW
83 $this->send_fake_message($sender, $recipient, 'Notification', 1);
84 $this->send_fake_message($sender, $recipient, 'Notification', 1);
85 $this->send_fake_message($sender, $recipient, 'Notification', 1);
79f6c36c
MN
86 $this->send_fake_message($sender, $recipient);
87 $this->send_fake_message($sender, $recipient);
88 $this->send_fake_message($sender, $recipient);
89
90 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION);
91 $this->assertEquals(message_count_unread_messages($recipient), 3);
92
93 \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE);
94 $this->assertEquals(message_count_unread_messages($recipient), 0);
95 }
96
5b0769db
MN
97 /**
98 * Test count_blocked_users.
99 *
100 */
101 public function test_message_count_blocked_users() {
102 // Set this user as the admin.
103 $this->setAdminUser();
104
105 // Create users to add to the admin's contact list.
106 $user1 = $this->getDataGenerator()->create_user();
107 $user2 = $this->getDataGenerator()->create_user();
108
109 $this->assertEquals(0, \core_message\api::count_blocked_users());
110
111 // Add 1 blocked and 1 normal contact to admin's contact list.
112 message_add_contact($user1->id);
113 message_add_contact($user2->id, 1);
114
115 $this->assertEquals(0, \core_message\api::count_blocked_users($user2));
116 $this->assertEquals(1, \core_message\api::count_blocked_users());
117 }
5cdebf2d 118}