/admin/tool/dataprivacy/tests/task_test.php

https://github.com/markn86/moodle · PHP · 237 lines · 103 code · 40 blank · 94 comment · 1 complexity · 081d7b5ea6e1071ac9a7241c705fba8f 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 for scheduled tasks.
  18. *
  19. * @package tool_dataprivacy
  20. * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once('data_privacy_testcase.php');
  25. use tool_dataprivacy\api;
  26. /**
  27. * Tests for scheduled tasks.
  28. *
  29. * @package tool_dataprivacy
  30. * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class tool_dataprivacy_task_testcase extends data_privacy_testcase {
  34. /**
  35. * Test tearDown.
  36. */
  37. public function tearDown(): void {
  38. \core_privacy\local\request\writer::reset();
  39. }
  40. /**
  41. * Ensure that a delete data request for pre-existing deleted users
  42. * is created when there are not any existing data requests
  43. * for that particular user.
  44. */
  45. public function test_delete_existing_deleted_users_task_no_previous_requests() {
  46. global $DB;
  47. $this->resetAfterTest();
  48. $this->setAdminUser();
  49. // Enable automatic creation of delete data requests.
  50. set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
  51. // Create a user.
  52. $user = $this->getDataGenerator()->create_user();
  53. // Mark the user as deleted.
  54. $user->deleted = 1;
  55. $DB->update_record('user', $user);
  56. // The user should not have a delete data request.
  57. $this->assertCount(0, api::get_data_requests($user->id, [],
  58. [api::DATAREQUEST_TYPE_DELETE]));
  59. $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users');
  60. // After running the scheduled task, the deleted user should have a delete data request.
  61. $this->assertCount(1, api::get_data_requests($user->id, [],
  62. [api::DATAREQUEST_TYPE_DELETE]));
  63. }
  64. /**
  65. * Ensure that a delete data request for pre-existing deleted users
  66. * is not being created when automatic creation of delete data requests is disabled.
  67. */
  68. public function test_delete_existing_deleted_users_task_automatic_creation_disabled() {
  69. global $DB;
  70. $this->resetAfterTest();
  71. $this->setAdminUser();
  72. // Disable automatic creation of delete data requests.
  73. set_config('automaticdeletionrequests', 0, 'tool_dataprivacy');
  74. // Create a user.
  75. $user = $this->getDataGenerator()->create_user();
  76. // Mark the user as deleted.
  77. $user->deleted = 1;
  78. $DB->update_record('user', $user);
  79. // The user should not have a delete data request.
  80. $this->assertCount(0, api::get_data_requests($user->id, [],
  81. [api::DATAREQUEST_TYPE_DELETE]));
  82. $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users');
  83. // After running the scheduled task, the deleted user should still not have a delete data request.
  84. $this->assertCount(0, api::get_data_requests($user->id, [],
  85. [api::DATAREQUEST_TYPE_DELETE]));
  86. }
  87. /**
  88. * Ensure that a delete data request for pre-existing deleted users
  89. * is created when there are existing non-delete data requests
  90. * for that particular user.
  91. */
  92. public function test_delete_existing_deleted_users_task_existing_export_data_requests() {
  93. global $DB;
  94. $this->resetAfterTest();
  95. $this->setAdminUser();
  96. // Enable automatic creation of delete data requests.
  97. set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
  98. // Create a user.
  99. $user = $this->getDataGenerator()->create_user();
  100. // Create export data request for the user.
  101. api::create_data_request($user->id, api::DATAREQUEST_TYPE_EXPORT);
  102. // Mark the user as deleted.
  103. $user->deleted = 1;
  104. $DB->update_record('user', $user);
  105. // The user should have a export data request.
  106. $this->assertCount(1, api::get_data_requests($user->id, [],
  107. [api::DATAREQUEST_TYPE_EXPORT]));
  108. // The user should not have a delete data request.
  109. $this->assertCount(0, api::get_data_requests($user->id, [],
  110. [api::DATAREQUEST_TYPE_DELETE]));
  111. $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users');
  112. // After running the scheduled task, the deleted user should have a delete data request.
  113. $this->assertCount(1, api::get_data_requests($user->id, [],
  114. [api::DATAREQUEST_TYPE_DELETE]));
  115. }
  116. /**
  117. * Ensure that a delete data request for pre-existing deleted users
  118. * is not created when there are existing ongoing delete data requests
  119. * for that particular user.
  120. */
  121. public function test_delete_existing_deleted_users_task_existing_ongoing_delete_data_requests() {
  122. global $DB;
  123. $this->resetAfterTest();
  124. $this->setAdminUser();
  125. // Enable automatic creation of delete data requests.
  126. set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
  127. // Create a user.
  128. $user = $this->getDataGenerator()->create_user();
  129. $this->setUser($user);
  130. // Create delete data request for the user.
  131. $datarequest = api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE);
  132. $requestid = $datarequest->get('id');
  133. api::update_request_status($requestid, api::DATAREQUEST_STATUS_AWAITING_APPROVAL);
  134. // The user should have an ongoing delete data request.
  135. $this->assertCount(1, api::get_data_requests($user->id,
  136. [api::DATAREQUEST_STATUS_AWAITING_APPROVAL], [api::DATAREQUEST_TYPE_DELETE]));
  137. // Mark the user as deleted.
  138. $user->deleted = 1;
  139. $DB->update_record('user', $user);
  140. // The user should still have the existing ongoing delete data request.
  141. $this->assertCount(1, \tool_dataprivacy\api::get_data_requests($user->id,
  142. [api::DATAREQUEST_STATUS_AWAITING_APPROVAL], [api::DATAREQUEST_TYPE_DELETE]));
  143. $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users');
  144. // After running the scheduled task, the user should have only one delete data request.
  145. $this->assertCount(1, api::get_data_requests($user->id, [],
  146. [api::DATAREQUEST_TYPE_DELETE]));
  147. }
  148. /**
  149. * Ensure that a delete data request for pre-existing deleted users
  150. * is not created when there are existing finished delete data requests
  151. * for that particular user.
  152. */
  153. public function test_delete_existing_deleted_users_task_existing_finished_delete_data_requests() {
  154. global $DB;
  155. $this->resetAfterTest();
  156. $this->setAdminUser();
  157. // Enable automatic creation of delete data requests.
  158. set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
  159. // Create a user.
  160. $user = $this->getDataGenerator()->create_user();
  161. $this->setUser($user);
  162. // Create delete data request for the user.
  163. $datarequest = api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE);
  164. $requestid = $datarequest->get('id');
  165. api::update_request_status($requestid, api::DATAREQUEST_STATUS_CANCELLED);
  166. // The user should have a delete data request.
  167. $this->assertCount(1, api::get_data_requests($user->id, [],
  168. [api::DATAREQUEST_TYPE_DELETE]));
  169. // The user should not have an ongoing data requests.
  170. $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE));
  171. // Mark the user as deleted.
  172. $user->deleted = 1;
  173. $DB->update_record('user', $user);
  174. // The user should still have the existing cancelled delete data request.
  175. $this->assertCount(1, \tool_dataprivacy\api::get_data_requests($user->id,
  176. [api::DATAREQUEST_STATUS_CANCELLED], [api::DATAREQUEST_TYPE_DELETE]));
  177. $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users');
  178. // After running the scheduled task, the user should still have one delete data requests.
  179. $this->assertCount(1, api::get_data_requests($user->id, [],
  180. [api::DATAREQUEST_TYPE_DELETE]));
  181. // The user should only have the existing cancelled delete data request.
  182. $this->assertCount(1, \tool_dataprivacy\api::get_data_requests($user->id,
  183. [api::DATAREQUEST_STATUS_CANCELLED], [api::DATAREQUEST_TYPE_DELETE]));
  184. }
  185. /**
  186. * Helper to execute a particular task.
  187. *
  188. * @param string $task The task.
  189. */
  190. private function execute_task($task) {
  191. // Run the scheduled task.
  192. ob_start();
  193. $task = \core\task\manager::get_scheduled_task($task);
  194. $task->execute();
  195. ob_end_clean();
  196. }
  197. }