[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/tests/ -> structure_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   * Quiz events tests.
  19   *
  20   * @package   mod_quiz
  21   * @category  test
  22   * @copyright 2013 Adrian Greeve
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/mod/quiz/attemptlib.php');
  30  
  31  /**
  32   * Unit tests for quiz events.
  33   *
  34   * @copyright  2013 Adrian Greeve
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class mod_quiz_structure_testcase extends advanced_testcase {
  38  
  39      /**
  40       * Create a course with an empty quiz.
  41       * @return array with three elements quiz, cm and course.
  42       */
  43      protected function prepare_quiz_data() {
  44  
  45          $this->resetAfterTest(true);
  46  
  47          // Create a course.
  48          $course = $this->getDataGenerator()->create_course();
  49  
  50          // Make a quiz.
  51          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
  52  
  53          $quiz = $quizgenerator->create_instance(array('course' => $course->id, 'questionsperpage' => 0,
  54              'grade' => 100.0, 'sumgrades' => 2, 'preferredbehaviour' => 'immediatefeedback'));
  55  
  56          $cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id);
  57  
  58          return array($quiz, $cm, $course);
  59      }
  60  
  61      /**
  62       * Creat a test quiz.
  63       *
  64       * $layout looks like this:
  65       * $layout = array(
  66       *     'Heading 1'
  67       *     array('TF1', 1, 'truefalse'),
  68       *     'Heading 2*'
  69       *     array('TF2', 2, 'truefalse'),
  70       * );
  71       * That is, either a string, which represents a section heading,
  72       * or an array that represents a question.
  73       *
  74       * If the section heading ends with *, that section is shuffled.
  75       *
  76       * The elements in the question array are name, page number, and question type.
  77       *
  78       * @param array $layout as above.
  79       * @return quiz the created quiz.
  80       */
  81      protected function create_test_quiz($layout) {
  82          list($quiz, $cm, $course) = $this->prepare_quiz_data();
  83          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  84          $cat = $questiongenerator->create_question_category();
  85  
  86          $headings = array();
  87          $slot = 1;
  88          $lastpage = 0;
  89          foreach ($layout as $item) {
  90              if (is_string($item)) {
  91                  if (isset($headings[$lastpage + 1])) {
  92                      throw new coding_exception('Sections cannot be empty.');
  93                  }
  94                  $headings[$lastpage + 1] = $item;
  95  
  96              } else {
  97                  list($name, $page, $qtype) = $item;
  98                  if ($page < 1 || !($page == $lastpage + 1 ||
  99                          (!isset($headings[$lastpage + 1]) && $page == $lastpage))) {
 100                      throw new coding_exception('Page numbers wrong.');
 101                  }
 102                  $q = $questiongenerator->create_question($qtype, null,
 103                          array('name' => $name, 'category' => $cat->id));
 104  
 105                  quiz_add_quiz_question($q->id, $quiz, $page);
 106                  $lastpage = $page;
 107              }
 108          }
 109  
 110          $quizobj = new quiz($quiz, $cm, $course);
 111          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 112          if (isset($headings[1])) {
 113              list($heading, $shuffle) = $this->parse_section_name($headings[1]);
 114              $sections = $structure->get_sections();
 115              $firstsection = reset($sections);
 116              $structure->set_section_heading($firstsection->id, $heading);
 117              $structure->set_section_shuffle($firstsection->id, $shuffle);
 118              unset($headings[1]);
 119          }
 120  
 121          foreach ($headings as $startpage => $heading) {
 122              list($heading, $shuffle) = $this->parse_section_name($heading);
 123              $id = $structure->add_section_heading($startpage, $heading);
 124              $structure->set_section_shuffle($id, $shuffle);
 125          }
 126  
 127          return $quizobj;
 128      }
 129  
 130      /**
 131       * Verify that the given layout matches that expected.
 132       * @param array $expectedlayout as for $layout in {@link create_test_quiz()}.
 133       * @param \mod_quiz\structure $structure the structure to test.
 134       */
 135      protected function assert_quiz_layout($expectedlayout, \mod_quiz\structure $structure) {
 136          $sections = $structure->get_sections();
 137  
 138          $slot = 1;
 139          foreach ($expectedlayout as $item) {
 140              if (is_string($item)) {
 141                  list($heading, $shuffle) = $this->parse_section_name($item);
 142                  $section = array_shift($sections);
 143  
 144                  if ($slot > 1 && $section->heading == '' && $section->firstslot == 1) {
 145                      // The array $expectedlayout did not contain default first quiz section, so skip over it.
 146                      $section = array_shift($sections);
 147                  }
 148  
 149                  $this->assertEquals($slot, $section->firstslot);
 150                  $this->assertEquals($heading, $section->heading);
 151                  $this->assertEquals($shuffle, $section->shufflequestions);
 152  
 153              } else {
 154                  list($name, $page, $qtype) = $item;
 155                  $question = $structure->get_question_in_slot($slot);
 156                  $this->assertEquals($name,  $question->name);
 157                  $this->assertEquals($slot,  $question->slot,  'Slot number wrong for question ' . $name);
 158                  $this->assertEquals($qtype, $question->qtype, 'Question type wrong for question ' . $name);
 159                  $this->assertEquals($page,  $question->page,  'Page number wrong for question ' . $name);
 160  
 161                  $slot += 1;
 162              }
 163          }
 164  
 165          if ($slot - 1 != count($structure->get_slots())) {
 166              $this->fail('The quiz contains more slots than expected.');
 167          }
 168  
 169          if (!empty($sections)) {
 170              $section = array_shift($sections);
 171              if ($section->heading != '' || $section->firstslot != 1) {
 172                  $this->fail('Unexpected section (' . $section->heading .') found in the quiz.');
 173              }
 174          }
 175      }
 176  
 177      /**
 178       * Parse the section name, optionally followed by a * to mean shuffle, as
 179       * used by create_test_quiz as assert_quiz_layout.
 180       * @param string $heading the heading.
 181       * @return array with two elements, the heading and the shuffle setting.
 182       */
 183      protected function parse_section_name($heading) {
 184          if (substr($heading, -1) == '*') {
 185              return array(substr($heading, 0, -1), 1);
 186          } else {
 187              return array($heading, 0);
 188          }
 189      }
 190  
 191      public function test_get_quiz_slots() {
 192          $quizobj = $this->create_test_quiz(array(
 193                  array('TF1', 1, 'truefalse'),
 194                  array('TF2', 1, 'truefalse'),
 195              ));
 196          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 197  
 198          // Are the correct slots returned?
 199          $slots = $structure->get_slots();
 200          $this->assertCount(2, $structure->get_slots());
 201      }
 202  
 203      public function test_quiz_has_one_section_by_default() {
 204          $quizobj = $this->create_test_quiz(array(
 205                  array('TF1', 1, 'truefalse'),
 206              ));
 207          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 208  
 209          $sections = $structure->get_sections();
 210          $this->assertCount(1, $sections);
 211  
 212          $section = array_shift($sections);
 213          $this->assertEquals(1, $section->firstslot);
 214          $this->assertEquals('', $section->heading);
 215          $this->assertEquals(0, $section->shufflequestions);
 216      }
 217  
 218      public function test_get_sections() {
 219          $quizobj = $this->create_test_quiz(array(
 220                  'Heading 1*',
 221                  array('TF1', 1, 'truefalse'),
 222                  'Heading 2*',
 223                  array('TF2', 2, 'truefalse'),
 224          ));
 225          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 226  
 227          $sections = $structure->get_sections();
 228          $this->assertCount(2, $sections);
 229  
 230          $section = array_shift($sections);
 231          $this->assertEquals(1, $section->firstslot);
 232          $this->assertEquals('Heading 1', $section->heading);
 233          $this->assertEquals(1, $section->shufflequestions);
 234  
 235          $section = array_shift($sections);
 236          $this->assertEquals(2, $section->firstslot);
 237          $this->assertEquals('Heading 2', $section->heading);
 238          $this->assertEquals(1, $section->shufflequestions);
 239      }
 240  
 241      public function test_remove_section_heading() {
 242          $quizobj = $this->create_test_quiz(array(
 243                  'Heading 1',
 244                  array('TF1', 1, 'truefalse'),
 245                  'Heading 2',
 246                  array('TF2', 2, 'truefalse'),
 247              ));
 248          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 249  
 250          $sections = $structure->get_sections();
 251          $section = end($sections);
 252          $structure->remove_section_heading($section->id);
 253  
 254          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 255          $this->assert_quiz_layout(array(
 256                  'Heading 1',
 257                  array('TF1', 1, 'truefalse'),
 258                  array('TF2', 2, 'truefalse'),
 259              ), $structure);
 260      }
 261  
 262      /**
 263       * @expectedException coding_exception
 264       */
 265      public function test_cannot_remove_first_section() {
 266          $quizobj = $this->create_test_quiz(array(
 267                  'Heading 1',
 268                  array('TF1', 1, 'truefalse'),
 269          ));
 270          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 271  
 272          $sections = $structure->get_sections();
 273          $section = reset($sections);
 274  
 275          $structure->remove_section_heading($section->id);
 276      }
 277  
 278      public function test_move_slot_to_the_same_place_does_nothing() {
 279          $quizobj = $this->create_test_quiz(array(
 280                  array('TF1', 1, 'truefalse'),
 281                  array('TF2', 1, 'truefalse'),
 282                  array('TF3', 2, 'truefalse'),
 283              ));
 284          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 285  
 286          $idtomove = $structure->get_question_in_slot(2)->slotid;
 287          $idmoveafter = $structure->get_question_in_slot(1)->slotid;
 288          $structure->move_slot($idtomove, $idmoveafter, '1');
 289  
 290          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 291          $this->assert_quiz_layout(array(
 292                  array('TF1', 1, 'truefalse'),
 293                  array('TF2', 1, 'truefalse'),
 294                  array('TF3', 2, 'truefalse'),
 295              ), $structure);
 296      }
 297  
 298      public function test_move_slot_end_of_one_page_to_start_of_next() {
 299          $quizobj = $this->create_test_quiz(array(
 300                  array('TF1', 1, 'truefalse'),
 301                  array('TF2', 1, 'truefalse'),
 302                  array('TF3', 2, 'truefalse'),
 303              ));
 304          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 305  
 306          $idtomove = $structure->get_question_in_slot(2)->slotid;
 307          $idmoveafter = $structure->get_question_in_slot(2)->slotid;
 308          $structure->move_slot($idtomove, $idmoveafter, '2');
 309  
 310          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 311          $this->assert_quiz_layout(array(
 312                  array('TF1', 1, 'truefalse'),
 313                  array('TF2', 2, 'truefalse'),
 314                  array('TF3', 2, 'truefalse'),
 315              ), $structure);
 316      }
 317  
 318      public function test_end_of_one_section_to_start_of_next() {
 319          $quizobj = $this->create_test_quiz(array(
 320                  array('TF1', 1, 'truefalse'),
 321                  array('TF2', 1, 'truefalse'),
 322                  'Heading',
 323                  array('TF3', 2, 'truefalse'),
 324              ));
 325          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 326  
 327          $idtomove = $structure->get_question_in_slot(2)->slotid;
 328          $idmoveafter = $structure->get_question_in_slot(2)->slotid;
 329          $structure->move_slot($idtomove, $idmoveafter, '2');
 330  
 331          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 332          $this->assert_quiz_layout(array(
 333                  array('TF1', 1, 'truefalse'),
 334                  'Heading',
 335                  array('TF2', 2, 'truefalse'),
 336                  array('TF3', 2, 'truefalse'),
 337              ), $structure);
 338      }
 339  
 340      public function test_start_of_one_section_to_end_of_previous() {
 341          $quizobj = $this->create_test_quiz(array(
 342                  array('TF1', 1, 'truefalse'),
 343                  'Heading',
 344                  array('TF2', 2, 'truefalse'),
 345                  array('TF3', 2, 'truefalse'),
 346              ));
 347          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 348  
 349          $idtomove = $structure->get_question_in_slot(2)->slotid;
 350          $idmoveafter = $structure->get_question_in_slot(1)->slotid;
 351          $structure->move_slot($idtomove, $idmoveafter, '1');
 352  
 353          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 354          $this->assert_quiz_layout(array(
 355                  array('TF1', 1, 'truefalse'),
 356                  array('TF2', 1, 'truefalse'),
 357                  'Heading',
 358                  array('TF3', 2, 'truefalse'),
 359              ), $structure);
 360      }
 361      public function test_move_slot_on_same_page() {
 362          $quizobj = $this->create_test_quiz(array(
 363                  array('TF1', 1, 'truefalse'),
 364                  array('TF2', 1, 'truefalse'),
 365                  array('TF3', 1, 'truefalse'),
 366              ));
 367          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 368  
 369          $idtomove = $structure->get_question_in_slot(2)->slotid;
 370          $idmoveafter = $structure->get_question_in_slot(3)->slotid;
 371          $structure->move_slot($idtomove, $idmoveafter, '1');
 372  
 373          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 374          $this->assert_quiz_layout(array(
 375                  array('TF1', 1, 'truefalse'),
 376                  array('TF3', 1, 'truefalse'),
 377                  array('TF2', 1, 'truefalse'),
 378          ), $structure);
 379      }
 380  
 381      public function test_move_slot_up_onto_previous_page() {
 382          $quizobj = $this->create_test_quiz(array(
 383                  array('TF1', 1, 'truefalse'),
 384                  array('TF2', 2, 'truefalse'),
 385                  array('TF3', 2, 'truefalse'),
 386          ));
 387          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 388  
 389          $idtomove = $structure->get_question_in_slot(3)->slotid;
 390          $idmoveafter = $structure->get_question_in_slot(1)->slotid;
 391          $structure->move_slot($idtomove, $idmoveafter, '1');
 392  
 393          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 394          $this->assert_quiz_layout(array(
 395                  array('TF1', 1, 'truefalse'),
 396                  array('TF3', 1, 'truefalse'),
 397                  array('TF2', 2, 'truefalse'),
 398          ), $structure);
 399      }
 400  
 401      public function test_move_slot_emptying_a_page_renumbers_pages() {
 402          $quizobj = $this->create_test_quiz(array(
 403                  array('TF1', 1, 'truefalse'),
 404                  array('TF2', 2, 'truefalse'),
 405                  array('TF3', 3, 'truefalse'),
 406          ));
 407          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 408  
 409          $idtomove = $structure->get_question_in_slot(2)->slotid;
 410          $idmoveafter = $structure->get_question_in_slot(3)->slotid;
 411          $structure->move_slot($idtomove, $idmoveafter, '3');
 412  
 413          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 414          $this->assert_quiz_layout(array(
 415                  array('TF1', 1, 'truefalse'),
 416                  array('TF3', 2, 'truefalse'),
 417                  array('TF2', 2, 'truefalse'),
 418          ), $structure);
 419      }
 420  
 421      /**
 422       * @expectedException coding_exception
 423       */
 424      public function test_move_slot_too_small_page_number_detected() {
 425          $quizobj = $this->create_test_quiz(array(
 426                  array('TF1', 1, 'truefalse'),
 427                  array('TF2', 2, 'truefalse'),
 428                  array('TF3', 3, 'truefalse'),
 429          ));
 430          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 431  
 432          $idtomove = $structure->get_question_in_slot(3)->slotid;
 433          $idmoveafter = $structure->get_question_in_slot(2)->slotid;
 434          $structure->move_slot($idtomove, $idmoveafter, '1');
 435      }
 436  
 437      /**
 438       * @expectedException coding_exception
 439       */
 440      public function test_move_slot_too_large_page_number_detected() {
 441          $quizobj = $this->create_test_quiz(array(
 442                  array('TF1', 1, 'truefalse'),
 443                  array('TF2', 2, 'truefalse'),
 444                  array('TF3', 3, 'truefalse'),
 445          ));
 446          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 447  
 448          $idtomove = $structure->get_question_in_slot(1)->slotid;
 449          $idmoveafter = $structure->get_question_in_slot(2)->slotid;
 450          $structure->move_slot($idtomove, $idmoveafter, '4');
 451      }
 452  
 453      public function test_move_slot_within_section() {
 454          $quizobj = $this->create_test_quiz(array(
 455                  'Heading 1',
 456                  array('TF1', 1, 'truefalse'),
 457                  array('TF2', 1, 'truefalse'),
 458                  'Heading 2',
 459                  array('TF3', 2, 'truefalse'),
 460              ));
 461          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 462  
 463          $idtomove = $structure->get_question_in_slot(1)->slotid;
 464          $idmoveafter = $structure->get_question_in_slot(2)->slotid;
 465          $structure->move_slot($idtomove, $idmoveafter, '1');
 466  
 467          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 468          $this->assert_quiz_layout(array(
 469                  'Heading 1',
 470                  array('TF2', 1, 'truefalse'),
 471                  array('TF1', 1, 'truefalse'),
 472                  'Heading 2',
 473                  array('TF3', 2, 'truefalse'),
 474              ), $structure);
 475      }
 476  
 477      public function test_move_slot_to_new_section() {
 478          $quizobj = $this->create_test_quiz(array(
 479                  'Heading 1',
 480                  array('TF1', 1, 'truefalse'),
 481                  array('TF2', 1, 'truefalse'),
 482                  'Heading 2',
 483                  array('TF3', 2, 'truefalse'),
 484              ));
 485          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 486  
 487          $idtomove = $structure->get_question_in_slot(2)->slotid;
 488          $idmoveafter = $structure->get_question_in_slot(3)->slotid;
 489          $structure->move_slot($idtomove, $idmoveafter, '2');
 490  
 491          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 492          $this->assert_quiz_layout(array(
 493                  'Heading 1',
 494                  array('TF1', 1, 'truefalse'),
 495                  'Heading 2',
 496                  array('TF3', 2, 'truefalse'),
 497                  array('TF2', 2, 'truefalse'),
 498              ), $structure);
 499      }
 500  
 501      public function test_move_slot_to_start() {
 502          $quizobj = $this->create_test_quiz(array(
 503                  'Heading 1',
 504                  array('TF1', 1, 'truefalse'),
 505                  'Heading 2',
 506                  array('TF2', 2, 'truefalse'),
 507                  array('TF3', 2, 'truefalse'),
 508              ));
 509          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 510  
 511          $idtomove = $structure->get_question_in_slot(3)->slotid;
 512          $structure->move_slot($idtomove, 0, '1');
 513  
 514          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 515          $this->assert_quiz_layout(array(
 516                  'Heading 1',
 517                  array('TF3', 1, 'truefalse'),
 518                  array('TF1', 1, 'truefalse'),
 519                  'Heading 2',
 520                  array('TF2', 2, 'truefalse'),
 521              ), $structure);
 522      }
 523  
 524      public function test_move_slot_down_to_start_of_second_section() {
 525          $quizobj = $this->create_test_quiz(array(
 526                  'Heading 1',
 527                  array('TF1', 1, 'truefalse'),
 528                  array('TF2', 1, 'truefalse'),
 529                  'Heading 2',
 530                  array('TF3', 2, 'truefalse'),
 531              ));
 532          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 533  
 534          $idtomove = $structure->get_question_in_slot(2)->slotid;
 535          $idmoveafter = $structure->get_question_in_slot(2)->slotid;
 536          $structure->move_slot($idtomove, $idmoveafter, '2');
 537  
 538          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 539          $this->assert_quiz_layout(array(
 540                  'Heading 1',
 541                  array('TF1', 1, 'truefalse'),
 542                  'Heading 2',
 543                  array('TF2', 2, 'truefalse'),
 544                  array('TF3', 2, 'truefalse'),
 545              ), $structure);
 546      }
 547  
 548      public function test_move_first_slot_down_to_start_of_page_2() {
 549          $quizobj = $this->create_test_quiz(array(
 550                  'Heading 1',
 551                  array('TF1', 1, 'truefalse'),
 552                  array('TF2', 2, 'truefalse'),
 553              ));
 554          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 555  
 556          $idtomove = $structure->get_question_in_slot(1)->slotid;
 557          $structure->move_slot($idtomove, 0, '2');
 558  
 559          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 560          $this->assert_quiz_layout(array(
 561                  'Heading 1',
 562                  array('TF1', 1, 'truefalse'),
 563                  array('TF2', 1, 'truefalse'),
 564              ), $structure);
 565      }
 566  
 567      public function test_move_first_slot_to_same_place_on_page_1() {
 568          $quizobj = $this->create_test_quiz(array(
 569                  'Heading 1',
 570                  array('TF1', 1, 'truefalse'),
 571                  array('TF2', 2, 'truefalse'),
 572              ));
 573          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 574  
 575          $idtomove = $structure->get_question_in_slot(1)->slotid;
 576          $structure->move_slot($idtomove, 0, '1');
 577  
 578          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 579          $this->assert_quiz_layout(array(
 580                  'Heading 1',
 581                  array('TF1', 1, 'truefalse'),
 582                  array('TF2', 2, 'truefalse'),
 583              ), $structure);
 584      }
 585  
 586      public function test_move_first_slot_to_before_page_1() {
 587          $quizobj = $this->create_test_quiz(array(
 588                  'Heading 1',
 589                  array('TF1', 1, 'truefalse'),
 590                  array('TF2', 2, 'truefalse'),
 591              ));
 592          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 593  
 594          $idtomove = $structure->get_question_in_slot(1)->slotid;
 595          $structure->move_slot($idtomove, 0, '');
 596  
 597          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 598          $this->assert_quiz_layout(array(
 599                  'Heading 1',
 600                  array('TF1', 1, 'truefalse'),
 601                  array('TF2', 2, 'truefalse'),
 602              ), $structure);
 603      }
 604  
 605      public function test_move_slot_up_to_start_of_second_section() {
 606          $quizobj = $this->create_test_quiz(array(
 607                  'Heading 1',
 608                  array('TF1', 1, 'truefalse'),
 609                  'Heading 2',
 610                  array('TF2', 2, 'truefalse'),
 611                  'Heading 3',
 612                  array('TF3', 3, 'truefalse'),
 613                  array('TF4', 3, 'truefalse'),
 614              ));
 615          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 616  
 617          $idtomove = $structure->get_question_in_slot(3)->slotid;
 618          $idmoveafter = $structure->get_question_in_slot(1)->slotid;
 619          $structure->move_slot($idtomove, $idmoveafter, '2');
 620  
 621          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 622          $this->assert_quiz_layout(array(
 623                  'Heading 1',
 624                  array('TF1', 1, 'truefalse'),
 625                  'Heading 2',
 626                  array('TF3', 2, 'truefalse'),
 627                  array('TF2', 2, 'truefalse'),
 628                  'Heading 3',
 629                  array('TF4', 3, 'truefalse'),
 630              ), $structure);
 631      }
 632  
 633      public function test_quiz_remove_slot() {
 634          $quizobj = $this->create_test_quiz(array(
 635                  array('TF1', 1, 'truefalse'),
 636                  array('TF2', 1, 'truefalse'),
 637                  'Heading 2',
 638                  array('TF3', 2, 'truefalse'),
 639              ));
 640          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 641  
 642          $structure->remove_slot(2);
 643  
 644          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 645          $this->assert_quiz_layout(array(
 646                  array('TF1', 1, 'truefalse'),
 647                  'Heading 2',
 648                  array('TF3', 2, 'truefalse'),
 649              ), $structure);
 650      }
 651  
 652      public function test_quiz_removing_a_random_question_deletes_the_question() {
 653          global $DB;
 654  
 655          $this->resetAfterTest(true);
 656          $this->setAdminUser();
 657  
 658          $quizobj = $this->create_test_quiz(array(
 659                  array('TF1', 1, 'truefalse'),
 660              ));
 661  
 662          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 663          $cat = $questiongenerator->create_question_category();
 664          quiz_add_random_questions($quizobj->get_quiz(), 1, $cat->id, 1, false);
 665          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 666          $randomq = $DB->get_record('question', array('qtype' => 'random'));
 667  
 668          $structure->remove_slot(2);
 669  
 670          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 671          $this->assert_quiz_layout(array(
 672                  array('TF1', 1, 'truefalse'),
 673              ), $structure);
 674          $this->assertFalse($DB->record_exists('question', array('id' => $randomq->id)));
 675      }
 676  
 677      /**
 678       * @expectedException coding_exception
 679       */
 680      public function test_cannot_remove_last_slot_in_a_section() {
 681          $quizobj = $this->create_test_quiz(array(
 682                  array('TF1', 1, 'truefalse'),
 683                  array('TF2', 1, 'truefalse'),
 684                  'Heading 2',
 685                  array('TF3', 2, 'truefalse'),
 686              ));
 687          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 688  
 689          $structure->remove_slot(3);
 690      }
 691  
 692      public function test_can_remove_last_question_in_a_quiz() {
 693          $quizobj = $this->create_test_quiz(array(
 694                  'Heading 1',
 695                  array('TF1', 1, 'truefalse'),
 696              ));
 697          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 698  
 699          $structure->remove_slot(1);
 700  
 701          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 702          $cat = $questiongenerator->create_question_category();
 703          $q = $questiongenerator->create_question('truefalse', null,
 704                  array('name' => 'TF2', 'category' => $cat->id));
 705  
 706          quiz_add_quiz_question($q->id, $quizobj->get_quiz(), 0);
 707          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 708  
 709          $this->assert_quiz_layout(array(
 710                  'Heading 1',
 711                  array('TF2', 1, 'truefalse'),
 712          ), $structure);
 713      }
 714  
 715      public function test_add_question_updates_headings() {
 716          $quizobj = $this->create_test_quiz(array(
 717                  array('TF1', 1, 'truefalse'),
 718                  'Heading 2',
 719                  array('TF2', 2, 'truefalse'),
 720          ));
 721          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 722  
 723          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 724          $cat = $questiongenerator->create_question_category();
 725          $q = $questiongenerator->create_question('truefalse', null,
 726                  array('name' => 'TF3', 'category' => $cat->id));
 727  
 728          quiz_add_quiz_question($q->id, $quizobj->get_quiz(), 1);
 729  
 730          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 731          $this->assert_quiz_layout(array(
 732                  array('TF1', 1, 'truefalse'),
 733                  array('TF3', 1, 'truefalse'),
 734                  'Heading 2',
 735                  array('TF2', 2, 'truefalse'),
 736          ), $structure);
 737      }
 738  
 739      public function test_add_question_at_end_does_not_update_headings() {
 740          $quizobj = $this->create_test_quiz(array(
 741                  array('TF1', 1, 'truefalse'),
 742                  'Heading 2',
 743                  array('TF2', 2, 'truefalse'),
 744          ));
 745          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 746  
 747          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 748          $cat = $questiongenerator->create_question_category();
 749          $q = $questiongenerator->create_question('truefalse', null,
 750                  array('name' => 'TF3', 'category' => $cat->id));
 751  
 752          quiz_add_quiz_question($q->id, $quizobj->get_quiz(), 0);
 753  
 754          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 755          $this->assert_quiz_layout(array(
 756                  array('TF1', 1, 'truefalse'),
 757                  'Heading 2',
 758                  array('TF2', 2, 'truefalse'),
 759                  array('TF3', 2, 'truefalse'),
 760          ), $structure);
 761      }
 762  
 763      public function test_remove_page_break() {
 764          $quizobj = $this->create_test_quiz(array(
 765                  array('TF1', 1, 'truefalse'),
 766                  array('TF2', 2, 'truefalse'),
 767              ));
 768          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 769  
 770          $slotid = $structure->get_question_in_slot(2)->slotid;
 771          $slots = $structure->update_page_break($slotid, \mod_quiz\repaginate::LINK);
 772  
 773          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 774          $this->assert_quiz_layout(array(
 775                  array('TF1', 1, 'truefalse'),
 776                  array('TF2', 1, 'truefalse'),
 777              ), $structure);
 778      }
 779  
 780      public function test_add_page_break() {
 781          $quizobj = $this->create_test_quiz(array(
 782                  array('TF1', 1, 'truefalse'),
 783                  array('TF2', 1, 'truefalse'),
 784          ));
 785          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 786  
 787          $slotid = $structure->get_question_in_slot(2)->slotid;
 788          $slots = $structure->update_page_break($slotid, \mod_quiz\repaginate::UNLINK);
 789  
 790          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 791          $this->assert_quiz_layout(array(
 792                  array('TF1', 1, 'truefalse'),
 793                  array('TF2', 2, 'truefalse'),
 794          ), $structure);
 795      }
 796  
 797      public function test_update_question_dependency() {
 798          $quizobj = $this->create_test_quiz(array(
 799                  array('TF1', 1, 'truefalse'),
 800                  array('TF2', 1, 'truefalse'),
 801          ));
 802          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 803  
 804          // Test adding a dependency.
 805          $slotid = $structure->get_slot_id_for_slot(2);
 806          $structure->update_question_dependency($slotid, true);
 807  
 808          // Having called update page break, we need to reload $structure.
 809          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 810          $this->assertEquals(1, $structure->is_question_dependent_on_previous_slot(2));
 811  
 812          // Test removing a dependency.
 813          $structure->update_question_dependency($slotid, false);
 814  
 815          // Having called update page break, we need to reload $structure.
 816          $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 817          $this->assertEquals(0, $structure->is_question_dependent_on_previous_slot(2));
 818      }
 819  }


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