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] [Versions 401 and 402] [Versions 401 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;
  26  
  27  use core_xapi\xapi_exception;
  28  use core_xapi\local\statement;
  29  use core_xapi\local\statement\item_agent;
  30  use core_xapi\local\statement\item_verb;
  31  use core_xapi\local\statement\item_activity;
  32  use advanced_testcase;
  33  use core\event\base;
  34  
  35  defined('MOODLE_INTERNAL') || die();
  36  
  37  /**
  38   * Contains test cases for testing xAPI statement handler base methods.
  39   *
  40   * @package    core_xapi
  41   * @since      Moodle 3.9
  42   * @copyright  2020 Ferran Recio
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class handler_test extends advanced_testcase {
  46  
  47      /**
  48       * Setup to ensure that fixtures are loaded.
  49       */
  50      public static function setupBeforeClass(): void {
  51          global $CFG;
  52          require_once($CFG->dirroot.'/lib/xapi/tests/helper.php');
  53      }
  54  
  55      /**
  56       * Test handler creation.
  57       */
  58      public function test_handler_create() {
  59          // Get an existent handler.
  60          $handler = handler::create('fake_component');
  61          $this->assertEquals(get_class($handler), 'fake_component\\xapi\\handler');
  62  
  63          // Get a non existent handler.
  64          $this->expectException(xapi_exception::class);
  65          $value = handler::create('potato_omelette');
  66      }
  67  
  68      /**
  69       * Test support group.
  70       */
  71      public function test_support_group_actor() {
  72          global $CFG;
  73          // Get an existent handler.
  74          $this->resetAfterTest();
  75          $handler = handler::create('fake_component');
  76          $this->assertEquals(get_class($handler), 'fake_component\\xapi\\handler');
  77          $CFG->xapitestforcegroupactors = false;
  78          $this->assertEquals(false, $handler->supports_group_actors());
  79      }
  80  
  81      /**
  82       * Test for process_statements method.
  83       */
  84      public function test_process_statements() {
  85  
  86          $this->resetAfterTest();
  87          $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
  88  
  89          $user = $this->getDataGenerator()->create_user();
  90  
  91          $testhelper = new test_helper();
  92          $testhelper->init_log();
  93  
  94          // Generate a 2 statements array (one accepted one not).
  95          $statements = [];
  96  
  97          $statement = new statement();
  98          $statement->set_actor(item_agent::create_from_user($user));
  99          $statement->set_verb(item_verb::create_from_id('cook'));
 100          $statement->set_object(item_activity::create_from_id('paella'));
 101          $statements[] = $statement;
 102  
 103          $statement2 = new statement();
 104          $statement2->set_actor(item_agent::create_from_user($user));
 105          $statement2->set_verb(item_verb::create_from_id('invalid'));
 106          $statement2->set_object(item_activity::create_from_id('paella'));
 107          $statements[] = $statement2;
 108  
 109          $handler = handler::create('fake_component');
 110          $result = $handler->process_statements($statements);
 111  
 112          // Check results.
 113          $this->assertCount(2, $result);
 114          $this->assertEquals(true, $result[0]);
 115          $this->assertEquals(false, $result[1]);
 116  
 117          // Check log entries.
 118          $log = $testhelper->get_last_log_entry();
 119          $this->assertNotEmpty($log);
 120  
 121          // Validate statement information on log.
 122          $value = $log->get_name();
 123          $this->assertEquals($value, 'xAPI test statement');
 124          $value = $log->get_description();
 125          // Due to logstore limitation, event must use a real component (core_xapi).
 126          $this->assertEquals($value, 'User \''.$user->id.'\' send a statement to component \'core_xapi\'');
 127          $this->assertTrue($log->compare_statement($statement));
 128      }
 129  }