Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
namespace tool_brickfield;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/admin/tool/brickfield/tests/area_test_base.php');
23
 
24
/**
25
 * Class tool_brickfield_area_testcase
26
 *
27
 * @package    tool_brickfield
28
 * @copyright  2020 onward: Brickfield Education Labs, https://www.brickfield.ie
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
1441 ariadna 31
final class area_test extends area_test_base {
1 efrain 32
    /**
33
     * Test for the area assign intro
34
     */
11 efrain 35
    public function test_assign(): void {
1 efrain 36
        $this->resetAfterTest();
37
        $course = $this->getDataGenerator()->create_course();
38
        $assign1 = $this->getDataGenerator()->create_module('assign', array(
39
            'course' => $course->id, 'name' => 'Test!', 'intro' => '<p>Here we go</p>',
40
            'introformat' => FORMAT_HTML));
41
        list($course1, $cm1) = get_course_and_cm_from_instance($assign1->id, 'assign');
42
        $assign2 = $this->getDataGenerator()->create_module('assign', array(
43
            'course' => SITEID, 'name' => 'Test2!', 'intro' => 'Something',
44
            'introformat' => FORMAT_MOODLE));
45
        list($course2, $cm2) = get_course_and_cm_from_instance($assign2->id, 'assign');
46
 
47
        $c = new \tool_brickfield\local\areas\mod_assign\intro();
48
        $this->assertEquals('mod_assign', $c->get_component());
49
        $this->assertEquals('assign', $c->get_tablename());
50
        $resultsrs = $c->find_course_areas($course1->id);
51
        $resultsrs2 = $c->find_course_areas($course2->id);
52
        // Set up a results array from the recordset for easier testing.
53
        $results = array_merge($this->array_from_recordset($resultsrs), $this->array_from_recordset($resultsrs2));
54
        $this->assertEquals([
55
            (object)[
56
                'type' => area_base::TYPE_FIELD,
57
                'contextid' => \context_module::instance($cm1->id)->id,
58
                'component' => $c->get_component(),
59
                'tablename' => $c->get_tablename(),
60
                'fieldorarea' => $c->get_fieldname(),
61
                'itemid' => $assign1->id,
62
                'cmid' => $cm1->id,
63
                'courseid' => $course1->id,
64
                'content' => $assign1->intro,
65
            ],
66
            (object)[
67
                'type' => area_base::TYPE_FIELD,
68
                'contextid' => \context_module::instance($cm2->id)->id,
69
                'component' => $c->get_component(),
70
                'tablename' => $c->get_tablename(),
71
                'fieldorarea' => $c->get_fieldname(),
72
                'itemid' => $assign2->id,
73
                'cmid' => $cm2->id,
74
                'courseid' => $course2->id,
75
                'content' => $assign2->intro,
76
            ]
77
        ], $results);
78
 
79
        // Emulate the course_module_updated event.
80
        $event = \core\event\course_module_updated::create_from_cm($cm1);
81
        $relevantresultsrs = $c->find_relevant_areas($event);
82
        // Set up a relevantresults array from the recordset for easier testing.
83
        $relevantresults = $this->array_from_recordset($relevantresultsrs);
84
        $this->assertEquals([$results[0]], $relevantresults);
85
    }
86
 
87
    /**
88
     * Test for the area questiontext
89
     */
11 efrain 90
    public function test_questiontext(): void {
1 efrain 91
        $this->resetAfterTest();
92
        /** @var \core_question_generator $generator */
93
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
94
        $component = 'core_question';
95
 
1441 ariadna 96
        [$category, $course, $qcat, $questions, $qbank] = $generator->setup_course_and_questions();
1 efrain 97
 
98
        $c = new \tool_brickfield\local\areas\core_question\questiontext();
99
        // Set up results arrays from the recordset for easier testing.
1441 ariadna 100
        $course1areas = $this->array_from_recordset($c->find_course_areas($course->id));
1 efrain 101
 
1441 ariadna 102
         // Assert the core_question area exists for the individual question's context, courseid and categoryid.
1 efrain 103
        $this->assert_area_in_array(
104
            $course1areas,
105
            $component,
1441 ariadna 106
            \context_module::instance($qbank->cmid)->id,
107
            $questions[0]->id,
108
            $course->id,
1 efrain 109
            null
110
        );
111
 
112
        // Emulate the question_created event.
1441 ariadna 113
        $event = \core\event\question_created::create_from_question_instance($questions[1],
114
            \context_module::instance($qbank->cmid));
1 efrain 115
        $relevantresults = $this->array_from_recordset($c->find_relevant_areas($event));
116
        $this->assert_area_in_array(
1441 ariadna 117
                $relevantresults,
1 efrain 118
            $component,
1441 ariadna 119
            $relevantresults[0]->contextid,
1 efrain 120
            $relevantresults[0]->itemid,
121
            $relevantresults[0]->courseid,
122
            $relevantresults[0]->categoryid
123
        );
124
    }
125
 
126
    /**
127
     * test for the area questionanswers
128
     */
