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_comment;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/question/bank/comment/lib.php');
23
 
24
 
25
/**
26
 * Comment lib unit tests.
27
 *
28
 * @package    qbank_comment
29
 * @copyright  2021 Catalyst IT Australia Pty Ltd
30
 * @author     Matt Porritt <mattp@catalyst-au.net>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
1441 ariadna 33
final class lib_test extends \advanced_testcase {
1 efrain 34
 
35
    /**
36
     * Test the comment validation callback.
37
     */
11 efrain 38
    public function test_qbank_comment_comment_validate(): void {
1 efrain 39
        $commentparams = new \stdClass();
40
        $commentparams->commentarea = 'question';
41
        $commentparams->component = 'qbank_comment';
42
 
43
        $isvalid = qbank_comment_comment_validate($commentparams);
44
        $this->assertTrue($isvalid);
45
 
46
        $this->expectException('comment_exception');
47
        $commentparams->commentarea = 'core_comment';
48
        $commentparams->component = 'blog_comment';
49
        qbank_comment_comment_validate($commentparams);
50
 
51
    }
52
 
53
    /**
54
     * Test the comment display callback.
55
     */
11 efrain 56
    public function test_qbank_comment_comment_display(): void {
1 efrain 57
        $comment = new \stdClass();
58
        $comment->text = 'test';
59
        $comments = [$comment];
60
 
61
        $commentparams = new \stdClass();
62
        $commentparams->commentarea = 'question';
63
        $commentparams->component = 'qbank_comment';
64
 
65
        $responses = qbank_comment_comment_display($comments, $commentparams);
66
        $this->assertEquals($comment->text, $responses[0]->text);
67
 
68
        $this->expectException('comment_exception');
69
        $commentparams->commentarea = 'core_comment';
70
        $commentparams->component = 'blog_comment';
71
        qbank_comment_comment_display($comments, $commentparams);
72
 
73
    }
74
 
75
    /**
76
     * Test the comment preview callback.
77
     */
11 efrain 78
    public function test_qbank_comment_preview_display(): void {
1 efrain 79
        $this->resetAfterTest();
80
        global $PAGE;
81
        $PAGE->set_url('/');
82
 
83
        // Make a test question.
84
        $category = $this->getDataGenerator()->create_category();
85
        $course = $this->getDataGenerator()->create_course(['category' => $category->id]);
86
        $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
1441 ariadna 87
        $qbank = self::getDataGenerator()->create_module('qbank', ['course' => $course->id]);
88
        $context = \context_module::instance($qbank->cmid);
1 efrain 89
        $qcat = $qgen->create_question_category(['contextid' => $context->id]);
90
        $question = $qgen->create_question('shortanswer', null, ['category' => $qcat->id, 'idnumber' => 'q1']);
91
 
92
        $result = qbank_comment_preview_display($question, $course->id);
93
 
94
        // User doesn't have perms so expecting no output.
95
        $this->assertEmpty($result);
96
 
97
        // Expect output.
98
        $this->setAdminUser();
99
        $result = qbank_comment_preview_display($question, $course->id);
100
        $this->assertStringContainsString('comment-action-post', $result);
101
    }
102
 
103
    /**
104
     * Test the comment preview callback.
105
     */
11 efrain 106
    public function test_qbank_comment_output_fragment_question_comment(): void {
1 efrain 107
        $this->resetAfterTest();
108
        $this->setAdminUser();
109
        global $PAGE;
110
        $PAGE->set_url('/');
111
 
112
        // Make a test question.
113
        $category = $this->getDataGenerator()->create_category();
114
        $course = $this->getDataGenerator()->create_course(['category' => $category->id]);
1441 ariadna 115
        $qbank = self::getDataGenerator()->create_module('qbank', ['course' => $course->id]);
116
        $context = \context_module::instance($qbank->cmid);
1 efrain 117
        $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
118
        $qcat = $qgen->create_question_category(['contextid' => $context->id]);
119
        $question = $qgen->create_question('shortanswer', null, ['category' => $qcat->id, 'idnumber' => 'q1']);
120
        $args = [
121
            'questionid' => $question->id,
122
            'courseid' => $course->id,
123
            ];
124
 
125
        $result = qbank_comment_output_fragment_question_comment($args);
126
 
127
        // Expect output.
128
        $this->assertStringContainsString('comment-action-post', $result);
129
    }
130
}