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  /**
  18   * Generator testcase for the gradingforum_guide generator.
  19   *
  20   * @package    gradingform_guide
  21   * @category   test
  22   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace gradingform_guide;
  27  
  28  use context_module;
  29  use gradingform_controller;
  30  use gradingform_guide_controller;
  31  
  32  /**
  33   * Generator testcase for the gradingforum_guide generator.
  34   *
  35   * @package    gradingform_guide
  36   * @category   test
  37   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class generator_test extends \advanced_testcase {
  41  
  42      /**
  43       * Test guide creation.
  44       */
  45      public function test_guide_creation(): void {
  46          global $DB;
  47          $this->resetAfterTest(true);
  48  
  49          // Fetch generators.
  50          $generator = \testing_util::get_data_generator();
  51          $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
  52  
  53          // Create items required for testing.
  54          $course = $generator->create_course();
  55          $module = $generator->create_module('assign', ['course' => $course]);
  56          $user = $generator->create_user();
  57          $context = context_module::instance($module->cmid);
  58  
  59          // Data for testing.
  60          $name = 'myfirstguide';
  61          $description = 'My first guide';
  62          $criteria = [
  63              'Alphabet' => [
  64                  'description' => 'How well you know your alphabet',
  65                  'descriptionmarkers' => 'Basic literacy: Alphabet',
  66                  'maxscore' => 5,
  67              ],
  68              'Times tables' => [
  69                  'description' => 'How well you know your times-tables',
  70                  'descriptionmarkers' => 'Basic numeracy: Multiplication',
  71                  'maxscore' => 10,
  72              ],
  73          ];
  74  
  75          // Unit under test.
  76          $this->setUser($user);
  77          $controller = $guidegenerator->create_instance($context, 'mod_assign', 'submission', $name, $description, $criteria);
  78  
  79          $this->assertInstanceOf(gradingform_guide_controller::class, $controller);
  80  
  81          $definition = $controller->get_definition();
  82          $this->assertEquals('guide', $definition->method);
  83          $this->assertNotEmpty($definition->id);
  84          $this->assertEquals($name, $definition->name);
  85          $this->assertEquals($description, $definition->description);
  86          $this->assertEquals(gradingform_controller::DEFINITION_STATUS_READY, $definition->status);
  87          $this->assertNotEmpty($definition->timecreated);
  88          $this->assertNotEmpty($definition->timemodified);
  89          $this->assertEquals($user->id, $definition->usercreated);
  90  
  91          $this->assertNotEmpty($definition->guide_criteria);
  92          $this->assertCount(2, $definition->guide_criteria);
  93  
  94          // Check the alphabet criteria.
  95          $criteriaids = array_keys($definition->guide_criteria);
  96  
  97          $alphabet = $definition->guide_criteria[$criteriaids[0]];
  98          $this->assertNotEmpty($alphabet['id']);
  99          $this->assertEquals(1, $alphabet['sortorder']);
 100          $this->assertEquals('How well you know your alphabet', $alphabet['description']);
 101          $this->assertEquals('Basic literacy: Alphabet', $alphabet['descriptionmarkers']);
 102          $this->assertEquals(5, $alphabet['maxscore']);
 103  
 104          // Check the times tables criteria.
 105          $tables = $definition->guide_criteria[$criteriaids[1]];
 106          $this->assertNotEmpty($tables['id']);
 107          $this->assertEquals(2, $tables['sortorder']);
 108          $this->assertEquals('How well you know your times-tables', $tables['description']);
 109          $this->assertEquals('Basic numeracy: Multiplication', $tables['descriptionmarkers']);
 110          $this->assertEquals(10, $tables['maxscore']);
 111      }
 112  
 113      /**
 114       * Test the get_criterion_for_values function.
 115       * This is used for finding criterion and level information within a guide.
 116       */
 117      public function test_get_criterion_for_values(): void {
 118          global $DB;
 119          $this->resetAfterTest(true);
 120  
 121          // Fetch generators.
 122          $generator = \testing_util::get_data_generator();
 123          $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
 124  
 125          // Create items required for testing.
 126          $course = $generator->create_course();
 127          $module = $generator->create_module('assign', ['course' => $course]);
 128          $user = $generator->create_user();
 129          $context = context_module::instance($module->cmid);
 130  
 131          // Data for testing.
 132          $name = 'myfirstguide';
 133          $description = 'My first guide';
 134          $criteria = [
 135              'Alphabet' => [
 136                  'description' => 'How well you know your alphabet',
 137                  'descriptionmarkers' => 'Basic literacy: Alphabet',
 138                  'maxscore' => 5,
 139              ],
 140              'Times tables' => [
 141                  'description' => 'How well you know your times-tables',
 142                  'descriptionmarkers' => 'Basic numeracy: Multiplication',
 143                  'maxscore' => 10,
 144              ],
 145          ];
 146  
 147          $this->setUser($user);
 148          $controller = $guidegenerator->create_instance($context, 'mod_assign', 'submission', $name, $description, $criteria);
 149  
 150          // Valid criterion.
 151          $result = $guidegenerator->get_criterion_for_values($controller, 'Alphabet', 2);
 152          $this->assertEquals('Alphabet', $result->shortname);
 153          $this->assertEquals('How well you know your alphabet', $result->description);
 154          $this->assertEquals('Basic literacy: Alphabet', $result->descriptionmarkers);
 155          $this->assertEquals(5, $result->maxscore);
 156  
 157          // Invalid criterion.
 158          $result = $guidegenerator->get_criterion_for_values($controller, 'Foo', 0);
 159          $this->assertNull($result);
 160      }
 161  
 162      /**
 163       * Tests for the get_test_guide function.
 164       */
 165      public function test_get_test_guide(): void {
 166          global $DB;
 167          $this->resetAfterTest(true);
 168  
 169          // Fetch generators.
 170          $generator = \testing_util::get_data_generator();
 171          $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
 172  
 173          // Create items required for testing.
 174          $course = $generator->create_course();
 175          $module = $generator->create_module('assign', ['course' => $course]);
 176          $user = $generator->create_user();
 177          $context = context_module::instance($module->cmid);
 178  
 179          $this->setUser($user);
 180          $guide = $guidegenerator->get_test_guide($context, 'assign', 'submissions');
 181          $definition = $guide->get_definition();
 182  
 183          $this->assertEquals('testguide', $definition->name);
 184          $this->assertEquals('Description text', $definition->description);
 185          $this->assertEquals(gradingform_controller::DEFINITION_STATUS_READY, $definition->status);
 186  
 187          // Should create a guide with 2 criterion.
 188          $this->assertCount(2, $definition->guide_criteria);
 189      }
 190  
 191      /**
 192       * Test the get_submitted_form_data function.
 193       */
 194      public function test_get_submitted_form_data(): void {
 195          global $DB;
 196          $this->resetAfterTest(true);
 197  
 198          // Fetch generators.
 199          $generator = \testing_util::get_data_generator();
 200          $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
 201  
 202          // Create items required for testing.
 203          $course = $generator->create_course();
 204          $module = $generator->create_module('assign', ['course' => $course]);
 205          $user = $generator->create_user();
 206          $context = context_module::instance($module->cmid);
 207  
 208          $this->setUser($user);
 209          $controller = $guidegenerator->get_test_guide($context, 'assign', 'submissions');
 210  
 211          $result = $guidegenerator->get_submitted_form_data($controller, 93, [
 212              'Spelling mistakes' => [
 213                  'score' => 10,
 214                  'remark' => 'Pretty good but you had a couple of errors',
 215              ],
 216              'Pictures' => [
 217                  'score' => 15,
 218                  'remark' => 'Lots of nice pictures!',
 219              ]
 220          ]);
 221  
 222          $this->assertIsArray($result);
 223          $this->assertEquals(93, $result['itemid']);
 224          $this->assertIsArray($result['criteria']);
 225          $this->assertCount(2, $result['criteria']);
 226  
 227          $spelling = $guidegenerator->get_criterion_for_values($controller, 'Spelling mistakes');
 228          $this->assertIsArray($result['criteria'][$spelling->id]);
 229  
 230          $this->assertEquals(10, $result['criteria'][$spelling->id]['score']);
 231          $this->assertEquals('Pretty good but you had a couple of errors', $result['criteria'][$spelling->id]['remark']);
 232  
 233          $pictures = $guidegenerator->get_criterion_for_values($controller, 'Pictures', 2);
 234          $this->assertIsArray($result['criteria'][$pictures->id]);
 235          $this->assertEquals(15, $result['criteria'][$pictures->id]['score']);
 236          $this->assertEquals('Lots of nice pictures!', $result['criteria'][$pictures->id]['remark']);
 237      }
 238  
 239      /**
 240       * Test the get_test_form_data function.
 241       */
 242      public function test_get_test_form_data(): void {
 243          global $DB;
 244          $this->resetAfterTest(true);
 245  
 246          // Fetch generators.
 247          $generator = \testing_util::get_data_generator();
 248          $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
 249  
 250          // Create items required for testing.
 251          $course = $generator->create_course();
 252          $module = $generator->create_module('assign', ['course' => $course]);
 253          $user = $generator->create_user();
 254          $context = context_module::instance($module->cmid);
 255  
 256          $this->setUser($user);
 257          $controller = $guidegenerator->get_test_guide($context, 'assign', 'submissions');
 258  
 259          // Unit under test.
 260          $result = $guidegenerator->get_test_form_data(
 261              $controller,
 262              1839,
 263              10, 'Propper good speling',
 264              0, 'ASCII art is not a picture'
 265          );
 266  
 267          $this->assertIsArray($result);
 268          $this->assertEquals(1839, $result['itemid']);
 269          $this->assertIsArray($result['criteria']);
 270          $this->assertCount(2, $result['criteria']);
 271  
 272          $spelling = $guidegenerator->get_criterion_for_values($controller, 'Spelling mistakes');
 273          $this->assertIsArray($result['criteria'][$spelling->id]);
 274          $this->assertEquals(10, $result['criteria'][$spelling->id]['score']);
 275          $this->assertEquals('Propper good speling', $result['criteria'][$spelling->id]['remark']);
 276  
 277          $pictures = $guidegenerator->get_criterion_for_values($controller, 'Pictures');
 278          $this->assertIsArray($result['criteria'][$pictures->id]);
 279          $this->assertEquals(0, $result['criteria'][$pictures->id]['score']);
 280          $this->assertEquals('ASCII art is not a picture', $result['criteria'][$pictures->id]['remark']);
 281      }
 282  }