MDL-51036 report_competency: Invalid reference to scale when null
[moodle.git] / admin / tool / lp / tests / lib_test.php
CommitLineData
6cc8480a
FM
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 tool_lp
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
25defined('MOODLE_INTERNAL') || die();
26use tool_lp\user_competency;
27
28global $CFG;
29
30/**
31 * Lib testcase.
32 *
33 * @package tool_lp
34 * @copyright 2015 Frédéric Massart - FMCorz.net
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class tool_lp_lib_testcase extends advanced_testcase {
38
39 public function test_comment_add_user_competency() {
40 $this->resetAfterTest();
41 $dg = $this->getDataGenerator();
42 $lpg = $dg->get_plugin_generator('tool_lp');
43
44 $u1 = $dg->create_user();
45 $u2 = $dg->create_user();
46 $u3 = $dg->create_user();
47 $reviewerroleid = $dg->create_role();
48 assign_capability('tool/lp:planview', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
943989c2 49 assign_capability('tool/lp:usercompetencycomment', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
6cc8480a
FM
50 $dg->role_assign($reviewerroleid, $u2->id, context_user::instance($u1->id));
51 $dg->role_assign($reviewerroleid, $u3->id, context_user::instance($u1->id));
52 accesslib_clear_all_caches_for_unit_testing();
53
54 $f1 = $lpg->create_framework();
55 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // In 1 plan.
56 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // In 2 plans.
57 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // Orphan.
58
59 $p1 = $lpg->create_plan(array('userid' => $u1->id));
60 $lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c1->get_id()));
61 $lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c2->get_id()));
62 $p2 = $lpg->create_plan(array('userid' => $u1->id));
63 $lpg->create_plan_competency(array('planid' => $p2->get_id(), 'competencyid' => $c2->get_id()));
64
65 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c1->get_id(),
66 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
67 $uc2 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c2->get_id(),
68 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
69 $uc3 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c3->get_id(),
70 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
71
72 // Post a comment for the user competency being in one plan. The reviewer is messaged.
73 $this->setUser($u1);
74 $comment = $uc1->get_comment_object();
75 $sink = $this->redirectMessages();
76 $comment->add('Hello world!');
77 $messages = $sink->get_messages();
78 $sink->close();
79 $this->assertCount(1, $messages);
80 $message = array_pop($messages);
81
82 $expectedurlname = $c1->get_shortname();
83 $expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
84 'userid' => $u1->id,
85 'competencyid' => $c1->get_id(),
86 'planid' => $p1->get_id()
87 ));
88 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
89 $this->assertEquals($u2->id, $message->useridto);
90 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
91 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
92 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
93 $this->assertEquals($expectedurl->out(false), $message->contexturl);
94 $this->assertEquals($expectedurlname, $message->contexturlname);
95
96 // Reviewer posts a comment for the user competency being in two plans. Owner is messaged.
97 $this->setUser($u2);
98 $comment = $uc2->get_comment_object();
99 $sink = $this->redirectMessages();
100 $comment->add('Hello world!');
101 $messages = $sink->get_messages();
102 $sink->close();
103 $this->assertCount(1, $messages);
104 $message = array_pop($messages);
105
106 $expectedurlname = $c2->get_shortname();
107 $expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
108 'userid' => $u1->id,
109 'competencyid' => $c2->get_id(),
110 'planid' => $p1->get_id()
111 ));
112 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
113 $this->assertEquals($u1->id, $message->useridto);
114 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
115 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
116 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
117 $this->assertEquals($expectedurl->out(false), $message->contexturl);
118 $this->assertEquals($expectedurlname, $message->contexturlname);
119
120 // Reviewer posts a comment for the user competency being in no plans. User is messaged.
121 $this->setUser($u2);
122 $comment = $uc3->get_comment_object();
123 $sink = $this->redirectMessages();
124 $comment->add('Hello world!');
125 $messages = $sink->get_messages();
126 $sink->close();
127 $this->assertCount(1, $messages);
128 $message = array_pop($messages);
129
130 $expectedurlname = get_string('userplans', 'tool_lp');
131 $expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
132 'userid' => $u1->id,
133 ));
134 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
135 $this->assertEquals($u1->id, $message->useridto);
136 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
137 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
138 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
139 $this->assertEquals($expectedurl->out(false), $message->contexturl);
140 $this->assertEquals($expectedurlname, $message->contexturlname);
141
142 // A comment is posted by another user, reviewer and owner are messaged.
143 $this->setUser($u3);
144 $comment = $uc3->get_comment_object();
145 $sink = $this->redirectMessages();
146 $comment->add('Hello world!');
147 $messages = $sink->get_messages();
148 $sink->close();
149 $this->assertCount(2, $messages);
150 $message1 = array_shift($messages);
151 $message2 = array_shift($messages);
152 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
153 $this->assertEquals($u1->id, $message1->useridto);
154 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
155 $this->assertEquals($u2->id, $message2->useridto);
156
157 // A comment is posted in HTML.
158 $this->setUser($u2);
159 $comment = $uc3->get_comment_object();
160 $sink = $this->redirectMessages();
161 $comment->add('<em>Hello world!</em>', FORMAT_HTML);
162 $messages = $sink->get_messages();
163 $sink->close();
164 $this->assertCount(1, $messages);
165 $message = array_pop($messages);
166
167 $expectedurlname = get_string('userplans', 'tool_lp');
168 $expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
169 'userid' => $u1->id,
170 ));
171 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
172 $this->assertEquals($u1->id, $message->useridto);
173 $this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false);
174 $this->assertTrue(strpos($message->fullmessagehtml, '<em>Hello world!</em>') !== false);
175 $this->assertEquals(FORMAT_HTML, $message->fullmessageformat);
176 $this->assertEquals($expectedurl->out(false), $message->contexturl);
177 $this->assertEquals($expectedurlname, $message->contexturlname);
178 }
179
180}