PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/message/tests/migrate_message_data_task_test.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 333 lines | 200 code | 52 blank | 81 comment | 22 complexity | e5fadb0a5d1eb833c2cb20740c9c61fa 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. * Tests the migrate message data task.
  18. *
  19. * @package core_message
  20. * @category test
  21. * @copyright 2018 Mark Nelson <markn@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/tests/messagelib_test.php');
  27. /**
  28. * Class for testing the migrate message data task.
  29. *
  30. * @package core_message
  31. * @category test
  32. * @copyright 2018 Mark Nelson <markn@moodle.com>
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. class core_message_migrate_message_data_task_testcase extends advanced_testcase {
  36. /**
  37. * Test set up.
  38. *
  39. * This is executed before running any test in this file.
  40. */
  41. public function setUp() {
  42. $this->resetAfterTest();
  43. }
  44. /**
  45. * Test migrating legacy messages.
  46. */
  47. public function test_migrating_messages() {
  48. global $DB;
  49. // Create users to test with.
  50. $user1 = $this->getDataGenerator()->create_user();
  51. $user2 = $this->getDataGenerator()->create_user();
  52. $user3 = $this->getDataGenerator()->create_user();
  53. // Get the current time minus some, to make sure our data is migrated accurately and just not using the current timestamp.
  54. $now = time();
  55. $timedeleted1 = $now - (2 * DAYSECS);
  56. $timedeleted2 = $now - (2 * DAYSECS) + 1;
  57. $timeread1 = $now - DAYSECS;
  58. $timeread2 = $now - DAYSECS + 1;
  59. $timeread3 = $now - DAYSECS + 2;
  60. // Send messages from user 1 to user 2.
  61. $m1 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 1, false, $timeread1);
  62. $m2 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 2);
  63. $m3 = $this->create_legacy_message_or_notification($user1->id, $user2->id, 3);
  64. // Send messages from user 3 to user 1.
  65. $m4 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 4, false, $timeread2);
  66. $m5 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 5);
  67. $m6 = $this->create_legacy_message_or_notification($user3->id, $user1->id, 6);
  68. // Send messages from user 3 to user 2.
  69. $m7 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 7, false, $timeread3);
  70. $m8 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 8);
  71. $m9 = $this->create_legacy_message_or_notification($user3->id, $user2->id, 9);
  72. // Let's delete some messages, not using API here as it does not use the legacy tables.
  73. $messageupdate = new stdClass();
  74. $messageupdate->id = $m1;
  75. $messageupdate->timeusertodeleted = $timedeleted1;
  76. $DB->update_record('message_read', $messageupdate);
  77. $messageupdate = new stdClass();
  78. $messageupdate->id = $m5;
  79. $messageupdate->timeuserfromdeleted = $timedeleted2;
  80. $DB->update_record('message', $messageupdate);
  81. // Now, let's execute the task for user 1.
  82. $task = new \core_message\task\migrate_message_data();
  83. $task->set_custom_data(
  84. [
  85. 'userid' => $user1->id
  86. ]
  87. );
  88. $task->execute();
  89. // Ok, now we need to confirm all is good.
  90. // Remember - we are only converting the messages related to user 1.
  91. $this->assertEquals(2, $DB->count_records('message'));
  92. $this->assertEquals(1, $DB->count_records('message_read'));
  93. $this->assertEquals(6, $DB->count_records('messages'));
  94. $this->assertEquals(0, $DB->count_records('notifications'));
  95. $this->assertEquals(0, $DB->count_records('message_popup_notifications'));
  96. // Get the conversations.
  97. $conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
  98. $conversation2 = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
  99. // Confirm what we have in the messages table is correct.
  100. $messages = $DB->get_records('messages', [], 'timecreated ASC');
  101. $i = 1;
  102. foreach ($messages as $message) {
  103. $useridfrom = $user1->id;
  104. $conversationid = $conversation1;
  105. if ($i > 3) {
  106. $useridfrom = $user3->id;
  107. $conversationid = $conversation2;
  108. }
  109. if ($i == 1) {
  110. $messagereadid1 = $message->id;
  111. $messagedeletedid1 = $message->id;
  112. } else if ($i == 4) {
  113. $messagereadid2 = $message->id;
  114. } else if ($i == 5) {
  115. $messagedeletedid2 = $message->id;
  116. }
  117. $this->assertEquals($useridfrom, $message->useridfrom);
  118. $this->assertEquals($conversationid, $message->conversationid);
  119. $this->assertEquals('Subject ' . $i, $message->subject);
  120. $this->assertEquals('Full message ' . $i, $message->fullmessage);
  121. $this->assertEquals(FORMAT_PLAIN, $message->fullmessageformat);
  122. $this->assertEquals('Full message HTML '. $i, $message->fullmessagehtml);
  123. $this->assertEquals('Small message ' . $i, $message->smallmessage);
  124. $this->assertEquals($i, $message->timecreated);
  125. $i++;
  126. }
  127. // Confirm there are 4 actions.
  128. $this->assertEquals(4, $DB->count_records('message_user_actions'));
  129. // Confirm the messages that were marked as read have actions associated with them.
  130. $muas = $DB->get_records('message_user_actions', ['action' => \core_message\api::MESSAGE_ACTION_READ], 'timecreated DESC');
  131. $this->assertCount(2, $muas);
  132. // Message user action for message read by user 1 (referring to $m4).
  133. $mua1 = array_shift($muas);
  134. // Message user action for message read by user 2 (referring to $m1).
  135. $mua2 = array_shift($muas);
  136. $this->assertEquals($user1->id, $mua1->userid);
  137. $this->assertEquals($messagereadid2, $mua1->messageid);
  138. $this->assertEquals($timeread2, $mua1->timecreated);
  139. $this->assertEquals($user2->id, $mua2->userid);
  140. $this->assertEquals($messagereadid1, $mua2->messageid);
  141. $this->assertEquals($timeread1, $mua2->timecreated);
  142. // Confirm the messages that were deleted have actions associated with them.
  143. $muas = $DB->get_records('message_user_actions', ['action' => \core_message\api::MESSAGE_ACTION_DELETED],
  144. 'timecreated DESC');
  145. $this->assertCount(2, $muas);
  146. // Message user action for message deleted by user 3 (referring to $m5).
  147. $mua1 = array_shift($muas);
  148. // Message user action for message deleted by user 2 (referring to $m1).
  149. $mua2 = array_shift($muas);
  150. $this->assertEquals($user3->id, $mua1->userid);
  151. $this->assertEquals($messagedeletedid2, $mua1->messageid);
  152. $this->assertEquals($timedeleted2, $mua1->timecreated);
  153. $this->assertEquals($user2->id, $mua2->userid);
  154. $this->assertEquals($messagedeletedid1, $mua2->messageid);
  155. $this->assertEquals($timedeleted1, $mua2->timecreated);
  156. }
  157. /**
  158. * Test migrating legacy notifications.
  159. */
  160. public function test_migrating_notifications() {
  161. global $DB;
  162. // Create users to test with.
  163. $user1 = $this->getDataGenerator()->create_user();
  164. $user2 = $this->getDataGenerator()->create_user();
  165. $user3 = $this->getDataGenerator()->create_user();
  166. // Get the current time minus some, to make sure our data is migrated accurately and just not using the current timestamp.
  167. $timeread = time() - DAYSECS;
  168. // Send notifications from user 1 to user 2.
  169. $this->create_legacy_message_or_notification($user1->id, $user2->id, 1, true, $timeread);
  170. $this->create_legacy_message_or_notification($user1->id, $user2->id, 2, true);
  171. $this->create_legacy_message_or_notification($user1->id, $user2->id, 3, true);
  172. // Send notifications from user 3 to user 1.
  173. $this->create_legacy_message_or_notification($user3->id, $user1->id, 4, true, $timeread);
  174. $this->create_legacy_message_or_notification($user3->id, $user1->id, 5, true);
  175. $this->create_legacy_message_or_notification($user3->id, $user1->id, 6, true);
  176. // Send notifications from user 3 to user 2.
  177. $this->create_legacy_message_or_notification($user3->id, $user2->id, 7, true, $timeread);
  178. $this->create_legacy_message_or_notification($user3->id, $user2->id, 8, true);
  179. $this->create_legacy_message_or_notification($user3->id, $user2->id, 9, true);
  180. // Now, let's execute the task for user 1.
  181. $task = new \core_message\task\migrate_message_data();
  182. $task->set_custom_data(
  183. [
  184. 'userid' => $user1->id
  185. ]
  186. );
  187. $task->execute();
  188. // Ok, now we need to confirm all is good.
  189. // Remember - we are only converting the notifications related to user 1.
  190. $this->assertEquals(2, $DB->count_records('message'));
  191. $this->assertEquals(1, $DB->count_records('message_read'));
  192. $this->assertEquals(3, $DB->count_records('message_popup'));
  193. $this->assertEquals(6, $DB->count_records('notifications'));
  194. $this->assertEquals(6, $DB->count_records('message_popup_notifications'));
  195. // Confirm what we have in the notifications table is correct.
  196. $notifications = $DB->get_records('notifications', [], 'timecreated ASC');
  197. $popupnotifications = $DB->get_records('message_popup_notifications', [], 'notificationid ASC', 'notificationid');
  198. $i = 1;
  199. foreach ($notifications as $notification) {
  200. // Assert the correct id is stored in the 'message_popup_notifications' table.
  201. $this->assertArrayHasKey($notification->id, $popupnotifications);
  202. $useridfrom = $user1->id;
  203. $useridto = $user2->id;
  204. if ($i > 3) {
  205. $useridfrom = $user3->id;
  206. $useridto = $user1->id;
  207. }
  208. $this->assertEquals($useridfrom, $notification->useridfrom);
  209. $this->assertEquals($useridto, $notification->useridto);
  210. $this->assertEquals('Subject ' . $i, $notification->subject);
  211. $this->assertEquals('Full message ' . $i, $notification->fullmessage);
  212. $this->assertEquals(FORMAT_PLAIN, $notification->fullmessageformat);
  213. $this->assertEquals('Full message HTML '. $i, $notification->fullmessagehtml);
  214. $this->assertEquals('Small message ' . $i, $notification->smallmessage);
  215. $this->assertEquals('mod_assign', $notification->component);
  216. $this->assertEquals('assign_notification', $notification->eventtype);
  217. $this->assertEquals('https://www.google.com', $notification->contexturl);
  218. $this->assertEquals('google', $notification->contexturlname);
  219. $this->assertEquals($i, $notification->timecreated);
  220. if (($i == 1) || ($i == 4)) {
  221. $this->assertEquals($timeread, $notification->timeread);
  222. } else {
  223. $this->assertNull($notification->timeread);
  224. }
  225. $i++;
  226. }
  227. }
  228. /**
  229. * Creates a legacy message or notification to be used for testing.
  230. *
  231. * @param int $useridfrom The user id from
  232. * @param int $useridto The user id to
  233. * @param int $timecreated
  234. * @param bool $notification
  235. * @param int|null $timeread The time the message/notification was read, null if it hasn't been.
  236. * @return int The id of the message (in either the message or message_read table)
  237. * @throws dml_exception
  238. */
  239. private function create_legacy_message_or_notification($useridfrom, $useridto, $timecreated = null,
  240. $notification = false, $timeread = null) {
  241. global $DB;
  242. $tabledata = new \stdClass();
  243. if (is_null($timecreated)) {
  244. $timecreated = time();
  245. }
  246. if (!is_null($timeread)) {
  247. $table = 'message_read';
  248. $tabledata->timeread = $timeread;
  249. } else {
  250. $table = 'message';
  251. }
  252. if ($notification) {
  253. $tabledata->eventtype = 'assign_notification';
  254. $tabledata->component = 'mod_assign';
  255. $tabledata->notification = 1;
  256. $tabledata->contexturl = 'https://www.google.com';
  257. $tabledata->contexturlname = 'google';
  258. } else {
  259. $tabledata->eventtype = 'instantmessage';
  260. $tabledata->component = 'moodle';
  261. $tabledata->notification = 0;
  262. }
  263. $tabledata->useridfrom = $useridfrom;
  264. $tabledata->useridto = $useridto;
  265. $tabledata->subject = 'Subject ' . $timecreated;
  266. $tabledata->fullmessage = 'Full message ' . $timecreated;
  267. $tabledata->fullmessageformat = FORMAT_PLAIN;
  268. $tabledata->fullmessagehtml = 'Full message HTML ' . $timecreated;
  269. $tabledata->smallmessage = 'Small message ' . $timecreated;
  270. $tabledata->timecreated = $timecreated;
  271. $id = $DB->insert_record($table, $tabledata);
  272. // Insert into the legacy 'message_popup' table if it is a notification.
  273. if ($notification) {
  274. $mp = new stdClass();
  275. $mp->messageid = $id;
  276. $mp->isread = (!is_null($timeread)) ? 1 : 0;
  277. $DB->insert_record('message_popup', $mp);
  278. }
  279. return $id;
  280. }
  281. }