[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/quiz/tests/ -> lib_test.php (source)

   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 (some of) mod/quiz/locallib.php.
  19   *
  20   * @package    mod_quiz
  21   * @category   test
  22   * @copyright  2008 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * @copyright  2008 The Open University
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  33   */
  34  class mod_quiz_lib_testcase extends advanced_testcase {
  35      public function test_quiz_has_grades() {
  36          $quiz = new stdClass();
  37          $quiz->grade = '100.0000';
  38          $quiz->sumgrades = '100.0000';
  39          $this->assertTrue(quiz_has_grades($quiz));
  40          $quiz->sumgrades = '0.0000';
  41          $this->assertFalse(quiz_has_grades($quiz));
  42          $quiz->grade = '0.0000';
  43          $this->assertFalse(quiz_has_grades($quiz));
  44          $quiz->sumgrades = '100.0000';
  45          $this->assertFalse(quiz_has_grades($quiz));
  46      }
  47  
  48      public function test_quiz_format_grade() {
  49          $quiz = new stdClass();
  50          $quiz->decimalpoints = 2;
  51          $this->assertEquals(quiz_format_grade($quiz, 0.12345678), format_float(0.12, 2));
  52          $this->assertEquals(quiz_format_grade($quiz, 0), format_float(0, 2));
  53          $this->assertEquals(quiz_format_grade($quiz, 1.000000000000), format_float(1, 2));
  54          $quiz->decimalpoints = 0;
  55          $this->assertEquals(quiz_format_grade($quiz, 0.12345678), '0');
  56      }
  57  
  58      public function test_quiz_get_grade_format() {
  59          $quiz = new stdClass();
  60          $quiz->decimalpoints = 2;
  61          $this->assertEquals(quiz_get_grade_format($quiz), 2);
  62          $this->assertEquals($quiz->questiondecimalpoints, -1);
  63          $quiz->questiondecimalpoints = 2;
  64          $this->assertEquals(quiz_get_grade_format($quiz), 2);
  65          $quiz->decimalpoints = 3;
  66          $quiz->questiondecimalpoints = -1;
  67          $this->assertEquals(quiz_get_grade_format($quiz), 3);
  68          $quiz->questiondecimalpoints = 4;
  69          $this->assertEquals(quiz_get_grade_format($quiz), 4);
  70      }
  71  
  72      public function test_quiz_format_question_grade() {
  73          $quiz = new stdClass();
  74          $quiz->decimalpoints = 2;
  75          $quiz->questiondecimalpoints = 2;
  76          $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.12, 2));
  77          $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 2));
  78          $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 2));
  79          $quiz->decimalpoints = 3;
  80          $quiz->questiondecimalpoints = -1;
  81          $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.123, 3));
  82          $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 3));
  83          $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 3));
  84          $quiz->questiondecimalpoints = 4;
  85          $this->assertEquals(quiz_format_question_grade($quiz, 0.12345678), format_float(0.1235, 4));
  86          $this->assertEquals(quiz_format_question_grade($quiz, 0), format_float(0, 4));
  87          $this->assertEquals(quiz_format_question_grade($quiz, 1.000000000000), format_float(1, 4));
  88      }
  89  
  90      /**
  91       * Test deleting a quiz instance.
  92       */
  93      public function test_quiz_delete_instance() {
  94          global $SITE, $DB;
  95          $this->resetAfterTest(true);
  96          $this->setAdminUser();
  97  
  98          // Setup a quiz with 1 standard and 1 random question.
  99          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 100          $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0));
 101  
 102          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 103          $cat = $questiongenerator->create_question_category();
 104          $standardq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
 105  
 106          quiz_add_quiz_question($standardq->id, $quiz);
 107          quiz_add_random_questions($quiz, 0, $cat->id, 1, false);
 108  
 109          // Get the random question.
 110          $randomq = $DB->get_record('question', array('qtype' => 'random'));
 111  
 112          quiz_delete_instance($quiz->id);
 113  
 114          // Check that the random question was deleted.
 115          $count = $DB->count_records('question', array('id' => $randomq->id));
 116          $this->assertEquals(0, $count);
 117          // Check that the standard question was not deleted.
 118          $count = $DB->count_records('question', array('id' => $standardq->id));
 119          $this->assertEquals(1, $count);
 120  
 121          // Check that all the slots were removed.
 122          $count = $DB->count_records('quiz_slots', array('quizid' => $quiz->id));
 123          $this->assertEquals(0, $count);
 124  
 125          // Check that the quiz was removed.
 126          $count = $DB->count_records('quiz', array('id' => $quiz->id));
 127          $this->assertEquals(0, $count);
 128      }
 129  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1