Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [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   * 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       * @expectedException moodle_exception
  72       */
  73      public function test_set_and_get_grading_area() {
  74          global $DB;
  75  
  76          $this->resetAfterTest(true);
  77  
  78          //sleep(2); // to make sure the microtime will always return unique values // No sleeping in tests!!! --skodak
  79          $areaname1 = 'area1-' . (string)microtime(true);
  80          $areaname2 = 'area2-' . (string)microtime(true);
  81          $fakecontext = (object)array(
  82              'id'            => 42,
  83              'contextlevel'  => CONTEXT_MODULE,
  84              'instanceid'    => 22,
  85              'path'          => '/1/3/15/42',
  86              'depth'         => 4);
  87  
  88          // non-existing area
  89          $gradingman = get_grading_manager($fakecontext, 'mod_foobar', $areaname1);
  90          $this->assertNull($gradingman->get_active_method());
  91  
  92          // creates area implicitly and sets active method
  93          $this->assertTrue($gradingman->set_active_method('rubric'));
  94          $this->assertEquals('rubric', $gradingman->get_active_method());
  95  
  96          // repeat setting of already set active method
  97          $this->assertFalse($gradingman->set_active_method('rubric'));
  98  
  99          // switch the manager to another area
 100          $gradingman->set_area($areaname2);
 101          $this->assertNull($gradingman->get_active_method());
 102  
 103          // switch back and ask again
 104          $gradingman->set_area($areaname1);
 105          $this->assertEquals('rubric', $gradingman->get_active_method());
 106  
 107          // attempting to set an invalid method
 108          $gradingman->set_active_method('no_one_should_ever_try_to_implement_a_method_with_this_silly_name');
 109      }
 110  
 111      /**
 112       * Unit test to check the tokenize method
 113       */
 114      public function test_tokenize() {
 115  
 116          $UTFfailuremessage = 'A test using UTF-8 characters has failed. Consider updating PHP and PHP\'s PCRE or INTL extensions (MDL-30494)';
 117  
 118          $needle = "    šašek, \n\n   \r    a král;  \t";
 119          $tokens = testable_grading_manager::tokenize($needle);
 120          $this->assertEquals(2, count($tokens), $UTFfailuremessage);
 121          $this->assertTrue(in_array('šašek', $tokens), $UTFfailuremessage);
 122          $this->assertTrue(in_array('král', $tokens), $UTFfailuremessage);
 123  
 124          $needle = ' "   šašek a král "    ';
 125          $tokens = testable_grading_manager::tokenize($needle);
 126          $this->assertEquals(1, count($tokens));
 127          $this->assertTrue(in_array('šašek a král', $tokens));
 128  
 129          $needle = '""';
 130          $tokens = testable_grading_manager::tokenize($needle);
 131          $this->assertTrue(empty($tokens));
 132  
 133          $needle = '"0"';
 134          $tokens = testable_grading_manager::tokenize($needle);
 135          $this->assertEquals(1, count($tokens));
 136          $this->assertTrue(in_array('0', $tokens));
 137  
 138          $needle = '<span>Aha</span>, then who\'s a bad guy here he?';
 139          $tokens = testable_grading_manager::tokenize($needle);
 140          $this->assertEquals(8, count($tokens));
 141          $this->assertTrue(in_array('span', $tokens)); // Extracted the tag name
 142          $this->assertTrue(in_array('Aha', $tokens));
 143          $this->assertTrue(in_array('who', $tokens)); // Removed the trailing 's
 144          $this->assertTrue(!in_array('a', $tokens)); //Single letter token was dropped
 145          $this->assertTrue(in_array('he', $tokens)); // Removed the trailing ?
 146  
 147          $needle = 'grammar, "english language"';
 148          $tokens = testable_grading_manager::tokenize($needle);
 149          $this->assertTrue(in_array('grammar', $tokens));
 150          $this->assertTrue(in_array('english', $tokens));
 151          $this->assertTrue(in_array('language', $tokens));
 152          $this->assertTrue(!in_array('english language', $tokens)); // Quoting part of the string is not supported
 153      }
 154  }