Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403]

   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   * Behat editpdf-related steps definitions.
  19   *
  20   * @package    assignfeedback_editpdf
  21   * @category   test
  22   * @copyright  2013 Jerome Mouneyrac
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  27  
  28  require_once (__DIR__ . '/../../../../../../lib/behat/behat_base.php');
  29  
  30  /**
  31   * Steps definitions related with the editpdf.
  32   *
  33   * @package    assignfeedback_editpdf
  34   * @category   test
  35   * @copyright  2013 Jerome Mouneyrac
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class behat_assignfeedback_editpdf extends behat_base {
  39  
  40      /**
  41       * Checks that Ghostscript is installed.
  42       *
  43       * @Given /^ghostscript is installed$/
  44       */
  45      public function ghostscript_is_installed() {
  46          $testpath = assignfeedback_editpdf\pdf::test_gs_path();
  47          if (!extension_loaded('zlib') or
  48              $testpath->status !== assignfeedback_editpdf\pdf::GSPATH_OK) {
  49              throw new \Moodle\BehatExtension\Exception\SkippedException;
  50          }
  51      }
  52  
  53      /**
  54       * Draw on the pdf.
  55       *
  56       * @When /^I draw on the pdf$/
  57       */
  58      public function i_draw_on_the_pdf() {
  59          // There appears to be a bug with detecting changes to
  60          // annotations. If a PDF is annotated, then the student
  61          // updates the submission, if the teacher then draws the
  62          // exact same annotations on the new PDF, the readonly
  63          // pages are not updated and the student's view of the
  64          // annotated PDF still shows the old PDF. So we add some
  65          // randomness in this step to ensure the annotations are
  66          // different every time.
  67          //
  68          // This was added to test MDL-75898. See MDL-76659 for
  69          // more details about the bug.
  70          //
  71          // Note - the start, move and end locations must all be different.
  72          // If they are the same, it's possible the PDF tool selected does not activate.
  73          $startx = 100 + rand(0, 50);
  74          $starty = 250 + rand(0, 50);
  75          $js = ' (function() {
  76      var instance = M.assignfeedback_editpdf.instance;
  77      var event = { clientX: ' . $startx . ', clientY: ' . $starty . ', preventDefault: function() {} };
  78      instance.edit_start(event);
  79  }()); ';
  80          $this->execute_script($js);
  81          sleep(1);
  82  
  83          // Move slightly in one direction.
  84          $movex = $startx + 50;
  85          $movey = $starty + 30;
  86          $js = ' (function() {
  87      var instance = M.assignfeedback_editpdf.instance;
  88      var event = { clientX: ' . $movex . ', clientY: ' . $movey . ', preventDefault: function() {} };
  89      instance.edit_move(event);
  90  }()); ';
  91          $this->execute_script($js);
  92          sleep(1);
  93  
  94          // Move a little further to stop.
  95          $endx = $movex + 15;
  96          $endy = $movey + 15;
  97          $js = ' (function() {
  98      var instance = M.assignfeedback_editpdf.instance;
  99      var event = { clientX: ' . $endx . ', clientY: ' . $endy . ', preventDefault: function() {} };
 100      instance.edit_end(event);
 101  }()); ';
 102          $this->execute_script($js);
 103          sleep(1);
 104      }
 105  
 106      /**
 107       * I wait for all pages in the PDF document to be converted to images and loaded.
 108       *
 109       * @Given /^I wait for the complete PDF to load$/
 110       */
 111      public function i_wait_for_all_editpdf_pages_to_load() {
 112          // No need to wait if not running JS.
 113          if (!$this->running_javascript()) {
 114              return;
 115          }
 116  
 117          // Ensure that the document is ready, and all pages are loaded.
 118          $conditions = [
 119              'typeof M !== "undefined"',
 120              'typeof M.assignfeedback_editpdf !== "undefined"',
 121              'typeof M.assignfeedback_editpdf.instance !== "undefined"',
 122              'M.assignfeedback_editpdf.instance.documentstatus === 2',
 123              'M.assignfeedback_editpdf.instance.pagecount === M.assignfeedback_editpdf.instance.pages.length',
 124          ];
 125          $js = implode(' && ', $conditions);
 126  
 127          $this->getSession()->wait(self::get_timeout() * 1000, "({$js})");
 128      }
 129  }