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 |
/**
|
|
|
18 |
* Unit tests helper for xAPI library.
|
|
|
19 |
*
|
|
|
20 |
* This file contains unit test helpers related to xAPI library.
|
|
|
21 |
*
|
|
|
22 |
* @package core_xapi
|
|
|
23 |
* @copyright 2020 Ferran Recio
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
namespace core_xapi;
|
|
|
27 |
|
|
|
28 |
use core_xapi\local\state;
|
|
|
29 |
use core_xapi\local\statement\item_activity;
|
|
|
30 |
use core_xapi\local\statement\item_agent;
|
|
|
31 |
|
|
|
32 |
defined('MOODLE_INTERNAL') || die();
|
|
|
33 |
|
|
|
34 |
require_once(__DIR__ . '/fixtures/handler.php');
|
|
|
35 |
require_once(__DIR__ . '/fixtures/xapi_test_statement_post.php');
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Contains helper functions for xAPI PHPUnit Tests.
|
|
|
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 test_helper {
|
|
|
46 |
|
|
|
47 |
/** @var \core\log\reader contains a valid logstore reader. */
|
|
|
48 |
private $store;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Constructor for a xAPI test helper.
|
|
|
52 |
*
|
|
|
53 |
*/
|
|
|
54 |
public function init_log() {
|
|
|
55 |
// Enable logs.
|
|
|
56 |
set_config('jsonformat', 1, 'logstore_standard');
|
|
|
57 |
set_config('enabled_stores', 'logstore_standard', 'tool_log');
|
|
|
58 |
set_config('buffersize', 0, 'logstore_standard');
|
|
|
59 |
set_config('logguests', 1, 'logstore_standard');
|
|
|
60 |
$manager = get_log_manager(true);
|
|
|
61 |
$stores = $manager->get_readers();
|
|
|
62 |
$this->store = $stores['logstore_standard'];
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Return the last log entry from standardlog.
|
|
|
67 |
*
|
|
|
68 |
* @return \core\event\base|null The last log event or null if none found.
|
|
|
69 |
*/
|
|
|
70 |
public function get_last_log_entry(): ?\core\event\base {
|
|
|
71 |
|
|
|
72 |
$select = "component = :component";
|
|
|
73 |
$params = ['component' => 'core_xapi'];
|
|
|
74 |
$records = $this->store->get_events_select($select, $params, 'timecreated DESC', 0, 1);
|
|
|
75 |
|
|
|
76 |
if (empty($records)) {
|
|
|
77 |
return null;
|
|
|
78 |
}
|
|
|
79 |
return array_pop($records);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Return a valid state object with the params passed.
|
|
|
85 |
*
|
|
|
86 |
* All tests are based on craft different types of states. This function
|
|
|
87 |
* is made to prevent redundant code on the test.
|
|
|
88 |
*
|
|
|
89 |
* @param array $info array of overriden state data (default []).
|
|
|
90 |
* @param bool $createindatabase Whether the state object should be created in database too or not.
|
|
|
91 |
* @return state the resulting state
|
|
|
92 |
*/
|
|
|
93 |
public static function create_state(array $info = [], bool $createindatabase = false): state {
|
|
|
94 |
global $USER;
|
|
|
95 |
|
|
|
96 |
$component = $info['component'] ?? 'fake_component';
|
|
|
97 |
$agent = $info['agent'] ?? item_agent::create_from_user($USER);
|
|
|
98 |
$activity = $info['activity'] ?? item_activity::create_from_id('12345');
|
|
|
99 |
$stateid = $info['stateid'] ?? 'state';
|
|
|
100 |
$data = $info['data'] ?? json_decode('{"progress":0,"answers":[[["AA"],[""]],[{"answers":[]}]],"answered":[true,false]}');
|
|
|
101 |
$registration = $info['registration'] ?? null;
|
|
|
102 |
|
|
|
103 |
$state = new state($agent, $activity, $stateid, (object)$data, $registration);
|
|
|
104 |
|
|
|
105 |
if ($createindatabase) {
|
|
|
106 |
try {
|
|
|
107 |
$handler = handler::create($component);
|
|
|
108 |
$statestore = $handler->get_state_store();
|
|
|
109 |
} catch (\Exception $exception) {
|
|
|
110 |
// If the component is not available, use the standard one to force it's creation.
|
|
|
111 |
$statestore = new state_store($component);
|
|
|
112 |
}
|
|
|
113 |
$statestore->put($state);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return $state;
|
|
|
117 |
}
|
|
|
118 |
}
|