Línea 18... |
Línea 18... |
18 |
|
18 |
|
19 |
use qtype_multianswer;
|
19 |
use qtype_multianswer;
|
20 |
use qtype_multianswer_edit_form;
|
20 |
use qtype_multianswer_edit_form;
|
21 |
use qtype_multichoice_base;
|
21 |
use qtype_multichoice_base;
|
- |
|
22 |
use question_bank;
|
22 |
use question_bank;
|
23 |
use stdClass;
|
Línea 23... |
Línea 24... |
23 |
use test_question_maker;
|
24 |
use test_question_maker;
|
Línea 24... |
Línea 25... |
24 |
|
25 |
|
Línea 37... |
Línea 38... |
37 |
* @package qtype_multianswer
|
38 |
* @package qtype_multianswer
|
38 |
* @copyright 2011 The Open University
|
39 |
* @copyright 2011 The Open University
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
40 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
40 |
* @covers \qtype_multianswer
|
41 |
* @covers \qtype_multianswer
|
41 |
*/
|
42 |
*/
|
42 |
class question_type_test extends \advanced_testcase {
|
43 |
final class question_type_test extends \advanced_testcase {
|
43 |
/** @var qtype_multianswer instance of the question type class to test. */
|
44 |
/** @var qtype_multianswer instance of the question type class to test. */
|
44 |
protected $qtype;
|
45 |
protected $qtype;
|
Línea 45... |
Línea 46... |
45 |
|
46 |
|
- |
|
47 |
protected function setUp(): void {
|
46 |
protected function setUp(): void {
|
48 |
parent::setUp();
|
47 |
$this->qtype = new qtype_multianswer();
|
49 |
$this->qtype = new qtype_multianswer();
|
Línea 48... |
Línea 50... |
48 |
}
|
50 |
}
|
49 |
|
51 |
|
- |
|
52 |
protected function tearDown(): void {
|
50 |
protected function tearDown(): void {
|
53 |
$this->qtype = null;
|
Línea 51... |
Línea 54... |
51 |
$this->qtype = null;
|
54 |
parent::tearDown();
|
52 |
}
|
55 |
}
|
53 |
|
56 |
|
Línea 75... |
Línea 78... |
75 |
$q->stamp = make_unique_id_code();
|
78 |
$q->stamp = make_unique_id_code();
|
76 |
$q->timecreated = time();
|
79 |
$q->timecreated = time();
|
77 |
$q->timemodified = time();
|
80 |
$q->timemodified = time();
|
78 |
$q->createdby = $USER->id;
|
81 |
$q->createdby = $USER->id;
|
79 |
$q->modifiedby = $USER->id;
|
82 |
$q->modifiedby = $USER->id;
|
- |
|
83 |
$q->options = new stdClass();
|
Línea 80... |
Línea 84... |
80 |
|
84 |
|
81 |
$sadata = new \stdClass();
|
85 |
$sadata = new \stdClass();
|
82 |
$sadata->id = 1;
|
86 |
$sadata->id = 1;
|
83 |
$sadata->qtype = 'shortanswer';
|
87 |
$sadata->qtype = 'shortanswer';
|
- |
|
88 |
$sadata->defaultmark = 1;
|
84 |
$sadata->defaultmark = 1;
|
89 |
$sadata->options = new stdClass();
|
85 |
$sadata->options->usecase = true;
|
90 |
$sadata->options->usecase = true;
|
86 |
$sadata->options->answers[1] = (object) array('answer' => 'Bow-wow', 'fraction' => 0);
|
91 |
$sadata->options->answers[1] = (object) array('answer' => 'Bow-wow', 'fraction' => 0);
|
87 |
$sadata->options->answers[2] = (object) array('answer' => 'Wiggly worm', 'fraction' => 0);
|
92 |
$sadata->options->answers[2] = (object) array('answer' => 'Wiggly worm', 'fraction' => 0);
|
Línea 88... |
Línea 93... |
88 |
$sadata->options->answers[3] = (object) array('answer' => 'Pussy-cat', 'fraction' => 1);
|
93 |
$sadata->options->answers[3] = (object) array('answer' => 'Pussy-cat', 'fraction' => 1);
|
89 |
|
94 |
|
90 |
$mcdata = new \stdClass();
|
95 |
$mcdata = new \stdClass();
|
91 |
$mcdata->id = 1;
|
96 |
$mcdata->id = 1;
|
- |
|
97 |
$mcdata->qtype = 'multichoice';
|
92 |
$mcdata->qtype = 'multichoice';
|
98 |
$mcdata->defaultmark = 1;
|
93 |
$mcdata->defaultmark = 1;
|
99 |
$mcdata->options = new stdClass();
|
94 |
$mcdata->options->single = true;
|
100 |
$mcdata->options->single = true;
|
95 |
$mcdata->options->answers[1] = (object) array('answer' => 'Dog', 'fraction' => 0);
|
101 |
$mcdata->options->answers[1] = (object) array('answer' => 'Dog', 'fraction' => 0);
|
Línea 188... |
Línea 194... |
188 |
$this->assertEquals(1, $questiondata->length);
|
194 |
$this->assertEquals(1, $questiondata->length);
|
189 |
$this->assertEquals(\core_question\local\bank\question_version_status::QUESTION_STATUS_READY, $questiondata->status);
|
195 |
$this->assertEquals(\core_question\local\bank\question_version_status::QUESTION_STATUS_READY, $questiondata->status);
|
190 |
$this->assertEquals($question->createdby, $questiondata->createdby);
|
196 |
$this->assertEquals($question->createdby, $questiondata->createdby);
|
191 |
$this->assertEquals($question->createdby, $questiondata->modifiedby);
|
197 |
$this->assertEquals($question->createdby, $questiondata->modifiedby);
|
192 |
$this->assertEquals('', $questiondata->idnumber);
|
198 |
$this->assertEquals('', $questiondata->idnumber);
|
193 |
$this->assertEquals($syscontext->id, $questiondata->contextid);
|
199 |
$this->assertEquals($category->contextid, $questiondata->contextid);
|
Línea 194... |
Línea 200... |
194 |
|
200 |
|
195 |
// Build the expected hint base.
|
201 |
// Build the expected hint base.
|
196 |
$hintbase = [
|
202 |
$hintbase = [
|
197 |
'questionid' => $questiondata->id,
|
203 |
'questionid' => $questiondata->id,
|
Línea 430... |
Línea 436... |
430 |
$this->qtype->get_question_options($questiondata);
|
436 |
$this->qtype->get_question_options($questiondata);
|
Línea 431... |
Línea 437... |
431 |
|
437 |
|
432 |
$this->assertCount(2, $questiondata->options->questions);
|
438 |
$this->assertCount(2, $questiondata->options->questions);
|
433 |
$this->assertEquals('subquestion_replacement', $questiondata->options->questions[$questiontodeletekey]->qtype);
|
439 |
$this->assertEquals('subquestion_replacement', $questiondata->options->questions[$questiontodeletekey]->qtype);
|
- |
|
440 |
}
|
- |
|
441 |
|
- |
|
442 |
/**
|
- |
|
443 |
* Saving a new version of the question should retain the original subquestion versions, with their own qtype data.
|
- |
|
444 |
*/
|
- |
|
445 |
public function test_save_question_options(): void {
|
- |
|
446 |
global $DB;
|
- |
|
447 |
$this->resetAfterTest(true);
|
- |
|
448 |
$this->setAdminUser();
|
- |
|
449 |
|
- |
|
450 |
/** @var \core_question_generator $generator */
|
- |
|
451 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
- |
|
452 |
$cat = $generator->create_question_category([]);
|
- |
|
453 |
$question = $generator->create_question('multianswer', 'twosubq', ['category' => $cat->id]);
|
- |
|
454 |
|
- |
|
455 |
get_question_options($question);
|
- |
|
456 |
$originalsubq1 = reset($question->options->questions);
|
- |
|
457 |
$originalsubq2 = next($question->options->questions);
|
- |
|
458 |
|
- |
|
459 |
// Assert that the original subquestions are the expected types, and they have options records.
|
- |
|
460 |
$this->assertEquals('shortanswer', $originalsubq1->qtype);
|
- |
|
461 |
$this->assertTrue($DB->record_exists('qtype_shortanswer_options', ['questionid' => $originalsubq1->id]));
|
- |
|
462 |
$this->assertEquals('multichoice', $originalsubq2->qtype);
|
- |
|
463 |
$this->assertTrue($DB->record_exists('qtype_multichoice_options', ['questionid' => $originalsubq2->id]));
|
- |
|
464 |
|
- |
|
465 |
// Edit the question, replacing the subquestions with two new questions of different types.
|
- |
|
466 |
$editedquestion = test_question_maker::get_question_data('multianswer', 'twosubq');
|
- |
|
467 |
$editedquestion->id = $question->id;
|
- |
|
468 |
$editedquestion->category = $cat->id;
|
- |
|
469 |
$editedquestion->context = \context_helper::instance_by_id($cat->contextid);
|
- |
|
470 |
$editedsubq1 = test_question_maker::get_question_form_data('multichoice', 'one_of_four');
|
- |
|
471 |
$editedsubq1->id = $originalsubq1->id;
|
- |
|
472 |
$editedsubq1->qtype = 'multichoice';
|
- |
|
473 |
$editedsubq2 = test_question_maker::get_question_form_data('shortanswer', 'frogtoad');
|
- |
|
474 |
$editedsubq2->id = $originalsubq2->id;
|
- |
|
475 |
$editedsubq2->qtype = 'shortanswer';
|
- |
|
476 |
$editedquestion->options->questions = [$editedsubq1, $editedsubq2];
|
- |
|
477 |
$this->qtype->save_question_options($editedquestion);
|
- |
|
478 |
|
- |
|
479 |
$newquestion = $DB->get_record('question', ['id' => $question->id]);
|
- |
|
480 |
get_question_options($newquestion);
|
- |
|
481 |
$newsubq1 = reset($newquestion->options->questions);
|
- |
|
482 |
$newsubq2 = next($newquestion->options->questions);
|
- |
|
483 |
|
- |
|
484 |
// The new subquestions are different types, and did not re-use IDs from the original subquestions.
|
- |
|
485 |
$this->assertEquals('multichoice', $newsubq1->qtype);
|
- |
|
486 |
$this->assertFalse(in_array($newsubq1->id, [$originalsubq1->id, $originalsubq2->id]));
|
- |
|
487 |
$this->assertEquals('shortanswer', $newsubq2->qtype);
|
- |
|
488 |
$this->assertFalse(in_array($newsubq2->id, [$originalsubq1->id, $originalsubq2->id]));
|
- |
|
489 |
|
- |
|
490 |
// The original questions and option records still exist.
|
- |
|
491 |
$this->assertTrue($DB->record_exists('question', ['id' => $originalsubq1->id]));
|
- |
|
492 |
$this->assertTrue($DB->record_exists('qtype_shortanswer_options', ['questionid' => $originalsubq1->id]));
|
- |
|
493 |
$this->assertTrue($DB->record_exists('question', ['id' => $originalsubq2->id]));
|
- |
|
494 |
$this->assertTrue($DB->record_exists('qtype_multichoice_options', ['questionid' => $originalsubq2->id]));
|
434 |
}
|
495 |
}
|