Merge branch 'MDL-40493-master-fix1' of git://github.com/damyon/moodle
[moodle.git] / mod / assign / feedback / editpdf / tests / editpdf_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 use \assignfeedback_editpdf\comments_quick_list;
29 use \assignfeedback_editpdf\document_services;
30 use \assignfeedback_editpdf\page_editor;
31 use \assignfeedback_editpdf\pdf;
32 use \assignfeedback_editpdf\comment;
33 use \assignfeedback_editpdf\annotation;
35 global $CFG;
36 require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
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  */
44 class assignfeedback_editpdf_testcase extends mod_assign_base_testcase {
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     }
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));
62         $user = $this->students[0];
63         $this->setUser($user);
65         // Create a file submission with the test pdf.
66         $submission = $assign->get_user_submission($user->id, true);
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);
80         $data = new stdClass();
81         $plugin = $assign->get_submission_plugin_by_type('file');
82         $plugin->save($submission, $data);
84         return $assign;
85     }
87     public function test_comments_quick_list() {
89         $this->setUser($this->teachers[0]);
91         $comments = comments_quick_list::get_comments();
93         $this->assertEmpty($comments);
95         $comment = comments_quick_list::add_comment('test', 45, 'red');
97         $comments = comments_quick_list::get_comments();
99         $this->assertEquals(count($comments), 1);
100         $first = reset($comments);
101         $this->assertEquals($comment, $first);
103         $commentbyid = comments_quick_list::get_comment($comment->id);
104         $this->assertEquals($comment, $commentbyid);
106         $result = comments_quick_list::remove_comment($comment->id);
108         $this->assertTrue($result);
110         $comments = comments_quick_list::get_comments();
111         $this->assertEmpty($comments);
112     }
114     public function test_page_editor() {
116         $assign = $this->create_assign_and_submit_pdf();
117         $this->setUser($this->teachers[0]);
119         $grade = $assign->get_user_grade($this->students[0]->id, true);
121         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
122         $this->assertFalse($notempty);
124         $comment = new comment();
126         $comment->rawtext = 'Comment text';
127         $comment->width = 100;
128         $comment->x = 100;
129         $comment->y = 100;
130         $comment->colour = 'red';
132         $comment2 = new comment();
134         $comment2->rawtext = 'Comment text 2';
135         $comment2->width = 100;
136         $comment2->x = 200;
137         $comment2->y = 100;
138         $comment2->colour = 'clear';
140         page_editor::set_comments($grade->id, 0, array($comment, $comment2));
142         $annotation = new annotation();
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';
152         $annotation2 = new annotation();
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';
162         page_editor::set_annotations($grade->id, 0, array($annotation, $annotation2));
164         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
165         // Still empty because all edits are still drafts.
166         $this->assertFalse($notempty);
168         $comments = page_editor::get_comments($grade->id, 0, false);
170         $this->assertEmpty($comments);
172         $comments = page_editor::get_comments($grade->id, 0, true);
174         $this->assertEquals(count($comments), 2);
176         $annotations = page_editor::get_annotations($grade->id, 0, false);
178         $this->assertEmpty($annotations);
180         $annotations = page_editor::get_annotations($grade->id, 0, true);
182         $this->assertEquals(count($annotations), 2);
184         $comment = reset($comments);
185         $annotation = reset($annotations);
187         page_editor::remove_comment($comment->id);
188         page_editor::remove_annotation($annotation->id);
190         $comments = page_editor::get_comments($grade->id, 0, true);
192         $this->assertEquals(count($comments), 1);
194         $annotations = page_editor::get_annotations($grade->id, 0, true);
196         $this->assertEquals(count($annotations), 1);
198         page_editor::release_drafts($grade->id);
200         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
202         $this->assertTrue($notempty);
204         page_editor::unrelease_drafts($grade->id);
206         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
208         $this->assertFalse($notempty);
209     }
211     public function test_document_services() {
213         $assign = $this->create_assign_and_submit_pdf();
214         $this->setUser($this->teachers[0]);
216         $grade = $assign->get_user_grade($this->students[0]->id, true);
218         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
219         $this->assertFalse($notempty);
221         $comment = new comment();
223         $comment->rawtext = 'Comment text';
224         $comment->width = 100;
225         $comment->x = 100;
226         $comment->y = 100;
227         $comment->colour = 'red';
229         page_editor::set_comments($grade->id, 0, array($comment));
231         $annotations = array();
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);
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);
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);
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);
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);
284         page_editor::release_drafts($grade->id);
286         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
288         $this->assertTrue($notempty);
290         $file = document_services::generate_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
291         $this->assertNotEmpty($file);
293         $file2 = document_services::get_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
295         $this->assertEquals($file, $file2);
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);
300         $this->assertEmpty($file3);
301     }