Merge branch 'wip-mdl-42222' of git://github.com/rajeshtaneja/moodle
[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;
9dbe0313
RT
57 $assign = $this->create_instance(array('assignsubmission_onlinetext_enabled' => 1,
58 'assignsubmission_file_enabled' => 1,
5c386472
DW
59 'assignsubmission_file_maxfiles' => 1,
60 'assignfeedback_editpdf_enabled' => 1,
61 'assignsubmission_file_maxsizebytes' => 1000000));
62
63 $user = $this->students[0];
64 $this->setUser($user);
65
66 // Create a file submission with the test pdf.
67 $submission = $assign->get_user_submission($user->id, true);
68
69 $fs = get_file_storage();
70 $pdfsubmission = (object) array(
71 'contextid' => $assign->get_context()->id,
72 'component' => 'assignsubmission_file',
73 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
74 'itemid' => $submission->id,
75 'filepath' => '/',
76 'filename' => 'submission.pdf'
77 );
78 $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
79 $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
80
81 $data = new stdClass();
82 $plugin = $assign->get_submission_plugin_by_type('file');
83 $plugin->save($submission, $data);
84
85 return $assign;
86 }
87
88 public function test_comments_quick_list() {
89
90 $this->setUser($this->teachers[0]);
91
92 $comments = comments_quick_list::get_comments();
93
94 $this->assertEmpty($comments);
95
96 $comment = comments_quick_list::add_comment('test', 45, 'red');
97
98 $comments = comments_quick_list::get_comments();
99
100 $this->assertEquals(count($comments), 1);
101 $first = reset($comments);
102 $this->assertEquals($comment, $first);
103
104 $commentbyid = comments_quick_list::get_comment($comment->id);
105 $this->assertEquals($comment, $commentbyid);
106
107 $result = comments_quick_list::remove_comment($comment->id);
108
109 $this->assertTrue($result);
110
111 $comments = comments_quick_list::get_comments();
112 $this->assertEmpty($comments);
113 }
114
115 public function test_page_editor() {
116
117 $assign = $this->create_assign_and_submit_pdf();
118 $this->setUser($this->teachers[0]);
119
120 $grade = $assign->get_user_grade($this->students[0]->id, true);
121
122 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
123 $this->assertFalse($notempty);
124
125 $comment = new comment();
126
127 $comment->rawtext = 'Comment text';
128 $comment->width = 100;
129 $comment->x = 100;
130 $comment->y = 100;
131 $comment->colour = 'red';
132
133 $comment2 = new comment();
134
135 $comment2->rawtext = 'Comment text 2';
136 $comment2->width = 100;
137 $comment2->x = 200;
138 $comment2->y = 100;
139 $comment2->colour = 'clear';
140
141 page_editor::set_comments($grade->id, 0, array($comment, $comment2));
142
143 $annotation = new annotation();
144
145 $annotation->path = '';
146 $annotation->x = 100;
147 $annotation->y = 100;
148 $annotation->endx = 200;
149 $annotation->endy = 200;
150 $annotation->type = 'line';
151 $annotation->colour = 'red';
152
153 $annotation2 = new annotation();
154
155 $annotation2->path = '';
156 $annotation2->x = 100;
157 $annotation2->y = 100;
158 $annotation2->endx = 200;
159 $annotation2->endy = 200;
160 $annotation2->type = 'rectangle';
161 $annotation2->colour = 'yellow';
162
163 page_editor::set_annotations($grade->id, 0, array($annotation, $annotation2));
164
165 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
166 // Still empty because all edits are still drafts.
167 $this->assertFalse($notempty);
168
169 $comments = page_editor::get_comments($grade->id, 0, false);
170
171 $this->assertEmpty($comments);
172
173 $comments = page_editor::get_comments($grade->id, 0, true);
174
175 $this->assertEquals(count($comments), 2);
176
177 $annotations = page_editor::get_annotations($grade->id, 0, false);
178
179 $this->assertEmpty($annotations);
180
181 $annotations = page_editor::get_annotations($grade->id, 0, true);
182
183 $this->assertEquals(count($annotations), 2);
184
185 $comment = reset($comments);
186 $annotation = reset($annotations);
187
188 page_editor::remove_comment($comment->id);
189 page_editor::remove_annotation($annotation->id);
190
191 $comments = page_editor::get_comments($grade->id, 0, true);
192
193 $this->assertEquals(count($comments), 1);
194
195 $annotations = page_editor::get_annotations($grade->id, 0, true);
196
197 $this->assertEquals(count($annotations), 1);
198
199 page_editor::release_drafts($grade->id);
200
201 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
202
203 $this->assertTrue($notempty);
204
205 page_editor::unrelease_drafts($grade->id);
206
207 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
208
209 $this->assertFalse($notempty);
210 }
211
212 public function test_document_services() {
213
214 $assign = $this->create_assign_and_submit_pdf();
215 $this->setUser($this->teachers[0]);
216
217 $grade = $assign->get_user_grade($this->students[0]->id, true);
218
219 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
220 $this->assertFalse($notempty);
221
222 $comment = new comment();
223
224 $comment->rawtext = 'Comment text';
225 $comment->width = 100;
226 $comment->x = 100;
227 $comment->y = 100;
228 $comment->colour = 'red';
229
230 page_editor::set_comments($grade->id, 0, array($comment));
231
232 $annotations = array();
233
234 $annotation = new annotation();
235 $annotation->path = '';
236 $annotation->x = 100;
237 $annotation->y = 100;
238 $annotation->endx = 200;
239 $annotation->endy = 200;
240 $annotation->type = 'line';
241 $annotation->colour = 'red';
242 array_push($annotations, $annotation);
243
244 $annotation = new annotation();
245 $annotation->path = '';
246 $annotation->x = 100;
247 $annotation->y = 100;
248 $annotation->endx = 200;
249 $annotation->endy = 200;
250 $annotation->type = 'rectangle';
251 $annotation->colour = 'yellow';
252 array_push($annotations, $annotation);
253
254 $annotation = new annotation();
255 $annotation->path = '';
256 $annotation->x = 100;
257 $annotation->y = 100;
258 $annotation->endx = 200;
259 $annotation->endy = 200;
260 $annotation->type = 'oval';
261 $annotation->colour = 'green';
262 array_push($annotations, $annotation);
263
264 $annotation = new annotation();
265 $annotation->path = '';
266 $annotation->x = 100;
267 $annotation->y = 100;
268 $annotation->endx = 200;
269 $annotation->endy = 116;
270 $annotation->type = 'highlight';
271 $annotation->colour = 'blue';
272 array_push($annotations, $annotation);
273
274 $annotation = new annotation();
275 $annotation->path = '100,100:105,105:110,100';
276 $annotation->x = 100;
277 $annotation->y = 100;
278 $annotation->endx = 110;
279 $annotation->endy = 105;
280 $annotation->type = 'pen';
281 $annotation->colour = 'black';
282 array_push($annotations, $annotation);
283 page_editor::set_annotations($grade->id, 0, $annotations);
284
285 page_editor::release_drafts($grade->id);
286
287 $notempty = page_editor::has_annotations_or_comments($grade->id, false);
288
289 $this->assertTrue($notempty);
290
291 $file = document_services::generate_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
292 $this->assertNotEmpty($file);
293
294 $file2 = document_services::get_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
295
296 $this->assertEquals($file, $file2);
297
298 document_services::delete_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
299 $file3 = document_services::get_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
300
301 $this->assertEmpty($file3);
302 }
303}