PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/question/type/random/tests/questiontype_test.php

http://github.com/moodle/moodle
PHP | 132 lines | 72 code | 27 blank | 33 comment | 1 complexity | 9be84b85f537fad978c03dbf03a02287 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Unit tests for the random question type class.
  18. *
  19. * @package qtype
  20. * @subpackage random
  21. * @copyright 2010 The Open University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  27. require_once($CFG->dirroot . '/question/type/random/questiontype.php');
  28. /**
  29. * Unit tests for the random question type class.
  30. *
  31. * @copyright 2010 The Open University
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class qtype_random_test extends advanced_testcase {
  35. protected $qtype;
  36. protected function setUp() {
  37. $this->qtype = new qtype_random();
  38. }
  39. protected function tearDown() {
  40. $this->qtype = null;
  41. }
  42. public function test_name() {
  43. $this->assertEquals($this->qtype->name(), 'random');
  44. }
  45. public function test_can_analyse_responses() {
  46. $this->assertFalse($this->qtype->can_analyse_responses());
  47. }
  48. public function test_get_random_guess_score() {
  49. $this->assertNull($this->qtype->get_random_guess_score(null));
  50. }
  51. public function test_load_question() {
  52. $this->resetAfterTest();
  53. $syscontext = context_system::instance();
  54. /** @var core_question_generator $generator */
  55. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  56. $category = $generator->create_question_category(['contextid' => $syscontext->id]);
  57. $fromform = test_question_maker::get_question_form_data('random');
  58. $fromform->category = $category->id . ',' . $syscontext->id;
  59. $question = new stdClass();
  60. $question->category = $category->id;
  61. $question->qtype = 'random';
  62. $question->createdby = 0;
  63. $this->qtype->save_question($question, $fromform);
  64. $questiondata = question_bank::load_question_data($question->id);
  65. $this->assertEquals(['id', 'category', 'parent', 'name', 'questiontext', 'questiontextformat',
  66. 'generalfeedback', 'generalfeedbackformat', 'defaultmark', 'penalty', 'qtype',
  67. 'length', 'stamp', 'version', 'hidden', 'timecreated', 'timemodified',
  68. 'createdby', 'modifiedby', 'idnumber', 'contextid', 'options', 'hints', 'categoryobject'],
  69. array_keys(get_object_vars($questiondata)));
  70. $this->assertEquals($category->id, $questiondata->category);
  71. // Random questions are not real questions. This is signaled by parent
  72. // being non-zero - and in fact equal to question id.
  73. $this->assertEquals($questiondata->id, $questiondata->parent);
  74. $this->assertEquals('Random (' . $category->name . ')', $questiondata->name);
  75. $this->assertEquals(0, $questiondata->questiontext); // Used to store 'Select from subcategories'.
  76. $this->assertEquals('random', $questiondata->qtype);
  77. $this->assertEquals(1, $questiondata->length);
  78. $this->assertEquals(0, $questiondata->hidden);
  79. $this->assertEquals($category->contextid, $questiondata->contextid);
  80. // Options - not used.
  81. $this->assertEquals(['answers'], array_keys(get_object_vars($questiondata->options)));
  82. $this->assertEquals([], $questiondata->options->answers);
  83. // Hints - not used.
  84. $this->assertEquals([], $questiondata->hints);
  85. }
  86. public function test_get_possible_responses() {
  87. $this->assertEquals(array(), $this->qtype->get_possible_responses(null));
  88. }
  89. public function test_question_creation() {
  90. $this->resetAfterTest();
  91. question_bank::get_qtype('random')->clear_caches_before_testing();
  92. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  93. $cat = $generator->create_question_category();
  94. $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  95. $question2 = $generator->create_question('numerical', null, array('category' => $cat->id));
  96. $randomquestion = $generator->create_question('random', null, array('category' => $cat->id));
  97. $expectedids = array($question1->id, $question2->id);
  98. $actualids = question_bank::get_qtype('random')->get_available_questions_from_category($cat->id, 0);
  99. sort($expectedids);
  100. sort($actualids);
  101. $this->assertEquals($expectedids, $actualids);
  102. $q = question_bank::load_question($randomquestion->id);
  103. $this->assertContains($q->id, array($question1->id, $question2->id));
  104. }
  105. }