Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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  namespace core_xapi;
  18  
  19  use core_xapi\local\statement\item_activity;
  20  use advanced_testcase;
  21  
  22  /**
  23   * Contains test cases for testing xAPI API base methods.
  24   *
  25   * @package    core_xapi
  26   * @since      Moodle 4.2
  27   * @covers     \core_xapi\api
  28   * @copyright  2023 Sara Arjona (sara@moodle.com)
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class api_test extends advanced_testcase {
  32  
  33      /**
  34       * Setup to ensure that fixtures are loaded.
  35       */
  36      public static function setUpBeforeClass(): void {
  37          global $CFG;
  38          require_once($CFG->dirroot.'/lib/xapi/tests/helper.php');
  39      }
  40  
  41      /**
  42       * Testing remove_states_from_component method.
  43       *
  44       * @return void
  45       */
  46      public function test_remove_states_from_component(): void {
  47          global $DB;
  48  
  49          $this->resetAfterTest();
  50  
  51          // Scenario.
  52          $this->setAdminUser();
  53  
  54          // Add a few xAPI state records to database.
  55          test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
  56          test_helper::create_state(['activity' => item_activity::create_from_id('2')], true);
  57          test_helper::create_state(['activity' => item_activity::create_from_id('3')], true);
  58          test_helper::create_state(['activity' => item_activity::create_from_id('4')], true);
  59          test_helper::create_state(['activity' => item_activity::create_from_id('5'), 'component' => 'mod_h5pactivity'], true);
  60          test_helper::create_state(['activity' => item_activity::create_from_id('6'), 'component' => 'mod_h5pactivity'], true);
  61          test_helper::create_state(['activity' => item_activity::create_from_id('7'), 'component' => 'mod_h5pactivity'], true);
  62          test_helper::create_state(['activity' => item_activity::create_from_id('8'), 'component' => 'unexisting'], true);
  63          test_helper::create_state(['activity' => item_activity::create_from_id('9'), 'component' => 'unexisting'], true);
  64  
  65          // Check no state has been removed (because there are no entries for the another_component).
  66          api::remove_states_from_component('another_component');
  67          $this->assertEquals(9, $DB->count_records('xapi_states'));
  68  
  69          // Check states for the fake_component have been removed.
  70          api::remove_states_from_component('fake_component');
  71          $this->assertEquals(5, $DB->count_records('xapi_states'));
  72          $this->assertEquals(0, $DB->count_records('xapi_states', ['component' => 'fake_component']));
  73          $this->assertEquals(3, $DB->count_records('xapi_states', ['component' => 'mod_h5pactivity']));
  74          $this->assertEquals(2, $DB->count_records('xapi_states', ['component' => 'unexisting']));
  75  
  76          // Check states for the mod_h5pactivity have been removed too.
  77          api::remove_states_from_component('mod_h5pactivity');
  78          $this->assertEquals(2, $DB->count_records('xapi_states'));
  79          $this->assertEquals(0, $DB->count_records('xapi_states', ['component' => 'mod_h5pactivity']));
  80  
  81          // Check states for the unexisting component have been removed (using the default state_store).
  82          api::remove_states_from_component('unexisting');
  83          $this->assertEquals(0, $DB->count_records('xapi_states'));
  84      }
  85  
  86      /**
  87       * Testing execute_state_cleanup method.
  88       *
  89       * @return void
  90       */
  91      public function test_execute_state_cleanup(): void {
  92          global $DB;
  93  
  94          $this->resetAfterTest();
  95  
  96          // Scenario.
  97          $this->setAdminUser();
  98  
  99          // Add a few xAPI state records to database.
 100          test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
 101          test_helper::create_state(['activity' => item_activity::create_from_id('2')], true);
 102          test_helper::create_state(['activity' => item_activity::create_from_id('3')], true);
 103          test_helper::create_state(['activity' => item_activity::create_from_id('4')], true);
 104          test_helper::create_state(['activity' => item_activity::create_from_id('5'), 'component' => 'mod_h5pactivity'], true);
 105          test_helper::create_state(['activity' => item_activity::create_from_id('6'), 'component' => 'mod_h5pactivity'], true);
 106          test_helper::create_state(['activity' => item_activity::create_from_id('7'), 'component' => 'mod_h5pactivity'], true);
 107  
 108          // Perform test.
 109          api::execute_state_cleanup();
 110  
 111          // Check no state has been removed yet (because the entries are not old enough).
 112          $this->assertEquals(7, $DB->count_records('xapi_states'));
 113  
 114          // Make the existing state entries older.
 115          $timepast = time() - 2;
 116          $DB->set_field('xapi_states', 'timecreated', $timepast);
 117          $DB->set_field('xapi_states', 'timemodified', $timepast);
 118  
 119          // Create 1 more state, that shouldn't be removed after the cleanup.
 120          test_helper::create_state(['activity' => item_activity::create_from_id('8'), 'component' => 'mod_h5pactivity'], true);
 121  
 122          // Set the config to remove states older than 1 second.
 123          set_config('xapicleanupperiod', 1);
 124  
 125          // Check old states have been removed.
 126          api::execute_state_cleanup();
 127          $this->assertEquals(5, $DB->count_records('xapi_states'));
 128          $this->assertEquals(4, $DB->count_records('xapi_states', ['component' => 'fake_component']));
 129          $this->assertEquals(1, $DB->count_records('xapi_states', ['component' => 'mod_h5pactivity']));
 130          $this->assertEquals(0, $DB->count_records('xapi_states', ['component' => 'my_component']));
 131      }
 132  }