11 efrain 129
    public function test_questionanswers(): void {
1 efrain 130
        global $DB;
131
 
132
        $this->resetAfterTest();
133
        /** @var \core_question_generator $generator */
134
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
135
        $course = $this->getDataGenerator()->create_course();
1441 ariadna 136
        $qbank = $this->getDataGenerator()->create_module('qbank', ['course' => $course->id]);
137
        $cat = $generator->create_question_category(['contextid' => \context_module::instance($qbank->cmid)->id]);
1 efrain 138
        $question1 = $generator->create_question('multichoice', null,
139
            ['name' => 'Example multichoice question', 'category' => $cat->id]);
140
        $question2 = $generator->create_question('numerical', null,
141
            ['name' => 'Example numerical question', 'category' => $cat->id]);
142
 
143
        $dbanswers = $DB->get_records('question_answers', [] , 'id');
144
        $this->assertNotEmpty(count($dbanswers));
145
 
146
        $c = new \tool_brickfield\local\areas\core_question\questionanswers();
147
        $resultsrs = $c->find_course_areas($course->id);
148
        $results = $this->array_from_recordset($resultsrs);
149
 
150
        // There will be the same number of results as the number of records in the question_answers table.
151
        $this->assertEquals(count($dbanswers), count($results));
152
 
153
        // Emulate the question_updated event.
154
        $event = \core\event\question_updated::create_from_question_instance($question1,
155
            \context_course::instance($course->id));
156
        $relevantresultsrs = $c->find_relevant_areas($event);
157
        // Set up a relevantresults array from the recordset for easier testing.
158
        $relevantresults = $this->array_from_recordset($relevantresultsrs);
159
 
160
        $dbanswers = array_values($DB->get_records('question_answers', ['question' => $question1->id], 'id'));
161
        $this->assertEquals(count($dbanswers), count($relevantresults));
162
        foreach ($dbanswers as $i => $dbanswer) {
163
            $relevantresult = $relevantresults[$i];
164
            $this->assertEquals($dbanswer->answer, $relevantresult->content);
165
            $this->assertEquals('question', $relevantresult->reftable);
166
            $this->assertEquals($question1->id, $relevantresult->refid);
167
            $this->assertEquals($dbanswer->id, $relevantresult->itemid);
168
        }
169
    }
170
 
171
    /**
172
     * Test for the areas choice intro and choice options
173
     */
11 efrain 174
    public function test_choice(): void {
1 efrain 175
        global $DB;
176
        $this->resetAfterTest();
177
        $course = $this->getDataGenerator()->create_course();
178
        $choice1 = $this->getDataGenerator()->create_module('choice', [
179
            'course' => $course->id, 'option' => ['fried rice', 'spring rolls', 'sweet and sour pork']
180
        ]);
181
        list($course1, $cm1) = get_course_and_cm_from_instance($choice1->id, 'choice');
182
        $choice2 = $this->getDataGenerator()->create_module('choice', [
183
            'course' => $course->id, 'option' => ['blue', 'red']
184
        ]);
185
        list($course2, $cm2) = get_course_and_cm_from_instance($choice2->id, 'choice');
186
 
187
        // Testing the choice intro.
188
        $c = new \tool_brickfield\local\areas\mod_choice\intro();
189
        $resultsrs = $c->find_course_areas($course->id);
190
        // Set up a results array from the recordset for easier testing.
191
        $results = $this->array_from_recordset($resultsrs);
192
 
193
        $this->assertCount(2, $results);
194
        $this->assertEquals($cm1->id, $results[0]->cmid);
195
        $this->assertEquals($choice2->id, $results[1]->itemid);
196
 
197
        // Emulate the course_module_created event.
198
        $event = \core\event\course_module_created::create_from_cm($cm1);
199
        $relevantresultsrs = $c->find_relevant_areas($event);
200
        $relevantresults = $this->array_from_recordset($relevantresultsrs);
201
        $this->assertEquals([$results[0]], $relevantresults);
202
 
203
        // Testing the choice options.
204
        $c = new \tool_brickfield\local\areas\mod_choice\option();
205
        $resultsrs = $c->find_course_areas($course->id);
206
        // Set up a results array from the recordset for easier testing.
207
        $results = $this->array_from_recordset($resultsrs);
208
 
209
        $this->assertCount(5, $results);
210
        $this->assertEquals($cm2->id, $results[3]->cmid);
211
        $this->assertEquals('choice_options', $results[3]->tablename);
212
        $this->assertEquals('choice', $results[3]->reftable);
213
        $this->assertEquals($choice2->id, $results[3]->refid);
214
        $options3 = $DB->get_records_menu('choice_options', ['choiceid' => $choice2->id], 'id', 'text,id');
215
        $this->assertEquals($options3['blue'], $results[3]->itemid);
216
        $this->assertEquals('blue', $results[3]->content);
217
 
218
        // Emulate the course_module_updated event.
219
        $event = \core\event\course_module_updated::create_from_cm($cm2);
220
        $relevantresultsrs = $c->find_relevant_areas($event);
221
        $relevantresults = $this->array_from_recordset($relevantresultsrs);
222
        $this->assertEquals([$results[3], $results[4]], $relevantresults);
223
    }
224
}