MDL-32149 add quiz unit tests
[moodle.git] / mod / quiz / report / tests / reportlib_test.php
blob4464b4688f7dc2f0ae7d7eecbe9e4955765652cf
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.
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/>.
17 /**
18 * Unit tests for (some of) mod/quiz/report/reportlib.php
20 * @package mod
21 * @subpackage quiz
22 * @category phpunit
23 * @copyright 2008 Jamie Pratt me@jamiep.org
24 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
28 defined('MOODLE_INTERNAL') || die();
30 global $CFG;
31 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php'); // Include the code to test
34 /**
35 * This class contains the test cases for the functions in reportlib.php.
37 * @copyright 2008 Jamie Pratt me@jamiep.org
38 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
40 class question_reportlib_testcase extends basic_testcase {
41 public function test_quiz_report_index_by_keys() {
42 $datum = array();
43 $object = new stdClass();
44 $object->qid = 3;
45 $object->aid = 101;
46 $object->response = '';
47 $object->grade = 3;
48 $datum[] = $object;
50 $indexed = quiz_report_index_by_keys($datum, array('aid', 'qid'));
52 $this->assertEquals($indexed[101][3]->qid, 3);
53 $this->assertEquals($indexed[101][3]->aid, 101);
54 $this->assertEquals($indexed[101][3]->response, '');
55 $this->assertEquals($indexed[101][3]->grade, 3);
57 $indexed = quiz_report_index_by_keys($datum, array('aid', 'qid'), false);
59 $this->assertEquals($indexed[101][3][0]->qid, 3);
60 $this->assertEquals($indexed[101][3][0]->aid, 101);
61 $this->assertEquals($indexed[101][3][0]->response, '');
62 $this->assertEquals($indexed[101][3][0]->grade, 3);
65 public function test_quiz_report_scale_summarks_as_percentage() {
66 $quiz = new stdClass();
67 $quiz->sumgrades = 10;
68 $quiz->decimalpoints = 2;
70 $this->assertEquals('12.34567%',
71 quiz_report_scale_summarks_as_percentage(1.234567, $quiz, false));
72 $this->assertEquals('12.35%',
73 quiz_report_scale_summarks_as_percentage(1.234567, $quiz, true));
74 $this->assertEquals('-',
75 quiz_report_scale_summarks_as_percentage('-', $quiz, true));