Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 core_question;
18
 
19
use core_question\local\bank\question_edit_contexts;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/question/editlib.php');
25
 
26
/**
27
 * Unit tests for the question own question bank view class.
28
 *
29
 * @package    core_question
30
 * @copyright  2024 the Open University
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @covers \core_question\local\bank\view
33
 */
34
final class question_bank_view_test extends \advanced_testcase {
35
 
36
    public function test_viewing_question_bank_should_not_load_hidden_question(): void {
37
        $this->resetAfterTest();
38
        $this->setAdminUser();
39
        $generator = $this->getDataGenerator();
40
        /** @var core_question_generator $questiongenerator */
41
        $questiongenerator = $generator->get_plugin_generator('core_question');
42
 
43
        // Create a course and a quiz.
44
        $course = $generator->create_course();
45
        $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
46
        $context = \context_module::instance($quiz->cmid);
47
        $cm = get_coursemodule_from_instance('quiz', $quiz->id);
48
 
49
        // Create a question in the default category.
50
        $contexts = new question_edit_contexts($context);
51
        $cat = question_get_default_category($context->id, true);
52
        $question = $questiongenerator->create_question('numerical', null,
53
            ['name' => 'Example question', 'category' => $cat->id]);
54
        // Create another version.
55
        $newversion = $questiongenerator->update_question($question, null, ['name' => 'This is the latest version']);
56
        // Add them to the quiz.
57
        quiz_add_quiz_question($newversion->id, $quiz);
58
        // Generate the view.
59
        $params = [
60
            'qpage' => 0,
61
            'qperpage' => 20,
62
            'cat' => $cat->id . ',' . $context->id,
63
            'recurse' => false,
64
            'qbshowtext' => false,
65
            'tabname' => 'editq',
66
        ];
67
        $extraparams = ['cmid' => $cm->id];
68
        $view = new \core_question\local\bank\view($contexts, new \moodle_url('/'), $course, $cm, $params, $extraparams);
69
        ob_start();
70
        $view->display();
71
        $html = ob_get_clean();
72
        // Verify the output should included the latest version.
73
        $this->assertStringContainsString('This is the latest version', $html);
74
        $this->assertStringNotContainsString('Example question', $html);
75
        // Delete the latest version.
76
        question_delete_question($newversion->id);
77
 
78
        // Verify the output should display the old version with status ready.
79
        ob_start();
80
        $view->display();
81
        $html = ob_get_clean();
82
        $this->assertStringContainsString('Example question', $html);
83
        $this->assertStringNotContainsString('This is the latest version', $html);
84
 
85
        // Use show hidden question filter.
86
        $params['filter'] = [
87
            'category' => [
88
                'name' => 'category',
89
                'jointype' => 1,
90
                'values' => [$cat->id],
91
                'filteroptions' => [],
92
            ],
93
            'hidden' => [
94
                'name' => 'hidden',
95
                'jointype' => 1,
96
                'values' => [1],
97
                'filteroptions' => [],
98
            ],
99
        ];
100
        $view = new \core_question\local\bank\view($contexts, new \moodle_url('/'), $course, $cm, $params, $extraparams);
101
        ob_start();
102
        $view->display();
103
        $html = ob_get_clean();
104
        $this->assertStringContainsString('This is the latest version', $html);
105
        $this->assertStringNotContainsString('Example question', $html);
106
    }
107
}