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\task; 18 19 use core_xapi\local\statement\item_activity; 20 use advanced_testcase; 21 use core_xapi\test_helper; 22 23 /** 24 * Contains test cases for testing the scheduled task state_cleanup_task. 25 * 26 * @package core_xapi 27 * @since Moodle 4.2 28 * @covers \core_xapi\task\state_cleanup_task 29 * @copyright 2023 Sara Arjona (sara@moodle.com) 30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 31 */ 32 class state_cleanup_task_test extends advanced_testcase { 33 34 /** 35 * Setup to ensure that fixtures are loaded. 36 */ 37 public static function setUpBeforeClass(): void { 38 global $CFG; 39 require_once($CFG->dirroot.'/lib/xapi/tests/helper.php'); 40 } 41 42 /** 43 * Testing execute method in state_cleanup_task. 44 */ 45 public function test_state_cleanup_task(): void { 46 global $DB; 47 48 $this->resetAfterTest(); 49 50 // Scenario. 51 $this->setAdminUser(); 52 53 // Add a few xAPI state records to database. 54 test_helper::create_state(['activity' => item_activity::create_from_id('1')], true); 55 test_helper::create_state(['activity' => item_activity::create_from_id('2')], true); 56 test_helper::create_state(['activity' => item_activity::create_from_id('3')], true); 57 test_helper::create_state(['activity' => item_activity::create_from_id('4')], true); 58 test_helper::create_state(['activity' => item_activity::create_from_id('5'), 'component' => 'mod_h5pactivity'], true); 59 test_helper::create_state(['activity' => item_activity::create_from_id('6'), 'component' => 'mod_h5pactivity'], true); 60 test_helper::create_state(['activity' => item_activity::create_from_id('7'), 'component' => 'mod_h5pactivity'], true); 61 62 // Perform test. 63 $task = new state_cleanup_task(); 64 $task->execute(); 65 66 // Check no state has been removed yet (because the entries are not old enough). 67 $this->assertEquals(7, $DB->count_records('xapi_states')); 68 69 // Make the existing state entries older. 70 $timepast = time() - 2; 71 $DB->set_field('xapi_states', 'timecreated', $timepast); 72 $DB->set_field('xapi_states', 'timemodified', $timepast); 73 74 // Create 1 more state, that shouldn't be removed after the cleanup. 75 test_helper::create_state(['activity' => item_activity::create_from_id('8'), 'component' => 'mod_h5pactivity'], true); 76 77 // Set the config to remove states older than 1 second. 78 set_config('xapicleanupperiod', 1); 79 80 // Check old states have been removed. 81 $task->execute(); 82 $this->assertEquals(5, $DB->count_records('xapi_states')); 83 $this->assertEquals(4, $DB->count_records('xapi_states', ['component' => 'fake_component'])); 84 $this->assertEquals(1, $DB->count_records('xapi_states', ['component' => 'mod_h5pactivity'])); 85 $this->assertEquals(0, $DB->count_records('xapi_states', ['component' => 'my_component'])); 86 } 87 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body