[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/assign/feedback/editpdf/tests/ -> editpdf_test.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1