Merge branch 'wip-mdl-42222' of git://github.com/rajeshtaneja/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_onlinetext_enabled' => 1,
58                                                'assignsubmission_file_enabled' => 1,
59                                                'assignsubmission_file_maxfiles' => 1,
60                                                'assignfeedback_editpdf_enabled' => 1,
61                                                'assignsubmission_file_maxsizebytes' => 1000000));
63         $user = $this->students[0];
64         $this->setUser($user);
66         // Create a file submission with the test pdf.
67         $submission = $assign->get_user_submission($user->id, true);
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);
81         $data = new stdClass();
82         $plugin = $assign->get_submission_plugin_by_type('file');
83         $plugin->save($submission, $data);
85         return $assign;
86     }
88     public function test_comments_quick_list() {
90         $this->setUser($this->teachers[0]);
92         $comments = comments_quick_list::get_comments();
94         $this->assertEmpty($comments);
96         $comment = comments_quick_list::add_comment('test', 45, 'red');
98         $comments = comments_quick_list::get_comments();
100         $this->assertEquals(count($comments), 1);
101         $first = reset($comments);
102         $this->assertEquals($comment, $first);
104         $commentbyid = comments_quick_list::get_comment($comment->id);
105         $this->assertEquals($comment, $commentbyid);
107         $result = comments_quick_list::remove_comment($comment->id);
109         $this->assertTrue($result);
111         $comments = comments_quick_list::get_comments();
112         $this->assertEmpty($comments);
113     }
115     public function test_page_editor() {
117         $assign = $this->create_assign_and_submit_pdf();
118         $this->setUser($this->teachers[0]);
120         $grade = $assign->get_user_grade($this->students[0]->id, true);
122         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
123         $this->assertFalse($notempty);
125         $comment = new comment();
127         $comment->rawtext = 'Comment text';
128         $comment->width = 100;
129         $comment->x = 100;
130         $comment->y = 100;
131         $comment->colour = 'red';
133         $comment2 = new comment();
135         $comment2->rawtext = 'Comment text 2';
136         $comment2->width = 100;
137         $comment2->x = 200;
138         $comment2->y = 100;
139         $comment2->colour = 'clear';
141         page_editor::set_comments($grade->id, 0, array($comment, $comment2));
143         $annotation = new annotation();
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';
153         $annotation2 = new annotation();
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';
163         page_editor::set_annotations($grade->id, 0, array($annotation, $annotation2));
165         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
166         // Still empty because all edits are still drafts.
167         $this->assertFalse($notempty);
169         $comments = page_editor::get_comments($grade->id, 0, false);
171         $this->assertEmpty($comments);
173         $comments = page_editor::get_comments($grade->id, 0, true);
175         $this->assertEquals(count($comments), 2);
177         $annotations = page_editor::get_annotations($grade->id, 0, false);
179         $this->assertEmpty($annotations);
181         $annotations = page_editor::get_annotations($grade->id, 0, true);
183         $this->assertEquals(count($annotations), 2);
185         $comment = reset($comments);
186         $annotation = reset($annotations);
188         page_editor::remove_comment($comment->id);
189         page_editor::remove_annotation($annotation->id);
191         $comments = page_editor::get_comments($grade->id, 0, true);
193         $this->assertEquals(count($comments), 1);
195         $annotations = page_editor::get_annotations($grade->id, 0, true);
197         $this->assertEquals(count($annotations), 1);
199         page_editor::release_drafts($grade->id);
201         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
203         $this->assertTrue($notempty);
205         page_editor::unrelease_drafts($grade->id);
207         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
209         $this->assertFalse($notempty);
210     }
212     public function test_document_services() {
214         $assign = $this->create_assign_and_submit_pdf();
215         $this->setUser($this->teachers[0]);
217         $grade = $assign->get_user_grade($this->students[0]->id, true);
219         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
220         $this->assertFalse($notempty);
222         $comment = new comment();
224         $comment->rawtext = 'Comment text';
225         $comment->width = 100;
226         $comment->x = 100;
227         $comment->y = 100;
228         $comment->colour = 'red';
230         page_editor::set_comments($grade->id, 0, array($comment));
232         $annotations = array();
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);
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);
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);
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);
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);
285         page_editor::release_drafts($grade->id);
287         $notempty = page_editor::has_annotations_or_comments($grade->id, false);
289         $this->assertTrue($notempty);
291         $file = document_services::generate_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
292         $this->assertNotEmpty($file);
294         $file2 = document_services::get_feedback_document($assign->get_instance()->id, $grade->userid, $grade->attemptnumber);
296         $this->assertEquals($file, $file2);
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);
301         $this->assertEmpty($file3);
302     }