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  
  25  namespace core_xapi\local\statement;
  26  
  27  use advanced_testcase;
  28  use core_xapi\xapi_exception;
  29  use core_xapi\iri;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Contains test cases for testing statement verb class.
  35   *
  36   * @package    core_xapi
  37   * @since      Moodle 3.9
  38   * @copyright  2020 Ferran Recio
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class item_verb_test extends advanced_testcase {
  42  
  43      /**
  44       * Test item creation.
  45       */
  46      public function test_creation(): void {
  47  
  48          $data = (object) [
  49              'id' => iri::generate('cook', 'verb'),
  50          ];
  51          $item = item_verb::create_from_data($data);
  52  
  53          $this->assertEquals(json_encode($item), json_encode($data));
  54          $this->assertEquals($item->get_id(), 'cook');
  55      }
  56  
  57      /**
  58       * Test item creation from string.
  59       *
  60       * @dataProvider create_from_id_provider
  61       * @param string $id Object string ID (IRI or not)
  62       */
  63      public function test_create_from_id(string $id): void {
  64          $item = item_verb::create_from_id($id);
  65  
  66          $this->assertEquals($id, $item->get_id());
  67  
  68          // Check generated data.
  69          $data = $item->get_data();
  70          $this->assertEquals(iri::generate($id, 'verb'), $data->id);
  71      }
  72  
  73      /**
  74       * Data provider for the test_create_from_id tests.
  75       *
  76       * @return  array
  77       */
  78      public function create_from_id_provider() : array {
  79          return [
  80              'Fake IRI' => [
  81                  'cook',
  82              ],
  83              'Real IRI' => [
  84                  'http://adlnet.gov/expapi/verb/example',
  85              ],
  86          ];
  87      }
  88  
  89      /**
  90       * Test for invalid structures.
  91       *
  92       * @dataProvider invalid_data_provider
  93       * @param string  $id
  94       */
  95      public function test_invalid_data(string $id): void {
  96          $this->expectException(xapi_exception::class);
  97          $data = (object) [
  98              'id' => $id,
  99          ];
 100          $item = item_verb::create_from_data($data);
 101      }
 102  
 103      /**
 104       * Data provider for the test_invalid_data tests.
 105       *
 106       * @return  array
 107       */
 108      public function invalid_data_provider() : array {
 109          return [
 110              'Empty or null id' => [
 111                  '',
 112              ],
 113              'Invalid IRI value' => [
 114                  'invalid_iri_value',
 115              ],
 116          ];
 117      }
 118  }