Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

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