MDL-67789 xapi: Add support to save content state
This commit is contained in:
committed by
Sara Arjona
parent
12a8176926
commit
03a4abde0f
+234
-13
@@ -14,14 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* This file contains unit test related to xAPI library.
|
||||
*
|
||||
* @package core_xapi
|
||||
* @copyright 2020 Ferran Recio
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core_xapi;
|
||||
|
||||
use core_xapi\xapi_exception;
|
||||
@@ -30,15 +22,15 @@ use core_xapi\local\statement\item_agent;
|
||||
use core_xapi\local\statement\item_verb;
|
||||
use core_xapi\local\statement\item_activity;
|
||||
use advanced_testcase;
|
||||
use core\event\base;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
use core_xapi\local\state;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Contains test cases for testing xAPI statement handler base methods.
|
||||
* Contains test cases for testing xAPI handler base methods.
|
||||
*
|
||||
* @package core_xapi
|
||||
* @since Moodle 3.9
|
||||
* @covers \core_xapi\handler
|
||||
* @copyright 2020 Ferran Recio
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
@@ -47,7 +39,7 @@ class handler_test extends advanced_testcase {
|
||||
/**
|
||||
* Setup to ensure that fixtures are loaded.
|
||||
*/
|
||||
public static function setupBeforeClass(): void {
|
||||
public static function setUpBeforeClass(): void {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot.'/lib/xapi/tests/helper.php');
|
||||
}
|
||||
@@ -65,6 +57,19 @@ class handler_test extends advanced_testcase {
|
||||
$value = handler::create('potato_omelette');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test xAPI support.
|
||||
*/
|
||||
public function test_supports_xapi() {
|
||||
// Get an existent handler.
|
||||
$result = handler::supports_xapi('fake_component');
|
||||
$this->assertTrue($result);
|
||||
|
||||
// Get a non existent handler.
|
||||
$result = handler::supports_xapi('potato_omelette');
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test support group.
|
||||
*/
|
||||
@@ -115,6 +120,7 @@ class handler_test extends advanced_testcase {
|
||||
$this->assertEquals(false, $result[1]);
|
||||
|
||||
// Check log entries.
|
||||
/** @var \core_xapi\event\xapi_test_statement_post $log */
|
||||
$log = $testhelper->get_last_log_entry();
|
||||
$this->assertNotEmpty($log);
|
||||
|
||||
@@ -126,4 +132,219 @@ class handler_test extends advanced_testcase {
|
||||
$this->assertEquals($value, 'User \''.$user->id.'\' send a statement to component \'core_xapi\'');
|
||||
$this->assertTrue($log->compare_statement($statement));
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing save_state method.
|
||||
*/
|
||||
public function test_save_state(): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Scenario.
|
||||
$this->setAdminUser();
|
||||
$component = 'fake_component';
|
||||
$handler = handler::create($component);
|
||||
|
||||
// Check the state has been added.
|
||||
$state = test_helper::create_state();
|
||||
$this->assertEquals(0, $DB->count_records('xapi_states'));
|
||||
$result = $handler->save_state($state);
|
||||
$this->assertTrue($result);
|
||||
$records = $DB->get_records('xapi_states');
|
||||
$this->assertCount(1, $records);
|
||||
$record = reset($records);
|
||||
$this->check_state($component, $state, $record);
|
||||
|
||||
// Check the state has been updated.
|
||||
$statedata = '{"progress":0,"answers":[[["BB"],[""]],[{"answers":[]}]],"answered":[true,false]}';
|
||||
$state->set_state_data(json_decode($statedata));
|
||||
$result = $handler->save_state($state);
|
||||
$this->assertTrue($result);
|
||||
$records = $DB->get_records('xapi_states');
|
||||
$this->assertCount(1, $records);
|
||||
$record = reset($records);
|
||||
$this->check_state($component, $state, $record);
|
||||
|
||||
// Check an exception is thrown when the state is not valid.
|
||||
$this->expectException(xapi_exception::class);
|
||||
$state = test_helper::create_state(['stateid' => 'paella']);
|
||||
$result = $handler->save_state($state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing load_state method.
|
||||
*/
|
||||
public function test_load_state(): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Scenario.
|
||||
$this->setAdminUser();
|
||||
$component = 'fake_component';
|
||||
$handler = handler::create($component);
|
||||
|
||||
// Check the state is not found (when there are no states).
|
||||
$state = test_helper::create_state();
|
||||
$state->set_state_data(null);
|
||||
$this->assertEquals(0, $DB->count_records('xapi_states'));
|
||||
$result = $handler->load_state($state);
|
||||
$this->assertEquals(0, $DB->count_records('xapi_states'));
|
||||
$this->assertNull($result);
|
||||
|
||||
// Add, at least, one xAPI state record to database (with the default values).
|
||||
test_helper::create_state([], true);
|
||||
|
||||
// Check the state is found when it exists.
|
||||
$result = $handler->load_state($state);
|
||||
$records = $DB->get_records('xapi_states');
|
||||
$this->assertCount(1, $records);
|
||||
$record = reset($records);
|
||||
$this->check_state($component, $state, $record);
|
||||
$this->assertEquals($state->jsonSerialize(), $result->jsonSerialize());
|
||||
|
||||
// Check the state is not found when it doesn't exist.
|
||||
$state = test_helper::create_state(['activity' => item_activity::create_from_id('1')]);
|
||||
$state->set_state_data(null);
|
||||
$result = $handler->load_state($state);
|
||||
$records = $DB->get_records('xapi_states');
|
||||
$this->assertCount(1, $DB->get_records('xapi_states'));
|
||||
$this->assertNull($result);
|
||||
|
||||
// Check an exception is thrown when the state is not valid.
|
||||
$this->expectException(xapi_exception::class);
|
||||
$state = test_helper::create_state(['stateid' => 'paella']);
|
||||
$result = $handler->load_state($state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing delete_state method.
|
||||
*/
|
||||
public function test_delete_state(): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Scenario.
|
||||
$this->setAdminUser();
|
||||
$component = 'fake_component';
|
||||
$handler = handler::create($component);
|
||||
|
||||
// Check the state is not deleted (when there are no states).
|
||||
$state = test_helper::create_state();
|
||||
$this->assertEquals(0, $DB->count_records('xapi_states'));
|
||||
$result = $handler->delete_state($state);
|
||||
$this->assertTrue($result);
|
||||
$this->assertEquals(0, $DB->count_records('xapi_states'));
|
||||
|
||||
// Add, at least, one xAPI state record to database (with the default values).
|
||||
test_helper::create_state([], true);
|
||||
|
||||
// Check the state is not deleted if the given state doesn't meet its values.
|
||||
$state2 = test_helper::create_state(['activity' => item_activity::create_from_id('1')]);
|
||||
$result = $handler->delete_state($state2);
|
||||
$this->assertTrue($result);
|
||||
$this->assertCount(1, $DB->get_records('xapi_states'));
|
||||
|
||||
// Check the state is deleted if it exists.
|
||||
$result = $handler->delete_state($state);
|
||||
$this->assertTrue($result);
|
||||
$this->assertCount(0, $DB->get_records('xapi_states'));
|
||||
|
||||
// Check an exception is thrown when the state is not valid.
|
||||
$this->expectException(xapi_exception::class);
|
||||
$state = test_helper::create_state(['stateid' => 'paella']);
|
||||
$result = $handler->delete_state($state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing reset_states method.
|
||||
*/
|
||||
public function test_reset_states(): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Scenario.
|
||||
$this->setAdminUser();
|
||||
$component = 'fake_component';
|
||||
$handler = handler::create($component);
|
||||
|
||||
// Check the state is not reset (when there are no states).
|
||||
$this->assertCount(0, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
|
||||
$handler->reset_states();
|
||||
$this->assertCount(0, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
|
||||
|
||||
// Add, at least, one xAPI state record to database (with the default values).
|
||||
test_helper::create_state([], true);
|
||||
|
||||
// Check the state is not reset if the given state doesn't meet its values.
|
||||
$handler->reset_states('1');
|
||||
$this->assertCount(1, $DB->get_records('xapi_states'));
|
||||
$this->assertCount(0, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
|
||||
|
||||
// Check the state is reset if it exists.
|
||||
$handler->reset_states();
|
||||
$this->assertCount(1, $DB->get_records('xapi_states'));
|
||||
$this->assertCount(1, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
|
||||
|
||||
// Check the state is reset too when using some of the given parameters.
|
||||
test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
|
||||
$handler->reset_states('1');
|
||||
$this->assertCount(2, $DB->get_records('xapi_states'));
|
||||
$this->assertCount(2, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing wipe_states method.
|
||||
*/
|
||||
public function test_wipe_states(): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Scenario.
|
||||
$this->setAdminUser();
|
||||
$component = 'fake_component';
|
||||
$handler = handler::create($component);
|
||||
|
||||
// Check the state is not wiped (when there are no states).
|
||||
$this->assertCount(0, $DB->get_records('xapi_states'));
|
||||
$handler->wipe_states();
|
||||
$this->assertCount(0, $DB->get_records('xapi_states'));
|
||||
|
||||
// Add, at least, one xAPI state record to database (with the default values).
|
||||
test_helper::create_state([], true);
|
||||
|
||||
// Check the state is not wiped if the given state doesn't meet its values.
|
||||
$handler->wipe_states('1');
|
||||
$this->assertCount(1, $DB->get_records('xapi_states'));
|
||||
|
||||
// Check the state is wiped if it exists.
|
||||
$handler->wipe_states();
|
||||
$this->assertCount(0, $DB->get_records('xapi_states'));
|
||||
|
||||
// Check the state is wiped too when using some of the given parameters.
|
||||
test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
|
||||
$this->assertCount(1, $DB->get_records('xapi_states'));
|
||||
$handler->wipe_states('1');
|
||||
$this->assertCount(0, $DB->get_records('xapi_states'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given state and record are equals.
|
||||
*
|
||||
* @param string $component The component name in frankenstyle.
|
||||
* @param state $state The state to check.
|
||||
* @param stdClass $record The record to be compared with the state.
|
||||
*/
|
||||
private function check_state(string $component, state $state, stdClass $record): void {
|
||||
$this->assertEquals($component, $record->component);
|
||||
$this->assertEquals($state->get_activity_id(), $record->itemid);
|
||||
$this->assertEquals($state->get_user()->id, $record->userid);
|
||||
$this->assertEquals(json_encode($state->jsonSerialize()), $record->statedata);
|
||||
$this->assertEquals($state->get_registration(), $record->registration);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user