PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/calculatedsimple/tests/question_type_test.php

https://bitbucket.org/moodle/moodle
PHP | 121 lines | 67 code | 25 blank | 29 comment | 6 complexity | b9226b06f8fa572cf5debc1ca5f25d64 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. namespace qtype_calculatedsimple;
  17. use qtype_calculated_dataset_loader;
  18. use qtype_calculatedsimple;
  19. use qtype_calculatedsimple_edit_form;
  20. defined('MOODLE_INTERNAL') || die();
  21. global $CFG;
  22. require_once($CFG->dirroot . '/question/type/calculatedsimple/questiontype.php');
  23. require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  24. require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  25. require_once($CFG->dirroot . '/question/type/calculatedsimple/edit_calculatedsimple_form.php');
  26. /**
  27. * Unit tests for the calculatedsimple question type class.
  28. *
  29. * @package qtype_calculatedsimple
  30. * @copyright 2007 The Open University
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. *
  33. * @covers \question_type
  34. * @covers \qtype_calculatedsimple
  35. * @covers \question_wizard_form
  36. * @covers \question_edit_form
  37. * @covers \qtype_calculated_edit_form
  38. * @covers \qtype_calculatedsimple_edit_form
  39. *
  40. */
  41. class question_type_test extends \advanced_testcase {
  42. protected $qtype;
  43. protected function setUp(): void {
  44. $this->qtype = new qtype_calculatedsimple();
  45. }
  46. protected function tearDown(): void {
  47. $this->qtype = null;
  48. }
  49. public function test_name() {
  50. $this->assertEquals($this->qtype->name(), 'calculatedsimple');
  51. }
  52. public function test_can_analyse_responses() {
  53. $this->assertTrue($this->qtype->can_analyse_responses());
  54. }
  55. public function test_question_saving_sumwithvariants() {
  56. $this->resetAfterTest(true);
  57. $this->setAdminUser();
  58. $questiondata = \test_question_maker::get_question_data('calculatedsimple', 'sumwithvariants');
  59. $formdata = \test_question_maker::get_question_form_data('calculatedsimple', 'sumwithvariants');
  60. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  61. $cat = $generator->create_question_category(array());
  62. $formdata->category = "{$cat->id},{$cat->contextid}";
  63. qtype_calculatedsimple_edit_form::mock_submit((array)$formdata);
  64. $form = \qtype_calculatedsimple_test_helper::get_question_editing_form($cat, $questiondata);
  65. $this->assertTrue($form->is_validated());
  66. $fromform = $form->get_data();
  67. $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
  68. $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
  69. $actualquestiondata = end($actualquestionsdata);
  70. foreach ($questiondata as $property => $value) {
  71. if (!in_array($property, array('id', 'timemodified', 'timecreated', 'options', 'idnumber'))) {
  72. $this->assertEquals($value, $actualquestiondata->$property);
  73. }
  74. }
  75. foreach ($questiondata->options as $optionname => $value) {
  76. if ($optionname != 'answers') {
  77. $this->assertEquals($value, $actualquestiondata->options->$optionname);
  78. }
  79. }
  80. foreach ($questiondata->options->answers as $answer) {
  81. $actualanswer = array_shift($actualquestiondata->options->answers);
  82. foreach ($answer as $ansproperty => $ansvalue) {
  83. if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
  84. $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
  85. }
  86. }
  87. }
  88. $datasetloader = new qtype_calculated_dataset_loader($actualquestiondata->id);
  89. $this->assertEquals(10, $datasetloader->get_number_of_items());
  90. for ($itemno = 1; $itemno <= 10; $itemno++) {
  91. $item = $datasetloader->get_values($itemno);
  92. $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 2], $item['a']);
  93. $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 1], $item['b']);
  94. }
  95. }
  96. }