PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/message/tests/messagelib_test.php

https://github.com/itamart/moodle
PHP | 361 lines | 171 code | 67 blank | 123 comment | 1 complexity | eb01d5279f7d525190533fbb38f4ed1c MD5 | raw file
  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. * Test api's in message lib.
  18. *
  19. * @package core_message
  20. * @category test
  21. * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->dirroot . '/message/lib.php');
  27. /**
  28. * Test api's in message lib.
  29. *
  30. * @package core_message
  31. * @category test
  32. * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. class core_message_messagelib_testcase extends advanced_testcase {
  36. /** @var phpunit_message_sink keep track of messages. */
  37. protected $messagesink = null;
  38. /**
  39. * Test set up.
  40. *
  41. * This is executed before running any test in this file.
  42. */
  43. public function setUp() {
  44. $this->preventResetByRollback(); // Messaging is not compatible with transactions.
  45. $this->messagesink = $this->redirectMessages();
  46. $this->resetAfterTest();
  47. }
  48. /**
  49. * Send a fake message.
  50. *
  51. * {@link message_send()} does not support transaction, this function will simulate a message
  52. * sent from a user to another. We should stop using it once {@link message_send()} will support
  53. * transactions. This is not clean at all, this is just used to add rows to the table.
  54. *
  55. * @param stdClass $userfrom user object of the one sending the message.
  56. * @param stdClass $userto user object of the one receiving the message.
  57. * @param string $message message to send.
  58. */
  59. protected function send_fake_message($userfrom, $userto, $message = 'Hello world!') {
  60. global $DB;
  61. $record = new stdClass();
  62. $record->useridfrom = $userfrom->id;
  63. $record->useridto = $userto->id;
  64. $record->subject = 'No subject';
  65. $record->fullmessage = $message;
  66. $record->timecreated = time();
  67. $insert = $DB->insert_record('message', $record);
  68. }
  69. /**
  70. * Test message_get_blocked_users.
  71. */
  72. public function test_message_get_blocked_users() {
  73. // Set this user as the admin.
  74. $this->setAdminUser();
  75. // Create a user to add to the admin's contact list.
  76. $user1 = $this->getDataGenerator()->create_user();
  77. $user2 = $this->getDataGenerator()->create_user();
  78. // Add users to the admin's contact list.
  79. message_add_contact($user1->id);
  80. message_add_contact($user2->id, 1);
  81. $this->assertCount(1, message_get_blocked_users());
  82. // Block other user.
  83. message_block_contact($user1->id);
  84. $this->assertCount(2, message_get_blocked_users());
  85. }
  86. /**
  87. * Test message_get_contacts.
  88. */
  89. public function test_message_get_contacts() {
  90. global $USER, $CFG;
  91. // Set this user as the admin.
  92. $this->setAdminUser();
  93. $noreplyuser = core_user::get_noreply_user();
  94. $supportuser = core_user::get_support_user();
  95. // Create a user to add to the admin's contact list.
  96. $user1 = $this->getDataGenerator()->create_user();
  97. $user2 = $this->getDataGenerator()->create_user();
  98. $user3 = $this->getDataGenerator()->create_user(); // Stranger.
  99. // Add users to the admin's contact list.
  100. message_add_contact($user1->id);
  101. message_add_contact($user2->id);
  102. // Send some messages.
  103. $this->send_fake_message($user1, $USER);
  104. $this->send_fake_message($user2, $USER);
  105. $this->send_fake_message($user3, $USER);
  106. list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
  107. $this->assertCount(0, $onlinecontacts);
  108. $this->assertCount(2, $offlinecontacts);
  109. $this->assertCount(1, $strangers);
  110. // Send message from noreply and support users.
  111. $this->send_fake_message($noreplyuser, $USER);
  112. $this->send_fake_message($supportuser, $USER);
  113. list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
  114. $this->assertCount(0, $onlinecontacts);
  115. $this->assertCount(2, $offlinecontacts);
  116. $this->assertCount(3, $strangers);
  117. // Block 1 user.
  118. message_block_contact($user2->id);
  119. list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
  120. $this->assertCount(0, $onlinecontacts);
  121. $this->assertCount(1, $offlinecontacts);
  122. $this->assertCount(3, $strangers);
  123. // Noreply user being valid user.
  124. core_user::reset_internal_users();
  125. $CFG->noreplyuserid = $user3->id;
  126. $noreplyuser = core_user::get_noreply_user();
  127. list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
  128. $this->assertCount(0, $onlinecontacts);
  129. $this->assertCount(1, $offlinecontacts);
  130. $this->assertCount(2, $strangers);
  131. }
  132. /**
  133. * Test message_count_messages.
  134. */
  135. public function test_message_count_messages() {
  136. global $DB;
  137. // Create users to send and receive message.
  138. $userfrom = $this->getDataGenerator()->create_user();
  139. $userto = $this->getDataGenerator()->create_user();
  140. message_post_message($userfrom, $userto, 'Message 1', FORMAT_PLAIN);
  141. message_post_message($userfrom, $userto, 'Message 2', FORMAT_PLAIN);
  142. message_post_message($userto, $userfrom, 'Message 3', FORMAT_PLAIN);
  143. // Return 0 when no message.
  144. $messages = array();
  145. $this->assertEquals(0, message_count_messages($messages, 'Test', 'Test'));
  146. // Check number of messages from userfrom and userto.
  147. $messages = $this->messagesink->get_messages();
  148. $this->assertEquals(2, message_count_messages($messages, 'useridfrom', $userfrom->id));
  149. $this->assertEquals(1, message_count_messages($messages, 'useridfrom', $userto->id));
  150. }
  151. /**
  152. * Test message_count_unread_messages.
  153. */
  154. public function test_message_count_unread_messages() {
  155. // Create users to send and receive message.
  156. $userfrom1 = $this->getDataGenerator()->create_user();
  157. $userfrom2 = $this->getDataGenerator()->create_user();
  158. $userto = $this->getDataGenerator()->create_user();
  159. $this->assertEquals(0, message_count_unread_messages($userto));
  160. // Send fake messages.
  161. $this->send_fake_message($userfrom1, $userto);
  162. $this->send_fake_message($userfrom2, $userto);
  163. $this->assertEquals(2, message_count_unread_messages($userto));
  164. $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1));
  165. }
  166. /**
  167. * Test message_count_blocked_users.
  168. *
  169. */
  170. public function test_message_count_blocked_users() {
  171. // Set this user as the admin.
  172. $this->setAdminUser();
  173. // Create users to add to the admin's contact list.
  174. $user1 = $this->getDataGenerator()->create_user();
  175. $user2 = $this->getDataGenerator()->create_user();
  176. $this->assertEquals(0, message_count_blocked_users());
  177. // Add 1 blocked and 1 normal contact to admin's contact list.
  178. message_add_contact($user1->id);
  179. message_add_contact($user2->id, 1);
  180. $this->assertEquals(0, message_count_blocked_users($user2));
  181. $this->assertEquals(1, message_count_blocked_users());
  182. }
  183. /**
  184. * Test message_add_contact.
  185. */
  186. public function test_message_add_contact() {
  187. // Set this user as the admin.
  188. $this->setAdminUser();
  189. // Create a user to add to the admin's contact list.
  190. $user1 = $this->getDataGenerator()->create_user();
  191. $user2 = $this->getDataGenerator()->create_user();
  192. $user3 = $this->getDataGenerator()->create_user();
  193. message_add_contact($user1->id);
  194. message_add_contact($user2->id, 0);
  195. // Add duplicate contact and make sure only 1 record exists.
  196. message_add_contact($user2->id, 1);
  197. $this->assertNotEmpty(message_get_contact($user1->id));
  198. $this->assertNotEmpty(message_get_contact($user2->id));
  199. $this->assertEquals(false, message_get_contact($user3->id));
  200. $this->assertEquals(1, message_count_blocked_users());
  201. }
  202. /**
  203. * Test message_remove_contact.
  204. */
  205. public function test_message_remove_contact() {
  206. // Set this user as the admin.
  207. $this->setAdminUser();
  208. // Create a user to add to the admin's contact list.
  209. $user = $this->getDataGenerator()->create_user();
  210. // Add the user to the admin's contact list.
  211. message_add_contact($user->id);
  212. $this->assertNotEmpty(message_get_contact($user->id));
  213. // Remove user from admin's contact list.
  214. message_remove_contact($user->id);
  215. $this->assertEquals(false, message_get_contact($user->id));
  216. }
  217. /**
  218. * Test message_block_contact.
  219. */
  220. public function test_message_block_contact() {
  221. // Set this user as the admin.
  222. $this->setAdminUser();
  223. // Create a user to add to the admin's contact list.
  224. $user1 = $this->getDataGenerator()->create_user();
  225. $user2 = $this->getDataGenerator()->create_user();
  226. // Add users to the admin's contact list.
  227. message_add_contact($user1->id);
  228. message_add_contact($user2->id);
  229. $this->assertEquals(0, message_count_blocked_users());
  230. // Block 1 user.
  231. message_block_contact($user2->id);
  232. $this->assertEquals(1, message_count_blocked_users());
  233. }
  234. /**
  235. * Test message_unblock_contact.
  236. */
  237. public function test_message_unblock_contact() {
  238. // Set this user as the admin.
  239. $this->setAdminUser();
  240. // Create a user to add to the admin's contact list.
  241. $user1 = $this->getDataGenerator()->create_user();
  242. $user2 = $this->getDataGenerator()->create_user();
  243. // Add users to the admin's contact list.
  244. message_add_contact($user1->id);
  245. message_add_contact($user2->id, 1); // Add blocked contact.
  246. $this->assertEquals(1, message_count_blocked_users());
  247. // Unblock user.
  248. message_unblock_contact($user2->id);
  249. $this->assertEquals(0, message_count_blocked_users());
  250. }
  251. /**
  252. * Test message_search_users.
  253. */
  254. public function test_message_search_users() {
  255. // Set this user as the admin.
  256. $this->setAdminUser();
  257. // Create a user to add to the admin's contact list.
  258. $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
  259. $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
  260. // Add users to the admin's contact list.
  261. message_add_contact($user1->id);
  262. message_add_contact($user2->id); // Add blocked contact.
  263. $this->assertCount(1, message_search_users(0, 'Test1'));
  264. $this->assertCount(2, message_search_users(0, 'Test'));
  265. $this->assertCount(1, message_search_users(0, 'user1'));
  266. $this->assertCount(2, message_search_users(0, 'user'));
  267. }
  268. /**
  269. * Test message_search.
  270. */
  271. public function test_message_search() {
  272. global $USER;
  273. // Set this user as the admin.
  274. $this->setAdminUser();
  275. // Create a user to add to the admin's contact list.
  276. $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
  277. $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
  278. // Send few messages, real (read).
  279. message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
  280. message_post_message($USER, $user1, 'Message 2', FORMAT_PLAIN);
  281. message_post_message($USER, $user2, 'Message 3', FORMAT_PLAIN);
  282. $this->assertCount(2, message_search(array('Message'), true, false));
  283. $this->assertCount(3, message_search(array('Message'), true, true));
  284. // Send fake message (not-read).
  285. $this->send_fake_message($USER, $user1, 'Message 4');
  286. $this->send_fake_message($user1, $USER, 'Message 5');
  287. $this->assertCount(3, message_search(array('Message'), true, false));
  288. $this->assertCount(5, message_search(array('Message'), true, true));
  289. // If courseid given then should be 0.
  290. $this->assertEquals(false, message_search(array('Message'), true, true, ''));
  291. $this->assertEquals(false, message_search(array('Message'), true, true, 2));
  292. $this->assertCount(5, message_search(array('Message'), true, true, SITEID));
  293. }
  294. }