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   * Tests for notes library functions.
  19   *
  20   * @package    core_notes
  21   * @copyright  2015 onwards Ankit agarwal <ankit.agrr@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/notes/lib.php');
  29  /**
  30   * Class core_notes_lib_testcase
  31   *
  32   * @package    core_notes
  33   * @copyright  2015 onwards Ankit agarwal <ankit.agrr@gmail.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  35   */
  36  class core_notes_lib_testcase extends advanced_testcase {
  37  
  38      /**
  39       * @var stdClass The user.
  40       */
  41      private $user;
  42  
  43      /**
  44       * @var stdClass The course.
  45       */
  46      private $course;
  47  
  48      /**
  49       * @var \core_user\output\myprofile\tree The navigation tree.
  50       */
  51      private $tree;
  52  
  53      public function setUp(): void {
  54          $this->user = $this->getDataGenerator()->create_user();
  55          $this->course = $this->getDataGenerator()->create_course();
  56          $this->tree = new \core_user\output\myprofile\tree();
  57          $this->resetAfterTest();
  58      }
  59  
  60      /**
  61       * Tests the core_notes_myprofile_navigation() function.
  62       */
  63      public function test_core_notes_myprofile_navigation() {
  64          global $USER;
  65  
  66          // Set up the test.
  67          $this->setAdminUser();
  68          $iscurrentuser = true;
  69  
  70          // Enable notes.
  71          set_config('enablenotes', true);
  72  
  73          // Check the node tree is correct.
  74          core_notes_myprofile_navigation($this->tree, $USER, $iscurrentuser, $this->course);
  75          $reflector = new ReflectionObject($this->tree);
  76          $nodes = $reflector->getProperty('nodes');
  77          $nodes->setAccessible(true);
  78          $this->assertArrayHasKey('notes', $nodes->getValue($this->tree));
  79      }
  80  
  81      /**
  82       * Tests the core_notes_myprofile_navigation() function.
  83       */
  84      public function test_core_notes_myprofile_navigation_as_guest() {
  85          global $USER;
  86  
  87          $this->setGuestUser();
  88          $iscurrentuser = false;
  89  
  90          // Check the node tree is correct.
  91          core_notes_myprofile_navigation($this->tree, $USER, $iscurrentuser, $this->course);
  92          $reflector = new ReflectionObject($this->tree);
  93          $nodes = $reflector->getProperty('nodes');
  94          $nodes->setAccessible(true);
  95          $this->assertArrayNotHasKey('notes', $nodes->getValue($this->tree));
  96      }
  97  
  98      /**
  99       * Tests the core_notes_myprofile_navigation() function.
 100       */
 101      public function test_core_notes_myprofile_navigation_notes_disabled() {
 102          global $USER;
 103  
 104          $this->setAdminUser();
 105          $iscurrentuser = false;
 106  
 107          // Disable notes.
 108          set_config('enablenotes', false);
 109  
 110          // Check the node tree is correct.
 111          core_notes_myprofile_navigation($this->tree, $USER, $iscurrentuser, $this->course);
 112          $reflector = new ReflectionObject($this->tree);
 113          $nodes = $reflector->getProperty('nodes');
 114          $nodes->setAccessible(true);
 115          $this->assertArrayNotHasKey('notes', $nodes->getValue($this->tree));
 116      }
 117  }