Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  namespace core_question;
  18  
  19  use question_engine;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once (__DIR__ . '/../lib.php');
  25  require_once (__DIR__ . '/helpers.php');
  26  
  27  /**
  28   * This file contains tests for the {@link question_attempt_iterator} class.
  29   *
  30   * @package    core_question
  31   * @category   test
  32   * @copyright  2009 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class questionattemptiterator_test extends \advanced_testcase {
  36      private $quba;
  37      private $qas = array();
  38      private $iterator;
  39  
  40      protected function setUp(): void {
  41          $this->quba = question_engine::make_questions_usage_by_activity('unit_test',
  42                  \context_system::instance());
  43          $this->quba->set_preferred_behaviour('deferredfeedback');
  44  
  45          $slot = $this->quba->add_question(\test_question_maker::make_question('description'));
  46          $this->qas[$slot] = $this->quba->get_question_attempt($slot);
  47  
  48          $slot = $this->quba->add_question(\test_question_maker::make_question('description'));
  49          $this->qas[$slot] = $this->quba->get_question_attempt($slot);
  50  
  51          $this->iterator = $this->quba->get_attempt_iterator();
  52      }
  53  
  54      protected function tearDown(): void {
  55          $this->quba = null;
  56          $this->iterator = null;
  57      }
  58  
  59      public function test_foreach_loop() {
  60          $i = 1;
  61          foreach ($this->iterator as $key => $qa) {
  62              $this->assertEquals($i, $key);
  63              $this->assertSame($this->qas[$i], $qa);
  64              $i++;
  65          }
  66          $this->assertEquals(3, $i);
  67      }
  68  
  69      public function test_offsetExists_before_start() {
  70          $this->assertFalse(isset($this->iterator[0]));
  71      }
  72  
  73      public function test_offsetExists_at_start() {
  74          $this->assertTrue(isset($this->iterator[1]));
  75      }
  76  
  77      public function test_offsetExists_at_endt() {
  78          $this->assertTrue(isset($this->iterator[2]));
  79      }
  80  
  81      public function test_offsetExists_past_end() {
  82          $this->assertFalse(isset($this->iterator[3]));
  83      }
  84  
  85      public function test_offsetGet_before_start() {
  86          $this->expectException(\moodle_exception::class);
  87          $step = $this->iterator[0];
  88      }
  89  
  90      public function test_offsetGet_at_start() {
  91          $this->assertSame($this->qas[1], $this->iterator[1]);
  92      }
  93  
  94      public function test_offsetGet_at_end() {
  95          $this->assertSame($this->qas[2], $this->iterator[2]);
  96      }
  97  
  98      public function test_offsetGet_past_end() {
  99          $this->expectException(\moodle_exception::class);
 100          $step = $this->iterator[3];
 101      }
 102  
 103      public function test_cannot_set() {
 104          $this->expectException(\moodle_exception::class);
 105          $this->iterator[0] = null;
 106      }
 107  
 108      public function test_cannot_unset() {
 109          $this->expectException(\moodle_exception::class);
 110          unset($this->iterator[2]);
 111      }
 112  }