PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/cohort/tests/privacy/provider_test.php

https://bitbucket.org/moodle/moodle
PHP | 343 lines | 208 code | 45 blank | 90 comment | 1 complexity | 0f6590b45a814b37b685eed82e1b8791 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Base class for unit tests for core_cohort.
  18. *
  19. * @package core_cohort
  20. * @category test
  21. * @copyright 2018 Sara Arjona <sara@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. namespace core_cohort\privacy;
  25. defined('MOODLE_INTERNAL') || die();
  26. use core_cohort\privacy\provider;
  27. use core_privacy\local\request\approved_contextlist;
  28. use core_privacy\local\request\writer;
  29. use core_privacy\tests\provider_testcase;
  30. use core_privacy\local\request\approved_userlist;
  31. /**
  32. * Unit tests for cohort\classes\privacy\provider.php
  33. *
  34. * @copyright 2018 Sara Arjona <sara@moodle.com>
  35. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36. */
  37. class provider_test extends provider_testcase {
  38. /**
  39. * Basic setup for these tests.
  40. */
  41. public function setUp(): void {
  42. $this->resetAfterTest(true);
  43. }
  44. /**
  45. * Test getting the context for the user ID related to this plugin.
  46. */
  47. public function test_get_contexts_for_userid() {
  48. // Create system cohort and category cohort.
  49. $coursecategory = $this->getDataGenerator()->create_category();
  50. $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
  51. $systemctx = \context_system::instance();
  52. $categorycohort = $this->getDataGenerator()->create_cohort([
  53. 'contextid' => $coursecategoryctx->id,
  54. 'name' => 'Category cohort 1',
  55. ]);
  56. $systemcohort = $this->getDataGenerator()->create_cohort([
  57. 'contextid' => $systemctx->id,
  58. 'name' => 'System cohort 1'
  59. ]);
  60. // Create user and add to the system and category cohorts.
  61. $user = $this->getDataGenerator()->create_user();
  62. cohort_add_member($categorycohort->id, $user->id);
  63. cohort_add_member($systemcohort->id, $user->id);
  64. // User is member of 2 cohorts.
  65. $contextlist = provider::get_contexts_for_userid($user->id);
  66. $this->assertCount(2, (array) $contextlist->get_contextids());
  67. $this->assertContainsEquals($coursecategoryctx->id, $contextlist->get_contextids());
  68. $this->assertContainsEquals($systemctx->id, $contextlist->get_contextids());
  69. }
  70. /**
  71. * Test that data is exported correctly for this plugin.
  72. */
  73. public function test_export_user_data() {
  74. // Create system cohort and category cohort.
  75. $coursecategory = $this->getDataGenerator()->create_category();
  76. $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
  77. $systemctx = \context_system::instance();
  78. $categorycohort = $this->getDataGenerator()->create_cohort([
  79. 'contextid' => $coursecategoryctx->id,
  80. 'name' => 'Category cohort 1',
  81. ]);
  82. $systemcohort1 = $this->getDataGenerator()->create_cohort([
  83. 'contextid' => $systemctx->id,
  84. 'name' => 'System cohort 1'
  85. ]);
  86. $systemcohort2 = $this->getDataGenerator()->create_cohort([
  87. 'contextid' => $systemctx->id,
  88. 'name' => 'System cohort 2'
  89. ]);
  90. // Create user and add to the system and category cohorts.
  91. $user = $this->getDataGenerator()->create_user();
  92. cohort_add_member($categorycohort->id, $user->id);
  93. cohort_add_member($systemcohort1->id, $user->id);
  94. cohort_add_member($systemcohort2->id, $user->id);
  95. // Validate system cohort exported data.
  96. $writer = writer::with_context($systemctx);
  97. $this->assertFalse($writer->has_any_data());
  98. $this->export_context_data_for_user($user->id, $systemctx, 'core_cohort');
  99. $data = $writer->get_related_data([], 'cohort');
  100. $this->assertCount(2, $data);
  101. // Validate category cohort exported data.
  102. $writer = writer::with_context($coursecategoryctx);
  103. $this->assertFalse($writer->has_any_data());
  104. $this->export_context_data_for_user($user->id, $coursecategoryctx, 'core_cohort');
  105. $data = $writer->get_related_data([], 'cohort');
  106. $this->assertCount(1, $data);
  107. $this->assertEquals($categorycohort->name, reset($data)->name);
  108. }
  109. /**
  110. * Test for provider::delete_data_for_all_users_in_context().
  111. */
  112. public function test_delete_data_for_all_users_in_context() {
  113. global $DB;
  114. // Create system cohort and category cohort.
  115. $coursecategory = $this->getDataGenerator()->create_category();
  116. $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
  117. $systemctx = \context_system::instance();
  118. $categorycohort = $this->getDataGenerator()->create_cohort([
  119. 'contextid' => $coursecategoryctx->id,
  120. 'name' => 'Category cohort 1',
  121. 'idnumber' => '',
  122. 'description' => ''
  123. ]);
  124. $systemcohort = $this->getDataGenerator()->create_cohort([
  125. 'contextid' => $systemctx->id,
  126. 'name' => 'System cohort 1'
  127. ]);
  128. // Create user and add to the system and category cohorts.
  129. $user = $this->getDataGenerator()->create_user();
  130. cohort_add_member($categorycohort->id, $user->id);
  131. cohort_add_member($systemcohort->id, $user->id);
  132. // Before deletion, we should have 2 entries in the cohort_members table.
  133. $count = $DB->count_records('cohort_members');
  134. $this->assertEquals(2, $count);
  135. // Delete data based on system context.
  136. provider::delete_data_for_all_users_in_context($systemctx);
  137. // After deletion, the cohort_members entries should have been deleted.
  138. $count = $DB->count_records('cohort_members');
  139. $this->assertEquals(1, $count);
  140. // Delete data based on category context.
  141. provider::delete_data_for_all_users_in_context($coursecategoryctx);
  142. // After deletion, the cohort_members entries should have been deleted.
  143. $count = $DB->count_records('cohort_members');
  144. $this->assertEquals(0, $count);
  145. }
  146. /**
  147. * Test for provider::delete_data_for_user().
  148. */
  149. public function test_delete_data_for_user() {
  150. global $DB;
  151. // Create system cohort and category cohort.
  152. $coursecategory = $this->getDataGenerator()->create_category();
  153. $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
  154. $systemctx = \context_system::instance();
  155. $categorycohort = $this->getDataGenerator()->create_cohort([
  156. 'contextid' => $coursecategoryctx->id,
  157. 'name' => 'Category cohort 1',
  158. 'idnumber' => '',
  159. 'description' => ''
  160. ]);
  161. $systemcohort = $this->getDataGenerator()->create_cohort([
  162. 'contextid' => $systemctx->id,
  163. 'name' => 'System cohort 1'
  164. ]);
  165. // Create user and add to the system and category cohorts.
  166. $user1 = $this->getDataGenerator()->create_user();
  167. cohort_add_member($categorycohort->id, $user1->id);
  168. cohort_add_member($systemcohort->id, $user1->id);
  169. // Create another user and add to the system and category cohorts.
  170. $user2 = $this->getDataGenerator()->create_user();
  171. cohort_add_member($categorycohort->id, $user2->id);
  172. cohort_add_member($systemcohort->id, $user2->id);
  173. // Create another user and add to the system cohort.
  174. $user3 = $this->getDataGenerator()->create_user();
  175. cohort_add_member($systemcohort->id, $user3->id);
  176. // Before deletion, we should have 5 entries in the cohort_members table.
  177. $count = $DB->count_records('cohort_members');
  178. $this->assertEquals(5, $count);
  179. $contextlist = provider::get_contexts_for_userid($user1->id);
  180. $contexts = [];
  181. $contexts[] = \context_user::instance($user1->id)->id;
  182. $contexts = array_merge($contexts, $contextlist->get_contextids());
  183. $approvedcontextlist = new approved_contextlist($user1, 'cohort', $contexts);
  184. provider::delete_data_for_user($approvedcontextlist);
  185. // After deletion, the cohort_members entries for the first student should have been deleted.
  186. $count = $DB->count_records('cohort_members', ['userid' => $user1->id]);
  187. $this->assertEquals(0, $count);
  188. $count = $DB->count_records('cohort_members');
  189. $this->assertEquals(3, $count);
  190. // Confirm that the cohorts hasn't been removed.
  191. $cohortscount = $DB->get_records('cohort');
  192. $this->assertCount(2, (array) $cohortscount);
  193. }
  194. /**
  195. * Test that only users within a course context are fetched.
  196. */
  197. public function test_get_users_in_context() {
  198. $component = 'core_cohort';
  199. // Create system cohort and category cohort.
  200. $coursecategory = $this->getDataGenerator()->create_category();
  201. $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
  202. $systemctx = \context_system::instance();
  203. $categorycohort = $this->getDataGenerator()->create_cohort([
  204. 'contextid' => $coursecategoryctx->id,
  205. 'name' => 'Category cohort 1',
  206. ]);
  207. // Create user.
  208. $user = $this->getDataGenerator()->create_user();
  209. $userctx = \context_user::instance($user->id);
  210. $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component);
  211. provider::get_users_in_context($userlist1);
  212. $this->assertCount(0, $userlist1);
  213. $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component);
  214. provider::get_users_in_context($userlist2);
  215. $this->assertCount(0, $userlist2);
  216. $systemcohort = $this->getDataGenerator()->create_cohort([
  217. 'contextid' => $systemctx->id,
  218. 'name' => 'System cohort 1'
  219. ]);
  220. // Create user and add to the system and category cohorts.
  221. cohort_add_member($categorycohort->id, $user->id);
  222. cohort_add_member($systemcohort->id, $user->id);
  223. // The list of users within the coursecat context should contain user.
  224. $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component);
  225. provider::get_users_in_context($userlist1);
  226. $this->assertCount(1, $userlist1);
  227. $expected = [$user->id];
  228. $actual = $userlist1->get_userids();
  229. $this->assertEquals($expected, $actual);
  230. // The list of users within the system context should contain user.
  231. $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component);
  232. provider::get_users_in_context($userlist2);
  233. $this->assertCount(1, $userlist2);
  234. $expected = [$user->id];
  235. $actual = $userlist2->get_userids();
  236. $this->assertEquals($expected, $actual);
  237. // The list of users within the user context should be empty.
  238. $userlist3 = new \core_privacy\local\request\userlist($userctx, $component);
  239. provider::get_users_in_context($userlist3);
  240. $this->assertCount(0, $userlist3);
  241. }
  242. /**
  243. * Test that data for users in approved userlist is deleted.
  244. */
  245. public function test_delete_data_for_users() {
  246. $component = 'core_cohort';
  247. // Create system cohort and category cohort.
  248. $coursecategory = $this->getDataGenerator()->create_category();
  249. $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
  250. $systemctx = \context_system::instance();
  251. $categorycohort = $this->getDataGenerator()->create_cohort([
  252. 'contextid' => $coursecategoryctx->id,
  253. 'name' => 'Category cohort 1',
  254. ]);
  255. // Create user1.
  256. $user1 = $this->getDataGenerator()->create_user();
  257. $userctx1 = \context_user::instance($user1->id);
  258. // Create user2.
  259. $user2 = $this->getDataGenerator()->create_user();
  260. $systemcohort = $this->getDataGenerator()->create_cohort([
  261. 'contextid' => $systemctx->id,
  262. 'name' => 'System cohort 1'
  263. ]);
  264. // Create user and add to the system and category cohorts.
  265. cohort_add_member($categorycohort->id, $user1->id);
  266. cohort_add_member($systemcohort->id, $user1->id);
  267. cohort_add_member($categorycohort->id, $user2->id);
  268. $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component);
  269. provider::get_users_in_context($userlist1);
  270. $this->assertCount(2, $userlist1);
  271. $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component);
  272. provider::get_users_in_context($userlist2);
  273. $this->assertCount(1, $userlist2);
  274. // Convert $userlist1 into an approved_contextlist.
  275. $approvedlist1 = new approved_userlist($coursecategoryctx, $component, $userlist1->get_userids());
  276. // Delete using delete_data_for_user.
  277. provider::delete_data_for_users($approvedlist1);
  278. // Re-fetch users in coursecategoryctx.
  279. $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component);
  280. provider::get_users_in_context($userlist1);
  281. // The user data in coursecategoryctx should be deleted.
  282. $this->assertCount(0, $userlist1);
  283. // Re-fetch users in coursecategoryctx.
  284. $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component);
  285. provider::get_users_in_context($userlist2);
  286. // The user data in coursecontext2 should be still present.
  287. $this->assertCount(1, $userlist2);
  288. // Convert $userlist2 into an approved_contextlist in the user context.
  289. $approvedlist3 = new approved_userlist($userctx1, $component, $userlist2->get_userids());
  290. // Delete using delete_data_for_user.
  291. provider::delete_data_for_users($approvedlist3);
  292. // Re-fetch users in coursecontext1.
  293. $userlist3 = new \core_privacy\local\request\userlist($systemctx, $component);
  294. provider::get_users_in_context($userlist3);
  295. // The user data in systemcontext should not be deleted.
  296. $this->assertCount(1, $userlist3);
  297. }
  298. }