Differences Between: [Versions 310 and 311] [Versions 39 and 311]
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 namespace core_user; 18 19 defined('MOODLE_INTERNAL') || die(); 20 global $CFG; 21 require_once($CFG->dirroot . "/user/tests/fixtures/myprofile_fixtures.php"); 22 23 /** 24 * Unit tests for core_user\output\myprofile 25 * 26 * @package core_user 27 * @category test 28 * @copyright 2015 onwards Ankit Agarwal 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later (5) 30 */ 31 class myprofile_test extends \advanced_testcase { 32 /** 33 * Test node::__construct(). 34 */ 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); 46 } 47 48 /** 49 * Test category::node_add(). 50 */ 51 public function test_add_node() { 52 $tree = new \core_user\output\myprofile\tree(); 53 $category = new \core_user\output\myprofile\category('category', 'categorytitle'); 54 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); 63 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); 71 } 72 73 /** 74 * Test category::__construct(). 75 */ 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); 82 } 83 84 public function test_validate_after_order1() { 85 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 86 87 // Create nodes. 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); 91 92 $category->add_node($node3); 93 $category->add_node($node2); 94 $category->add_node($node1); 95 96 $this->expectException(\coding_exception::class); 97 $category->validate_after_order(); 98 99 } 100 101 public function test_validate_after_order2() { 102 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 103 104 // Create nodes. 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); 108 109 $category->add_node($node3); 110 $category->add_node($node2); 111 $category->add_node($node1); 112 113 $this->expectException(\coding_exception::class); 114 $category->validate_after_order(); 115 116 } 117 118 /** 119 * Test category::find_nodes_after(). 120 */ 121 public function test_find_nodes_after() { 122 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 123 124 // Create nodes. 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'); 131 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); 139 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); 153 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); 159 } 160 161 /** 162 * Test category::sort_nodes(). 163 */ 164 public function test_sort_nodes1() { 165 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 166 167 // Create nodes. 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'); 174 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); 182 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); 199 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); 205 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(); 211 } 212 213 /** 214 * Test category::sort_nodes() with a mix of content and non content nodes. 215 */ 216 public function test_sort_nodes2() { 217 $category = new \phpunit_fixture_myprofile_category('category', 'title', null); 218 219 // Create nodes. 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'); 226 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); 234 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); 251 } 252 253 /** 254 * Test tree::add_node(). 255 */ 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); 263 264 // Can't add node with same name. 265 $this->expectException(\coding_exception::class); 266 $tree->add_node($node1); 267 } 268 269 /** 270 * Test tree::add_category(). 271 */ 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); 279 280 // Can't add node with same name. 281 $this->expectException(\coding_exception::class); 282 $tree->add_category($category1); 283 } 284 285 /** 286 * Test tree::find_categories_after(). 287 */ 288 public function test_find_categories_after() { 289 $tree = new \phpunit_fixture_myprofile_tree('category', 'title', null); 290 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'); 298 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); 306 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); 320 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); 326 } 327 328 /** 329 * Test tree::sort_categories(). 330 */ 331 public function test_sort_categories() { 332 $tree = new \phpunit_fixture_myprofile_tree('category', 'title', null); 333 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'); 341 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); 349 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); 366 367 // Can't add category with same name. 368 $this->expectException(\coding_exception::class); 369 $tree->add_category($category1); 370 } 371 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body