2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 defined('MOODLE_INTERNAL') ||
die();
21 require_once($CFG->dirroot
. "/user/tests/fixtures/myprofile_fixtures.php");
24 * Unit tests for core_user\output\myprofile
28 * @copyright 2015 onwards Ankit Agarwal
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5)
31 class myprofile_test
extends \advanced_testcase
{
33 * Test node::__construct().
35 public function test_node__construct() {
36 $node = new \core_user\output\myprofile\node
('parentcat', 'nodename',
37 'nodetitle', 'after', 'www.google.com', 'description', new \
pix_icon('i/course', ''), 'class1 class2');
38 $this->assertSame('parentcat', $node->parentcat
);
39 $this->assertSame('nodename', $node->name
);
40 $this->assertSame('nodetitle', $node->title
);
41 $this->assertSame('after', $node->after
);
42 $url = new \
moodle_url('www.google.com');
43 $this->assertEquals($url, $node->url
);
44 $this->assertEquals(new \
pix_icon('i/course', ''), $node->icon
);
45 $this->assertSame('class1 class2', $node->classes
);
49 * Test category::node_add().
51 public function test_add_node() {
52 $tree = new \core_user\output\myprofile\tree
();
53 $category = new \core_user\output\myprofile\
category('category', 'categorytitle');
55 $node = new \core_user\output\myprofile\node
('category', 'nodename',
56 'nodetitle', null, 'www.iAmaZombie.com', 'description');
57 $category->add_node($node);
58 $this->assertCount(1, $category->nodes
);
59 $node = new \core_user\output\myprofile\node
('category', 'nodename2',
60 'nodetitle', null, 'www.WorldisGonnaEnd.com', 'description');
61 $category->add_node($node);
62 $this->assertCount(2, $category->nodes
);
64 $node = new \core_user\output\myprofile\node
('category', 'nodename3',
65 'nodetitle', null, 'www.TeamBeardsFTW.com', 'description');
66 $tree->add_node($node);
67 $tree->add_category($category);
68 $tree->sort_categories();
69 $category = $tree->categories
['category'];
70 $this->assertCount(3, $category->nodes
);
74 * Test category::__construct().
76 public function test_category__construct() {
77 $category = new \core_user\output\myprofile\
category('categoryname', 'title', 'after', 'class1 class2');
78 $this->assertSame('categoryname', $category->name
);
79 $this->assertSame('title', $category->title
);
80 $this->assertSame('after', $category->after
);
81 $this->assertSame('class1 class2', $category->classes
);
84 public function test_validate_after_order1() {
85 $category = new \
phpunit_fixture_myprofile_category('category', 'title', null);
88 $node1 = new \core_user\output\myprofile\node
('category', 'node1', 'nodetitle', null, null, 'content');
89 $node2 = new \core_user\output\myprofile\node
('category', 'node2', 'nodetitle', 'node1', null, 'content');
90 $node3 = new \core_user\output\myprofile\node
('category', 'node3', 'nodetitle', 'node2', null, null);
92 $category->add_node($node3);
93 $category->add_node($node2);
94 $category->add_node($node1);
96 $this->expectException(\coding_exception
::class);
97 $category->validate_after_order();
101 public function test_validate_after_order2() {
102 $category = new \
phpunit_fixture_myprofile_category('category', 'title', null);
105 $node1 = new \core_user\output\myprofile\node
('category', 'node1', 'nodetitle', null, null, null);
106 $node2 = new \core_user\output\myprofile\node
('category', 'node2', 'nodetitle', 'node1', null, 'content');
107 $node3 = new \core_user\output\myprofile\node
('category', 'node3', 'nodetitle', 'node2', null, null);
109 $category->add_node($node3);
110 $category->add_node($node2);
111 $category->add_node($node1);
113 $this->expectException(\coding_exception
::class);
114 $category->validate_after_order();
119 * Test category::find_nodes_after().
121 public function test_find_nodes_after() {
122 $category = new \
phpunit_fixture_myprofile_category('category', 'title', null);
125 $node1 = new \core_user\output\myprofile\node
('category', 'node1', 'nodetitle', null);
126 $node2 = new \core_user\output\myprofile\node
('category', 'node2', 'nodetitle', 'node1');
127 $node3 = new \core_user\output\myprofile\node
('category', 'node3', 'nodetitle', 'node2');
128 $node4 = new \core_user\output\myprofile\node
('category', 'node4', 'nodetitle', 'node3');
129 $node5 = new \core_user\output\myprofile\node
('category', 'node5', 'nodetitle', 'node3');
130 $node6 = new \core_user\output\myprofile\node
('category', 'node6', 'nodetitle', 'node1');
132 // Add the nodes in random order.
133 $category->add_node($node3);
134 $category->add_node($node2);
135 $category->add_node($node4);
136 $category->add_node($node1);
137 $category->add_node($node5);
138 $category->add_node($node6);
140 // After node 1 we should have node2 - node3 - node4 - node5 - node6.
141 $return = $category->find_nodes_after($node1);
142 $this->assertCount(5, $return);
143 $node = array_shift($return);
144 $this->assertEquals($node2, $node);
145 $node = array_shift($return);
146 $this->assertEquals($node3, $node);
147 $node = array_shift($return);
148 $this->assertEquals($node4, $node);
149 $node = array_shift($return);
150 $this->assertEquals($node5, $node);
151 $node = array_shift($return);
152 $this->assertEquals($node6, $node);
154 // Last check also verifies calls for all subsequent nodes, still do some random checking.
155 $return = $category->find_nodes_after($node6);
156 $this->assertCount(0, $return);
157 $return = $category->find_nodes_after($node3);
158 $this->assertCount(2, $return);
162 * Test category::sort_nodes().
164 public function test_sort_nodes1() {
165 $category = new \
phpunit_fixture_myprofile_category('category', 'title', null);
168 $node1 = new \core_user\output\myprofile\node
('category', 'node1', 'nodetitle', null);
169 $node2 = new \core_user\output\myprofile\node
('category', 'node2', 'nodetitle', 'node1');
170 $node3 = new \core_user\output\myprofile\node
('category', 'node3', 'nodetitle', 'node2');
171 $node4 = new \core_user\output\myprofile\node
('category', 'node4', 'nodetitle', 'node3');
172 $node5 = new \core_user\output\myprofile\node
('category', 'node5', 'nodetitle', 'node3');
173 $node6 = new \core_user\output\myprofile\node
('category', 'node6', 'nodetitle', 'node1');
175 // Add the nodes in random order.
176 $category->add_node($node3);
177 $category->add_node($node2);
178 $category->add_node($node4);
179 $category->add_node($node1);
180 $category->add_node($node5);
181 $category->add_node($node6);
183 // After node 1 we should have node2 - node3 - node4 - node5 - node6.
184 $category->sort_nodes();
185 $nodes = $category->nodes
;
186 $this->assertCount(6, $nodes);
187 $node = array_shift($nodes);
188 $this->assertEquals($node1, $node);
189 $node = array_shift($nodes);
190 $this->assertEquals($node2, $node);
191 $node = array_shift($nodes);
192 $this->assertEquals($node3, $node);
193 $node = array_shift($nodes);
194 $this->assertEquals($node4, $node);
195 $node = array_shift($nodes);
196 $this->assertEquals($node5, $node);
197 $node = array_shift($nodes);
198 $this->assertEquals($node6, $node);
200 // Last check also verifies calls for all subsequent nodes, still do some random checking.
201 $return = $category->find_nodes_after($node6);
202 $this->assertCount(0, $return);
203 $return = $category->find_nodes_after($node3);
204 $this->assertCount(2, $return);
206 // Add a node with invalid 'after' and make sure an exception is thrown.
207 $node7 = new \core_user\output\myprofile\node
('category', 'node7', 'nodetitle', 'noderandom');
208 $category->add_node($node7);
209 $this->expectException(\coding_exception
::class);
210 $category->sort_nodes();
214 * Test category::sort_nodes() with a mix of content and non content nodes.
216 public function test_sort_nodes2() {
217 $category = new \
phpunit_fixture_myprofile_category('category', 'title', null);
220 $node1 = new \core_user\output\myprofile\node
('category', 'node1', 'nodetitle', null, null, 'content');
221 $node2 = new \core_user\output\myprofile\node
('category', 'node2', 'nodetitle', 'node1', null, 'content');
222 $node3 = new \core_user\output\myprofile\node
('category', 'node3', 'nodetitle', null);
223 $node4 = new \core_user\output\myprofile\node
('category', 'node4', 'nodetitle', 'node3');
224 $node5 = new \core_user\output\myprofile\node
('category', 'node5', 'nodetitle', 'node3');
225 $node6 = new \core_user\output\myprofile\node
('category', 'node6', 'nodetitle', 'node1', null, 'content');
227 // Add the nodes in random order.
228 $category->add_node($node3);
229 $category->add_node($node2);
230 $category->add_node($node4);
231 $category->add_node($node1);
232 $category->add_node($node5);
233 $category->add_node($node6);
235 // After node 1 we should have node2 - node6 - node3 - node4 - node5.
236 $category->sort_nodes();
237 $nodes = $category->nodes
;
238 $this->assertCount(6, $nodes);
239 $node = array_shift($nodes);
240 $this->assertEquals($node1, $node);
241 $node = array_shift($nodes);
242 $this->assertEquals($node2, $node);
243 $node = array_shift($nodes);
244 $this->assertEquals($node6, $node);
245 $node = array_shift($nodes);
246 $this->assertEquals($node3, $node);
247 $node = array_shift($nodes);
248 $this->assertEquals($node4, $node);
249 $node = array_shift($nodes);
250 $this->assertEquals($node5, $node);
254 * Test tree::add_node().
256 public function test_tree_add_node() {
257 $tree = new \
phpunit_fixture_myprofile_tree();
258 $node1 = new \core_user\output\myprofile\node
('category', 'node1', 'nodetitle');
259 $tree->add_node($node1);
260 $nodes = $tree->nodes
;
261 $node = array_shift($nodes);
262 $this->assertEquals($node1, $node);
264 // Can't add node with same name.
265 $this->expectException(\coding_exception
::class);
266 $tree->add_node($node1);
270 * Test tree::add_category().
272 public function test_tree_add_category() {
273 $tree = new \
phpunit_fixture_myprofile_tree();
274 $category1 = new \core_user\output\myprofile\
category('category', 'title');
275 $tree->add_category($category1);
276 $categories = $tree->categories
;
277 $category = array_shift($categories);
278 $this->assertEquals($category1, $category);
280 // Can't add node with same name.
281 $this->expectException(\coding_exception
::class);
282 $tree->add_category($category1);
286 * Test tree::find_categories_after().
288 public function test_find_categories_after() {
289 $tree = new \
phpunit_fixture_myprofile_tree('category', 'title', null);
291 // Create categories.
292 $category1 = new \core_user\output\myprofile\
category('category1', 'category1', null);
293 $category2 = new \core_user\output\myprofile\
category('category2', 'category2', 'category1');
294 $category3 = new \core_user\output\myprofile\
category('category3', 'category3', 'category2');
295 $category4 = new \core_user\output\myprofile\
category('category4', 'category4', 'category3');
296 $category5 = new \core_user\output\myprofile\
category('category5', 'category5', 'category3');
297 $category6 = new \core_user\output\myprofile\
category('category6', 'category6', 'category1');
299 // Add the categories in random order.
300 $tree->add_category($category3);
301 $tree->add_category($category2);
302 $tree->add_category($category4);
303 $tree->add_category($category1);
304 $tree->add_category($category5);
305 $tree->add_category($category6);
307 // After category 1 we should have category2 - category3 - category4 - category5 - category6.
308 $return = $tree->find_categories_after($category1);
309 $this->assertCount(5, $return);
310 $category = array_shift($return);
311 $this->assertEquals($category2, $category);
312 $category = array_shift($return);
313 $this->assertEquals($category3, $category);
314 $category = array_shift($return);
315 $this->assertEquals($category4, $category);
316 $category = array_shift($return);
317 $this->assertEquals($category5, $category);
318 $category = array_shift($return);
319 $this->assertEquals($category6, $category);
321 // Last check also verifies calls for all subsequent categories, still do some random checking.
322 $return = $tree->find_categories_after($category6);
323 $this->assertCount(0, $return);
324 $return = $tree->find_categories_after($category3);
325 $this->assertCount(2, $return);
329 * Test tree::sort_categories().
331 public function test_sort_categories() {
332 $tree = new \
phpunit_fixture_myprofile_tree('category', 'title', null);
334 // Create categories.
335 $category1 = new \core_user\output\myprofile\
category('category1', 'category1', null);
336 $category2 = new \core_user\output\myprofile\
category('category2', 'category2', 'category1');
337 $category3 = new \core_user\output\myprofile\
category('category3', 'category3', 'category2');
338 $category4 = new \core_user\output\myprofile\
category('category4', 'category4', 'category3');
339 $category5 = new \core_user\output\myprofile\
category('category5', 'category5', 'category3');
340 $category6 = new \core_user\output\myprofile\
category('category6', 'category6', 'category1');
342 // Add the categories in random order.
343 $tree->add_category($category3);
344 $tree->add_category($category2);
345 $tree->add_category($category4);
346 $tree->add_category($category1);
347 $tree->add_category($category5);
348 $tree->add_category($category6);
350 // After category 1 we should have category2 - category3 - category4 - category5 - category6.
351 $tree->sort_categories();
352 $categories = $tree->categories
;
353 $this->assertCount(6, $categories);
354 $category = array_shift($categories);
355 $this->assertEquals($category1, $category);
356 $category = array_shift($categories);
357 $this->assertEquals($category2, $category);
358 $category = array_shift($categories);
359 $this->assertEquals($category3, $category);
360 $category = array_shift($categories);
361 $this->assertEquals($category4, $category);
362 $category = array_shift($categories);
363 $this->assertEquals($category5, $category);
364 $category = array_shift($categories);
365 $this->assertEquals($category6, $category);
367 // Can't add category with same name.
368 $this->expectException(\coding_exception
::class);
369 $tree->add_category($category1);