Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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   * 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      public function test_validate_after_order1() {
  92          $category = new \phpunit_fixture_myprofile_category('category', 'title', null);
  93  
  94          // Create nodes.
  95          $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null, null, 'content');
  96          $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1', null, 'content');
  97          $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', 'node2', null, null);
  98  
  99          $category->add_node($node3);
 100          $category->add_node($node2);
 101          $category->add_node($node1);
 102  
 103          $this->expectException(coding_exception::class);
 104          $category->validate_after_order();
 105  
 106      }
 107  
 108      public function test_validate_after_order2() {
 109          $category = new \phpunit_fixture_myprofile_category('category', 'title', null);
 110  
 111          // Create nodes.
 112          $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null, null, null);
 113          $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1', null, 'content');
 114          $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', 'node2', null, null);
 115  
 116          $category->add_node($node3);
 117          $category->add_node($node2);
 118          $category->add_node($node1);
 119  
 120          $this->expectException(coding_exception::class);
 121          $category->validate_after_order();
 122  
 123      }
 124  
 125      /**
 126       * Test category::find_nodes_after().
 127       */
 128      public function test_find_nodes_after() {
 129          $category = new \phpunit_fixture_myprofile_category('category', 'title', null);
 130  
 131          // Create nodes.
 132          $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null);
 133          $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1');
 134          $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', 'node2');
 135          $node4 = new \core_user\output\myprofile\node('category', 'node4', 'nodetitle', 'node3');
 136          $node5 = new \core_user\output\myprofile\node('category', 'node5', 'nodetitle', 'node3');
 137          $node6 = new \core_user\output\myprofile\node('category', 'node6', 'nodetitle', 'node1');
 138  
 139          // Add the nodes in random order.
 140          $category->add_node($node3);
 141          $category->add_node($node2);
 142          $category->add_node($node4);
 143          $category->add_node($node1);
 144          $category->add_node($node5);
 145          $category->add_node($node6);
 146  
 147          // After node 1 we should have node2 - node3 - node4 - node5 - node6.
 148          $return = $category->find_nodes_after($node1);
 149          $this->assertCount(5, $return);
 150          $node = array_shift($return);
 151          $this->assertEquals($node2, $node);
 152          $node = array_shift($return);
 153          $this->assertEquals($node3, $node);
 154          $node = array_shift($return);
 155          $this->assertEquals($node4, $node);
 156          $node = array_shift($return);
 157          $this->assertEquals($node5, $node);
 158          $node = array_shift($return);
 159          $this->assertEquals($node6, $node);
 160  
 161          // Last check also verifies calls for all subsequent nodes, still do some random checking.
 162          $return = $category->find_nodes_after($node6);
 163          $this->assertCount(0, $return);
 164          $return = $category->find_nodes_after($node3);
 165          $this->assertCount(2, $return);
 166      }
 167  
 168      /**
 169       * Test category::sort_nodes().
 170       */
 171      public function test_sort_nodes1() {
 172          $category = new \phpunit_fixture_myprofile_category('category', 'title', null);
 173  
 174          // Create nodes.
 175          $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null);
 176          $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1');
 177          $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', 'node2');
 178          $node4 = new \core_user\output\myprofile\node('category', 'node4', 'nodetitle', 'node3');
 179          $node5 = new \core_user\output\myprofile\node('category', 'node5', 'nodetitle', 'node3');
 180          $node6 = new \core_user\output\myprofile\node('category', 'node6', 'nodetitle', 'node1');
 181  
 182          // Add the nodes in random order.
 183          $category->add_node($node3);
 184          $category->add_node($node2);
 185          $category->add_node($node4);
 186          $category->add_node($node1);
 187          $category->add_node($node5);
 188          $category->add_node($node6);
 189  
 190          // After node 1 we should have node2 - node3 - node4 - node5 - node6.
 191          $category->sort_nodes();
 192          $nodes = $category->nodes;
 193          $this->assertCount(6, $nodes);
 194          $node = array_shift($nodes);
 195          $this->assertEquals($node1, $node);
 196          $node = array_shift($nodes);
 197          $this->assertEquals($node2, $node);
 198          $node = array_shift($nodes);
 199          $this->assertEquals($node3, $node);
 200          $node = array_shift($nodes);
 201          $this->assertEquals($node4, $node);
 202          $node = array_shift($nodes);
 203          $this->assertEquals($node5, $node);
 204          $node = array_shift($nodes);
 205          $this->assertEquals($node6, $node);
 206  
 207          // Last check also verifies calls for all subsequent nodes, still do some random checking.
 208          $return = $category->find_nodes_after($node6);
 209          $this->assertCount(0, $return);
 210          $return = $category->find_nodes_after($node3);
 211          $this->assertCount(2, $return);
 212  
 213          // Add a node with invalid 'after' and make sure an exception is thrown.
 214          $node7 = new \core_user\output\myprofile\node('category', 'node7', 'nodetitle', 'noderandom');
 215          $category->add_node($node7);
 216          $this->expectException(coding_exception::class);
 217          $category->sort_nodes();
 218      }
 219  
 220      /**
 221       * Test category::sort_nodes() with a mix of content and non content nodes.
 222       */
 223      public function test_sort_nodes2() {
 224          $category = new \phpunit_fixture_myprofile_category('category', 'title', null);
 225  
 226          // Create nodes.
 227          $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle', null, null, 'content');
 228          $node2 = new \core_user\output\myprofile\node('category', 'node2', 'nodetitle', 'node1', null, 'content');
 229          $node3 = new \core_user\output\myprofile\node('category', 'node3', 'nodetitle', null);
 230          $node4 = new \core_user\output\myprofile\node('category', 'node4', 'nodetitle', 'node3');
 231          $node5 = new \core_user\output\myprofile\node('category', 'node5', 'nodetitle', 'node3');
 232          $node6 = new \core_user\output\myprofile\node('category', 'node6', 'nodetitle', 'node1', null, 'content');
 233  
 234          // Add the nodes in random order.
 235          $category->add_node($node3);
 236          $category->add_node($node2);
 237          $category->add_node($node4);
 238          $category->add_node($node1);
 239          $category->add_node($node5);
 240          $category->add_node($node6);
 241  
 242          // After node 1 we should have node2 - node6 - node3 - node4 - node5.
 243          $category->sort_nodes();
 244          $nodes = $category->nodes;
 245          $this->assertCount(6, $nodes);
 246          $node = array_shift($nodes);
 247          $this->assertEquals($node1, $node);
 248          $node = array_shift($nodes);
 249          $this->assertEquals($node2, $node);
 250          $node = array_shift($nodes);
 251          $this->assertEquals($node6, $node);
 252          $node = array_shift($nodes);
 253          $this->assertEquals($node3, $node);
 254          $node = array_shift($nodes);
 255          $this->assertEquals($node4, $node);
 256          $node = array_shift($nodes);
 257          $this->assertEquals($node5, $node);
 258      }
 259  
 260      /**
 261       * Test tree::add_node().
 262       */
 263      public function test_tree_add_node() {
 264          $tree = new \phpunit_fixture_myprofile_tree();
 265          $node1 = new \core_user\output\myprofile\node('category', 'node1', 'nodetitle');
 266          $tree->add_node($node1);
 267          $nodes = $tree->nodes;
 268          $node = array_shift($nodes);
 269          $this->assertEquals($node1, $node);
 270  
 271          // Can't add node with same name.
 272          $this->expectException(coding_exception::class);
 273          $tree->add_node($node1);
 274      }
 275  
 276      /**
 277       * Test tree::add_category().
 278       */
 279      public function test_tree_add_category() {
 280          $tree = new \phpunit_fixture_myprofile_tree();
 281          $category1 = new \core_user\output\myprofile\category('category', 'title');
 282          $tree->add_category($category1);
 283          $categories = $tree->categories;
 284          $category = array_shift($categories);
 285          $this->assertEquals($category1, $category);
 286  
 287          // Can't add node with same name.
 288          $this->expectException(coding_exception::class);
 289          $tree->add_category($category1);
 290      }
 291  
 292      /**
 293       * Test tree::find_categories_after().
 294       */
 295      public function test_find_categories_after() {
 296          $tree = new \phpunit_fixture_myprofile_tree('category', 'title', null);
 297  
 298          // Create categories.
 299          $category1 = new \core_user\output\myprofile\category('category1', 'category1', null);
 300          $category2 = new \core_user\output\myprofile\category('category2', 'category2', 'category1');
 301          $category3 = new \core_user\output\myprofile\category('category3', 'category3', 'category2');
 302          $category4 = new \core_user\output\myprofile\category('category4', 'category4', 'category3');
 303          $category5 = new \core_user\output\myprofile\category('category5', 'category5', 'category3');
 304          $category6 = new \core_user\output\myprofile\category('category6', 'category6', 'category1');
 305  
 306          // Add the categories in random order.
 307          $tree->add_category($category3);
 308          $tree->add_category($category2);
 309          $tree->add_category($category4);
 310          $tree->add_category($category1);
 311          $tree->add_category($category5);
 312          $tree->add_category($category6);
 313  
 314          // After category 1 we should have category2 - category3 - category4 - category5 - category6.
 315          $return = $tree->find_categories_after($category1);
 316          $this->assertCount(5, $return);
 317          $category = array_shift($return);
 318          $this->assertEquals($category2, $category);
 319          $category = array_shift($return);
 320          $this->assertEquals($category3, $category);
 321          $category = array_shift($return);
 322          $this->assertEquals($category4, $category);
 323          $category = array_shift($return);
 324          $this->assertEquals($category5, $category);
 325          $category = array_shift($return);
 326          $this->assertEquals($category6, $category);
 327  
 328          // Last check also verifies calls for all subsequent categories, still do some random checking.
 329          $return = $tree->find_categories_after($category6);
 330          $this->assertCount(0, $return);
 331          $return = $tree->find_categories_after($category3);
 332          $this->assertCount(2, $return);
 333      }
 334  
 335      /**
 336       * Test tree::sort_categories().
 337       */
 338      public function test_sort_categories() {
 339          $tree = new \phpunit_fixture_myprofile_tree('category', 'title', null);
 340  
 341          // Create categories.
 342          $category1 = new \core_user\output\myprofile\category('category1', 'category1', null);
 343          $category2 = new \core_user\output\myprofile\category('category2', 'category2', 'category1');
 344          $category3 = new \core_user\output\myprofile\category('category3', 'category3', 'category2');
 345          $category4 = new \core_user\output\myprofile\category('category4', 'category4', 'category3');
 346          $category5 = new \core_user\output\myprofile\category('category5', 'category5', 'category3');
 347          $category6 = new \core_user\output\myprofile\category('category6', 'category6', 'category1');
 348  
 349          // Add the categories in random order.
 350          $tree->add_category($category3);
 351          $tree->add_category($category2);
 352          $tree->add_category($category4);
 353          $tree->add_category($category1);
 354          $tree->add_category($category5);
 355          $tree->add_category($category6);
 356  
 357          // After category 1 we should have category2 - category3 - category4 - category5 - category6.
 358          $tree->sort_categories();
 359          $categories = $tree->categories;
 360          $this->assertCount(6, $categories);
 361          $category = array_shift($categories);
 362          $this->assertEquals($category1, $category);
 363          $category = array_shift($categories);
 364          $this->assertEquals($category2, $category);
 365          $category = array_shift($categories);
 366          $this->assertEquals($category3, $category);
 367          $category = array_shift($categories);
 368          $this->assertEquals($category4, $category);
 369          $category = array_shift($categories);
 370          $this->assertEquals($category5, $category);
 371          $category = array_shift($categories);
 372          $this->assertEquals($category6, $category);
 373  
 374          // Can't add category with same name.
 375          $this->expectException(coding_exception::class);
 376          $tree->add_category($category1);
 377      }
 378  }