/report/stats/tests/privacy_test.php

https://gitlab.com/unofficial-mirrors/moodle · PHP · 205 lines · 133 code · 23 blank · 49 comment · 1 complexity · 8544f7560400a5b789b899ab5c88e7a0 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 privacy functions.
  18. *
  19. * @package report_stats
  20. * @copyright 2018 Adrian Greeve <adriangreeve.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * Class report_stats_privacy_testcase
  26. *
  27. * @package report_stats
  28. * @copyright 2018 Adrian Greeve <adriangreeve.com>
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  30. */
  31. class report_stats_privacy_testcase extends advanced_testcase {
  32. /**
  33. * Convenience function to create stats.
  34. *
  35. * @param int $courseid Course ID for this record.
  36. * @param int $userid User ID for this record.
  37. * @param string $table Stat table to insert into.
  38. */
  39. protected function create_stats($courseid, $userid, $table) {
  40. global $DB;
  41. $data = (object) [
  42. 'courseid' => $courseid,
  43. 'userid' => $userid,
  44. 'roleid' => 0,
  45. 'timeend' => time(),
  46. 'statsreads' => rand(1, 50),
  47. 'statswrites' => rand(1, 50),
  48. 'stattype' => 'activity'
  49. ];
  50. $DB->insert_record($table, $data);
  51. }
  52. /**
  53. * Get all of the contexts related to a user and stat tables.
  54. */
  55. public function test_get_contexts_for_userid() {
  56. $this->resetAfterTest();
  57. $user1 = $this->getDataGenerator()->create_user();
  58. $user2 = $this->getDataGenerator()->create_user();
  59. $user3 = $this->getDataGenerator()->create_user();
  60. $course1 = $this->getDataGenerator()->create_course();
  61. $course2 = $this->getDataGenerator()->create_course();
  62. $course3 = $this->getDataGenerator()->create_course();
  63. $context1 = context_course::instance($course1->id);
  64. $context2 = context_course::instance($course2->id);
  65. $context3 = context_course::instance($course3->id);
  66. $this->create_stats($course1->id, $user1->id, 'stats_user_daily');
  67. $this->create_stats($course2->id, $user1->id, 'stats_user_monthly');
  68. $this->create_stats($course1->id, $user2->id, 'stats_user_weekly');
  69. $contextlist = \report_stats\privacy\provider::get_contexts_for_userid($user1->id);
  70. $this->assertCount(2, $contextlist->get_contextids());
  71. foreach ($contextlist->get_contexts() as $context) {
  72. $this->assertEquals(CONTEXT_COURSE, $context->contextlevel);
  73. $this->assertNotEquals($context3, $context);
  74. }
  75. $contextlist = \report_stats\privacy\provider::get_contexts_for_userid($user2->id);
  76. $this->assertCount(1, $contextlist->get_contextids());
  77. $this->assertEquals($context1, $contextlist->current());
  78. }
  79. /**
  80. * Test that stat data is exported as required.
  81. */
  82. public function test_export_user_data() {
  83. $this->resetAfterTest();
  84. $user = $this->getDataGenerator()->create_user();
  85. $course1 = $this->getDataGenerator()->create_course();
  86. $course2 = $this->getDataGenerator()->create_course();
  87. $context1 = context_course::instance($course1->id);
  88. $context2 = context_course::instance($course2->id);
  89. $this->create_stats($course1->id, $user->id, 'stats_user_daily');
  90. $this->create_stats($course1->id, $user->id, 'stats_user_daily');
  91. $this->create_stats($course2->id, $user->id, 'stats_user_weekly');
  92. $this->create_stats($course2->id, $user->id, 'stats_user_monthly');
  93. $this->create_stats($course1->id, $user->id, 'stats_user_monthly');
  94. $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'report_stats', [$context1->id, $context2->id]);
  95. \report_stats\privacy\provider::export_user_data($approvedlist);
  96. $writer = \core_privacy\local\request\writer::with_context($context1);
  97. $dailystats = (array) $writer->get_data([get_string('privacy:dailypath', 'report_stats')]);
  98. $this->assertCount(2, $dailystats);
  99. $monthlystats = (array) $writer->get_data([get_string('privacy:monthlypath', 'report_stats')]);
  100. $this->assertCount(1, $monthlystats);
  101. $data = array_shift($monthlystats);
  102. $this->assertEquals($course1->fullname, $data['course']);
  103. $writer = \core_privacy\local\request\writer::with_context($context2);
  104. $monthlystats = (array) $writer->get_data([get_string('privacy:monthlypath', 'report_stats')]);
  105. $this->assertCount(1, $monthlystats);
  106. $data = array_shift($monthlystats);
  107. $this->assertEquals($course2->fullname, $data['course']);
  108. $weeklystats = (array) $writer->get_data([get_string('privacy:weeklypath', 'report_stats')]);
  109. $this->assertCount(1, $weeklystats);
  110. $data = array_shift($weeklystats);
  111. $this->assertEquals($course2->fullname, $data['course']);
  112. }
  113. /**
  114. * Test that stat data is deleted for a whole context.
  115. */
  116. public function test_delete_data_for_all_users_in_context() {
  117. global $DB;
  118. $this->resetAfterTest();
  119. $user1 = $this->getDataGenerator()->create_user();
  120. $user2 = $this->getDataGenerator()->create_user();
  121. $course1 = $this->getDataGenerator()->create_course();
  122. $course2 = $this->getDataGenerator()->create_course();
  123. $context1 = context_course::instance($course1->id);
  124. $context2 = context_course::instance($course2->id);
  125. $this->create_stats($course1->id, $user1->id, 'stats_user_daily');
  126. $this->create_stats($course1->id, $user1->id, 'stats_user_daily');
  127. $this->create_stats($course1->id, $user1->id, 'stats_user_monthly');
  128. $this->create_stats($course1->id, $user2->id, 'stats_user_weekly');
  129. $this->create_stats($course2->id, $user2->id, 'stats_user_daily');
  130. $this->create_stats($course2->id, $user2->id, 'stats_user_weekly');
  131. $this->create_stats($course2->id, $user2->id, 'stats_user_monthly');
  132. $dailyrecords = $DB->get_records('stats_user_daily');
  133. $this->assertCount(3, $dailyrecords);
  134. $weeklyrecords = $DB->get_records('stats_user_weekly');
  135. $this->assertCount(2, $weeklyrecords);
  136. $monthlyrecords = $DB->get_records('stats_user_monthly');
  137. $this->assertCount(2, $monthlyrecords);
  138. // Delete all user data for course 1.
  139. \report_stats\privacy\provider::delete_data_for_all_users_in_context($context1);
  140. $dailyrecords = $DB->get_records('stats_user_daily');
  141. $this->assertCount(1, $dailyrecords);
  142. $weeklyrecords = $DB->get_records('stats_user_weekly');
  143. $this->assertCount(1, $weeklyrecords);
  144. $monthlyrecords = $DB->get_records('stats_user_monthly');
  145. $this->assertCount(1, $monthlyrecords);
  146. }
  147. /**
  148. * Test that stats are deleted for one user.
  149. */
  150. public function test_delete_data_for_user() {
  151. global $DB;
  152. $this->resetAfterTest();
  153. $user1 = $this->getDataGenerator()->create_user();
  154. $user2 = $this->getDataGenerator()->create_user();
  155. $course1 = $this->getDataGenerator()->create_course();
  156. $course2 = $this->getDataGenerator()->create_course();
  157. $context1 = context_course::instance($course1->id);
  158. $context2 = context_course::instance($course2->id);
  159. $this->create_stats($course1->id, $user1->id, 'stats_user_daily');
  160. $this->create_stats($course1->id, $user1->id, 'stats_user_daily');
  161. $this->create_stats($course1->id, $user1->id, 'stats_user_monthly');
  162. $this->create_stats($course1->id, $user2->id, 'stats_user_weekly');
  163. $this->create_stats($course2->id, $user2->id, 'stats_user_daily');
  164. $this->create_stats($course2->id, $user2->id, 'stats_user_weekly');
  165. $this->create_stats($course2->id, $user2->id, 'stats_user_monthly');
  166. $dailyrecords = $DB->get_records('stats_user_daily');
  167. $this->assertCount(3, $dailyrecords);
  168. $weeklyrecords = $DB->get_records('stats_user_weekly');
  169. $this->assertCount(2, $weeklyrecords);
  170. $monthlyrecords = $DB->get_records('stats_user_monthly');
  171. $this->assertCount(2, $monthlyrecords);
  172. // Delete all user data for course 1.
  173. $approvedlist = new \core_privacy\local\request\approved_contextlist($user1, 'report_stats', [$context1->id]);
  174. \report_stats\privacy\provider::delete_data_for_user($approvedlist);
  175. $dailyrecords = $DB->get_records('stats_user_daily');
  176. $this->assertCount(1, $dailyrecords);
  177. $weeklyrecords = $DB->get_records('stats_user_weekly');
  178. $this->assertCount(2, $weeklyrecords);
  179. $monthlyrecords = $DB->get_records('stats_user_monthly');
  180. $this->assertCount(1, $monthlyrecords);
  181. }
  182. }