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 agent 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_agent_testcase extends advanced_testcase {
  42  
  43      /**
  44       * Test item creation.
  45       */
  46      public function test_create(): void {
  47          global $CFG;
  48  
  49          $this->resetAfterTest();
  50          $user = $this->getDataGenerator()->create_user();
  51  
  52          // Ceate using account.
  53          $data = (object) [
  54              'objectType' => 'Agent',
  55              'account' => (object) [
  56                  'homePage' => $CFG->wwwroot,
  57                  'name' => $user->id,
  58              ],
  59          ];
  60          $item = item_agent::create_from_data($data);
  61  
  62          $this->assertEquals(json_encode($item), json_encode($data));
  63          $itemuser = $item->get_user();
  64          $this->assertEquals($itemuser->id, $user->id);
  65          $itemusers = $item->get_all_users();
  66          $this->assertCount(1, $itemusers);
  67  
  68          // Ceate using mbox.
  69          $data = (object) [
  70              'objectType' => 'Agent',
  71              'mbox' => $user->email,
  72          ];
  73          $item = item_agent::create_from_data($data);
  74  
  75          $this->assertEquals(json_encode($item), json_encode($data));
  76          $itemuser = $item->get_user();
  77          $this->assertEquals($itemuser->id, $user->id);
  78          $itemusers = $item->get_all_users();
  79          $this->assertCount(1, $itemusers);
  80      }
  81  
  82      /**
  83       * Test item creation from Record.
  84       */
  85      public function test_create_from_user(): void {
  86          global $CFG;
  87  
  88          $this->resetAfterTest();
  89          $user = $this->getDataGenerator()->create_user();
  90  
  91          $item = item_agent::create_from_user($user);
  92  
  93          $itemuser = $item->get_user();
  94          $this->assertEquals($itemuser->id, $user->id);
  95          $itemusers = $item->get_all_users();
  96          $this->assertCount(1, $itemusers);
  97  
  98          // Check generated data.
  99          $data = $item->get_data();
 100          $this->assertEquals('Agent', $data->objectType);
 101          $this->assertEquals($CFG->wwwroot, $data->account->homePage);
 102          $this->assertEquals($user->id, $data->account->name);
 103      }
 104  
 105      /**
 106       * Test for invalid structures.
 107       *
 108       * @dataProvider invalid_data_provider
 109       * @param string $objecttype object type attribute
 110       * @param bool $validhome if valid homepage is user
 111       * @param bool $validid if valid group id is used
 112       */
 113      public function test_invalid_data(string $objecttype, bool $validhome, bool $validid): void {
 114          global $CFG;
 115  
 116          // Create one course with a group if necessary.
 117          $id = 'Wrong ID';
 118          if ($validid) {
 119              $this->resetAfterTest();
 120              $user = $this->getDataGenerator()->create_user();
 121              $id = $user->id;
 122          }
 123  
 124          $homepage = 'Invalid homepage!';
 125          if ($validhome) {
 126              $homepage = $CFG->wwwroot;
 127          }
 128  
 129          $data = (object) [
 130              'objectType' => $objecttype,
 131              'account' => (object) [
 132                  'homePage' => $homepage,
 133                  'name' => $id,
 134              ],
 135          ];
 136  
 137          $this->expectException(xapi_exception::class);
 138          $item = item_agent::create_from_data($data);
 139      }
 140  
 141      /**
 142       * Data provider for the test_invalid_data tests.
 143       *
 144       * @return  array
 145       */
 146      public function invalid_data_provider() : array {
 147          return [
 148              'Wrong objecttype' => [
 149                  'Invalid', true, true
 150              ],
 151              'Wrong homepage' => [
 152                  'Agent', false, true
 153              ],
 154              'Wrong id' => [
 155                  'Agent', true, false
 156              ],
 157          ];
 158      }
 159  
 160      /**
 161       * Test non supported account identifier xAPI formats.
 162       *
 163       * @dataProvider unspupported_create_provider
 164       * @param bool $usembox
 165       * @param bool $useaccount
 166       * @param bool $usesha1
 167       * @param bool $useopenid
 168       */
 169      public function test_unspupported_create(bool $usembox, bool $useaccount, bool $usesha1, bool $useopenid): void {
 170          global $CFG;
 171  
 172          $this->resetAfterTest();
 173          $user = $this->getDataGenerator()->create_user();
 174  
 175          // Ceate using both account and mbox.
 176          $data = (object) [
 177              'objectType' => 'Agent'
 178          ];
 179  
 180          if ($usembox) {
 181              $data->mbox = $user->email;
 182          }
 183          if ($useaccount) {
 184              $data->account = (object) [
 185                  'homePage' => $CFG->wwwroot,
 186                  'name' => $user->id,
 187              ];
 188          }
 189          if ($usesha1) {
 190              $data->mbox_sha1sum = sha1($user->email);
 191          }
 192          if ($useopenid) {
 193              // Note: this is not a real openid, it's just a value to test.
 194              $data->openid = 'https://www.moodle.openid.com/accounts/o8/id';
 195          }
 196  
 197          $this->expectException(xapi_exception::class);
 198          $item = item_agent::create_from_data($data);
 199      }
 200  
 201      /**
 202       * Data provider for the unsupported identifiers tests.
 203       *
 204       * @return  array
 205       */
 206      public function unspupported_create_provider() : array {
 207          return [
 208              'Both mbox and account' => [
 209                  true, true, false, false
 210              ],
 211              'Email SHA1' => [
 212                  false, false, false, false
 213              ],
 214              'Open ID' => [
 215                  false, false, false, false
 216              ],
 217          ];
 218      }
 219  
 220      /**
 221       * Test for missing object type.
 222       */
 223      public function test_missing_object_type(): void {
 224          $data = (object) ['id' => -1];
 225          $this->expectException(xapi_exception::class);
 226          $item = item_agent::create_from_data($data);
 227      }
 228  
 229      /**
 230       * Test for invalid user id.
 231       */
 232      public function test_inexistent_agent(): void {
 233          global $CFG;
 234          $data = (object) [
 235              'objectType' => 'Agent',
 236              'account' => (object) [
 237                  'homePage' => $CFG->wwwroot,
 238                  'name' => 0,
 239              ],
 240          ];
 241          $this->expectException(xapi_exception::class);
 242          $item = item_agent::create_from_data($data);
 243      }
 244  
 245      /**
 246       * Test for invalid agent record.
 247       */
 248      public function test_inexistent_agent_id(): void {
 249          $user = (object) ['name' => 'Me'];
 250          $this->expectException(xapi_exception::class);
 251          $item = item_agent::create_from_user($user);
 252      }
 253  }