diff --git a/grade/grading/tests/lib_test.php b/grade/grading/tests/lib_test.php new file mode 100644 index 0000000000000..e28c85bc175e2 --- /dev/null +++ b/grade/grading/tests/lib_test.php @@ -0,0 +1,138 @@ +. + +/** + * Unit tests for the advanced grading subsystem + * + * @package core + * @subpackage grading + * @category phpunit + * @copyright 2011 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/grade/grading/lib.php'); // Include the code to test + + +/** + * Makes protected method accessible for testing purposes + */ +class testable_grading_manager extends grading_manager { +} + + +/** + * Test cases for the grading manager API + */ +class grading_manager_testcase extends advanced_testcase { + public function test_basic_instantiation() { + $manager1 = get_grading_manager(); + + $fakecontext = (object)array( + 'id' => 42, + 'contextlevel' => CONTEXT_MODULE, + 'instanceid' => 22, + 'path' => '/1/3/15/42', + 'depth' => 4); + + $manager2 = get_grading_manager($fakecontext); + $manager3 = get_grading_manager($fakecontext, 'assignment_upload'); + $manager4 = get_grading_manager($fakecontext, 'assignment_upload', 'submission'); + } + + public function test_set_and_get_grading_area() { + global $DB; + + $this->resetAfterTest(true); + + //sleep(2); // to make sure the microtime will always return unique values // No sleeping in tests!!! --skodak + $areaname1 = 'area1-' . (string)microtime(true); + $areaname2 = 'area2-' . (string)microtime(true); + $fakecontext = (object)array( + 'id' => 42, + 'contextlevel' => CONTEXT_MODULE, + 'instanceid' => 22, + 'path' => '/1/3/15/42', + 'depth' => 4); + + // non-existing area + $gradingman = get_grading_manager($fakecontext, 'mod_foobar', $areaname1); + $this->assertNull($gradingman->get_active_method()); + + // creates area implicitly and sets active method + $this->assertTrue($gradingman->set_active_method('rubric')); + $this->assertEquals('rubric', $gradingman->get_active_method()); + + // repeat setting of already set active method + $this->assertFalse($gradingman->set_active_method('rubric')); + + // switch the manager to another area + $gradingman->set_area($areaname2); + $this->assertNull($gradingman->get_active_method()); + + // switch back and ask again + $gradingman->set_area($areaname1); + $this->assertEquals('rubric', $gradingman->get_active_method()); + + // attempting to set an invalid method + $this->setExpectedException('moodle_exception'); + $gradingman->set_active_method('no_one_should_ever_try_to_implement_a_method_with_this_silly_name'); + } + + public function test_tokenize() { + + $UTFfailuremessage = 'A test using UTF-8 characters has failed. Consider updating PHP and PHP\'s PCRE or INTL extensions (MDL-30494)'; + + $needle = " šašek, \n\n \r a král; \t"; + $tokens = testable_grading_manager::tokenize($needle); + $this->assertEquals(2, count($tokens), $UTFfailuremessage); + $this->assertTrue(in_array('šašek', $tokens), $UTFfailuremessage); + $this->assertTrue(in_array('král', $tokens), $UTFfailuremessage); + + $needle = ' " šašek a král " '; + $tokens = testable_grading_manager::tokenize($needle); + $this->assertEquals(1, count($tokens)); + $this->assertTrue(in_array('šašek a král', $tokens)); + + $needle = '""'; + $tokens = testable_grading_manager::tokenize($needle); + $this->assertTrue(empty($tokens)); + + $needle = '"0"'; + $tokens = testable_grading_manager::tokenize($needle); + $this->assertEquals(1, count($tokens)); + $this->assertTrue(in_array('0', $tokens)); + + $needle = 'Aha, then who\'s a bad guy here he?'; + $tokens = testable_grading_manager::tokenize($needle); + $this->assertEquals(8, count($tokens)); + $this->assertTrue(in_array('span', $tokens)); // Extracted the tag name + $this->assertTrue(in_array('Aha', $tokens)); + $this->assertTrue(in_array('who', $tokens)); // Removed the trailing 's + $this->assertTrue(!in_array('a', $tokens)); //Single letter token was dropped + $this->assertTrue(in_array('he', $tokens)); // Removed the trailing ? + + $needle = 'grammar, "english language"'; + $tokens = testable_grading_manager::tokenize($needle); + $this->assertTrue(in_array('grammar', $tokens)); + $this->assertTrue(in_array('english', $tokens)); + $this->assertTrue(in_array('language', $tokens)); + $this->assertTrue(!in_array('english language', $tokens)); // Quoting part of the string is not supported + } +} diff --git a/grade/simpletest/testreportlib.php b/grade/simpletest/testreportlib.php deleted file mode 100644 index dbfd982da4aed..0000000000000 --- a/grade/simpletest/testreportlib.php +++ /dev/null @@ -1,48 +0,0 @@ -. - -/** - * Unit tests for grade/report/lib.php. - * - * @author nicolas@moodle.com - * @license http://www.gnu.org/copyleft/gpl.html GNU Public License - * @package moodlecore - */ - - -if (!defined('MOODLE_INTERNAL')) { - die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page -} - -require_once($CFG->dirroot.'/grade/report/lib.php'); - -/** - * @TODO create a set of mock objects to simulate the database operations. We don't want to connect to any real sql server. - */ -class gradereportlib_test extends UnitTestCaseUsingDatabase { - var $courseid = 1; - var $context = null; - var $report = null; - public static $includecoverage = array('grade/report/lib.php'); - - function setUp() { - //$this->report = new grade_report($this->courseid, $this->context); - } - -} - - diff --git a/grade/tests/edittree_test.php b/grade/tests/edittree_test.php new file mode 100644 index 0000000000000..7cac51651a8ef --- /dev/null +++ b/grade/tests/edittree_test.php @@ -0,0 +1,52 @@ +. + +/** + * Unit tests for grade/edit/tree/lib.php. + * + * @pacakge core_grade + * @category phpunit + * @author Andrew Davis + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot.'/grade/edit/tree/lib.php'); + + +/** + * Tests grade_edit_tree (deals with the data on the categories and items page in the gradebook) + */ +class gradeedittreelib_testcase extends basic_testcase { + var $courseid = 1; + var $context = null; + var $grade_edit_tree = null; + + public function test_format_number() { + $numinput = array( 0, 1, 1.01, '1.010', 1.2345); + $numoutput = array(0.0, 1.0, 1.01, 1.01, 1.2345); + + for ($i=0; $iassertEquals(grade_edit_tree::format_number($numinput[$i]),$numoutput[$i],$msg); + } + } + +} + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 36011c9ede8dc..f0d8f70db2922 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -38,6 +38,10 @@ course/tests + + grade/tests + grade/grading/tests +