[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/tests/ -> locallib_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 Tim Hunt
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  31  
  32  
  33  /**
  34   * Unit tests for (some of) mod/quiz/locallib.php.
  35   *
  36   * @copyright  2008 Tim Hunt
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class mod_quiz_locallib_testcase extends advanced_testcase {
  40  
  41      public function test_quiz_rescale_grade() {
  42          $quiz = new stdClass();
  43          $quiz->decimalpoints = 2;
  44          $quiz->questiondecimalpoints = 3;
  45          $quiz->grade = 10;
  46          $quiz->sumgrades = 10;
  47          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, false), 0.12345678);
  48          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, true), format_float(0.12, 2));
  49          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, 'question'),
  50              format_float(0.123, 3));
  51          $quiz->sumgrades = 5;
  52          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, false), 0.24691356);
  53          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, true), format_float(0.25, 2));
  54          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, 'question'),
  55              format_float(0.247, 3));
  56      }
  57  
  58      public function test_quiz_attempt_state_in_progress() {
  59          $attempt = new stdClass();
  60          $attempt->state = quiz_attempt::IN_PROGRESS;
  61          $attempt->timefinish = 0;
  62  
  63          $quiz = new stdClass();
  64          $quiz->timeclose = 0;
  65  
  66          $this->assertEquals(mod_quiz_display_options::DURING, quiz_attempt_state($quiz, $attempt));
  67      }
  68  
  69      public function test_quiz_attempt_state_recently_submitted() {
  70          $attempt = new stdClass();
  71          $attempt->state = quiz_attempt::FINISHED;
  72          $attempt->timefinish = time() - 10;
  73  
  74          $quiz = new stdClass();
  75          $quiz->timeclose = 0;
  76  
  77          $this->assertEquals(mod_quiz_display_options::IMMEDIATELY_AFTER, quiz_attempt_state($quiz, $attempt));
  78      }
  79  
  80      public function test_quiz_attempt_state_sumitted_quiz_never_closes() {
  81          $attempt = new stdClass();
  82          $attempt->state = quiz_attempt::FINISHED;
  83          $attempt->timefinish = time() - 7200;
  84  
  85          $quiz = new stdClass();
  86          $quiz->timeclose = 0;
  87  
  88          $this->assertEquals(mod_quiz_display_options::LATER_WHILE_OPEN, quiz_attempt_state($quiz, $attempt));
  89      }
  90  
  91      public function test_quiz_attempt_state_sumitted_quiz_closes_later() {
  92          $attempt = new stdClass();
  93          $attempt->state = quiz_attempt::FINISHED;
  94          $attempt->timefinish = time() - 7200;
  95  
  96          $quiz = new stdClass();
  97          $quiz->timeclose = time() + 3600;
  98  
  99          $this->assertEquals(mod_quiz_display_options::LATER_WHILE_OPEN, quiz_attempt_state($quiz, $attempt));
 100      }
 101  
 102      public function test_quiz_attempt_state_sumitted_quiz_closed() {
 103          $attempt = new stdClass();
 104          $attempt->state = quiz_attempt::FINISHED;
 105          $attempt->timefinish = time() - 7200;
 106  
 107          $quiz = new stdClass();
 108          $quiz->timeclose = time() - 3600;
 109  
 110          $this->assertEquals(mod_quiz_display_options::AFTER_CLOSE, quiz_attempt_state($quiz, $attempt));
 111      }
 112  
 113      public function test_quiz_attempt_state_never_sumitted_quiz_never_closes() {
 114          $attempt = new stdClass();
 115          $attempt->state = quiz_attempt::ABANDONED;
 116          $attempt->timefinish = 1000; // A very long time ago!
 117  
 118          $quiz = new stdClass();
 119          $quiz->timeclose = 0;
 120  
 121          $this->assertEquals(mod_quiz_display_options::LATER_WHILE_OPEN, quiz_attempt_state($quiz, $attempt));
 122      }
 123  
 124      public function test_quiz_attempt_state_never_sumitted_quiz_closes_later() {
 125          $attempt = new stdClass();
 126          $attempt->state = quiz_attempt::ABANDONED;
 127          $attempt->timefinish = time() - 7200;
 128  
 129          $quiz = new stdClass();
 130          $quiz->timeclose = time() + 3600;
 131  
 132          $this->assertEquals(mod_quiz_display_options::LATER_WHILE_OPEN, quiz_attempt_state($quiz, $attempt));
 133      }
 134  
 135      public function test_quiz_attempt_state_never_sumitted_quiz_closed() {
 136          $attempt = new stdClass();
 137          $attempt->state = quiz_attempt::ABANDONED;
 138          $attempt->timefinish = time() - 7200;
 139  
 140          $quiz = new stdClass();
 141          $quiz->timeclose = time() - 3600;
 142  
 143          $this->assertEquals(mod_quiz_display_options::AFTER_CLOSE, quiz_attempt_state($quiz, $attempt));
 144      }
 145  
 146      public function test_quiz_question_tostring() {
 147          $question = new stdClass();
 148          $question->qtype = 'multichoice';
 149          $question->name = 'The question name';
 150          $question->questiontext = '<p>What sort of <b>inequality</b> is x &lt; y<img alt="?" src="..."></p>';
 151          $question->questiontextformat = FORMAT_HTML;
 152  
 153          $summary = quiz_question_tostring($question);
 154          $this->assertEquals('<span class="questionname">The question name</span> ' .
 155                  '<span class="questiontext">What sort of INEQUALITY is x &lt; y[?]' . "\n" . '</span>', $summary);
 156      }
 157  
 158      /**
 159       * Test quiz_view
 160       * @return void
 161       */
 162      public function test_quiz_view() {
 163          global $CFG;
 164  
 165          $CFG->enablecompletion = 1;
 166          $this->resetAfterTest();
 167  
 168          $this->setAdminUser();
 169          // Setup test data.
 170          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 171          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
 172                                                              array('completion' => 2, 'completionview' => 1));
 173          $context = context_module::instance($quiz->cmid);
 174          $cm = get_coursemodule_from_instance('quiz', $quiz->id);
 175  
 176          // Trigger and capture the event.
 177          $sink = $this->redirectEvents();
 178  
 179          quiz_view($quiz, $course, $cm, $context);
 180  
 181          $events = $sink->get_events();
 182          // 2 additional events thanks to completion.
 183          $this->assertCount(3, $events);
 184          $event = array_shift($events);
 185  
 186          // Checking that the event contains the expected values.
 187          $this->assertInstanceOf('\mod_quiz\event\course_module_viewed', $event);
 188          $this->assertEquals($context, $event->get_context());
 189          $moodleurl = new \moodle_url('/mod/quiz/view.php', array('id' => $cm->id));
 190          $this->assertEquals($moodleurl, $event->get_url());
 191          $this->assertEventContextNotUsed($event);
 192          $this->assertNotEmpty($event->get_name());
 193          // Check completion status.
 194          $completion = new completion_info($course);
 195          $completiondata = $completion->get_data($cm);
 196          $this->assertEquals(1, $completiondata->completionstate);
 197      }
 198  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1