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