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 actor 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_actor_test 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  
  51          $item = item_agent::create_from_user($user);
  52          $data = $item->get_data();
  53  
  54          $item = item_actor::create_from_data($data);
  55  
  56          $this->assertEquals(json_encode($item), json_encode($data));
  57          $this->assertEquals('core_xapi\local\statement\item_agent', get_class($item));
  58  
  59          // Create without specify type.
  60          unset($data->objectType);
  61  
  62          $item = item_actor::create_from_data($data);
  63  
  64          $this->assertEquals(json_encode($item), json_encode($data));
  65          $this->assertEquals('core_xapi\local\statement\item_agent', get_class($item));
  66  
  67          // Check user.
  68          $itemuser = $item->get_user();
  69          $this->assertEquals($itemuser->id, $user->id);
  70          $itemusers = $item->get_all_users();
  71          $this->assertCount(1, $itemusers);
  72      }
  73  
  74      /**
  75       * Test item creation with group.
  76       */
  77      public function test_creation_group(): void {
  78          $this->resetAfterTest();
  79  
  80          $course = $this->getDataGenerator()->create_course();
  81          $user = $this->getDataGenerator()->create_user();
  82          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  83          $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  84          $this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user->id));
  85  
  86          $item = item_group::create_from_group($group);
  87          $data = $item->get_data();
  88  
  89          $item = item_actor::create_from_data($data);
  90  
  91          $this->assertEquals(json_encode($item), json_encode($data));
  92          $this->assertEquals('core_xapi\local\statement\item_group', get_class($item));
  93  
  94          // Check group.
  95          $itemgroup = $item->get_group();
  96          $this->assertEquals($itemgroup->id, $group->id);
  97          $itemusers = $item->get_all_users();
  98          $this->assertCount(1, $itemusers);
  99  
 100          // Code must prevent from using group as a single user.
 101          $this->expectException(xapi_exception::class);
 102          $itemusers = $item->get_user();
 103      }
 104  
 105      /**
 106       * Test for invalid structures.
 107       */
 108      public function test_invalid_data(): void {
 109          $this->expectException(xapi_exception::class);
 110          $data = (object) [
 111              'objectType' => 'Fake',
 112          ];
 113          $item = item_actor::create_from_data($data);
 114      }
 115  }