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] [Versions 39 and 310]

   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 advanced grading subsystem
  19   *
  20   * @package    core_grading
  21   * @category   phpunit
  22   * @copyright  2011 David Mudrak <david@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/grade/grading/lib.php'); // Include the code to test
  30  
  31  
  32  /**
  33   * Makes protected method accessible for testing purposes
  34   *
  35   * @package    core_grading
  36   * @category   phpunit
  37   * @copyright  2011 David Mudrak <david@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class testable_grading_manager extends grading_manager {
  41  }
  42  
  43  
  44  /**
  45   * Test cases for the grading manager API
  46   *
  47   * @package    core_grading
  48   * @category   phpunit
  49   * @copyright  2011 David Mudrak <david@moodle.com>
  50   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  51   */
  52  class core_grade_grading_manager_testcase extends advanced_testcase {
  53      public function test_basic_instantiation() {
  54          $manager1 = get_grading_manager();
  55  
  56          $fakecontext = (object)array(
  57              'id'            => 42,
  58              'contextlevel'  => CONTEXT_MODULE,
  59              'instanceid'    => 22,
  60              'path'          => '/1/3/15/42',
  61              'depth'         => 4);
  62  
  63          $manager2 = get_grading_manager($fakecontext);
  64          $manager3 = get_grading_manager($fakecontext, 'assignment_upload');
  65          $manager4 = get_grading_manager($fakecontext, 'assignment_upload', 'submission');
  66      }
  67  
  68      /**
  69       * Unit test to set and get grading areas
  70       */
  71      public function test_set_and_get_grading_area() {
  72          global $DB;
  73  
  74          $this->resetAfterTest(true);
  75  
  76          //sleep(2); // to make sure the microtime will always return unique values // No sleeping in tests!!! --skodak
  77          $areaname1 = 'area1-' . (string)microtime(true);
  78          $areaname2 = 'area2-' . (string)microtime(true);
  79          $fakecontext = (object)array(
  80              'id'            => 42,
  81              'contextlevel'  => CONTEXT_MODULE,
  82              'instanceid'    => 22,
  83              'path'          => '/1/3/15/42',
  84              'depth'         => 4);
  85  
  86          // non-existing area
  87          $gradingman = get_grading_manager($fakecontext, 'mod_foobar', $areaname1);
  88          $this->assertNull($gradingman->get_active_method());
  89  
  90          // creates area implicitly and sets active method
  91          $this->assertTrue($gradingman->set_active_method('rubric'));
  92          $this->assertEquals('rubric', $gradingman->get_active_method());
  93  
  94          // repeat setting of already set active method
  95          $this->assertFalse($gradingman->set_active_method('rubric'));
  96  
  97          // switch the manager to another area
  98          $gradingman->set_area($areaname2);
  99          $this->assertNull($gradingman->get_active_method());
 100  
 101          // switch back and ask again
 102          $gradingman->set_area($areaname1);
 103          $this->assertEquals('rubric', $gradingman->get_active_method());
 104  
 105          // attempting to set an invalid method
 106          $this->expectException(moodle_exception::class);
 107          $gradingman->set_active_method('no_one_should_ever_try_to_implement_a_method_with_this_silly_name');
 108      }
 109  
 110      /**
 111       * Unit test to check the tokenize method
 112       */
 113      public function test_tokenize() {
 114  
 115          $UTFfailuremessage = 'A test using UTF-8 characters has failed. Consider updating PHP and PHP\'s PCRE or INTL extensions (MDL-30494)';
 116  
 117          $needle = "    šašek, \n\n   \r    a král;  \t";
 118          $tokens = testable_grading_manager::tokenize($needle);
 119          $this->assertEquals(2, count($tokens), $UTFfailuremessage);
 120          $this->assertTrue(in_array('šašek', $tokens), $UTFfailuremessage);
 121          $this->assertTrue(in_array('král', $tokens), $UTFfailuremessage);
 122  
 123          $needle = ' "   šašek a král "    ';
 124          $tokens = testable_grading_manager::tokenize($needle);
 125          $this->assertEquals(1, count($tokens));
 126          $this->assertTrue(in_array('šašek a král', $tokens));
 127  
 128          $needle = '""';
 129          $tokens = testable_grading_manager::tokenize($needle);
 130          $this->assertTrue(empty($tokens));
 131  
 132          $needle = '"0"';
 133          $tokens = testable_grading_manager::tokenize($needle);
 134          $this->assertEquals(1, count($tokens));
 135          $this->assertTrue(in_array('0', $tokens));
 136  
 137          $needle = '<span>Aha</span>, then who\'s a bad guy here he?';
 138          $tokens = testable_grading_manager::tokenize($needle);
 139          $this->assertEquals(8, count($tokens));
 140          $this->assertTrue(in_array('span', $tokens)); // Extracted the tag name
 141          $this->assertTrue(in_array('Aha', $tokens));
 142          $this->assertTrue(in_array('who', $tokens)); // Removed the trailing 's
 143          $this->assertTrue(!in_array('a', $tokens)); //Single letter token was dropped
 144          $this->assertTrue(in_array('he', $tokens)); // Removed the trailing ?
 145  
 146          $needle = 'grammar, "english language"';
 147          $tokens = testable_grading_manager::tokenize($needle);
 148          $this->assertTrue(in_array('grammar', $tokens));
 149          $this->assertTrue(in_array('english', $tokens));
 150          $this->assertTrue(in_array('language', $tokens));
 151          $this->assertTrue(!in_array('english language', $tokens)); // Quoting part of the string is not supported
 152      }
 153  }