|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 global $DB, $CFG; 00030 00031 if (empty($CFG->unittestprefix)) { 00032 die('You must define $CFG->unittestprefix to run these unit tests.'); 00033 } 00034 00035 if ($CFG->unittestprefix == $CFG->prefix) { 00036 die('Eh? Do you play with the fire? Fireman Sam won\'t come dude. The unittestprefix must be different from the standard prefix.'); 00037 } 00038 00039 require_once($CFG->dirroot . '/grade/grading/lib.php'); // Include the code to test 00040 00044 class testable_grading_manager extends grading_manager { 00045 } 00046 00050 class grading_manager_test extends UnitTestCase { 00051 00053 protected $realDB; 00054 00055 public function setUp() { 00056 global $DB, $CFG; 00057 00058 $this->realDB = $DB; 00059 $dbclass = get_class($this->realDB); 00060 $DB = new $dbclass(); 00061 $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix); 00062 $dbman = $DB->get_manager(); 00063 00064 // drop everything we have in the mock DB 00065 $dbman->delete_tables_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml'); 00066 // create all tables we need for this test case 00067 $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml', 'grading_areas'); 00068 } 00069 00070 public function tearDown() { 00071 global $DB, $CFG; 00072 00073 // clean everything we have in the mock DB 00074 //$DB->get_manager()->delete_tables_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml'); 00075 // switch to the real database 00076 $DB = $this->realDB; 00077 } 00078 00079 public function test_basic_instantiation() { 00080 00081 $manager1 = get_grading_manager(); 00082 00083 $fakecontext = (object)array( 00084 'id' => 42, 00085 'contextlevel' => CONTEXT_MODULE, 00086 'instanceid' => 22, 00087 'path' => '/1/3/15/42', 00088 'depth' => 4); 00089 00090 $manager2 = get_grading_manager($fakecontext); 00091 $manager3 = get_grading_manager($fakecontext, 'assignment_upload'); 00092 $manager4 = get_grading_manager($fakecontext, 'assignment_upload', 'submission'); 00093 } 00094 00095 public function test_set_and_get_grading_area() { 00096 global $DB; 00097 00098 sleep(2); // to make sure the microtime will always return unique values 00099 $areaname1 = 'area1-' . (string)microtime(true); 00100 $areaname2 = 'area2-' . (string)microtime(true); 00101 $fakecontext = (object)array( 00102 'id' => 42, 00103 'contextlevel' => CONTEXT_MODULE, 00104 'instanceid' => 22, 00105 'path' => '/1/3/15/42', 00106 'depth' => 4); 00107 00108 // non-existing area 00109 $gradingman = get_grading_manager($fakecontext, 'mod_foobar', $areaname1); 00110 $this->assertNull($gradingman->get_active_method()); 00111 00112 // creates area implicitly and sets active method 00113 $this->assertTrue($gradingman->set_active_method('rubric')); 00114 $this->assertEqual('rubric', $gradingman->get_active_method()); 00115 00116 // repeat setting of already set active method 00117 $this->assertFalse($gradingman->set_active_method('rubric')); 00118 00119 // switch the manager to another area 00120 $gradingman->set_area($areaname2); 00121 $this->assertNull($gradingman->get_active_method()); 00122 00123 // switch back and ask again 00124 $gradingman->set_area($areaname1); 00125 $this->assertEqual('rubric', $gradingman->get_active_method()); 00126 00127 // attempting to set an invalid method 00128 $this->expectException('moodle_exception'); 00129 $gradingman->set_active_method('no_one_should_ever_try_to_implement_a_method_with_this_silly_name'); 00130 } 00131 00132 public function test_tokenize() { 00133 00134 $UTFfailuremessage = 'A test using UTF-8 characters has failed. Consider updating PHP and PHP\'s PCRE or INTL extensions (MDL-30494)'; 00135 00136 $needle = " šašek, \n\n \r a král; \t"; 00137 $tokens = testable_grading_manager::tokenize($needle); 00138 $this->assertEqual(2, count($tokens), $UTFfailuremessage); 00139 $this->assertTrue(in_array('šašek', $tokens), $UTFfailuremessage); 00140 $this->assertTrue(in_array('král', $tokens), $UTFfailuremessage); 00141 00142 $needle = ' " šašek a král " '; 00143 $tokens = testable_grading_manager::tokenize($needle); 00144 $this->assertEqual(1, count($tokens)); 00145 $this->assertTrue(in_array('šašek a král', $tokens)); 00146 00147 $needle = '""'; 00148 $tokens = testable_grading_manager::tokenize($needle); 00149 $this->assertTrue(empty($tokens)); 00150 00151 $needle = '"0"'; 00152 $tokens = testable_grading_manager::tokenize($needle); 00153 $this->assertEqual(1, count($tokens)); 00154 $this->assertTrue(in_array('0', $tokens)); 00155 00156 $needle = '<span>Aha</span>, then who\'s a bad guy here he?'; 00157 $tokens = testable_grading_manager::tokenize($needle); 00158 $this->assertEqual(8, count($tokens)); 00159 $this->assertTrue(in_array('span', $tokens)); // Extracted the tag name 00160 $this->assertTrue(in_array('Aha', $tokens)); 00161 $this->assertTrue(in_array('who', $tokens)); // Removed the trailing 's 00162 $this->assertTrue(!in_array('a', $tokens)); //Single letter token was dropped 00163 $this->assertTrue(in_array('he', $tokens)); // Removed the trailing ? 00164 00165 $needle = 'grammar, "english language"'; 00166 $tokens = testable_grading_manager::tokenize($needle); 00167 $this->assertTrue(in_array('grammar', $tokens)); 00168 $this->assertTrue(in_array('english', $tokens)); 00169 $this->assertTrue(in_array('language', $tokens)); 00170 $this->assertTrue(!in_array('english language', $tokens)); // Quoting part of the string is not supported 00171 } 00172 }