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 activity 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_activity_test extends advanced_testcase {
  42  
  43      /**
  44       * Test item creation.
  45       */
  46      public function test_creation(): void {
  47  
  48          // Activity without definition.
  49          $data = (object) [
  50              'objectType' => 'Activity',
  51              'id' => iri::generate('paella', 'activity'),
  52          ];
  53          $item = item_activity::create_from_data($data);
  54  
  55          $this->assertEquals(json_encode($item), json_encode($data));
  56          $this->assertEquals($item->get_id(), 'paella');
  57          $this->assertNull($item->get_definition());
  58  
  59          // Add optional objectType.
  60          $data->objectType = 'Activity';
  61          $item = item_activity::create_from_data($data);
  62          $this->assertEquals(json_encode($item), json_encode($data));
  63  
  64          // Add definition.
  65          $data->definition = (object) [
  66              'interactionType' => 'choice',
  67          ];
  68          $item = item_activity::create_from_data($data);
  69  
  70          $this->assertEquals(json_encode($item), json_encode($data));
  71          $this->assertNotNull($item->get_definition());
  72      }
  73  
  74      /**
  75       * Test item creation from string.
  76       *
  77       * @dataProvider create_from_id_provider
  78       * @param string $id Object string ID (IRI or not)
  79       * @param bool $usedefinition if a valir definition must be attached or not
  80       */
  81      public function test_create_from_id(string $id, bool $usedefinition): void {
  82  
  83          $definition = null;
  84          if ($usedefinition) {
  85              $data = (object) [
  86                  'type' => iri::generate('example', 'id'),
  87                  'interactionType' => 'choice'
  88              ];
  89              $definition = item_definition::create_from_data($data);
  90          }
  91  
  92          $item = item_activity::create_from_id($id, $definition);
  93  
  94          $this->assertEquals($id, $item->get_id());
  95          $itemdefinition = $item->get_definition();
  96          if ($usedefinition) {
  97              $this->assertEquals('choice', $itemdefinition->get_interactiontype());
  98          } else {
  99              $this->assertNull($itemdefinition);
 100          }
 101  
 102          // Check generated data.
 103          $data = $item->get_data();
 104          $this->assertEquals('Activity', $data->objectType);
 105          $this->assertEquals(iri::generate($id, 'activity'), $data->id);
 106          if ($usedefinition) {
 107              $this->assertEquals('choice', $data->definition->interactionType);
 108          }
 109      }
 110  
 111      /**
 112       * Data provider for the test_create_from_id tests.
 113       *
 114       * @return  array
 115       */
 116      public function create_from_id_provider() : array {
 117          return [
 118              'Fake IRI with no definition' => [
 119                  'paella', false,
 120              ],
 121              'Fake IRI with definition' => [
 122                  'paella', true,
 123              ],
 124              'Real IRI with no definition' => [
 125                  'http://adlnet.gov/expapi/activities/example', false,
 126              ],
 127              'Real IRI with definition' => [
 128                  'http://adlnet.gov/expapi/activities/example', true,
 129              ],
 130          ];
 131      }
 132  
 133      /**
 134       * Test for invalid structures.
 135       *
 136       * @dataProvider invalid_data_provider
 137       * @param string  $type objectType attribute
 138       * @param string  $id activity ID
 139       */
 140      public function test_invalid_data(string $type, string $id): void {
 141  
 142          $data = (object) [
 143              'objectType' => $type,
 144          ];
 145          if (!empty($id)) {
 146              $data->id = $id;
 147          }
 148  
 149          $this->expectException(xapi_exception::class);
 150          $item = item_activity::create_from_data($data);
 151      }
 152  
 153      /**
 154       * Data provider for the test_invalid_data tests.
 155       *
 156       * @return  array
 157       */
 158      public function invalid_data_provider() : array {
 159          return [
 160              'Invalid Avtivity objectType' => [
 161                  'Invalid Type!', iri::generate('paella', 'activity'),
 162              ],
 163              'Invalid id value' => [
 164                  'Activity', 'Invalid_iri_value',
 165              ],
 166              'Non-existent id value' => [
 167                  'Activity', '',
 168              ],
 169          ];
 170      }
 171  
 172      /**
 173       * Test for missing object type.
 174       */
 175      public function test_missing_object_type(): void {
 176          $data = (object) ['id' => 42];
 177          $this->expectException(xapi_exception::class);
 178          $item = item_activity::create_from_data($data);
 179      }
 180  
 181      /**
 182       * Test for invalid activity objectType.
 183       */
 184      public function test_inexistent_agent(): void {
 185          global $CFG;
 186          $data = (object) [
 187              'objectType' => 'Invalid',
 188              'id' => -1,
 189          ];
 190          $this->expectException(xapi_exception::class);
 191          $item = item_activity::create_from_data($data);
 192      }
 193  }