[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 26 defined('MOODLE_INTERNAL') || die(); 27 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; 34 35 global $CFG; 36 require_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 */ 44 class 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_onlinetext_enabled' => 1, 58 'assignsubmission_file_enabled' => 1, 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 304 /** 305 * Test that modifying the annotated pdf form return true when modified 306 * and false when not modified. 307 */ 308 public function test_is_feedback_modified() { 309 global $DB; 310 $assign = $this->create_assign_and_submit_pdf(); 311 $this->setUser($this->teachers[0]); 312 313 $grade = $assign->get_user_grade($this->students[0]->id, true); 314 315 $notempty = page_editor::has_annotations_or_comments($grade->id, false); 316 $this->assertFalse($notempty); 317 318 $comment = new comment(); 319 320 $comment->rawtext = 'Comment text'; 321 $comment->width = 100; 322 $comment->x = 100; 323 $comment->y = 100; 324 $comment->colour = 'red'; 325 326 page_editor::set_comments($grade->id, 0, array($comment)); 327 328 $annotations = array(); 329 330 $annotation = new annotation(); 331 $annotation->path = ''; 332 $annotation->x = 100; 333 $annotation->y = 100; 334 $annotation->endx = 200; 335 $annotation->endy = 200; 336 $annotation->type = 'line'; 337 $annotation->colour = 'red'; 338 array_push($annotations, $annotation); 339 340 page_editor::set_annotations($grade->id, 0, $annotations); 341 342 $plugin = $assign->get_feedback_plugin_by_type('editpdf'); 343 $data = new stdClass(); 344 $data->editpdf_source_userid = $this->students[0]->id; 345 $this->assertTrue($plugin->is_feedback_modified($grade, $data)); 346 $plugin->save($grade, $data); 347 348 $annotation = new annotation(); 349 $annotation->gradeid = $grade->id; 350 $annotation->pageno = 0; 351 $annotation->path = ''; 352 $annotation->x = 100; 353 $annotation->y = 100; 354 $annotation->endx = 200; 355 $annotation->endy = 200; 356 $annotation->type = 'rectangle'; 357 $annotation->colour = 'yellow'; 358 359 $yellowannotationid = page_editor::add_annotation($annotation); 360 361 // Add a comment as well. 362 $comment = new comment(); 363 $comment->gradeid = $grade->id; 364 $comment->pageno = 0; 365 $comment->rawtext = 'Second Comment text'; 366 $comment->width = 100; 367 $comment->x = 100; 368 $comment->y = 100; 369 $comment->colour = 'red'; 370 page_editor::add_comment($comment); 371 372 $this->assertTrue($plugin->is_feedback_modified($grade, $data)); 373 $plugin->save($grade, $data); 374 375 // We should have two annotations. 376 $this->assertCount(2, page_editor::get_annotations($grade->id, 0, false)); 377 // And two comments. 378 $this->assertCount(2, page_editor::get_comments($grade->id, 0, false)); 379 380 // Add one annotation and delete another. 381 $annotation = new annotation(); 382 $annotation->gradeid = $grade->id; 383 $annotation->pageno = 0; 384 $annotation->path = '100,100:105,105:110,100'; 385 $annotation->x = 100; 386 $annotation->y = 100; 387 $annotation->endx = 110; 388 $annotation->endy = 105; 389 $annotation->type = 'pen'; 390 $annotation->colour = 'black'; 391 page_editor::add_annotation($annotation); 392 393 $annotations = page_editor::get_annotations($grade->id, 0, true); 394 page_editor::remove_annotation($yellowannotationid); 395 $this->assertTrue($plugin->is_feedback_modified($grade, $data)); 396 $plugin->save($grade, $data); 397 398 // We should have two annotations. 399 $this->assertCount(2, page_editor::get_annotations($grade->id, 0, false)); 400 // And two comments. 401 $this->assertCount(2, page_editor::get_comments($grade->id, 0, false)); 402 403 // Add a comment and then remove it. Should not be considered as modified. 404 $comment = new comment(); 405 $comment->gradeid = $grade->id; 406 $comment->pageno = 0; 407 $comment->rawtext = 'Third Comment text'; 408 $comment->width = 400; 409 $comment->x = 57; 410 $comment->y = 205; 411 $comment->colour = 'black'; 412 $comment->id = page_editor::add_comment($comment); 413 414 // We should now have three comments. 415 $this->assertCount(3, page_editor::get_comments($grade->id, 0, true)); 416 // Now delete the newest record. 417 page_editor::remove_comment($comment->id); 418 // Back to two comments. 419 $this->assertCount(2, page_editor::get_comments($grade->id, 0, true)); 420 // No modification. 421 $this->assertFalse($plugin->is_feedback_modified($grade, $data)); 422 } 423 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |