Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 export/import description (info) for question category in the Moodle XML format.
18
 *
19
 * @package    qformat_aiken
20
 * @copyright  2018 Jean-Michel Vedrine
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
defined('MOODLE_INTERNAL') || die();
25
 
26
global $CFG;
27
require_once($CFG->libdir . '/questionlib.php');
28
require_once($CFG->dirroot . '/question/format.php');
29
require_once($CFG->dirroot . '/question/format/aiken/format.php');
30
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31
require_once($CFG->dirroot . '/question/editlib.php');
32
 
33
/**
34
 * Unit tests for the Aiken question format export.
35
 *
36
 * @copyright  2018 Jean-Michel vedrine)
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class qformat_aiken_export_test extends advanced_testcase {
40
    /**
41
     * Assert that 2 strings are the same, ignoring ends of line.
42
     * We need to override this function because we don't want any output
43
     * @param   string    $expectedtext The expected string.
44
     * @param   string    $text The actual string.
45
     */
46
    public function assert_same_aiken($expectedtext, $text) {
47
        $this->assertEquals(
48
            phpunit_util::normalise_line_endings($expectedtext),
49
            phpunit_util::normalise_line_endings($text)
50
        );
51
    }
52
 
11 efrain 53
    public function test_export_questions(): void {
1 efrain 54
        $this->resetAfterTest();
55
        $this->setAdminUser();
56
        // Create a new course category and and a new course in that.
57
        $category = $this->getDataGenerator()->create_category();
58
        $course = $this->getDataGenerator()->create_course(array('category' => $category->id));
59
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
60
        $context = context_coursecat::instance($category->id);
61
        $cat = $generator->create_question_category(array('contextid' => $context->id));
62
        $question1 = $generator->create_question('shortanswer', null,
63
                array('category' => $cat->id));
64
        $question2 = $generator->create_question('essay', null,
65
                array('category' => $cat->id));
66
        $question3 = $generator->create_question('numerical', null,
67
                array('category' => $cat->id));
68
        $question4  = $generator->create_question('multichoice', 'one_of_four',
69
                array('category' => $cat->id));
70
        $question4  = $generator->create_question('multichoice', 'two_of_four',
71
                array('category' => $cat->id));
72
        $exporter = new qformat_aiken();
73
        $exporter->category = $cat;
74
        $exporter->setCourse($course);
75
        $expectedoutput = <<<EOT
76
Which is the oddest number?
77
A) One
78
B) Two
79
C) Three
80
D) Four
81
ANSWER: A
82
 
83
EOT;
84
        $this->assert_same_aiken($expectedoutput, $exporter->exportprocess());
85
    }
86
 
11 efrain 87
    public function test_export_multiline_question(): void {
1 efrain 88
        $this->resetAfterTest();
89
        $this->setAdminUser();
90
        // Create a new course category and and a new course in that.
91
        $category = $this->getDataGenerator()->create_category();
92
        $course = $this->getDataGenerator()->create_course(array('category' => $category->id));
93
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
94
        $context = context_coursecat::instance($category->id);
95
        $cat = $generator->create_question_category(array('contextid' => $context->id));
96
        $question  = $generator->create_question('multichoice', 'one_of_four',
97
                array('category' => $cat->id));
98
        $question->questiontext = <<<EOT
99
<p>Which is the</p>
100
<p>oddest number?</p>
101
EOT;
102
        $exporter = new qformat_aiken();
103
        $exporter->category = $cat;
104
        $exporter->setCourse($course);
105
        $expectedoutput = <<<EOT
106
Which is the oddest number?
107
A) One
108
B) Two
109
C) Three
110
D) Four
111
ANSWER: A
112
 
113
EOT;
114
        $this->assert_same_aiken($expectedoutput, $exporter->exportprocess());
115
    }
116
}