Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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 /** 18 * Tests for comments when the context is frozen. 19 * 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 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 /** 29 * Tests for comments when the context is frozen. 30 * 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 35 */ 36 class comment_context_freeze_testcase extends advanced_testcase { 37 /** 38 * Creates a comment by a student. 39 * 40 * Returns: 41 * - The comment object 42 * - The sudent that wrote the comment 43 * - The arguments used to create the comment 44 * 45 * @param stdClass $course Moodle course from the datagenerator 46 * @return array 47 */ 48 protected function create_student_comment_and_freeze_course($course): array { 49 set_config('contextlocking', 1); 50 51 $context = context_course::instance($course->id); 52 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 53 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; 64 65 // Create a comment by the student. 66 $this->setUser($student); 67 $comment = new comment($args); 68 $newcomment = $comment->add('New comment'); 69 70 // Freeze the context. 71 $this->setAdminUser(); 72 $context->set_locked(true); 73 74 return [$newcomment, $student, $args]; 75 } 76 77 /** 78 * Test that a student cannot delete their own comments in frozen contexts via the external service. 79 */ 80 public function test_delete_student_external() { 81 global $CFG; 82 require_once($CFG->dirroot . '/comment/lib.php'); 83 84 $this->resetAfterTest(); 85 86 $course = $this->getDataGenerator()->create_course(); 87 88 list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course); 89 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]); 98 } 99 100 /** 101 * Test that a student cannot delete their own comments in frozen contexts. 102 */ 103 public function test_delete_student() { 104 global $CFG; 105 require_once($CFG->dirroot . '/comment/lib.php'); 106 107 $this->resetAfterTest(); 108 109 $course = $this->getDataGenerator()->create_course(); 110 111 list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course); 112 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); 121 } 122 123 /** 124 * Test that an admin cannot delete comments in frozen contexts via the external service. 125 */ 126 public function test_delete_admin_external() { 127 global $CFG; 128 require_once($CFG->dirroot . '/comment/lib.php'); 129 130 $this->resetAfterTest(); 131 132 $course = $this->getDataGenerator()->create_course(); 133 134 list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course); 135 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]); 143 } 144 145 /** 146 * Test that an admin cannot delete comments in frozen contexts. 147 */ 148 public function test_delete_admin() { 149 global $CFG; 150 require_once($CFG->dirroot . '/comment/lib.php'); 151 152 $this->resetAfterTest(); 153 154 $course = $this->getDataGenerator()->create_course(); 155 156 list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course); 157 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); 165 } 166 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body