Automatically generated installer lang files
[moodle.git] / comment / tests / context_freeze_test.php
blob36f1d600bca25093441f22826d7c4947e691685c
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.
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/>.
17 /**
18 * Tests for comments when the context is frozen.
20 * @package core_comment
21 * @copyright 2019 University of Nottingham
22 * @author Neill Magill <neill.magill@nottingham.ac.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Tests for comments when the context is frozen.
31 * @package core_comment
32 * @copyright 2019 University of Nottingham
33 * @author Neill Magill <neill.magill@nottingham.ac.uk>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class comment_context_freeze_testcase extends advanced_testcase {
37 /**
38 * Creates a comment by a student.
40 * Returns:
41 * - The comment object
42 * - The sudent that wrote the comment
43 * - The arguments used to create the comment
45 * @param stdClass $course Moodle course from the datagenerator
46 * @return array
48 protected function create_student_comment_and_freeze_course($course): array {
49 set_config('contextlocking', 1);
51 $context = context_course::instance($course->id);
52 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
54 $args = new stdClass;
55 $args->context = $context;
56 $args->course = $course;
57 $args->area = 'page_comments';
58 $args->itemid = 0;
59 $args->component = 'block_comments';
60 $args->linktext = get_string('showcomments');
61 $args->notoggle = true;
62 $args->autostart = true;
63 $args->displaycancel = false;
65 // Create a comment by the student.
66 $this->setUser($student);
67 $comment = new comment($args);
68 $newcomment = $comment->add('New comment');
70 // Freeze the context.
71 $this->setAdminUser();
72 $context->set_locked(true);
74 return [$newcomment, $student, $args];
77 /**
78 * Test that a student cannot delete their own comments in frozen contexts via the external service.
80 public function test_delete_student_external() {
81 global $CFG;
82 require_once($CFG->dirroot . '/comment/lib.php');
84 $this->resetAfterTest();
86 $course = $this->getDataGenerator()->create_course();
88 list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course);
90 // Check that a student cannot delete their own comment.
91 $this->setUser($student);
92 $studentcomment = new comment($args);
93 $this->assertFalse($studentcomment->can_delete($newcomment->id));
94 $this->assertFalse($studentcomment->can_post());
95 $this->expectException(comment_exception::class);
96 $this->expectExceptionMessage(get_string('nopermissiontodelentry', 'error'));
97 core_comment_external::delete_comments([$newcomment->id]);
101 * Test that a student cannot delete their own comments in frozen contexts.
103 public function test_delete_student() {
104 global $CFG;
105 require_once($CFG->dirroot . '/comment/lib.php');
107 $this->resetAfterTest();
109 $course = $this->getDataGenerator()->create_course();
111 list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course);
113 // Check that a student cannot delete their own comment.
114 $this->setUser($student);
115 $studentcomment = new comment($args);
116 $this->assertFalse($studentcomment->can_delete($newcomment->id));
117 $this->assertFalse($studentcomment->can_post());
118 $this->expectException(comment_exception::class);
119 $this->expectExceptionMessage(get_string('nopermissiontocomment', 'error'));
120 $studentcomment->delete($newcomment->id);
124 * Test that an admin cannot delete comments in frozen contexts via the external service.
126 public function test_delete_admin_external() {
127 global $CFG;
128 require_once($CFG->dirroot . '/comment/lib.php');
130 $this->resetAfterTest();
132 $course = $this->getDataGenerator()->create_course();
134 list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course);
136 // Check that the admin user cannot delete the comment.
137 $admincomment = new comment($args);
138 $this->assertFalse($admincomment->can_delete($newcomment->id));
139 $this->assertFalse($admincomment->can_post());
140 $this->expectException(comment_exception::class);
141 $this->expectExceptionMessage(get_string('nopermissiontodelentry', 'error'));
142 core_comment_external::delete_comments([$newcomment->id]);
146 * Test that an admin cannot delete comments in frozen contexts.
148 public function test_delete_admin() {
149 global $CFG;
150 require_once($CFG->dirroot . '/comment/lib.php');
152 $this->resetAfterTest();
154 $course = $this->getDataGenerator()->create_course();
156 list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course);
158 // Check that the admin user cannot delete the comment.
159 $admincomment = new comment($args);
160 $this->assertFalse($admincomment->can_delete($newcomment->id));
161 $this->assertFalse($admincomment->can_post());
162 $this->expectException(comment_exception::class);
163 $this->expectExceptionMessage(get_string('nopermissiontocomment', 'error'));
164 $admincomment->delete($newcomment->id);