Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   * Tool analytics external functions tests.
  19   *
  20   * @package    tool_analytics
  21   * @category   external
  22   * @copyright  2019 David MonllaĆ³ {@link http://www.davidmonllao.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.8
  25   */
  26  
  27  namespace tool_analytics\external;
  28  
  29  use externallib_advanced_testcase;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  global $CFG;
  34  
  35  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  36  require_once($CFG->dirroot . '/analytics/tests/fixtures/test_indicator_max.php');
  37  require_once($CFG->dirroot . '/analytics/tests/fixtures/test_target_course_level_shortname.php');
  38  
  39  /**
  40   * Tool analytics external functions tests
  41   *
  42   * @package    tool_analytics
  43   * @category   external
  44   * @copyright  2019 David MonllaĆ³ {@link http://www.davidmonllao.com}
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   * @since      Moodle 3.8
  47   */
  48  class external_test extends externallib_advanced_testcase {
  49  
  50      /**
  51       * test_potential_contexts description
  52       */
  53      public function test_potential_contexts() {
  54          $this->resetAfterTest();
  55  
  56          $this->setAdminUser();
  57  
  58          // Include the all context levels so the misc. category get included.
  59          $this->assertCount(1, \tool_analytics\external::potential_contexts());
  60  
  61          // The frontpage is not included.
  62          $this->assertCount(0, \tool_analytics\external::potential_contexts('PHPUnit'));
  63  
  64          $target = \core_analytics\manager::get_target('test_target_course_level_shortname');
  65          $indicators = ['test_indicator_max' => \core_analytics\manager::get_indicator('test_indicator_max')];
  66          $model = \core_analytics\model::create($target, $indicators);
  67  
  68          $this->assertCount(1, \tool_analytics\external::potential_contexts(null, $model->get_id()));
  69      }
  70  
  71      /**
  72       * test_potential_contexts description
  73       */
  74      public function test_potential_contexts_no_manager() {
  75          $this->resetAfterTest();
  76  
  77          $user = $this->getDataGenerator()->create_user();
  78          $this->setUser($user);
  79  
  80          $this->expectException(\required_capability_exception::class);
  81          $this->assertCount(2, \tool_analytics\external::potential_contexts());
  82      }
  83  }