MDL-42023 assign: Edit PDF plugin - Damyon's contributions
[moodle.git] / mod / assign / feedback / editpdf / tests / editpdf_test.php
CommitLineData
5c386472
DW
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/**
18 * Unit tests for assignfeedback_editpdf\comments_quick_list
19 *
20 * @package assignfeedback_editpdf
21 * @category phpunit
22 * @copyright 2013 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28use \assignfeedback_editpdf\comments_quick_list;
29use \assignfeedback_editpdf\document_services;
30use \assignfeedback_editpdf\page_editor;
31use \assignfeedback_editpdf\pdf;
32use \assignfeedback_editpdf\comment;
33use \assignfeedback_editpdf\annotation;
34
35global $CFG;
36require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
37
38/**
39 * Unit tests for assignfeedback_editpdf\comments_quick_list
40 *
41 * @copyright 2013 Damyon Wiese
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
45
46 protected function setUp() {
47 // Skip this test if ghostscript is not supported.
48 if (!pdf::test_gs_path(false)) {
49 $this->markTestSkipped('Ghostscript not setup');
50 return;
51 }
52 parent::setUp();
53 }
54
55 protected function create_assign_and_submit_pdf() {
56 global $CFG;
57 $assign = $this->create_instance(array('assignsubmission_file_enabled' => 1,
58 'assignsubmission_file_maxfiles' => 1,
59 'assignfeedback_editpdf_enabled' => 1,
60 'assignsubmission_file_maxsizebytes' => 1000000));
61
62 $user = $this->students[0];
63 $this->setUser($user);
64
65 // Create a file submission with the test pdf.
66 $submission = $assign->get_user_submission($user->id, true);
67
68 $fs = get_file_storage();
69 $pdfsubmission = (object) array(
70 'contextid' => $assign->get_context()->id,
71 'component' => 'assignsubmission_file',
72 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
73 'itemid' => $submission->id,
74 'filepath' => '/',
75 'filename' => 'submission.pdf'
76 );
77 $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
78 $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
79
80 $data = new stdClass();
81 $plugin = $assign->get_submission_plugin_by_type('file');
82 $plugin->save($submission, $data);
83
84 return $assign;
85 }
86
87 public function test_comments_quick_list() {
88
89 $this->setUser($this->teachers[0]);
90
91 $comments = comments_quick_list::get_comments();
92
93 $this->assertEmpty($comments);
94
95 $comment = comments_quick_list::add_comment('test', 45, 'red');
96
97 $comments = comments_quick_list::get_comments();
98
99 $this->assertEquals(count($comments), 1);
100 $first = reset($comments);
101 $this->assertEquals($comment, $first);
102
103 $commentbyid = comments_quick_list::get_comment($comment->id);
104 $this->assertEquals($comment, $commentbyid);
105
106 $result = comments_quick_list::remove_comment($comment->id);
107
108 $this->assertTrue($result);
109
110 $comments = comments_quick_list::get_comments();
111 $this->assertEmpty($comments);
112 }
113
114 public function test_page_editor() {
115
116 $assign = $this->create_assign_and_submit_pdf();
117 $this->setUser($this->teachers[0]);
118
119 $grade = $assign->get_user_grade($this->students[0]->id, true);
120
121 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
122 $this->assertFalse($notempty);
123
124 $comment = new comment();
125
126 $comment->rawtext = 'Comment text';
127 $comment->width = 100;
128 $comment->x = 100;
129 $comment->y = 100;
130 $comment->colour = 'red';
131
132 $comment2 = new comment();
133
134 $comment2->rawtext = 'Comment text 2';
135 $comment2->width = 100;
136 $comment2->x = 200;
137 $comment2->y = 100;
138 $comment2->colour = 'clear';
139
140 page_editor::set_comments($grade->id, 0, array($comment, $comment2));
141
142 $annotation = new annotation();
143
144 $annotation->path = '';
145 $annotation->x = 100;
146 $annotation->y = 100;
147 $annotation->endx = 200;
148 $annotation->endy = 200;
149 $annotation->type = 'line';
150 $annotation->colour = 'red';
151
152 $annotation2 = new annotation();
153
154 $annotation2->path = '';
155 $annotation2->x = 100;
156 $annotation2->y = 100;
157 $annotation2->endx = 200;
158 $annotation2->endy = 200;
159 $annotation2->type = 'rectangle';
160 $annotation2->colour = 'yellow';
161
162 page_editor::set_annotations($grade->id, 0, array($annotation, $annotation2));
163
164 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
165 // Still empty because all edits are still drafts.
166 $this->assertFalse($notempty);
167
168 $comments = page_editor::get_comments($grade->id, 0, false);
169
170 $this->assertEmpty($comments);
171
172 $comments = page_editor::get_comments($grade->id, 0, true);
173
174 $this->assertEquals(count($comments), 2);
175
176 $annotations = page_editor::get_annotations($grade->id, 0, false);
177
178 $this->assertEmpty($annotations);
179
180 $annotations = page_editor::get_annotations($grade->id, 0, true);
181
182 $this->assertEquals(count($annotations), 2);
183
184 $comment = reset($comments);
185 $annotation = reset($annotations);
186
187 page_editor::remove_comment($comment->id);
188 page_editor::remove_annotation($annotation->id);
189
190 $comments = page_editor::get_comments($grade->id, 0, true);
191
192 $this->assertEquals(count($comments), 1);
193
194 $annotations = page_editor::get_annotations($grade->id, 0, true);
195
196 $this->assertEquals(count($annotations), 1);
197
198 page_editor::release_drafts($grade->id);
199
200 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
201
202 $this->assertTrue($notempty);
203
204 page_editor::unrelease_drafts($grade->id);
205
206 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
207
208 $this->assertFalse($notempty);
209 }
210
211 public function test_document_services() {
212
213 $assign = $this->create_assign_and_submit_pdf();
214 $this->setUser($this->teachers[0]);
215
216 $grade = $assign->get_user_grade($this->students[0]->id, true);
217
218 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
219 $this->assertFalse($notempty);
220
221 $comment = new comment();
222
223 $comment->rawtext = 'Comment text';
224 $comment->width = 100;
225 $comment->x = 100;
226 $comment->y = 100;
227 $comment->colour = 'red';
228
229 page_editor::set_comments($grade->id, 0, array($comment));
230
231 $annotations = array();
232
233 $annotation = new annotation();
234 $annotation->path = '';
235 $annotation->x = 100;
236 $annotation->y = 100;
237 $annotation->endx = 200;
238 $annotation->endy = 200;
239 $annotation->type = 'line';
240 $annotation->colour = 'red';
241 array_push($annotations, $annotation);
242
243 $annotation = new annotation();
244 $annotation->path = '';
245 $annotation->x = 100;
246 $annotation->y = 100;
247 $annotation->endx = 200;
248 $annotation->endy = 200;
249 $annotation->type = 'rectangle';
250 $annotation->colour = 'yellow';
251 array_push($annotations, $annotation);
252
253 $annotation = new annotation();
254 $annotation->path = '';
255 $annotation->x = 100;
256 $annotation->y = 100;
257 $annotation->endx = 200;
258 $annotation->endy = 200;
259 $annotation->type = 'oval';
260 $annotation->colour = 'green';
261 array_push($annotations, $annotation);
262
263 $annotation = new annotation();
264 $annotation->path = '';
265 $annotation->x = 100;
266 $annotation->y = 100;
267 $annotation->endx = 200;
268 $annotation->endy = 116;
269 $annotation->type = 'highlight';
270 $annotation->colour = 'blue';
271 array_push($annotations, $annotation);
272
273 $annotation = new annotation();
274 $annotation->path = '100,100:105,105:110,100';
275 $annotation->x = 100;
276 $annotation->y = 100;
277 $annotation->endx = 110;
278 $annotation->endy = 105;
279 $annotation->type = 'pen';
280 $annotation->colour = 'black';
281 array_push($annotations, $annotation);
282 page_editor::set_annotations($grade->id, 0, $annotations);
283
284 page_editor::release_drafts($grade->id);
285
286 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
287
288 $this->assertTrue($notempty);
289
290 $file = document_services::generate_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
291 $this->assertNotEmpty($file);
292
293 $file2 = document_services::get_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
294
295 $this->assertEquals($file, $file2);
296
297 document_services::delete_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
298 $file3 = document_services::get_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
299
300 $this->assertEmpty($file3);
301 }
302}