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]

   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 object 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_object_testcase extends advanced_testcase {
  42  
  43      /**
  44       * Test item creation with agent.
  45       */
  46      public function test_creation_agent(): void {
  47          $this->resetAfterTest();
  48  
  49          $user = $this->getDataGenerator()->create_user();
  50          $item = item_agent::create_from_user($user);
  51          $data = $item->get_data();
  52  
  53          $item = item_object::create_from_data($data);
  54  
  55          $this->assertEquals(json_encode($item), json_encode($data));
  56          $this->assertEquals('core_xapi\local\statement\item_agent', get_class($item));
  57      }
  58  
  59      /**
  60       * Test item creation with group.
  61       */
  62      public function test_creation_group(): void {
  63          $this->resetAfterTest();
  64  
  65          $course = $this->getDataGenerator()->create_course();
  66          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  67          $item = item_group::create_from_group($group);
  68          $data = $item->get_data();
  69  
  70          $item = item_object::create_from_data($data);
  71  
  72          $this->assertEquals(json_encode($item), json_encode($data));
  73          $this->assertEquals('core_xapi\local\statement\item_group', get_class($item));
  74      }
  75  
  76      /**
  77       * Test item creation with activity.
  78       */
  79      public function test_creation_activity(): void {
  80  
  81          $item = item_activity::create_from_id('paella');
  82          $data = $item->get_data();
  83          $item = item_object::create_from_data($data);
  84  
  85          $this->assertEquals(json_encode($item), json_encode($data));
  86          $this->assertEquals('core_xapi\local\statement\item_activity', get_class($item));
  87      }
  88  
  89      /**
  90       * Test unsupported item creation.
  91       */
  92      public function test_unsupported_activity(): void {
  93          $this->expectException(xapi_exception::class);
  94          $data = (object) [
  95              'objectType' => 'FakeType',
  96              'id' => -1,
  97          ];
  98          $item = item_object::create_from_data($data);
  99      }
 100  
 101      /**
 102       * Test for invalid structures.
 103       *
 104       * @dataProvider invalid_data_provider
 105       * @param string  $id
 106       */
 107      public function test_invalid_data(string $id): void {
 108          $this->expectException(xapi_exception::class);
 109          $data = (object) [
 110              'id' => $id,
 111          ];
 112          $item = item_object::create_from_data($data);
 113      }
 114  
 115      /**
 116       * Data provider for the test_invalid_data tests.
 117       *
 118       * @return  array
 119       */
 120      public function invalid_data_provider() : array {
 121          return [
 122              'Empty or null id' => [
 123                  '',
 124              ],
 125              'Invalid IRI value' => [
 126                  'invalid_iri_value',
 127              ],
 128          ];
 129      }
 130  }