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