Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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