Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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