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\event;
18
 
19
use advanced_testcase;
20
use cache;
21
use comment;
22
use context;
23
use context_course;
1441 ariadna 24
use context_module;
1 efrain 25
use core_question_generator;
26
use stdClass;
27
 
28
/**
29
 * Event tests for question comments.
30
 *
31
 * @package    qbank_comment
32
 * @copyright  2021 Catalyst IT Australia Pty Ltd
33
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class comment_created_deleted_test extends advanced_testcase {
1 efrain 37
 
38
    /** @var stdClass Keeps course object */
39
    private $course;
40
 
41
    /** @var context Keeps context */
42
    private $context;
43
 
44
    /** @var stdClass Keeps question object */
45
    private $questiondata;
46
 
47
    /** @var stdClass Keeps comment object */
48
    private $comment;
49
 
50
    /**
51
     * Setup test data.
52
     */
53
    public function setUp(): void {
54
        global $CFG;
55
        require_once($CFG->dirroot . '/comment/lib.php');
1441 ariadna 56
        parent::setUp();
1 efrain 57
 
58
        $this->resetAfterTest();
59
        $this->setAdminUser();
60
        $generator = $this->getDataGenerator();
61
 
62
        /** @var core_question_generator $questiongenerator */
63
        $questiongenerator = $generator->get_plugin_generator('core_question');
64
 
65
        // Create a course.
66
        $this->course = $generator->create_course();
1441 ariadna 67
        $qbank = self::getDataGenerator()->create_module('qbank', ['course' => $this->course->id]);
68
        $this->context = context_module::instance($qbank->cmid);
1 efrain 69
 
70
        // Create a question in the default category.
71
        $contexts = new \core_question\local\bank\question_edit_contexts($this->context);
1441 ariadna 72
        $cat = question_get_default_category($contexts->lowest()->id, true);
1 efrain 73
        $this->questiondata = $questiongenerator->create_question('numerical', null,
74
                ['name' => 'Example question', 'category' => $cat->id]);
75
 
76
        // Ensure the question is not in the cache.
77
        $cache = cache::make('core', 'questiondata');
78
        $cache->delete($this->questiondata->id);
79
 
80
        // Comment on question.
81
        $args = new stdClass;
82
        $args->context = $this->context;
83
        $args->course = $this->course;
84
        $args->area = 'question';
85
        $args->itemid = $this->questiondata->id;
86
        $args->component = 'qbank_comment';
87
        $args->linktext = get_string('commentheader', 'qbank_comment');
88
        $args->notoggle = true;
89
        $args->autostart = true;
90
        $args->displaycancel = false;
91
        $this->comment = new comment($args);
92
    }
93
 
94
    /**
95
     * Test comment_created event.
96
     */
11 efrain 97
    public function test_comment_created(): void {
1 efrain 98
        // Triggering and capturing the event.
99
        $sink = $this->redirectEvents();
100
        $this->comment->add('New comment');
101
        $events = $sink->get_events();
102
        $this->assertCount(1, $events);
103
        $event = reset($events);
104
 
105
        // Checking that the event contains the expected values.
106
        $this->assertInstanceOf('\qbank_comment\event\comment_created', $event);
107
        $this->assertEquals($this->context, $event->get_context());
108
        $this->assertStringContainsString('\'qbank_comment\' for the question with ID \''.$this->questiondata->id.'\'',
109
                $event->get_description());
110
    }
111
 
112
    /**
113
     * Test comment_created event.
114
     */
11 efrain 115
    public function test_comment_deleted(): void {
1 efrain 116
        // Triggering and capturing the event.
117
        $newcomment = $this->comment->add('New comment to delete');
118
        $sink = $this->redirectEvents();
119
        $this->comment->delete($newcomment->id);
120
        $events = $sink->get_events();
121
        $this->assertCount(1, $events);
122
        $event = reset($events);
123
 
124
        // Checking that the event contains the expected values.
125
        $this->assertInstanceOf('\qbank_comment\event\comment_deleted', $event);
126
        $this->assertEquals($this->context, $event->get_context());
127
        $this->assertStringContainsString('\'qbank_comment\' for the question with ID \''.$this->questiondata->id.'\'',
128
                $event->get_description());
129
    }
130
}