Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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  namespace qtype_ddmarker;
  18  
  19  use qtype_ddmarker_shape_circle;
  20  use qtype_ddmarker_shape_polygon;
  21  use qtype_ddmarker_shape_rectangle;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  global $CFG;
  25  
  26  require_once($CFG->dirroot . '/question/type/ddmarker/shapes.php');
  27  
  28  /**
  29   * Unit tests for the drag-and-drop words shape code.
  30   *
  31   * @package   qtype_ddmarker
  32   * @copyright 2012 The Open University
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class shapes_test extends \basic_testcase {
  36  
  37      public function test_polygon_valdiation_test_ok() {
  38          $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, 10; 20, 20; 10, 20');
  39          $this->assertFalse($shape->get_coords_interpreter_error()); // No errors.
  40      }
  41  
  42      public function test_polygon_valdiation_test_only_two_points() {
  43          $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, 10');
  44          $this->assertEquals(get_string('formerror_polygonmusthaveatleastthreepoints', 'qtype_ddmarker',
  45                          array('shape' => 'polygon', 'coordsstring' => get_string('shape_polygon_coords', 'qtype_ddmarker'))),
  46                  $shape->get_coords_interpreter_error());
  47      }
  48  
  49      public function test_polygon_valdiation_test_invalid_point() {
  50          $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, ; 20, 20; 10, 20');
  51          $this->assertEquals(get_string('formerror_onlyusewholepositivenumbers', 'qtype_ddmarker',
  52                          array('shape' => 'polygon', 'coordsstring' => get_string('shape_polygon_coords', 'qtype_ddmarker'))),
  53                  $shape->get_coords_interpreter_error());
  54      }
  55  
  56      public function test_polygon_valdiation_test_repeated_point() {
  57          $shape = new qtype_ddmarker_shape_polygon('70,220;90,200;95,150;120,150;140,200;150,230;'.
  58                  '150,230;150,240;120,240;110,240;90,240');
  59          $this->assertEquals(get_string('formerror_repeatedpoint', 'qtype_ddmarker',
  60                          array('shape' => 'polygon', 'coordsstring' => get_string('shape_polygon_coords', 'qtype_ddmarker'))),
  61                  $shape->get_coords_interpreter_error());
  62      }
  63  
  64      public function test_polygon_hit_test() {
  65          $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, 10; 20, 20; 10, 20');
  66          $this->assertTrue($shape->is_point_in_shape(array(15, 15)));
  67          $this->assertFalse($shape->is_point_in_shape(array(5, 5)));
  68          $this->assertFalse($shape->is_point_in_shape(array(5, 15)));
  69          $this->assertFalse($shape->is_point_in_shape(array(15, 25)));
  70          $this->assertFalse($shape->is_point_in_shape(array(25, 15)));
  71          $this->assertTrue($shape->is_point_in_shape(array(11, 11)));
  72          $this->assertTrue($shape->is_point_in_shape(array(19, 19)));
  73  
  74          // Test points right on the edge are in.
  75          $this->assertTrue($shape->is_point_in_shape(array(10, 10)));
  76          $this->assertTrue($shape->is_point_in_shape(array(10, 20)));
  77          $this->assertTrue($shape->is_point_in_shape(array(20, 20)));
  78          $this->assertTrue($shape->is_point_in_shape(array(20, 10)));
  79          $this->assertTrue($shape->is_point_in_shape(array(10, 15)));
  80          $this->assertTrue($shape->is_point_in_shape(array(15, 10)));
  81          $this->assertTrue($shape->is_point_in_shape(array(20, 15)));
  82          $this->assertTrue($shape->is_point_in_shape(array(15, 20)));
  83  
  84          // Should accept closed polygon coords or unclosed and it will model a closed polygon.
  85          $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, 10; 20, 20; 10, 20; 10, 10');
  86          $this->assertTrue($shape->is_point_in_shape(array(15, 15)));
  87          $this->assertFalse($shape->is_point_in_shape(array(5, 5)));
  88          $this->assertFalse($shape->is_point_in_shape(array(5, 15)));
  89          $this->assertFalse($shape->is_point_in_shape(array(15, 25)));
  90          $this->assertFalse($shape->is_point_in_shape(array(25, 15)));
  91          $this->assertTrue($shape->is_point_in_shape(array(11, 11)));
  92          $this->assertTrue($shape->is_point_in_shape(array(19, 19)));
  93  
  94          $shape = new qtype_ddmarker_shape_polygon('10, 10; 15, 5; 20, 10; 20, 20; 10, 20');
  95          $this->assertTrue($shape->is_point_in_shape(array(15, 15)));
  96          $this->assertFalse($shape->is_point_in_shape(array(5, 5)));
  97          $this->assertFalse($shape->is_point_in_shape(array(5, 15)));
  98          $this->assertFalse($shape->is_point_in_shape(array(15, 25)));
  99          $this->assertFalse($shape->is_point_in_shape(array(25, 15)));
 100          $this->assertTrue($shape->is_point_in_shape(array(11, 11)));
 101          $this->assertTrue($shape->is_point_in_shape(array(19, 19)));
 102          $this->assertTrue($shape->is_point_in_shape(array(15, 9)));
 103          $this->assertTrue($shape->is_point_in_shape(array(15, 10)));
 104  
 105          $shape = new qtype_ddmarker_shape_polygon('15, 5; 20, 10; 20, 20; 10, 20; 10, 10');
 106          $this->assertTrue($shape->is_point_in_shape(array(15, 10)));
 107  
 108          $shape = new qtype_ddmarker_shape_polygon('15, 5; 20, 10; 20, 20; 10, 20; 10, 10');
 109          $this->assertFalse($shape->is_point_in_shape(array(25, 10)));
 110  
 111          $shape = new qtype_ddmarker_shape_polygon('0, 0; 500, 0; 600, 1000; 0, 1200; 10, 10');
 112          $this->assertTrue($shape->is_point_in_shape(array(25, 10)));
 113      }
 114  
 115      public function test_circle_valdiation_test() {
 116          $shape = new qtype_ddmarker_shape_circle('10, 10; 10');
 117          $this->assertFalse($shape->get_coords_interpreter_error()); // No errors.
 118      }
 119  
 120      public function test_circle_hit_test() {
 121          $shape = new qtype_ddmarker_shape_circle('10, 10; 10');
 122          $this->assertTrue($shape->is_point_in_shape(array(19, 10)));
 123          $this->assertTrue($shape->is_point_in_shape(array(20, 10)));
 124          $this->assertFalse($shape->is_point_in_shape(array(21, 10)));
 125  
 126          $this->assertTrue($shape->is_point_in_shape(array(10, 1)));
 127          $this->assertTrue($shape->is_point_in_shape(array(10, 0)));
 128          $this->assertFalse($shape->is_point_in_shape(array(10, -1)));
 129  
 130          $this->assertFalse($shape->is_point_in_shape(array(15, 25)));
 131          $this->assertFalse($shape->is_point_in_shape(array(25, 15)));
 132          $this->assertTrue($shape->is_point_in_shape(array(11, 11)));
 133          $this->assertTrue($shape->is_point_in_shape(array(1, 10)));
 134          $this->assertTrue($shape->is_point_in_shape(array(17, 17)));
 135          $this->assertTrue($shape->is_point_in_shape(array(3, 3)));
 136          $this->assertFalse($shape->is_point_in_shape(array(2, 2)));
 137  
 138          // Should be exactly on the boundary - 3, 4, 5 right-angled triangle.
 139          $this->assertTrue($shape->is_point_in_shape(array(16, 18)));
 140      }
 141  
 142      public function test_rectangle_valdiation_test() {
 143          $shape = new qtype_ddmarker_shape_rectangle('1000, 4000; 500, 400');
 144          $this->assertFalse($shape->get_coords_interpreter_error()); // No errors.
 145      }
 146  
 147      public function test_rectangle_hit_test() {
 148          $shape = new qtype_ddmarker_shape_rectangle('1000, 4000; 500, 400');
 149          $this->assertFalse($shape->is_point_in_shape(array(999, 4200)));
 150          $this->assertTrue($shape->is_point_in_shape(array(1000, 4200)));
 151          $this->assertTrue($shape->is_point_in_shape(array(1001, 4200)));
 152          $this->assertTrue($shape->is_point_in_shape(array(1499, 4200)));
 153          $this->assertTrue($shape->is_point_in_shape(array(1500, 4200)));
 154          $this->assertFalse($shape->is_point_in_shape(array(1501, 4200)));
 155  
 156          $this->assertFalse($shape->is_point_in_shape(array(1250, 3999)));
 157          $this->assertTrue($shape->is_point_in_shape(array(1250, 4000)));
 158          $this->assertTrue($shape->is_point_in_shape(array(1250, 4400)));
 159          $this->assertFalse($shape->is_point_in_shape(array(1250, 4401)));
 160      }
 161  }