Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 */
1441 ariadna 32
final class state_cleanup_task_test extends advanced_testcase {
1 efrain 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');
1441 ariadna 40
        parent::setUpBeforeClass();
1 efrain 41
    }
42
 
43
    /**
44
     * Testing execute method in state_cleanup_task.
45
     */
46
    public function test_state_cleanup_task(): 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
 
63
        // Perform test.
64
        $task = new state_cleanup_task();
65
        $task->execute();
66
 
67
        // Check no state has been removed yet (because the entries are not old enough).
68
        $this->assertEquals(7, $DB->count_records('xapi_states'));
69
 
70
        // Make the existing state entries older.
71
        $timepast = time() - 2;
72
        $DB->set_field('xapi_states', 'timecreated', $timepast);
73
        $DB->set_field('xapi_states', 'timemodified', $timepast);
74
 
75
        // Create 1 more state, that shouldn't be removed after the cleanup.
76
        test_helper::create_state(['activity' => item_activity::create_from_id('8'), 'component' => 'mod_h5pactivity'], true);
77
 
78
        // Set the config to remove states older than 1 second.
79
        set_config('xapicleanupperiod', 1);
80
 
81
        // Check old states have been removed.
82
        $task->execute();
83
        $this->assertEquals(5, $DB->count_records('xapi_states'));
84
        $this->assertEquals(4, $DB->count_records('xapi_states', ['component' => 'fake_component']));
85
        $this->assertEquals(1, $DB->count_records('xapi_states', ['component' => 'mod_h5pactivity']));
86
        $this->assertEquals(0, $DB->count_records('xapi_states', ['component' => 'my_component']));
87
    }
88
}