PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/mod/assign/feedback/editpdf/tests/privacy/provider_test.php

https://bitbucket.org/moodle/moodle
PHP | 319 lines | 186 code | 50 blank | 83 comment | 2 complexity | 8a9f7e41f8b4001b89808cb45accee82 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. * Unit tests for assignfeedback_editpdf.
  18. *
  19. * @package assignfeedback_editpdf
  20. * @copyright 2018 Adrian Greeve <adrian@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace assignfeedback_editpdf\privacy;
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->dirroot . '/mod/assign/locallib.php');
  27. require_once($CFG->dirroot . '/mod/assign/tests/privacy/provider_test.php');
  28. use assignfeedback_editpdf\page_editor;
  29. use mod_assign\privacy\assign_plugin_request_data;
  30. /**
  31. * Unit tests for mod/assign/feedback/editpdf/classes/privacy/
  32. *
  33. * @copyright 2018 Adrian Greeve <adrian@moodle.com>
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class provider_test extends \mod_assign\privacy\provider_test {
  37. public function setUp(): void {
  38. // Skip this test if ghostscript is not supported.
  39. $result = \assignfeedback_editpdf\pdf::test_gs_path(false);
  40. if ($result->status !== \assignfeedback_editpdf\pdf::GSPATH_OK) {
  41. $this->markTestSkipped('Ghostscript not setup');
  42. return;
  43. }
  44. parent::setUp();
  45. }
  46. /**
  47. * Convenience function for creating feedback data.
  48. *
  49. * @param object $assign assign object
  50. * @param stdClass $student user object
  51. * @param stdClass $teacher user object
  52. * @return array Feedback plugin object and the grade object.
  53. */
  54. protected function create_feedback($assign, $student, $teacher) {
  55. global $CFG;
  56. // Create a file submission with the test pdf.
  57. $submission = $assign->get_user_submission($student->id, true);
  58. $this->setUser($student->id);
  59. $fs = get_file_storage();
  60. $pdfsubmission = (object) array(
  61. 'contextid' => $assign->get_context()->id,
  62. 'component' => 'assignsubmission_file',
  63. 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  64. 'itemid' => $submission->id,
  65. 'filepath' => '/',
  66. 'filename' => 'submission.pdf'
  67. );
  68. $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
  69. $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
  70. $data = new \stdClass();
  71. $plugin = $assign->get_submission_plugin_by_type('file');
  72. $plugin->save($submission, $data);
  73. $this->setUser($teacher->id);
  74. $plugin = $assign->get_feedback_plugin_by_type('editpdf');
  75. $grade = $assign->get_user_grade($student->id, true);
  76. $comment = new \assignfeedback_editpdf\comment();
  77. $comment->rawtext = 'Comment text';
  78. $comment->width = 100;
  79. $comment->x = 100;
  80. $comment->y = 100;
  81. $comment->colour = 'red';
  82. page_editor::set_comments($grade->id, 0, [$comment]);
  83. $annotation = new \assignfeedback_editpdf\annotation();
  84. $annotation->path = '';
  85. $annotation->x = 100;
  86. $annotation->y = 100;
  87. $annotation->endx = 200;
  88. $annotation->endy = 200;
  89. $annotation->type = 'line';
  90. $annotation->colour = 'red';
  91. page_editor::set_annotations($grade->id, 0, [$annotation]);
  92. $comments = page_editor::get_comments($grade->id, 0, true);
  93. $annotations = page_editor::get_annotations($grade->id, 0, false);
  94. page_editor::release_drafts($grade->id);
  95. $storedfile = \assignfeedback_editpdf\document_services::generate_feedback_document($assign->get_instance()->id, $student->id,
  96. $grade->attemptnumber);
  97. return [$plugin, $grade, $storedfile];
  98. }
  99. /**
  100. * Quick test to make sure that get_metadata returns something.
  101. */
  102. public function test_get_metadata() {
  103. $collection = new \core_privacy\local\metadata\collection('assignfeedback_editpdf');
  104. $collection = \assignfeedback_editpdf\privacy\provider::get_metadata($collection);
  105. $this->assertNotEmpty($collection);
  106. }
  107. /**
  108. * Test that feedback comments are exported for a user.
  109. */
  110. public function test_export_feedback_user_data() {
  111. $this->resetAfterTest();
  112. // Create course, assignment, submission, and then a feedback comment.
  113. $course = $this->getDataGenerator()->create_course();
  114. // Student.
  115. $user1 = $this->getDataGenerator()->create_user();
  116. // Teacher.
  117. $user2 = $this->getDataGenerator()->create_user();
  118. $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
  119. $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
  120. $assign = $this->create_instance(['course' => $course,
  121. 'assignsubmission_file_enabled' => 1,
  122. 'assignsubmission_file_maxfiles' => 1,
  123. 'assignfeedback_editpdf_enabled' => 1,
  124. 'assignsubmission_file_maxsizebytes' => 1000000]);
  125. $context = $assign->get_context();
  126. list($plugin, $grade, $storedfile) = $this->create_feedback($assign, $user1, $user2);
  127. // Check that we have data.
  128. $this->assertFalse($plugin->is_empty($grade));
  129. $writer = \core_privacy\local\request\writer::with_context($context);
  130. $this->assertFalse($writer->has_any_data());
  131. // The student should be able to see the teachers feedback.
  132. $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user1);
  133. \assignfeedback_editpdf\privacy\provider::export_feedback_user_data($exportdata);
  134. // print_object($writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')]));
  135. // print_object($writer->get_files(['PDF feedback', $storedfile->get_filename()]));
  136. $pdffile = $writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')])[$storedfile->get_filename()];
  137. // The writer should have returned a stored file.
  138. $this->assertInstanceOf('stored_file', $pdffile);
  139. }
  140. /**
  141. * Test that all feedback is deleted for a context.
  142. */
  143. public function test_delete_feedback_for_context() {
  144. $this->resetAfterTest();
  145. // Create course, assignment, submission, and then a feedback comment.
  146. $course = $this->getDataGenerator()->create_course();
  147. // Students.
  148. $user1 = $this->getDataGenerator()->create_user();
  149. $user2 = $this->getDataGenerator()->create_user();
  150. // Teacher.
  151. $user3 = $this->getDataGenerator()->create_user();
  152. $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
  153. $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
  154. $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
  155. $assign = $this->create_instance(['course' => $course,
  156. 'assignsubmission_file_enabled' => 1,
  157. 'assignsubmission_file_maxfiles' => 1,
  158. 'assignfeedback_editpdf_enabled' => 1,
  159. 'assignsubmission_file_maxsizebytes' => 1000000]);
  160. $context = $assign->get_context();
  161. list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
  162. list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
  163. // Check that we have data.
  164. $this->assertFalse($plugin1->is_empty($grade1));
  165. $this->assertFalse($plugin2->is_empty($grade2));
  166. $requestdata = new assign_plugin_request_data($context, $assign);
  167. \assignfeedback_editpdf\privacy\provider::delete_feedback_for_context($requestdata);
  168. // Check that we now have no data.
  169. $this->assertTrue($plugin1->is_empty($grade1));
  170. $this->assertTrue($plugin2->is_empty($grade2));
  171. }
  172. /**
  173. * Test that a grade item is deleted for a user.
  174. */
  175. public function test_delete_feedback_for_grade() {
  176. $this->resetAfterTest();
  177. // Create course, assignment, submission, and then a feedback comment.
  178. $course = $this->getDataGenerator()->create_course();
  179. // Students.
  180. $user1 = $this->getDataGenerator()->create_user();
  181. $user2 = $this->getDataGenerator()->create_user();
  182. // Teacher.
  183. $user3 = $this->getDataGenerator()->create_user();
  184. $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
  185. $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
  186. $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
  187. $assign = $this->create_instance(['course' => $course,
  188. 'assignsubmission_file_enabled' => 1,
  189. 'assignsubmission_file_maxfiles' => 1,
  190. 'assignfeedback_editpdf_enabled' => 1,
  191. 'assignsubmission_file_maxsizebytes' => 1000000]);
  192. $context = $assign->get_context();
  193. list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
  194. list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
  195. // Check that we have data.
  196. $this->assertFalse($plugin1->is_empty($grade1));
  197. $this->assertFalse($plugin2->is_empty($grade2));
  198. $requestdata = new assign_plugin_request_data($context, $assign, $grade1, [], $user1);
  199. \assignfeedback_editpdf\privacy\provider::delete_feedback_for_grade($requestdata);
  200. // Check that we now have no data for user 1.
  201. $this->assertTrue($plugin1->is_empty($grade1));
  202. // Check that user 2 data is still there.
  203. $this->assertFalse($plugin2->is_empty($grade2));
  204. }
  205. /**
  206. * Test that a grade item is deleted for a user.
  207. */
  208. public function test_delete_feedback_for_grades() {
  209. global $DB;
  210. $this->resetAfterTest();
  211. // Create course, assignment, submission, and then a feedback comment.
  212. $course = $this->getDataGenerator()->create_course();
  213. // Students.
  214. $user1 = $this->getDataGenerator()->create_user();
  215. $user2 = $this->getDataGenerator()->create_user();
  216. $user3 = $this->getDataGenerator()->create_user();
  217. $user4 = $this->getDataGenerator()->create_user();
  218. // Teacher.
  219. $user5 = $this->getDataGenerator()->create_user();
  220. $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
  221. $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
  222. $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
  223. $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
  224. $this->getDataGenerator()->enrol_user($user5->id, $course->id, 'editingteacher');
  225. $assign1 = $this->create_instance(['course' => $course,
  226. 'assignsubmission_file_enabled' => 1,
  227. 'assignsubmission_file_maxfiles' => 1,
  228. 'assignfeedback_editpdf_enabled' => 1,
  229. 'assignsubmission_file_maxsizebytes' => 1000000]);
  230. $assign2 = $this->create_instance(['course' => $course,
  231. 'assignsubmission_file_enabled' => 1,
  232. 'assignsubmission_file_maxfiles' => 1,
  233. 'assignfeedback_editpdf_enabled' => 1,
  234. 'assignsubmission_file_maxsizebytes' => 1000000]);
  235. $context = $assign1->get_context();
  236. list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign1, $user1, $user5);
  237. list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign1, $user2, $user5);
  238. list($plugin3, $grade3, $storedfile3) = $this->create_feedback($assign1, $user3, $user5);
  239. list($plugin4, $grade4, $storedfile4) = $this->create_feedback($assign2, $user3, $user5);
  240. list($plugin5, $grade5, $storedfile5) = $this->create_feedback($assign2, $user4, $user5);
  241. // Check that we have data.
  242. $this->assertFalse($plugin1->is_empty($grade1));
  243. $this->assertFalse($plugin2->is_empty($grade2));
  244. $this->assertFalse($plugin3->is_empty($grade3));
  245. $this->assertFalse($plugin4->is_empty($grade4));
  246. $this->assertFalse($plugin5->is_empty($grade5));
  247. // Check that there are also files generated.
  248. $files = $DB->get_records('files', ['component' => 'assignfeedback_editpdf', 'filearea' => 'download']);
  249. $this->assertCount(10, $files);
  250. $deletedata = new assign_plugin_request_data($context, $assign1);
  251. $deletedata->set_userids([$user1->id, $user3->id]);
  252. $deletedata->populate_submissions_and_grades();
  253. \assignfeedback_editpdf\privacy\provider::delete_feedback_for_grades($deletedata);
  254. // Check that we now have no data for user 1.
  255. $this->assertTrue($plugin1->is_empty($grade1));
  256. // Check that user 2 data is still there.
  257. $this->assertFalse($plugin2->is_empty($grade2));
  258. // User 3 in assignment 1 should be gone.
  259. $this->assertTrue($plugin3->is_empty($grade3));
  260. // User 3 in assignment 2 should still be here.
  261. $this->assertFalse($plugin4->is_empty($grade4));
  262. // User 4 in assignment 2 should also still be here.
  263. $this->assertFalse($plugin5->is_empty($grade5));
  264. // Check the files as well.
  265. $files = $DB->get_records('files', ['component' => 'assignfeedback_editpdf', 'filearea' => 'download']);
  266. // We should now only have six records here.
  267. $this->assertCount(6, $files);
  268. }
  269. }