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