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 qbank_history;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/question/editlib.php');
23
 
24
/**
25
 * Custom history view - qbank api test.
26
 *
27
 * @package    qbank_history
28
 * @copyright  2022 Catalyst IT Australia Pty Ltd
29
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @coversDefaultClass \qbank_history\question_history_view
32
 */
1441 ariadna 33
final class question_history_view_test extends \advanced_testcase {
1 efrain 34
 
35
    /**
36
     * Test that the history page shows all the versions of a question.
37
     *
38
     * @covers ::display
39
     */
11 efrain 40
    public function test_question_history_shows_all_versions(): void {
1 efrain 41
        $this->resetAfterTest();
42
        $this->setAdminUser();
43
        $generator = $this->getDataGenerator();
44
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
45
 
46
        // Create a course.
47
        $course = $generator->create_course();
1441 ariadna 48
        $qbank = $generator->create_module('qbank', ['course' => $course->id]);
49
        $cm = get_coursemodule_from_id('qbank', $qbank->cmid);
50
        $context = \context_module::instance($qbank->cmid);
1 efrain 51
 
52
        // Create a question in the default category.
53
        $contexts = new \core_question\local\bank\question_edit_contexts($context);
1441 ariadna 54
        $cat = $questiongenerator->create_question_category(['contextid' => $context->id]);
1 efrain 55
        $questiondata1 = $questiongenerator->create_question('numerical', null,
56
            ['name' => 'Example question', 'category' => $cat->id]);
57
 
58
        // Create a new version.
59
        $questiondata2 = $questiongenerator->update_question($questiondata1, null,
60
            ['name' => 'Example question second version']);
61
 
62
        $entry = get_question_bank_entry($questiondata1->id);
63
 
64
        $pagevars = [
65
            'qpage' => 0,
66
            'qperpage' => DEFAULT_QUESTIONS_PER_PAGE,
67
            'cat' => $cat->id . ',' . $cat->contextid,
68
            'tabname' => 'questions'
69
        ];
70
        // Generate the view.
71
        $viewclass = \qbank_history\question_history_view::class;
72
        $extraparams = [
73
            'view' => $viewclass,
74
            'entryid' => $entry->id,
75
            'returnurl' => "/",
76
        ];
1441 ariadna 77
        $view = new $viewclass($contexts, new \moodle_url('/'), $course, $cm, $pagevars, $extraparams);
1 efrain 78
        ob_start();
79
        $view->display();
80
        $html = ob_get_clean();
81
 
82
        // Verify the output includes the first version.
83
        $this->assertStringContainsString($questiondata1->name, $html);
84
 
85
        // Verify the output includes the second version.
86
        $this->assertStringContainsString($questiondata2->name, $html);
87
    }
88
 
89
    /**
90
     * Test that the question bank header in the history page shows the latest question.
91
     *
92
     * @covers ::display_question_bank_header
93
     */
11 efrain 94
    public function test_display_question_bank_header(): void {
1 efrain 95
        $this->resetAfterTest();
96
        $this->setAdminUser();
97
        $generator = $this->getDataGenerator();
98
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
99
 
100
        // Create a course.
101
        $course = $generator->create_course();
1441 ariadna 102
        $qbank = $generator->create_module('qbank', ['course' => $course->id]);
103
        $cm = get_coursemodule_from_id('qbank', $qbank->cmid);
104
        $context = \context_module::instance($cm->id);
1 efrain 105
 
106
        // Create a question in the default category.
107
        $contexts = new \core_question\local\bank\question_edit_contexts($context);
108
        $cat = $questiongenerator->create_question_category();
109
        $questiondata1 = $questiongenerator->create_question('numerical', null,
110
            ['name' => 'First version', 'category' => $cat->id]);
111
 
112
        $entry = get_question_bank_entry($questiondata1->id);
113
        $pagevars = [
114
            'qpage' => 0,
115
            'qperpage' => DEFAULT_QUESTIONS_PER_PAGE,
116
            'cat' => $cat->id . ',' . $cat->contextid,
117
            'tabname' => 'questions'
118
        ];
119
        // Generate the view.
120
        $viewclass = \qbank_history\question_history_view::class;
121
        $extraparams = [
122
            'view' => $viewclass,
123
            'entryid' => $entry->id,
124
            'returnurl' => "/",
125
        ];
1441 ariadna 126
        $view = new $viewclass($contexts, new \moodle_url('/'), $course, $cm, $pagevars, $extraparams);
1 efrain 127
        ob_start();
128
        $view->display_question_bank_header();
129
        $headerhtml = ob_get_clean();
130
        // Verify the output includes the latest version.
131
        $this->assertStringContainsString($questiondata1->name, $headerhtml);
132
 
133
        $questiondata2 = $questiongenerator->update_question($questiondata1, null,
134
            ['name' => 'Second version']);
1441 ariadna 135
        $view = new $viewclass($contexts, new \moodle_url('/'), $course, $cm, $pagevars, $extraparams);
1 efrain 136
        ob_start();
137
        $view->display_question_bank_header();
138
        $headerhtml = ob_get_clean();
139
 
140
        $this->assertStringContainsString($questiondata2->name, $headerhtml);
141
    }
142
}