Differences Between: [Versions 310 and 402] [Versions 39 and 402]
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 * Lib tests. 19 * 20 * @package core_competency 21 * @copyright 2015 Frédéric Massart - FMCorz.net 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 namespace core_competency; 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 use core_competency\plan; 29 use core_competency\url; 30 use core_competency\user_competency; 31 32 global $CFG; 33 34 /** 35 * Lib testcase. 36 * 37 * @package core_competency 38 * @copyright 2015 Frédéric Massart - FMCorz.net 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class lib_test extends \advanced_testcase { 42 43 public function test_comment_add_user_competency() { 44 global $DB, $PAGE; 45 $this->resetAfterTest(); 46 $dg = $this->getDataGenerator(); 47 $lpg = $dg->get_plugin_generator('core_competency'); 48 49 $u1 = $dg->create_user(['picture' => 1]); 50 $u2 = $dg->create_user(); 51 $u3 = $dg->create_user(); 52 $reviewerroleid = $dg->create_role(); 53 assign_capability('moodle/competency:planview', CAP_ALLOW, $reviewerroleid, \context_system::instance()->id, true); 54 assign_capability('moodle/competency:usercompetencycomment', CAP_ALLOW, $reviewerroleid, 55 \context_system::instance()->id, true); 56 $dg->role_assign($reviewerroleid, $u2->id, \context_user::instance($u1->id)); 57 $dg->role_assign($reviewerroleid, $u3->id, \context_user::instance($u1->id)); 58 accesslib_clear_all_caches_for_unit_testing(); 59 60 $f1 = $lpg->create_framework(); 61 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); // In 1 plan. 62 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); // In 2 plans. 63 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); // Orphan. 64 65 $p1 = $lpg->create_plan(array('userid' => $u1->id)); 66 $lpg->create_plan_competency(array('planid' => $p1->get('id'), 'competencyid' => $c1->get('id'))); 67 $lpg->create_plan_competency(array('planid' => $p1->get('id'), 'competencyid' => $c2->get('id'))); 68 $p2 = $lpg->create_plan(array('userid' => $u1->id)); 69 $lpg->create_plan_competency(array('planid' => $p2->get('id'), 'competencyid' => $c2->get('id'))); 70 71 $DB->set_field(plan::TABLE, 'timemodified', 1, array('id' => $p1->get('id'))); // Make plan 1 appear as old. 72 $p1->read(); 73 74 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c1->get('id'), 75 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id)); 76 $uc2 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c2->get('id'), 77 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id)); 78 $uc3 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c3->get('id'), 79 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id)); 80 81 // Post a comment for the user competency being in one plan. The reviewer is messaged. 82 $this->setUser($u1); 83 $comment = $uc1->get_comment_object(); 84 $sink = $this->redirectMessages(); 85 $comment->add('Hello world!'); 86 $messages = $sink->get_messages(); 87 $sink->close(); 88 $this->assertCount(1, $messages); 89 $message = array_pop($messages); 90 91 $expectedurlname = $c1->get('shortname'); 92 $expectedurl = url::user_competency_in_plan($u1->id, $c1->get('id'), $p1->get('id')); 93 $this->assertEquals(\core_user::get_noreply_user()->id, $message->useridfrom); 94 $this->assertEquals($u2->id, $message->useridto); 95 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false); 96 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false); 97 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat); 98 $this->assertEquals($expectedurl->out(false), $message->contexturl); 99 $this->assertEquals($expectedurlname, $message->contexturlname); 100 // Test customdata. 101 $customdata = json_decode($message->customdata); 102 $this->assertObjectHasAttribute('notificationiconurl', $customdata); 103 $this->assertStringContainsString('tokenpluginfile.php', $customdata->notificationiconurl); 104 $userpicture = new \user_picture($u1); 105 $userpicture->size = 1; // Use f1 size. 106 $userpicture->includetoken = $u2->id; 107 $this->assertEquals($userpicture->get_url($PAGE)->out(false), $customdata->notificationiconurl); 108 109 // Reviewer posts a comment for the user competency being in two plans. Owner is messaged. 110 $this->setUser($u2); 111 $comment = $uc2->get_comment_object(); 112 $sink = $this->redirectMessages(); 113 $comment->add('Hello world!'); 114 $messages = $sink->get_messages(); 115 $sink->close(); 116 $this->assertCount(1, $messages); 117 $message = array_pop($messages); 118 119 $expectedurlname = $c2->get('shortname'); 120 $expectedurl = url::user_competency_in_plan($u1->id, $c2->get('id'), $p2->get('id')); 121 $this->assertEquals(\core_user::get_noreply_user()->id, $message->useridfrom); 122 $this->assertEquals($u1->id, $message->useridto); 123 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false); 124 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false); 125 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat); 126 $this->assertEquals($expectedurl->out(false), $message->contexturl); 127 $this->assertEquals($expectedurlname, $message->contexturlname); 128 129 // Reviewer posts a comment for the user competency being in no plans. User is messaged. 130 $this->setUser($u2); 131 $comment = $uc3->get_comment_object(); 132 $sink = $this->redirectMessages(); 133 $comment->add('Hello world!'); 134 $messages = $sink->get_messages(); 135 $sink->close(); 136 $this->assertCount(1, $messages); 137 $message = array_pop($messages); 138 139 $expectedurlname = get_string('userplans', 'core_competency'); 140 $expectedurl = url::plans($u1->id); 141 $this->assertEquals(\core_user::get_noreply_user()->id, $message->useridfrom); 142 $this->assertEquals($u1->id, $message->useridto); 143 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false); 144 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false); 145 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat); 146 $this->assertEquals($expectedurl->out(false), $message->contexturl); 147 $this->assertEquals($expectedurlname, $message->contexturlname); 148 149 // A comment is posted by another user, reviewer and owner are messaged. 150 $this->setUser($u3); 151 $comment = $uc3->get_comment_object(); 152 $sink = $this->redirectMessages(); 153 $comment->add('Hello world!'); 154 $messages = $sink->get_messages(); 155 $sink->close(); 156 $this->assertCount(2, $messages); 157 $message1 = array_shift($messages); 158 $message2 = array_shift($messages); 159 $this->assertEquals(\core_user::get_noreply_user()->id, $message->useridfrom); 160 $this->assertEquals($u1->id, $message1->useridto); 161 $this->assertEquals(\core_user::get_noreply_user()->id, $message->useridfrom); 162 $this->assertEquals($u2->id, $message2->useridto); 163 164 // A comment is posted in HTML. 165 $this->setUser($u2); 166 $comment = $uc3->get_comment_object(); 167 $sink = $this->redirectMessages(); 168 $comment->add('<em>Hello world!</em>', FORMAT_HTML); 169 $messages = $sink->get_messages(); 170 $sink->close(); 171 $this->assertCount(1, $messages); 172 $message = array_pop($messages); 173 174 $expectedurlname = get_string('userplans', 'core_competency'); 175 $expectedurl = url::plans($u1->id); 176 $this->assertEquals(\core_user::get_noreply_user()->id, $message->useridfrom); 177 $this->assertEquals($u1->id, $message->useridto); 178 $this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false); 179 $this->assertTrue(strpos($message->fullmessagehtml, '<em>Hello world!</em>') !== false); 180 $this->assertEquals(FORMAT_HTML, $message->fullmessageformat); 181 $this->assertEquals($expectedurl->out(false), $message->contexturl); 182 $this->assertEquals($expectedurlname, $message->contexturlname); 183 } 184 185 /** 186 * Commenting on a plan. 187 */ 188 public function test_comment_add_plan() { 189 $this->resetAfterTest(); 190 $dg = $this->getDataGenerator(); 191 $lpg = $dg->get_plugin_generator('core_competency'); 192 193 $u1 = $dg->create_user(); 194 $u2 = $dg->create_user(); 195 $u3 = $dg->create_user(); 196 $userroleid = $dg->create_role(); 197 $reviewerroleid = $dg->create_role(); 198 assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $userroleid, \context_system::instance()->id, true); 199 assign_capability('moodle/competency:planviewown', CAP_ALLOW, $userroleid, \context_system::instance()->id, true); 200 assign_capability('moodle/competency:planviewdraft', CAP_ALLOW, $reviewerroleid, \context_system::instance()->id, true); 201 assign_capability('moodle/competency:planmanage', CAP_ALLOW, $reviewerroleid, \context_system::instance()->id, true); 202 assign_capability('moodle/competency:plancomment', CAP_ALLOW, $reviewerroleid, \context_system::instance()->id, true); 203 $dg->role_assign($userroleid, $u1->id, \context_user::instance($u1->id)); 204 $dg->role_assign($reviewerroleid, $u2->id, \context_user::instance($u1->id)); 205 $dg->role_assign($reviewerroleid, $u3->id, \context_system::instance()); 206 accesslib_clear_all_caches_for_unit_testing(); 207 208 $p1 = $lpg->create_plan(array('userid' => $u1->id)); 209 210 // Post a comment in own plan, no reviewer. Nobody is messaged. 211 $this->setUser($u1); 212 $comment = $p1->get_comment_object(); 213 $sink = $this->redirectMessages(); 214 $comment->add('Hello world!'); 215 $messages = $sink->get_messages(); 216 $sink->close(); 217 $this->assertCount(0, $messages); 218 219 // Post a comment in plan as someone else, no reviewer. The owner is messages. 220 $this->setUser($u3); 221 $comment = $p1->get_comment_object(); 222 $sink = $this->redirectMessages(); 223 $comment->add('Hello world!'); 224 $messages = $sink->get_messages(); 225 $sink->close(); 226 $this->assertCount(1, $messages); 227 $message = array_pop($messages); 228 $this->assertEquals(\core_user::get_noreply_user()->id, $message->useridfrom); 229 $this->assertEquals($u1->id, $message->useridto); 230 // Test customdata. 231 $customdata = json_decode($message->customdata); 232 $this->assertObjectHasAttribute('notificationiconurl', $customdata); 233 234 // Post a comment in a plan with reviewer. The reviewer is messaged. 235 $p1->set('reviewerid', $u2->id); 236 $p1->update(); 237 $this->setUser($u1); 238 $comment = $p1->get_comment_object(); 239 $sink = $this->redirectMessages(); 240 $comment->add('Hello world!'); 241 $messages = $sink->get_messages(); 242 $sink->close(); 243 $this->assertCount(1, $messages); 244 $message = array_pop($messages); 245 $this->assertEquals(\core_user::get_noreply_user()->id, $message->useridfrom); 246 $this->assertEquals($u2->id, $message->useridto); 247 248 // Post a comment as reviewer in a plan being reviewed. The owner is messaged. 249 $p1->set('reviewerid', $u2->id); 250 $p1->update(); 251 $this->setUser($u2); 252 $comment = $p1->get_comment_object(); 253 $sink = $this->redirectMessages(); 254 $comment->add('Hello world!'); 255 $messages = $sink->get_messages(); 256 $sink->close(); 257 $this->assertCount(1, $messages); 258 $message = array_pop($messages); 259 $this->assertEquals(\core_user::get_noreply_user()->id, $message->useridfrom); 260 $this->assertEquals($u1->id, $message->useridto); 261 262 // Post a comment as someone else in a plan being reviewed. The owner and reviewer are messaged. 263 $p1->set('reviewerid', $u2->id); 264 $p1->update(); 265 $this->setUser($u3); 266 $comment = $p1->get_comment_object(); 267 $sink = $this->redirectMessages(); 268 $comment->add('Hello world!'); 269 $messages = $sink->get_messages(); 270 $sink->close(); 271 $this->assertCount(2, $messages); 272 $message1 = array_shift($messages); 273 $message2 = array_shift($messages); 274 $this->assertEquals(\core_user::get_noreply_user()->id, $message1->useridfrom); 275 $this->assertEquals($u1->id, $message1->useridto); 276 $this->assertEquals(\core_user::get_noreply_user()->id, $message2->useridfrom); 277 $this->assertEquals($u2->id, $message2->useridto); 278 279 $p1->set('reviewerid', null); 280 $p1->update(); 281 282 // Test message content. 283 $this->setUser($u3); 284 $comment = $p1->get_comment_object(); 285 $sink = $this->redirectMessages(); 286 $comment->add('Hello world!'); 287 $messages = $sink->get_messages(); 288 $sink->close(); 289 $this->assertCount(1, $messages); 290 $message = array_pop($messages); 291 292 $expectedurlname = $p1->get('name'); 293 $expectedurl = url::plan($p1->get('id')); 294 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false); 295 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false); 296 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat); 297 $this->assertEquals($expectedurl->out(false), $message->contexturl); 298 $this->assertEquals($expectedurlname, $message->contexturlname); 299 300 // Test message content as HTML. 301 $this->setUser($u3); 302 $comment = $p1->get_comment_object(); 303 $sink = $this->redirectMessages(); 304 $comment->add('<em>Hello world!</em>', FORMAT_HTML); 305 $messages = $sink->get_messages(); 306 $sink->close(); 307 $this->assertCount(1, $messages); 308 $message = array_pop($messages); 309 310 $expectedurlname = $p1->get('name'); 311 $expectedurl = url::plan($p1->get('id')); 312 $this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false); 313 $this->assertTrue(strpos($message->fullmessagehtml, '<em>Hello world!</em>') !== false); 314 $this->assertEquals(FORMAT_HTML, $message->fullmessageformat); 315 $this->assertEquals($expectedurl->out(false), $message->contexturl); 316 $this->assertEquals($expectedurlname, $message->contexturlname); 317 } 318 319 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body