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
namespace qbank_customfields;
18
 
19
/**
20
 * Class qbank_customfields_question_handler_testcase
21
 *
22
 * @package     qbank_customfields
23
 * @copyright   2021 Catalyst IT Australia Pty Ltd
24
 * @author      Matt Porritt <mattp@catalyst-au.net>
25
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
1441 ariadna 27
final class question_handler_test extends \advanced_testcase {
1 efrain 28
 
29
    /**
30
     * Question setup helper method.
31
     *
32
     * @return int The question id.
33
     * @throws coding_exception
34
     */
35
    protected function setup_question(): int {
1441 ariadna 36
        $course = self::getDataGenerator()->create_course();
37
        $qbank = self::getDataGenerator()->create_module('qbank', ['course' => $course->id]);
1 efrain 38
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
1441 ariadna 39
        $context = \context_module::instance($qbank->cmid);
1 efrain 40
        $questioncategory = $questiongenerator->create_question_category(['contextid' => $context->id]);
41
        $questiondata = ['category' => $questioncategory->id, 'idnumber' => 'q1'];
42
        $question = $questiongenerator->create_question('shortanswer', null, $questiondata);
43
 
44
        return $question->id;
45
 
46
    }
47
 
48
    /**
49
     * Test custom field data.
50
     */
51
    public function test_get_field_data(): void {
52
        $this->resetAfterTest();
53
        $fieldgenerator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
54
 
55
        $instanceid = $this->setup_question();
56
        $fieldvalue = 'test field text';
57
 
58
        $categorydata = new \stdClass();
59
        $categorydata->component = 'qbank_customfields';
60
        $categorydata->area = 'question';
61
        $categorydata->name = 'test category';
62
 
63
        $customfieldcatid = $fieldgenerator->create_category($categorydata)->get('id');
64
        $field = $fieldgenerator->create_field(['categoryid' => $customfieldcatid, 'type' => 'text', 'shortname' => 'f1']);
65
        $fieldgenerator->add_instance_data($field, $instanceid, $fieldvalue);
66
 
67
        // Get the field data.
68
        $customfieldhandler = customfield\question_handler::create();
69
        $fieldinstancedata = $customfieldhandler->get_field_data($field, $instanceid);
70
 
71
        $this->assertEquals($categorydata->name, $fieldinstancedata->get_field()->get_category()->get('name'));
72
        $this->assertEquals($fieldvalue, $fieldinstancedata->get_value());
73
    }
74
 
75
    /**
76
     * Test getting custom field data for table display.
77
     */
78
    public function test_display_custom_field_table(): void {
79
        $this->resetAfterTest();
80
        $fieldgenerator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
81
 
82
        $instanceid = $this->setup_question();
83
        $fieldvalue = 'test field text';
84
 
85
        $categorydata = new \stdClass();
86
        $categorydata->component = 'qbank_customfields';
87
        $categorydata->area = 'question';
88
        $categorydata->name = 'test category';
89
 
90
        $customfieldcatid = $fieldgenerator->create_category($categorydata)->get('id');
91
        $field = $fieldgenerator->create_field(['categoryid' => $customfieldcatid, 'type' => 'text', 'shortname' => 'f1']);
92
        $fieldgenerator->add_instance_data($field, $instanceid, $fieldvalue);
93
 
94
        // Get the field data.
95
        $customfieldhandler = customfield\question_handler::create();
96
        $fieldinstancedata = $customfieldhandler->get_field_data($field, $instanceid);
97
        $output = $customfieldhandler->display_custom_field_table($fieldinstancedata);
98
 
99
        $this->assertStringContainsString($fieldvalue, $output);
100
    }
101
 
102
    /**
103
     * Test getting categories and field data for a specific instance.
104
     */
105
    public function test_get_categories_fields_data(): void {
106
        $this->resetAfterTest();
107
        $fieldgenerator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
108
 
109
        $instanceid = $this->setup_question();
110
        $field1value = 'first field text';
111
        $field2value = 'second field text';
112
        $field3value = 'third field text';
113
 
114
        $categorydata = new \stdClass();
115
        $categorydata->component = 'qbank_customfields';
116
        $categorydata->area = 'question';
117
        $categorydata->name = 'test category';
118
 
119
        $customfieldcat1id = $fieldgenerator->create_category($categorydata)->get('id');
120
        $categorydata->name = 'test category two';
121
        $customfieldcat2id = $fieldgenerator->create_category($categorydata)->get('id');
122
 
123
        $field1 = $fieldgenerator->create_field(['categoryid' => $customfieldcat1id, 'type' => 'text', 'shortname' => 'f1']);
124
        $fieldgenerator->add_instance_data($field1, $instanceid, $field1value);
125
        $field2 = $fieldgenerator->create_field(['categoryid' => $customfieldcat1id, 'type' => 'text', 'shortname' => 'f2']);
126
        $fieldgenerator->add_instance_data($field2, $instanceid, $field2value);
127
 
128
        $field3 = $fieldgenerator->create_field(['categoryid' => $customfieldcat2id, 'type' => 'text', 'shortname' => 'f3']);
129
        $fieldgenerator->add_instance_data($field3, $instanceid, $field3value);
130
 
131
        // Get the field data.
132
        $customfieldhandler = customfield\question_handler::create();
133
        $outputdata = $customfieldhandler->get_categories_fields_data($instanceid);
134
 
135
        $this->assertEquals($field1value, $outputdata['test category'][0]['value']);
136
        $this->assertEquals($field2value, $outputdata['test category'][1]['value']);
137
        $this->assertEquals($field3value, $outputdata['test category two'][0]['value']);
138
 
139
        // While we're here, lets test the rendering of this data.
140
        $outputhtml = $customfieldhandler->display_custom_categories_fields($outputdata);
141
 
142
        $this->assertStringContainsString($field1value, $outputhtml);
143
        $this->assertStringContainsString($field2value, $outputhtml);
144
        $this->assertStringContainsString($field3value, $outputhtml);
145
    }
146
}