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

/auth/lti/tests/privacy/provider_test.php

https://bitbucket.org/moodle/moodle
PHP | 240 lines | 126 code | 36 blank | 78 comment | 0 complexity | 7aef64e63e19c7d10989c182645d12e3 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. namespace auth_lti\privacy;
  17. use core_privacy\local\request\approved_contextlist;
  18. use core_privacy\local\request\userlist;
  19. use core_privacy\local\request\writer;
  20. use core_privacy\tests\provider_testcase;
  21. use core_privacy\local\request\approved_userlist;
  22. /**
  23. * Test for the auth_lti privacy provider.
  24. *
  25. * @package auth_lti
  26. * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
  27. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28. * @coversDefaultClass \auth_lti\privacy\provider
  29. */
  30. class provider_test extends provider_testcase {
  31. /**
  32. * Set up method.
  33. */
  34. public function setUp(): void {
  35. $this->resetAfterTest();
  36. $this->setAdminUser();
  37. }
  38. /**
  39. * Check that a user context is returned if there is any user data for this user.
  40. *
  41. * @covers ::get_contexts_for_userid
  42. */
  43. public function test_get_contexts_for_userid() {
  44. $user = $this->getDataGenerator()->create_user();
  45. $this->assertEmpty(provider::get_contexts_for_userid($user->id));
  46. $auth = get_auth_plugin('lti');
  47. $auth->create_user_binding('https://lms.example.com', 'abc123', $user->id);
  48. $contextlist = provider::get_contexts_for_userid($user->id);
  49. // Check that we only get back one context.
  50. $this->assertCount(1, $contextlist);
  51. // Check that a context is returned is the expected.
  52. $usercontext = \context_user::instance($user->id);
  53. $this->assertEquals($usercontext->id, $contextlist->get_contextids()[0]);
  54. }
  55. /**
  56. * Test that user data is exported correctly.
  57. *
  58. * @covers ::export_user_data
  59. */
  60. public function test_export_user_data() {
  61. $user = $this->getDataGenerator()->create_user();
  62. $auth = get_auth_plugin('lti');
  63. $auth->create_user_binding('https://lms.example.com', 'abc123', $user->id);
  64. $usercontext = \context_user::instance($user->id);
  65. $writer = writer::with_context($usercontext);
  66. $this->assertFalse($writer->has_any_data());
  67. $approvedlist = new approved_contextlist($user, 'auth_lti', [$usercontext->id]);
  68. provider::export_user_data($approvedlist);
  69. $data = $writer->get_data([get_string('privacy:metadata:auth_lti', 'auth_lti'), 'https://lms.example.com']);
  70. $this->assertEquals('https://lms.example.com', $data->issuer);
  71. $this->assertEquals(hash('sha256', 'https://lms.example.com'), $data->issuer256);
  72. $this->assertEquals('abc123', $data->sub);
  73. $this->assertEquals(hash('sha256', 'abc123'), $data->sub256);
  74. }
  75. /**
  76. * Test deleting all user data for a specific context.
  77. *
  78. * @covers ::delete_data_for_all_users_in_context
  79. */
  80. public function test_delete_data_for_all_users_in_context() {
  81. global $DB;
  82. $auth = get_auth_plugin('lti');
  83. $user1 = $this->getDataGenerator()->create_user();
  84. $auth->create_user_binding('https://lms.example.com', 'abc123', $user1->id);
  85. $user1context = \context_user::instance($user1->id);
  86. $user2 = $this->getDataGenerator()->create_user();
  87. $auth->create_user_binding('https://lms.example.com', 'def456', $user2->id);
  88. // Verify there are two linked logins.
  89. $ltiaccounts = $DB->get_records('auth_lti_linked_login');
  90. $this->assertCount(2, $ltiaccounts);
  91. // Delete everything for the first user context.
  92. provider::delete_data_for_all_users_in_context($user1context);
  93. // Get all LTI linked accounts match with user1.
  94. $ltiaccounts = $DB->get_records('auth_lti_linked_login', ['userid' => $user1->id]);
  95. $this->assertCount(0, $ltiaccounts);
  96. // Verify there is now only one linked login.
  97. $ltiaccounts = $DB->get_records('auth_lti_linked_login');
  98. $this->assertCount(1, $ltiaccounts);
  99. }
  100. /**
  101. * This should work identical to the above test.
  102. *
  103. * @covers ::delete_data_for_user
  104. */
  105. public function test_delete_data_for_user() {
  106. global $DB;
  107. $auth = get_auth_plugin('lti');
  108. $user1 = $this->getDataGenerator()->create_user();
  109. $auth->create_user_binding('https://lms.example.com', 'abc123', $user1->id);
  110. $user1context = \context_user::instance($user1->id);
  111. $user2 = $this->getDataGenerator()->create_user();
  112. $auth->create_user_binding('https://lms.example.com', 'def456', $user2->id);
  113. // Verify there are two linked logins.
  114. $ltiaccounts = $DB->get_records('auth_lti_linked_login');
  115. $this->assertCount(2, $ltiaccounts);
  116. // Delete everything for the first user.
  117. $approvedlist = new approved_contextlist($user1, 'auth_lti', [$user1context->id]);
  118. provider::delete_data_for_user($approvedlist);
  119. // Get all LTI accounts linked with user1.
  120. $ltiaccounts = $DB->get_records('auth_lti_linked_login', ['userid' => $user1->id]);
  121. $this->assertCount(0, $ltiaccounts);
  122. // Verify there is only one linked login now.
  123. $ltiaccounts = $DB->get_records('auth_lti_linked_login', array());
  124. $this->assertCount(1, $ltiaccounts);
  125. }
  126. /**
  127. * Test that only users with a user context are fetched.
  128. *
  129. * @covers ::get_users_in_context
  130. */
  131. public function test_get_users_in_context() {
  132. $auth = get_auth_plugin('lti');
  133. $component = 'auth_lti';
  134. $user = $this->getDataGenerator()->create_user();
  135. $usercontext = \context_user::instance($user->id);
  136. // The list of users should not return anything yet (no linked login yet).
  137. $userlist = new userlist($usercontext, $component);
  138. provider::get_users_in_context($userlist);
  139. $this->assertCount(0, $userlist);
  140. $auth->create_user_binding('https://lms.example.com', 'abc123', $user->id);
  141. // The list of users for user context should return the user.
  142. provider::get_users_in_context($userlist);
  143. $this->assertCount(1, $userlist);
  144. $expected = [$user->id];
  145. $actual = $userlist->get_userids();
  146. $this->assertEquals($expected, $actual);
  147. // The list of users for system context should not return any users.
  148. $systemcontext = \context_system::instance();
  149. $userlist = new userlist($systemcontext, $component);
  150. provider::get_users_in_context($userlist);
  151. $this->assertCount(0, $userlist);
  152. }
  153. /**
  154. * Test that data for users in approved userlist is deleted.
  155. *
  156. * @covers ::delete_data_for_users
  157. */
  158. public function test_delete_data_for_users() {
  159. $auth = get_auth_plugin('lti');
  160. $component = 'auth_lti';
  161. $user1 = $this->getDataGenerator()->create_user();
  162. $usercontext1 = \context_user::instance($user1->id);
  163. $user2 = $this->getDataGenerator()->create_user();
  164. $usercontext2 = \context_user::instance($user2->id);
  165. $auth->create_user_binding('https://lms.example.com', 'abc123', $user1->id);
  166. $auth->create_user_binding('https://lms.example.com', 'def456', $user2->id);
  167. // The list of users for usercontext1 should return user1.
  168. $userlist1 = new userlist($usercontext1, $component);
  169. provider::get_users_in_context($userlist1);
  170. $this->assertCount(1, $userlist1);
  171. $expected = [$user1->id];
  172. $actual = $userlist1->get_userids();
  173. $this->assertEquals($expected, $actual);
  174. // The list of users for usercontext2 should return user2.
  175. $userlist2 = new userlist($usercontext2, $component);
  176. provider::get_users_in_context($userlist2);
  177. $this->assertCount(1, $userlist2);
  178. $expected = [$user2->id];
  179. $actual = $userlist2->get_userids();
  180. $this->assertEquals($expected, $actual);
  181. // Add userlist1 to the approved user list.
  182. $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
  183. // Delete user data using delete_data_for_user for usercontext1.
  184. provider::delete_data_for_users($approvedlist);
  185. // Re-fetch users in usercontext1 - The user list should now be empty.
  186. $userlist1 = new userlist($usercontext1, $component);
  187. provider::get_users_in_context($userlist1);
  188. $this->assertCount(0, $userlist1);
  189. // Re-fetch users in usercontext2 - The user list should not be empty (user2).
  190. $userlist2 = new userlist($usercontext2, $component);
  191. provider::get_users_in_context($userlist2);
  192. $this->assertCount(1, $userlist2);
  193. // User data should be only removed in the user context.
  194. $systemcontext = \context_system::instance();
  195. // Add userlist2 to the approved user list in the system context.
  196. $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
  197. // Delete user1 data using delete_data_for_user.
  198. provider::delete_data_for_users($approvedlist);
  199. // Re-fetch users in usercontext2 - The user list should not be empty (user2).
  200. $userlist2 = new userlist($usercontext2, $component);
  201. provider::get_users_in_context($userlist2);
  202. $this->assertCount(1, $userlist2);
  203. }
  204. }