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;
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
 */
1441 ariadna 31
final class api_test extends advanced_testcase {
1 efrain 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');
1441 ariadna 39
        parent::setUpBeforeClass();
1 efrain 40
    }
41
 
42
    /**
43
     * Testing remove_states_from_component method.
44
     *
45
     * @return void
46
     */
47
    public function test_remove_states_from_component(): void {
48
        global $DB;
49
 
50
        $this->resetAfterTest();
51
 
52
        // Scenario.
53
        $this->setAdminUser();
54
 
55
        // Add a few xAPI state records to database.
56
        test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
57
        test_helper::create_state(['activity' => item_activity::create_from_id('2')], true);
58
        test_helper::create_state(['activity' => item_activity::create_from_id('3')], true);
59
        test_helper::create_state(['activity' => item_activity::create_from_id('4')], true);
60
        test_helper::create_state(['activity' => item_activity::create_from_id('5'), 'component' => 'mod_h5pactivity'], true);
61
        test_helper::create_state(['activity' => item_activity::create_from_id('6'), 'component' => 'mod_h5pactivity'], true);
62
        test_helper::create_state(['activity' => item_activity::create_from_id('7'), 'component' => 'mod_h5pactivity'], true);
63
        test_helper::create_state(['activity' => item_activity::create_from_id('8'), 'component' => 'unexisting'], true);
64
        test_helper::create_state(['activity' => item_activity::create_from_id('9'), 'component' => 'unexisting'], true);
65
 
66
        // Check no state has been removed (because there are no entries for the another_component).
67
        api::remove_states_from_component('another_component');
68
        $this->assertEquals(9, $DB->count_records('xapi_states'));
69
 
70
        // Check states for the fake_component have been removed.
71
        api::remove_states_from_component('fake_component');
72
        $this->assertEquals(5, $DB->count_records('xapi_states'));
73
        $this->assertEquals(0, $DB->count_records('xapi_states', ['component' => 'fake_component']));
74
        $this->assertEquals(3, $DB->count_records('xapi_states', ['component' => 'mod_h5pactivity']));
75
        $this->assertEquals(2, $DB->count_records('xapi_states', ['component' => 'unexisting']));
76
 
77
        // Check states for the mod_h5pactivity have been removed too.
78
        api::remove_states_from_component('mod_h5pactivity');
79
        $this->assertEquals(2, $DB->count_records('xapi_states'));
80
        $this->assertEquals(0, $DB->count_records('xapi_states', ['component' => 'mod_h5pactivity']));
81
 
82
        // Check states for the unexisting component have been removed (using the default state_store).
83
        api::remove_states_from_component('unexisting');
84
        $this->assertEquals(0, $DB->count_records('xapi_states'));
85
    }
86
 
87
    /**
88
     * Testing execute_state_cleanup method.
89
     *
90
     * @return void
91
     */
92
    public function test_execute_state_cleanup(): void {
93
        global $DB;
94
 
95
        $this->resetAfterTest();
96
 
97
        // Scenario.
98
        $this->setAdminUser();
99
 
100
        // Add a few xAPI state records to database.
101
        test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
102
        test_helper::create_state(['activity' => item_activity::create_from_id('2')], true);
103
        test_helper::create_state(['activity' => item_activity::create_from_id('3')], true);
104
        test_helper::create_state(['activity' => item_activity::create_from_id('4')], true);
105
        test_helper::create_state(['activity' => item_activity::create_from_id('5'), 'component' => 'mod_h5pactivity'], true);
106
        test_helper::create_state(['activity' => item_activity::create_from_id('6'), 'component' => 'mod_h5pactivity'], true);
107
        test_helper::create_state(['activity' => item_activity::create_from_id('7'), 'component' => 'mod_h5pactivity'], true);
108
 
109
        // Perform test.
110
        api::execute_state_cleanup();
111
 
112
        // Check no state has been removed yet (because the entries are not old enough).
113
        $this->assertEquals(7, $DB->count_records('xapi_states'));
114
 
115
        // Make the existing state entries older.
116
        $timepast = time() - 2;
117
        $DB->set_field('xapi_states', 'timecreated', $timepast);
118
        $DB->set_field('xapi_states', 'timemodified', $timepast);
119
 
120
        // Create 1 more state, that shouldn't be removed after the cleanup.
121
        test_helper::create_state(['activity' => item_activity::create_from_id('8'), 'component' => 'mod_h5pactivity'], true);
122
 
123
        // Set the config to remove states older than 1 second.
124
        set_config('xapicleanupperiod', 1);
125
 
126
        // Check old states have been removed.
127
        api::execute_state_cleanup();
128
        $this->assertEquals(5, $DB->count_records('xapi_states'));
129
        $this->assertEquals(4, $DB->count_records('xapi_states', ['component' => 'fake_component']));
130
        $this->assertEquals(1, $DB->count_records('xapi_states', ['component' => 'mod_h5pactivity']));
131
        $this->assertEquals(0, $DB->count_records('xapi_states', ['component' => 'my_component']));
132
    }
133
}