PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/assign/feedback/editpdf/tests/privacy_test.php

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