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   * This file contains unit test related to xAPI library.
  19   *
  20   * @package    core_xapi
  21   * @copyright  2020 Ferran Recio
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_xapi;
  25  
  26  use advanced_testcase;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Contains test cases for testing xAPI iri class.
  32   *
  33   * @package    core_xapi
  34   * @since      Moodle 3.9
  35   * @copyright  2020 Ferran Recio
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class iri_test extends advanced_testcase {
  39  
  40      /**
  41       * Setup to ensure that fixtures are loaded.
  42       */
  43      public static function setupBeforeClass(): void {
  44          global $CFG;
  45          require_once($CFG->dirroot.'/lib/xapi/tests/helper.php');
  46      }
  47  
  48      /**
  49       * Test IRI generation.
  50       *
  51       * @dataProvider iri_samples_provider
  52       * @param string $value Value to generate IRI
  53       * @param string $expected Expected result
  54       * @param string $type = null If some special type is provided
  55       */
  56      public function test_generate(string $value, string $expected, string $type = null) {
  57          $iri = iri::generate($value, $type);
  58          $this->assertEquals($iri, $expected);
  59      }
  60  
  61      /**
  62       * Test IRI extraction.
  63       *
  64       * @dataProvider iri_samples_provider
  65       * @param string $expected Expected result
  66       * @param string $value Value to generate IRI
  67       * @param string $type = null If some special type is provided
  68       */
  69      public function test_extract(string $expected, string $value, string $type = null) {
  70          $extract = iri::extract($value, $type);
  71          $this->assertEquals($extract, $expected);
  72      }
  73  
  74      /**
  75       * Data provider for the test_generate and test_extract tests.
  76       *
  77       * @return  array
  78       */
  79      public function iri_samples_provider() : array {
  80          global $CFG;
  81  
  82          return [
  83              'Fake IRI without type' => [
  84                  'paella',
  85                  "{$CFG->wwwroot}/xapi/element/paella",
  86                  null,
  87              ],
  88              'Real IRI without type' => [
  89                  'http://adlnet.gov/expapi/activities/example',
  90                  'http://adlnet.gov/expapi/activities/example',
  91                  null,
  92              ],
  93              'Fake IRI with type' => [
  94                  'paella',
  95                  "{$CFG->wwwroot}/xapi/dish/paella",
  96                  'dish',
  97              ],
  98              'Real IRI with type' => [
  99                  'http://adlnet.gov/expapi/activities/example',
 100                  'http://adlnet.gov/expapi/activities/example',
 101                  'dish',
 102              ],
 103          ];
 104      }
 105  
 106      /**
 107       * Test IRI generation.
 108       *
 109       * @dataProvider iri_check_provider
 110       * @param string $value Value to generate IRI
 111       * @param bool $expected Expected result
 112       */
 113      public function test_check(string $value, bool $expected) {
 114          $check = iri::check($value);
 115          $this->assertEquals($check, $expected);
 116      }
 117  
 118      /**
 119       * Data provider for the test_check.
 120       *
 121       * @return  array
 122       */
 123      public function iri_check_provider() : array {
 124          return [
 125              'Real IRI http' => [
 126                  'http://adlnet.gov/expapi/activities/example',
 127                  true,
 128              ],
 129              'Real IRI https' => [
 130                  'https://adlnet.gov/expapi/activities/example',
 131                  true,
 132              ],
 133              'Invalid IRI' => [
 134                  'paella',
 135                  false,
 136              ],
 137          ];
 138      }
 139  }