See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 * Unit tests for core_user\output\myprofile 19 * 20 * @package core_user 21 * @category test 22 * @copyright 2015 onwards Ankit Agarwal 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5) 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 global $CFG; 28 require_once($CFG->dirroot . "/user/tests/fixtures/myprofile_fixtures.php"); 29 30 /** 31 * Unit tests for core_user\output\myprofile 32 * 33 * @package core_user 34 * @category test 35 * @copyright 2015 onwards Ankit Agarwal 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5) 37 */ 38 class core_user_myprofile_testcase extends advanced_testcase { 39 /** 40 * Test node::__construct(). 41 */ 42 public function test_node__construct() { 43 $node = new \core_user\output\myprofile\node('parentcat', 'nodename', 44 'nodetitle', 'after', 'www.google.com', 'description', new pix_icon('i/course', ''), 'class1 class2'); 45 $this->assertSame('parentcat', $node->parentcat); 46 $this->assertSame('nodename', $node->name); 47 $this->assertSame('nodetitle', $node->title); 48 $this->assertSame('after', $node->after); 49 $url = new moodle_url('www.google.com'); 50 $this->assertEquals($url, $node->url); 51 $this->assertEquals(new pix_icon('i/course', ''), $node->icon); 52 $this->assertSame('class1 class2', $node->classes); 53 } 54 55 /** 56 * Test category::node_add(). 57 */ 58 public function test_add_node() { 59 $tree = new \core_user\output\myprofile\tree(); 60 $category = new \core_user\output\myprofile\category('category', 'categorytitle'); 61 62 $node = new \core_user\output\myprofile\node('category', 'nodename', 63 'nodetitle', null, 'www.iAmaZombie.com', 'description'); 64 $category->add_node($node); 65 $this->assertCount(1, $category->nodes); 66 $node = new \core_user\output\myprofile\node('category', 'nodename2', 67 'nodetitle', null, 'www.WorldisGonnaEnd.com', 'description'); 68 $category->add_node($node); 69 $this->assertCount(2, $category->nodes); 70 71 $node = new \core_user\output\myprofile\node('category', 'nodename3', 72 'nodetitle', null, 'www.TeamBeardsFTW.com', 'description'); 73 $tree->add_node($node); 74 $tree->add_category($category); 75 $tree->sort_categories(); 76 $category = $tree->categories['category']; 77 $this->assertCount(3, $category->nodes); 78 } 79 80 /** 81 * Test category::__construct(). 82 */ 83 public function test_category__construct() { 84 $category = new \core_user\output\myprofile\category('categoryname', 'title', 'after', 'class1 class2'); 85 $this->assertSame('categoryname', $category->name); 86 $this->assertSame('title', $category->title); 87 $this->assertSame('after', $category->after); 88 $this->assertSame('class1 class2', $category->classes); 89 } 90 91 /** 92 * @expectedException coding_exception 93 */ 94 public function test_validate_after_order1() { 95 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 96 97 // Create nodes. 98 $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null, null, 'content'); 99 $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1', null, 'content'); 100 $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', 'node2', null, null); 101 102 $category->add_node($node3); 103 $category->add_node($node2); 104 $category->add_node($node1); 105 106 $category->validate_after_order(); 107 108 } 109 110 /** 111 * @expectedException coding_exception 112 */ 113 public function test_validate_after_order2() { 114 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 115 116 // Create nodes. 117 $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null, null, null); 118 $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1', null, 'content'); 119 $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', 'node2', null, null); 120 121 $category->add_node($node3); 122 $category->add_node($node2); 123 $category->add_node($node1); 124 125 $category->validate_after_order(); 126 127 } 128 129 /** 130 * Test category::find_nodes_after(). 131 */ 132 public function test_find_nodes_after() { 133 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 134 135 // Create nodes. 136 $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null); 137 $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1'); 138 $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', 'node2'); 139 $node4 = new \core_user\output\myprofile\node('category', 'node4', 'nodetitle', 'node3'); 140 $node5 = new \core_user\output\myprofile\node('category', 'node5', 'nodetitle', 'node3'); 141 $node6 = new \core_user\output\myprofile\node('category', 'node6', 'nodetitle', 'node1'); 142 143 // Add the nodes in random order. 144 $category->add_node($node3); 145 $category->add_node($node2); 146 $category->add_node($node4); 147 $category->add_node($node1); 148 $category->add_node($node5); 149 $category->add_node($node6); 150 151 // After node 1 we should have node2 - node3 - node4 - node5 - node6. 152 $return = $category->find_nodes_after($node1); 153 $this->assertCount(5, $return); 154 $node = array_shift($return); 155 $this->assertEquals($node2, $node); 156 $node = array_shift($return); 157 $this->assertEquals($node3, $node); 158 $node = array_shift($return); 159 $this->assertEquals($node4, $node); 160 $node = array_shift($return); 161 $this->assertEquals($node5, $node); 162 $node = array_shift($return); 163 $this->assertEquals($node6, $node); 164 165 // Last check also verifies calls for all subsequent nodes, still do some random checking. 166 $return = $category->find_nodes_after($node6); 167 $this->assertCount(0, $return); 168 $return = $category->find_nodes_after($node3); 169 $this->assertCount(2, $return); 170 } 171 172 /** 173 * Test category::sort_nodes(). 174 * 175 * @expectedException coding_exception 176 */ 177 public function test_sort_nodes1() { 178 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 179 180 // Create nodes. 181 $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null); 182 $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1'); 183 $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', 'node2'); 184 $node4 = new \core_user\output\myprofile\node('category', 'node4', 'nodetitle', 'node3'); 185 $node5 = new \core_user\output\myprofile\node('category', 'node5', 'nodetitle', 'node3'); 186 $node6 = new \core_user\output\myprofile\node('category', 'node6', 'nodetitle', 'node1'); 187 188 // Add the nodes in random order. 189 $category->add_node($node3); 190 $category->add_node($node2); 191 $category->add_node($node4); 192 $category->add_node($node1); 193 $category->add_node($node5); 194 $category->add_node($node6); 195 196 // After node 1 we should have node2 - node3 - node4 - node5 - node6. 197 $category->sort_nodes(); 198 $nodes = $category->nodes; 199 $this->assertCount(6, $nodes); 200 $node = array_shift($nodes); 201 $this->assertEquals($node1, $node); 202 $node = array_shift($nodes); 203 $this->assertEquals($node2, $node); 204 $node = array_shift($nodes); 205 $this->assertEquals($node3, $node); 206 $node = array_shift($nodes); 207 $this->assertEquals($node4, $node); 208 $node = array_shift($nodes); 209 $this->assertEquals($node5, $node); 210 $node = array_shift($nodes); 211 $this->assertEquals($node6, $node); 212 213 // Last check also verifies calls for all subsequent nodes, still do some random checking. 214 $return = $category->find_nodes_after($node6); 215 $this->assertCount(0, $return); 216 $return = $category->find_nodes_after($node3); 217 $this->assertCount(2, $return); 218 219 // Add a node with invalid 'after' and make sure an exception is thrown. 220 $node7 = new \core_user\output\myprofile\node('category', 'node7', 'nodetitle', 'noderandom'); 221 $category->add_node($node7); 222 $category->sort_nodes(); 223 } 224 225 /** 226 * Test category::sort_nodes() with a mix of content and non content nodes. 227 */ 228 public function test_sort_nodes2() { 229 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 230 231 // Create nodes. 232 $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null, null, 'content'); 233 $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1', null, 'content'); 234 $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', null); 235 $node4 = new \core_user\output\myprofile\node('category', 'node4', 'nodetitle', 'node3'); 236 $node5 = new \core_user\output\myprofile\node('category', 'node5', 'nodetitle', 'node3'); 237 $node6 = new \core_user\output\myprofile\node('category', 'node6', 'nodetitle', 'node1', null, 'content'); 238 239 // Add the nodes in random order. 240 $category->add_node($node3); 241 $category->add_node($node2); 242 $category->add_node($node4); 243 $category->add_node($node1); 244 $category->add_node($node5); 245 $category->add_node($node6); 246 247 // After node 1 we should have node2 - node6 - node3 - node4 - node5. 248 $category->sort_nodes(); 249 $nodes = $category->nodes; 250 $this->assertCount(6, $nodes); 251 $node = array_shift($nodes); 252 $this->assertEquals($node1, $node); 253 $node = array_shift($nodes); 254 $this->assertEquals($node2, $node); 255 $node = array_shift($nodes); 256 $this->assertEquals($node6, $node); 257 $node = array_shift($nodes); 258 $this->assertEquals($node3, $node); 259 $node = array_shift($nodes); 260 $this->assertEquals($node4, $node); 261 $node = array_shift($nodes); 262 $this->assertEquals($node5, $node); 263 } 264 265 /** 266 * Test tree::add_node(). 267 * 268 * @expectedException coding_exception 269 */ 270 public function test_tree_add_node() { 271 $tree = new \phpunit_fixture_myprofile_tree(); 272 $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle'); 273 $tree->add_node($node1); 274 $nodes = $tree->nodes; 275 $node = array_shift($nodes); 276 $this->assertEquals($node1, $node); 277 278 // Can't add node with same name. 279 $tree->add_node($node1); 280 } 281 282 /** 283 * Test tree::add_category(). 284 * 285 * @expectedException coding_exception 286 */ 287 public function test_tree_add_category() { 288 $tree = new \phpunit_fixture_myprofile_tree(); 289 $category1 = new \core_user\output\myprofile\category('category', 'title'); 290 $tree->add_category($category1); 291 $categories = $tree->categories; 292 $category = array_shift($categories); 293 $this->assertEquals($category1, $category); 294 295 // Can't add node with same name. 296 $tree->add_category($category1); 297 } 298 299 /** 300 * Test tree::find_categories_after(). 301 */ 302 public function test_find_categories_after() { 303 $tree = new \phpunit_fixture_myprofile_tree('category', 'title', null); 304 305 // Create categories. 306 $category1 = new \core_user\output\myprofile\category('category1', 'category1', null); 307 $category2 = new \core_user\output\myprofile\category('category2', 'category2', 'category1'); 308 $category3 = new \core_user\output\myprofile\category('category3', 'category3', 'category2'); 309 $category4 = new \core_user\output\myprofile\category('category4', 'category4', 'category3'); 310 $category5 = new \core_user\output\myprofile\category('category5', 'category5', 'category3'); 311 $category6 = new \core_user\output\myprofile\category('category6', 'category6', 'category1'); 312 313 // Add the categories in random order. 314 $tree->add_category($category3); 315 $tree->add_category($category2); 316 $tree->add_category($category4); 317 $tree->add_category($category1); 318 $tree->add_category($category5); 319 $tree->add_category($category6); 320 321 // After category 1 we should have category2 - category3 - category4 - category5 - category6. 322 $return = $tree->find_categories_after($category1); 323 $this->assertCount(5, $return); 324 $category = array_shift($return); 325 $this->assertEquals($category2, $category); 326 $category = array_shift($return); 327 $this->assertEquals($category3, $category); 328 $category = array_shift($return); 329 $this->assertEquals($category4, $category); 330 $category = array_shift($return); 331 $this->assertEquals($category5, $category); 332 $category = array_shift($return); 333 $this->assertEquals($category6, $category); 334 335 // Last check also verifies calls for all subsequent categories, still do some random checking. 336 $return = $tree->find_categories_after($category6); 337 $this->assertCount(0, $return); 338 $return = $tree->find_categories_after($category3); 339 $this->assertCount(2, $return); 340 } 341 342 /** 343 * Test tree::sort_categories(). 344 * 345 * @expectedException coding_exception 346 */ 347 public function test_sort_categories() { 348 $tree = new \phpunit_fixture_myprofile_tree('category', 'title', null); 349 350 // Create categories. 351 $category1 = new \core_user\output\myprofile\category('category1', 'category1', null); 352 $category2 = new \core_user\output\myprofile\category('category2', 'category2', 'category1'); 353 $category3 = new \core_user\output\myprofile\category('category3', 'category3', 'category2'); 354 $category4 = new \core_user\output\myprofile\category('category4', 'category4', 'category3'); 355 $category5 = new \core_user\output\myprofile\category('category5', 'category5', 'category3'); 356 $category6 = new \core_user\output\myprofile\category('category6', 'category6', 'category1'); 357 358 // Add the categories in random order. 359 $tree->add_category($category3); 360 $tree->add_category($category2); 361 $tree->add_category($category4); 362 $tree->add_category($category1); 363 $tree->add_category($category5); 364 $tree->add_category($category6); 365 366 // After category 1 we should have category2 - category3 - category4 - category5 - category6. 367 $tree->sort_categories(); 368 $categories = $tree->categories; 369 $this->assertCount(6, $categories); 370 $category = array_shift($categories); 371 $this->assertEquals($category1, $category); 372 $category = array_shift($categories); 373 $this->assertEquals($category2, $category); 374 $category = array_shift($categories); 375 $this->assertEquals($category3, $category); 376 $category = array_shift($categories); 377 $this->assertEquals($category4, $category); 378 $category = array_shift($categories); 379 $this->assertEquals($category5, $category); 380 $category = array_shift($categories); 381 $this->assertEquals($category6, $category); 382 383 // Can't add category with same name. 384 $tree->add_category($category1); 385 } 386 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body