MDL-29794 Initial support for re-using a shared grading form
[moodle.git] / grade / grading / simpletest / testlib.php
CommitLineData
4333580e
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
64402867 19 * Unit tests for the advanced grading subsystem
4333580e
DM
20 *
21 * @package core
22 * @subpackage grading
23 * @copyright 2011 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $DB, $CFG;
30
31if (empty($CFG->unittestprefix)) {
32 die('You must define $CFG->unittestprefix to run these unit tests.');
33}
34
64402867
DM
35if ($CFG->unittestprefix == $CFG->prefix) {
36 die('Eh? Do you play with the fire? Fireman Sam won\'t come dude. The unittestprefix must be different from the standard prefix.');
37}
38
4333580e
DM
39require_once($CFG->dirroot . '/grade/grading/lib.php'); // Include the code to test
40
41/**
42 * Makes protected method accessible for testing purposes
43 */
44class testable_grading_manager extends grading_manager {
45}
46
47/**
48 * Test cases for the grading manager API
49 */
50class grading_manager_test extends UnitTestCase {
51
64402867
DM
52 /** @var moodle_database current real driver instance */
53 protected $realDB;
54
4333580e 55 public function setUp() {
64402867
DM
56 global $DB, $CFG;
57
58 $this->realDB = $DB;
59 $dbclass = get_class($this->realDB);
60 $DB = new $dbclass();
61 $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix);
62 $dbman = $DB->get_manager();
63
64 // drop everything we have in the mock DB
65 $dbman->delete_tables_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml');
66 // create all tables we need for this test case
67 $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml', 'grading_areas');
4333580e
DM
68 }
69
70 public function tearDown() {
64402867
DM
71 global $DB, $CFG;
72
73 // clean everything we have in the mock DB
74 //$DB->get_manager()->delete_tables_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml');
75 // switch to the real database
76 $DB = $this->realDB;
4333580e
DM
77 }
78
79 public function test_basic_instantiation() {
80
81 $manager1 = get_grading_manager();
82
83 $fakecontext = (object)array(
84 'id' => 42,
85 'contextlevel' => CONTEXT_MODULE,
86 'instanceid' => 22,
87 'path' => '/1/3/15/42',
88 'depth' => 4);
89
90 $manager2 = get_grading_manager($fakecontext);
91 $manager3 = get_grading_manager($fakecontext, 'assignment_upload');
92 $manager4 = get_grading_manager($fakecontext, 'assignment_upload', 'submission');
93 }
64402867
DM
94
95 public function test_set_and_get_grading_area() {
96 global $DB;
97
98 sleep(2); // to make sure the microtime will always return unique values
9b8550f8
DM
99 $areaname1 = 'area1-' . (string)microtime(true);
100 $areaname2 = 'area2-' . (string)microtime(true);
64402867
DM
101 $fakecontext = (object)array(
102 'id' => 42,
103 'contextlevel' => CONTEXT_MODULE,
104 'instanceid' => 22,
105 'path' => '/1/3/15/42',
106 'depth' => 4);
107
108 // non-existing area
9b8550f8 109 $gradingman = get_grading_manager($fakecontext, 'mod_foobar', $areaname1);
64402867
DM
110 $this->assertNull($gradingman->get_active_method());
111
9b8550f8 112 // creates area implicitly and sets active method
3bd217aa 113 $this->assertTrue($gradingman->set_active_method('rubric'));
64402867
DM
114 $this->assertEqual('rubric', $gradingman->get_active_method());
115
3bd217aa
DM
116 // repeat setting of already set active method
117 $this->assertFalse($gradingman->set_active_method('rubric'));
118
9b8550f8
DM
119 // switch the manager to another area
120 $gradingman->set_area($areaname2);
121 $this->assertNull($gradingman->get_active_method());
122
123 // switch back and ask again
124 $gradingman->set_area($areaname1);
125 $this->assertEqual('rubric', $gradingman->get_active_method());
126
64402867
DM
127 // attempting to set an invalid method
128 $this->expectException('moodle_exception');
129 $gradingman->set_active_method('no_one_should_ever_try_to_implement_a_method_with_this_silly_name');
130 }
20836db9
DM
131
132 public function test_tokenize() {
133
134 $needle = " šašek, \n\n \r a král; \t";
135 $tokens = testable_grading_manager::tokenize($needle);
136 $this->assertEqual(2, count($tokens));
137 $this->assertTrue(in_array('šašek', $tokens));
138 $this->assertTrue(in_array('král', $tokens));
139
140 $needle = ' " šašek a král " ';
141 $tokens = testable_grading_manager::tokenize($needle);
142 $this->assertEqual(1, count($tokens));
143 $this->assertTrue(in_array('šašek a král', $tokens));
144
145 $needle = '""';
146 $tokens = testable_grading_manager::tokenize($needle);
147 $this->assertTrue(empty($tokens));
148
149 $needle = '"0"';
150 $tokens = testable_grading_manager::tokenize($needle);
151 $this->assertEqual(1, count($tokens));
152 $this->assertTrue(in_array('0', $tokens));
153
154 $needle = '<span>Aha</span>, then who\'s a bad guy here he?';
155 $tokens = testable_grading_manager::tokenize($needle);
156 $this->assertTrue(in_array('span', $tokens));
157 $this->assertTrue(in_array('Aha', $tokens));
158 $this->assertTrue(in_array('who', $tokens));
159 }
4333580e 160}