first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,193 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
use core_xapi\iri;
defined('MOODLE_INTERNAL') || die();
/**
* Contains test cases for testing statement activity class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_activity_test extends advanced_testcase {
/**
* Test item creation.
*/
public function test_creation(): void {
// Activity without definition.
$data = (object) [
'objectType' => 'Activity',
'id' => iri::generate('paella', 'activity'),
];
$item = item_activity::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertEquals($item->get_id(), 'paella');
$this->assertNull($item->get_definition());
// Add optional objectType.
$data->objectType = 'Activity';
$item = item_activity::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
// Add definition.
$data->definition = (object) [
'interactionType' => 'choice',
];
$item = item_activity::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertNotNull($item->get_definition());
}
/**
* Test item creation from string.
*
* @dataProvider create_from_id_provider
* @param string $id Object string ID (IRI or not)
* @param bool $usedefinition if a valir definition must be attached or not
*/
public function test_create_from_id(string $id, bool $usedefinition): void {
$definition = null;
if ($usedefinition) {
$data = (object) [
'type' => iri::generate('example', 'id'),
'interactionType' => 'choice'
];
$definition = item_definition::create_from_data($data);
}
$item = item_activity::create_from_id($id, $definition);
$this->assertEquals($id, $item->get_id());
$itemdefinition = $item->get_definition();
if ($usedefinition) {
$this->assertEquals('choice', $itemdefinition->get_interactiontype());
} else {
$this->assertNull($itemdefinition);
}
// Check generated data.
$data = $item->get_data();
$this->assertEquals('Activity', $data->objectType);
$this->assertEquals(iri::generate($id, 'activity'), $data->id);
if ($usedefinition) {
$this->assertEquals('choice', $data->definition->interactionType);
}
}
/**
* Data provider for the test_create_from_id tests.
*
* @return array
*/
public function create_from_id_provider(): array {
return [
'Fake IRI with no definition' => [
'paella', false,
],
'Fake IRI with definition' => [
'paella', true,
],
'Real IRI with no definition' => [
'http://adlnet.gov/expapi/activities/example', false,
],
'Real IRI with definition' => [
'http://adlnet.gov/expapi/activities/example', true,
],
];
}
/**
* Test for invalid structures.
*
* @dataProvider invalid_data_provider
* @param string $type objectType attribute
* @param string $id activity ID
*/
public function test_invalid_data(string $type, string $id): void {
$data = (object) [
'objectType' => $type,
];
if (!empty($id)) {
$data->id = $id;
}
$this->expectException(xapi_exception::class);
$item = item_activity::create_from_data($data);
}
/**
* Data provider for the test_invalid_data tests.
*
* @return array
*/
public function invalid_data_provider(): array {
return [
'Invalid Avtivity objectType' => [
'Invalid Type!', iri::generate('paella', 'activity'),
],
'Invalid id value' => [
'Activity', 'Invalid_iri_value',
],
'Non-existent id value' => [
'Activity', '',
],
];
}
/**
* Test for missing object type.
*/
public function test_missing_object_type(): void {
$data = (object) ['id' => 42];
$this->expectException(xapi_exception::class);
$item = item_activity::create_from_data($data);
}
/**
* Test for invalid activity objectType.
*/
public function test_inexistent_agent(): void {
global $CFG;
$data = (object) [
'objectType' => 'Invalid',
'id' => -1,
];
$this->expectException(xapi_exception::class);
$item = item_activity::create_from_data($data);
}
}
@@ -0,0 +1,115 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
use core_xapi\iri;
defined('MOODLE_INTERNAL') || die();
/**
* Contains test cases for testing statement actor class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_actor_test extends advanced_testcase {
/**
* Test item creation with agent.
*/
public function test_creation_agent(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$item = item_agent::create_from_user($user);
$data = $item->get_data();
$item = item_actor::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertEquals('core_xapi\local\statement\item_agent', get_class($item));
// Create without specify type.
unset($data->objectType);
$item = item_actor::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertEquals('core_xapi\local\statement\item_agent', get_class($item));
// Check user.
$itemuser = $item->get_user();
$this->assertEquals($itemuser->id, $user->id);
$itemusers = $item->get_all_users();
$this->assertCount(1, $itemusers);
}
/**
* Test item creation with group.
*/
public function test_creation_group(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user->id));
$item = item_group::create_from_group($group);
$data = $item->get_data();
$item = item_actor::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertEquals('core_xapi\local\statement\item_group', get_class($item));
// Check group.
$itemgroup = $item->get_group();
$this->assertEquals($itemgroup->id, $group->id);
$itemusers = $item->get_all_users();
$this->assertCount(1, $itemusers);
// Code must prevent from using group as a single user.
$this->expectException(xapi_exception::class);
$itemusers = $item->get_user();
}
/**
* Test for invalid structures.
*/
public function test_invalid_data(): void {
$this->expectException(xapi_exception::class);
$data = (object) [
'objectType' => 'Fake',
];
$item = item_actor::create_from_data($data);
}
}
@@ -0,0 +1,253 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
use core_xapi\iri;
defined('MOODLE_INTERNAL') || die();
/**
* Contains test cases for testing statement agent class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_agent_test extends advanced_testcase {
/**
* Test item creation.
*/
public function test_create(): void {
global $CFG;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
// Ceate using account.
$data = (object) [
'objectType' => 'Agent',
'account' => (object) [
'homePage' => $CFG->wwwroot,
'name' => $user->id,
],
];
$item = item_agent::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$itemuser = $item->get_user();
$this->assertEquals($itemuser->id, $user->id);
$itemusers = $item->get_all_users();
$this->assertCount(1, $itemusers);
// Ceate using mbox.
$data = (object) [
'objectType' => 'Agent',
'mbox' => $user->email,
];
$item = item_agent::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$itemuser = $item->get_user();
$this->assertEquals($itemuser->id, $user->id);
$itemusers = $item->get_all_users();
$this->assertCount(1, $itemusers);
}
/**
* Test item creation from Record.
*/
public function test_create_from_user(): void {
global $CFG;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$item = item_agent::create_from_user($user);
$itemuser = $item->get_user();
$this->assertEquals($itemuser->id, $user->id);
$itemusers = $item->get_all_users();
$this->assertCount(1, $itemusers);
// Check generated data.
$data = $item->get_data();
$this->assertEquals('Agent', $data->objectType);
$this->assertEquals($CFG->wwwroot, $data->account->homePage);
$this->assertEquals($user->id, $data->account->name);
}
/**
* Test for invalid structures.
*
* @dataProvider invalid_data_provider
* @param string $objecttype object type attribute
* @param bool $validhome if valid homepage is user
* @param bool $validid if valid group id is used
*/
public function test_invalid_data(string $objecttype, bool $validhome, bool $validid): void {
global $CFG;
// Create one course with a group if necessary.
$id = 'Wrong ID';
if ($validid) {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$id = $user->id;
}
$homepage = 'Invalid homepage!';
if ($validhome) {
$homepage = $CFG->wwwroot;
}
$data = (object) [
'objectType' => $objecttype,
'account' => (object) [
'homePage' => $homepage,
'name' => $id,
],
];
$this->expectException(xapi_exception::class);
$item = item_agent::create_from_data($data);
}
/**
* Data provider for the test_invalid_data tests.
*
* @return array
*/
public function invalid_data_provider(): array {
return [
'Wrong objecttype' => [
'Invalid', true, true
],
'Wrong homepage' => [
'Agent', false, true
],
'Wrong id' => [
'Agent', true, false
],
];
}
/**
* Test non supported account identifier xAPI formats.
*
* @dataProvider unspupported_create_provider
* @param bool $usembox
* @param bool $useaccount
* @param bool $usesha1
* @param bool $useopenid
*/
public function test_unspupported_create(bool $usembox, bool $useaccount, bool $usesha1, bool $useopenid): void {
global $CFG;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
// Ceate using both account and mbox.
$data = (object) [
'objectType' => 'Agent'
];
if ($usembox) {
$data->mbox = $user->email;
}
if ($useaccount) {
$data->account = (object) [
'homePage' => $CFG->wwwroot,
'name' => $user->id,
];
}
if ($usesha1) {
$data->mbox_sha1sum = sha1($user->email);
}
if ($useopenid) {
// Note: this is not a real openid, it's just a value to test.
$data->openid = 'https://www.moodle.openid.com/accounts/o8/id';
}
$this->expectException(xapi_exception::class);
$item = item_agent::create_from_data($data);
}
/**
* Data provider for the unsupported identifiers tests.
*
* @return array
*/
public function unspupported_create_provider(): array {
return [
'Both mbox and account' => [
true, true, false, false
],
'Email SHA1' => [
false, false, false, false
],
'Open ID' => [
false, false, false, false
],
];
}
/**
* Test for missing object type.
*/
public function test_missing_object_type(): void {
$data = (object) ['id' => -1];
$this->expectException(xapi_exception::class);
$item = item_agent::create_from_data($data);
}
/**
* Test for invalid user id.
*/
public function test_inexistent_agent(): void {
global $CFG;
$data = (object) [
'objectType' => 'Agent',
'account' => (object) [
'homePage' => $CFG->wwwroot,
'name' => 0,
],
];
$this->expectException(xapi_exception::class);
$item = item_agent::create_from_data($data);
}
/**
* Test for invalid agent record.
*/
public function test_inexistent_agent_id(): void {
$user = (object) ['name' => 'Me'];
$this->expectException(xapi_exception::class);
$item = item_agent::create_from_user($user);
}
}
@@ -0,0 +1,122 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\iri;
use core_xapi\xapi_exception;
/**
* Contains test cases for testing statement attachment class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_attachment_test extends advanced_testcase {
/**
* Test item creation.
*/
public function test_create(): void {
$data = $this->get_generic_data();
$item = item_attachment::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
}
/**
* return a generic data to create a valid item.
*
* @return \stdClass the creation data
*/
private function get_generic_data(): \stdClass {
return (object) [
'usageType' => iri::generate('example', 'attachment'),
'display' => (object) [
'en-US' => 'Example',
],
'description' => (object) [
'en-US' => 'Description example',
],
"contentType" => "image/jpg",
"length" => 1234,
"sha2" => "b94c0f1cffb77475c6f1899111a0181efe1d6177"
];
}
/**
* Test for invalid values.
*
* @dataProvider invalid_values_data
* @param string $attr attribute to modify
* @param mixed $newvalue new value (null means unset)
*/
public function test_invalid_values(string $attr, $newvalue): void {
$data = $this->get_generic_data();
if ($newvalue === null) {
unset($data->$attr);
} else {
$data->$attr = $newvalue;
}
$this->expectException(xapi_exception::class);
$item = item_attachment::create_from_data($data);
}
/**
* Data provider for the test_invalid_values tests.
*
* @return array
*/
public function invalid_values_data(): array {
return [
'No usageType attachment' => [
'usageType', null
],
'Invalid usageType attachment' => [
'usageType', 'Invalid IRI'
],
'No display attachment' => [
'display', null
],
'No contentType attachment' => [
'contentType', null
],
'No length attachment' => [
'length', null
],
'Invalid length attachment' => [
'length', 'Invalid'
],
'No sha2 attachment' => [
'sha2', null
],
];
}
}
@@ -0,0 +1,62 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
/**
* Contains test cases for testing statement context class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_context_test extends advanced_testcase {
/**
* Test item creation.
*/
public function test_create(): void {
$data = $this->get_generic_data();
$item = item_context::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
}
/**
* Return a generic data to create a valid item.
*
* @return sdtClass the creation data
*/
private function get_generic_data(): \stdClass {
// For now context has no data validation so a generic data is enough.
return (object) [
'usageType' => '51a6f860-1997-11e3-8ffd-0800200c9a66',
];
}
}
@@ -0,0 +1,104 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
use core_xapi\iri;
defined('MOODLE_INTERNAL') || die();
/**
* Contains test cases for testing statement definition class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_definition_test extends advanced_testcase {
/**
* Test item_definition creation.
*
* @dataProvider creation_provider
* @param string $interactiontype
*/
public function test_creation(string $interactiontype): void {
// Activity without interactionType.
$data = (object) [
'type' => iri::generate('example', 'id'),
];
// Add interactionType.
if (!empty($interactiontype)) {
$data->interactionType = $interactiontype;
}
$item = item_definition::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
if (empty($interactiontype)) {
$this->assertNull($item->get_interactiontype());
} else {
$this->assertEquals($interactiontype, $item->get_interactiontype());
}
}
/**
* Data provider for the test_creation tests.
*
* @return array
*/
public function creation_provider(): array {
return [
'No interactionType' => [''],
'Choice' => ['choice'],
'fill-in' => ['fill-in'],
'long-fill-in' => ['long-fill-in'],
'true-false' => ['true-false'],
'matching' => ['matching'],
'performance' => ['performance'],
'sequencing' => ['sequencing'],
'likert' => ['likert'],
'numeric' => ['numeric'],
'other' => ['other'],
'compound' => ['compound'],
];
}
/**
* Test for invalid structures.
*/
public function test_invalid_data(): void {
// Activity without interactionType.
$data = (object) [
'interactionType' => 'Invalid value!',
];
$this->expectException(xapi_exception::class);
$item = item_definition::create_from_data($data);
}
}
@@ -0,0 +1,204 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
use core_xapi\iri;
defined('MOODLE_INTERNAL') || die();
/**
* Contains test cases for testing statement group class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_group_test extends advanced_testcase {
/**
* Test item creation.
*/
public function test_create(): void {
global $CFG;
$this->resetAfterTest();
// Create one course with a group.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$user2 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user2->id));
$data = (object) [
'objectType' => 'Group',
'account' => (object) [
'homePage' => $CFG->wwwroot,
'name' => $group->id,
],
];
$item = item_group::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$itemgroup = $item->get_group();
$this->assertEquals($itemgroup->id, $group->id);
$itemusers = $item->get_all_users();
$this->assertCount(2, $itemusers);
// Get user in group item must throw an exception.
$this->expectException(xapi_exception::class);
$itemusers = $item->get_user();
}
/**
* Test item creation from Record.
*/
public function test_create_from_group(): void {
global $CFG;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$item = item_group::create_from_group($group);
$itemgroup = $item->get_group();
$this->assertEquals($itemgroup->id, $group->id);
// Check generated data.
$data = $item->get_data();
$this->assertEquals('Group', $data->objectType);
$this->assertEquals($CFG->wwwroot, $data->account->homePage);
$this->assertEquals($group->id, $data->account->name);
}
/**
* Test for invalid structures.
*
* @dataProvider invalid_data_provider
* @param string $objecttype object type attribute
* @param bool $validhome if valid homepage is user
* @param bool $validid if valid group id is used
*/
public function test_invalid_data(string $objecttype, bool $validhome, bool $validid): void {
global $CFG;
// Create one course with a group if necessary.
$id = 'Wrong ID';
if ($validid) {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$id = $group->id;
}
$homepage = 'Invalid homepage!';
if ($validhome) {
$homepage = $CFG->wwwroot;
}
$data = (object) [
'objectType' => $objecttype,
'account' => (object) [
'homePage' => $homepage,
'name' => $id,
],
];
$this->expectException(xapi_exception::class);
$item = item_group::create_from_data($data);
}
/**
* Data provider for the test_invalid_data tests.
*
* @return array
*/
public function invalid_data_provider(): array {
return [
'Wrong objecttype' => [
'Invalid', true, true
],
'Wrong homepage' => [
'Group', false, true
],
'Wrong id' => [
'Group', true, false
],
];
}
/**
* Test for missing object type.
*/
public function test_missing_object_type(): void {
$data = (object) ['id' => -1];
$this->expectException(xapi_exception::class);
$item = item_group::create_from_data($data);
}
/**
* Test for invalid anonymous group.
*/
public function test_invalid_anonymous_group(): void {
$data = (object) [
'objectType' => 'Group'
];
$this->expectException(xapi_exception::class);
$item = item_group::create_from_data($data);
}
/**
* Test for invalid anonymous group.
*/
public function test_inexistent_group(): void {
global $CFG;
$data = (object) [
'objectType' => 'Group',
'account' => (object) [
'homePage' => $CFG->wwwroot,
'name' => 0,
],
];
$this->expectException(xapi_exception::class);
$item = item_group::create_from_data($data);
}
/**
* Test for invalid group record.
*/
public function test_inexistent_group_id(): void {
$group = (object) ['name' => 'My Group'];
$this->expectException(xapi_exception::class);
$item = item_group::create_from_group($group);
}
}
@@ -0,0 +1,130 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
use core_xapi\iri;
defined('MOODLE_INTERNAL') || die();
/**
* Contains test cases for testing statement object class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_object_test extends advanced_testcase {
/**
* Test item creation with agent.
*/
public function test_creation_agent(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$item = item_agent::create_from_user($user);
$data = $item->get_data();
$item = item_object::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertEquals('core_xapi\local\statement\item_agent', get_class($item));
}
/**
* Test item creation with group.
*/
public function test_creation_group(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$item = item_group::create_from_group($group);
$data = $item->get_data();
$item = item_object::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertEquals('core_xapi\local\statement\item_group', get_class($item));
}
/**
* Test item creation with activity.
*/
public function test_creation_activity(): void {
$item = item_activity::create_from_id('paella');
$data = $item->get_data();
$item = item_object::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertEquals('core_xapi\local\statement\item_activity', get_class($item));
}
/**
* Test unsupported item creation.
*/
public function test_unsupported_activity(): void {
$this->expectException(xapi_exception::class);
$data = (object) [
'objectType' => 'FakeType',
'id' => -1,
];
$item = item_object::create_from_data($data);
}
/**
* Test for invalid structures.
*
* @dataProvider invalid_data_provider
* @param string $id
*/
public function test_invalid_data(string $id): void {
$this->expectException(xapi_exception::class);
$data = (object) [
'id' => $id,
];
$item = item_object::create_from_data($data);
}
/**
* Data provider for the test_invalid_data tests.
*
* @return array
*/
public function invalid_data_provider(): array {
return [
'Empty or null id' => [
'',
],
'Invalid IRI value' => [
'invalid_iri_value',
],
];
}
}
@@ -0,0 +1,138 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
/**
* Contains test cases for testing statement result class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_result_test extends advanced_testcase {
/**
* Test item creation.
*/
public function test_creation(): void {
$data = $this->get_generic_data();
$item = item_result::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertNull($item->get_duration());
$score = $item->get_score();
$this->assertEquals(json_encode($score), json_encode($data->score));
}
/**
* Return a generic data to create a valid item.
*
* @return \stdClass the creation data
*/
private function get_generic_data(): \stdClass {
return (object) [
'score' => (object)[
'min' => 0,
'max' => 100,
'raw' => 50,
'scaled' => 0.5,
],
'completion' => true,
'success' => true,
];
}
/**
* Test for duration values.
*
* @dataProvider duration_values_data
* @param string|null $duration specified duration
* @param int|null $seconds calculated seconds
* @param bool $exception if exception is expected
*/
public function test_duration_values(?string $duration, ?int $seconds, bool $exception): void {
if ($exception) {
$this->expectException(xapi_exception::class);
}
$data = $this->get_generic_data();
if ($duration !== null) {
$data->duration = $duration;
}
$item = item_result::create_from_data($data);
$this->assertEquals($seconds, $item->get_duration());
}
/**
* Data provider for the test_duration_values tests.
*
* @return array
*/
public function duration_values_data(): array {
return [
'No duration' => [
null, null, false
],
'Empty duration' => [
'', null, false
],
'1 minute duration' => [
'PT1M', 60, false
],
'1 hour duration' => [
'PT1H', 3600, false
],
'1 second duration' => [
'PT1S', 1, false
],
'1.11 second duration (dot variant)' => [
'PT1.11S', 1, false
],
'1,11 second duration (comma variant)' => [
'PT1.11S', 1, false
],
'90 minutes 5 seconds duration' => [
'PT1H30M5S', 5405, false
],
'90 minutes 05 seconds duration' => [
'PT1H30M05S', 5405, false
],
'Half year duration' => [
'P0.5Y', null, true
],
'Incorrect format' => [
'INVALID', null, true
],
];
}
}
@@ -0,0 +1,56 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
/**
* Contains test cases for testing statement score class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_score_test extends advanced_testcase {
/**
* Test item creation.
*/
public function test_create(): void {
$data = (object) [
'scaled' => 0.5,
'raw' => 5,
'min' => 0,
'max' => 10,
];
$item = item_score::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
}
}
@@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
defined('MOODLE_INTERNAL') || die();
/**
* Contains test cases for testing statement base class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_test extends advanced_testcase {
/**
* Test item creation.
*/
public function test_create(): void {
// This is a generic item so check that it can create and item and json encode later.
$data = (object) [
'this' => 'is',
'just' => 1,
'example' => ['of', 'structure'],
];
$item = item::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
}
}
@@ -0,0 +1,118 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local\statement;
use advanced_testcase;
use core_xapi\xapi_exception;
use core_xapi\iri;
defined('MOODLE_INTERNAL') || die();
/**
* Contains test cases for testing statement verb class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_verb_test extends advanced_testcase {
/**
* Test item creation.
*/
public function test_creation(): void {
$data = (object) [
'id' => iri::generate('cook', 'verb'),
];
$item = item_verb::create_from_data($data);
$this->assertEquals(json_encode($item), json_encode($data));
$this->assertEquals($item->get_id(), 'cook');
}
/**
* Test item creation from string.
*
* @dataProvider create_from_id_provider
* @param string $id Object string ID (IRI or not)
*/
public function test_create_from_id(string $id): void {
$item = item_verb::create_from_id($id);
$this->assertEquals($id, $item->get_id());
// Check generated data.
$data = $item->get_data();
$this->assertEquals(iri::generate($id, 'verb'), $data->id);
}
/**
* Data provider for the test_create_from_id tests.
*
* @return array
*/
public function create_from_id_provider(): array {
return [
'Fake IRI' => [
'cook',
],
'Real IRI' => [
'http://adlnet.gov/expapi/verb/example',
],
];
}
/**
* Test for invalid structures.
*
* @dataProvider invalid_data_provider
* @param string $id
*/
public function test_invalid_data(string $id): void {
$this->expectException(xapi_exception::class);
$data = (object) [
'id' => $id,
];
$item = item_verb::create_from_data($data);
}
/**
* Data provider for the test_invalid_data tests.
*
* @return array
*/
public function invalid_data_provider(): array {
return [
'Empty or null id' => [
'',
],
'Invalid IRI value' => [
'invalid_iri_value',
],
];
}
}
+588
View File
@@ -0,0 +1,588 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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\local;
use core_xapi\local\statement\item;
use core_xapi\local\statement\item_actor;
use core_xapi\local\statement\item_object;
use core_xapi\local\statement\item_activity;
use core_xapi\local\statement\item_verb;
use core_xapi\local\statement\item_agent;
use core_xapi\local\statement\item_group;
use core_xapi\local\statement\item_result;
use core_xapi\local\statement\item_attachment;
use core_xapi\local\statement\item_context;
use core_xapi\iri;
use core_xapi\xapi_exception;
use advanced_testcase;
use stdClass;
defined('MOODLE_INTERNAL') || die();
/**
* Contains test cases for testing statement class.
*
* @package core_xapi
* @since Moodle 3.9
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class statement_test extends advanced_testcase {
/**
* Returns a valid item for a specific attribute.
*
* @param string $itemname statement item name
* @return item the resulting item
*/
private function get_valid_item(string $itemname): item {
global $USER, $CFG;
switch ($itemname) {
case 'attachments':
case 'attachment':
$data = (object) [
'usageType' => iri::generate('example', 'attachment'),
'display' => (object) [
'en-US' => 'Example',
],
'description' => (object) [
'en-US' => 'Description example',
],
"contentType" => "image/jpg",
"length" => 1234,
"sha2" => "b94c0f1cffb77475c6f1899111a0181efe1d6177"
];
return item_attachment::create_from_data($data);
case 'authority':
$data = (object) [
'objectType' => 'Agent',
'account' => (object) [
'homePage' => $CFG->wwwroot,
'name' => $USER->id,
],
];
return item_agent::create_from_data($data);
}
// For now, the rest of the optional properties have no validation
// so we create a standard stdClass for all of them.
$data = (object)[
'some' => 'data',
];
$classname = 'core_xapi\local\statement\item_'.$itemname;
if (class_exists($classname)) {
$item = $classname::create_from_data($data);
} else {
$item = item::create_from_data($data);
}
return $item;
}
/**
* Test statement creation.
*
* @dataProvider create_provider
* @param bool $useagent if use agent as actor (or group if false)
* @param array $extras extra item elements
* @param array $extravalues extra string values
*/
public function test_create(bool $useagent, array $extras, array $extravalues): void {
$this->resetAfterTest();
// Create one course with a group.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user->id));
$this->setUser($user);
// Our statement.
$statement = new statement();
// Populate statement.
if ($useagent) {
$statement->set_actor(item_agent::create_from_user($user));
} else {
$statement->set_actor(item_group::create_from_group($group));
}
$statement->set_verb(item_verb::create_from_id('cook'));
$statement->set_object(item_activity::create_from_id('paella'));
foreach ($extras as $extra) {
$method = 'set_'.$extra;
$item = $this->get_valid_item($extra);
$statement->$method($item);
}
// For now extra values have no validation.
foreach ($extravalues as $extra) {
$method = 'set_'.$extra;
$statement->$method('Example');
}
// Check resulting statement.
if ($useagent) {
$stuser = $statement->get_user();
$this->assertEquals($user->id, $stuser->id);
$stusers = $statement->get_all_users();
$this->assertCount(1, $stusers);
} else {
$stgroup = $statement->get_group();
$this->assertEquals($group->id, $stgroup->id);
$stusers = $statement->get_all_users();
$this->assertCount(1, $stusers);
$stuser = array_shift($stusers);
$this->assertEquals($user->id, $stuser->id);
}
$this->assertEquals('cook', $statement->get_verb_id());
$this->assertEquals('paella', $statement->get_activity_id());
// Check resulting json (only first node structure, internal structure
// depends on every item json_encode test).
$data = json_decode(json_encode($statement));
$this->assertNotEmpty($data->actor);
$this->assertNotEmpty($data->verb);
$this->assertNotEmpty($data->object);
$allextras = ['context', 'result', 'timestamp', 'stored', 'authority', 'version', 'attachments'];
$alldefined = array_merge($extras, $extravalues);
foreach ($allextras as $extra) {
if (in_array($extra, $alldefined)) {
$this->assertObjectHasProperty($extra, $data);
$this->assertNotEmpty($data->$extra);
} else {
$this->assertObjectNotHasProperty($extra, $data);
}
}
}
/**
* Data provider for the test_create and test_create_from_data tests.
*
* @return array
*/
public function create_provider(): array {
return [
'Agent statement with no extras' => [
true, [], []
],
'Agent statement with context' => [
true, ['context'], []
],
'Agent statement with result' => [
true, ['result'], []
],
'Agent statement with timestamp' => [
true, [], ['timestamp']
],
'Agent statement with stored' => [
true, [], ['stored']
],
'Agent statement with authority' => [
true, ['authority'], []
],
'Agent statement with version' => [
true, [], ['version']
],
'Group statement with no extras' => [
false, [], []
],
'Group statement with context' => [
false, ['context'], []
],
'Group statement with result' => [
false, ['result'], []
],
'Group statement with timestamp' => [
false, [], ['timestamp']
],
'Group statement with stored' => [
false, [], ['stored']
],
'Group statement with authority' => [
false, ['authority'], []
],
'Group statement with version' => [
false, [], ['version']
],
];
}
/**
* Test statement creation from xAPI statement data.
*
* @dataProvider create_provider
* @param bool $useagent if use agent as actor (or group if false)
* @param array $extras extra item elements
* @param array $extravalues extra string values
*/
public function test_create_from_data(bool $useagent, array $extras, array $extravalues): void {
$this->resetAfterTest();
// Create one course with a group.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user->id));
$this->setUser($user);
// Populate data.
if ($useagent) {
$actor = item_agent::create_from_user($user);
} else {
$actor = item_group::create_from_group($group);
}
$verb = item_verb::create_from_id('cook');
$object = item_activity::create_from_id('paella');
$data = (object) [
'actor' => $actor->get_data(),
'verb' => $verb->get_data(),
'object' => $object->get_data(),
];
foreach ($extras as $extra) {
$item = $this->get_valid_item($extra);
$data->$extra = $item->get_data();
}
// For now extra values have no validation.
foreach ($extravalues as $extra) {
$data->$extra = 'Example';
}
$statement = statement::create_from_data($data);
// Check resulting statement.
if ($useagent) {
$stuser = $statement->get_user();
$this->assertEquals($user->id, $stuser->id);
$stusers = $statement->get_all_users();
$this->assertCount(1, $stusers);
} else {
$stgroup = $statement->get_group();
$this->assertEquals($group->id, $stgroup->id);
$stusers = $statement->get_all_users();
$this->assertCount(1, $stusers);
$stuser = array_shift($stusers);
$this->assertEquals($user->id, $stuser->id);
}
$this->assertEquals('cook', $statement->get_verb_id());
$this->assertEquals('paella', $statement->get_activity_id());
// Check resulting json (only first node structure, internal structure
// depends on every item json_encode test).
$data = json_decode(json_encode($statement));
$this->assertNotEmpty($data->actor);
$this->assertNotEmpty($data->verb);
$this->assertNotEmpty($data->object);
$allextras = ['context', 'result', 'timestamp', 'stored', 'authority', 'version', 'attachments'];
$alldefined = array_merge($extras, $extravalues);
foreach ($allextras as $extra) {
if (in_array($extra, $alldefined)) {
$this->assertObjectHasProperty($extra, $data);
$this->assertNotEmpty($data->object);
} else {
$this->assertObjectNotHasProperty($extra, $data);
}
}
}
/**
* Test adding attachments to statement.
*
*/
public function test_add_attachment(): void {
// Our statement.
$statement = new statement();
$attachments = $statement->get_attachments();
$this->assertNull($attachments);
$item = $this->get_valid_item('attachment');
$itemdata = $item->get_data();
$statement->add_attachment($item);
$attachments = $statement->get_attachments();
$this->assertNotNull($attachments);
$this->assertCount(1, $attachments);
$attachment = current($attachments);
$attachmentdata = $attachment->get_data();
$this->assertEquals($itemdata->usageType, $attachmentdata->usageType);
$this->assertEquals($itemdata->length, $attachmentdata->length);
// Check resulting json.
$statementdata = json_decode(json_encode($statement));
$this->assertObjectHasProperty('attachments', $statementdata);
$this->assertNotEmpty($statementdata->attachments);
$this->assertCount(1, $statementdata->attachments);
}
/**
* Test adding attachments to statement.
*
*/
public function test_add_attachment_from_data(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$actor = item_agent::create_from_user($user);
$verb = item_verb::create_from_id('cook');
$object = item_activity::create_from_id('paella');
$data = (object) [
'actor' => $actor->get_data(),
'verb' => $verb->get_data(),
'object' => $object->get_data(),
];
$item = $this->get_valid_item('attachment');
$itemdata = $item->get_data();
$data->attachments = [$itemdata];
$statement = statement::create_from_data($data);
$attachments = $statement->get_attachments();
$this->assertNotNull($attachments);
$this->assertCount(1, $attachments);
$attachment = current($attachments);
$attachmentdata = $attachment->get_data();
$this->assertEquals($itemdata->usageType, $attachmentdata->usageType);
$this->assertEquals($itemdata->length, $attachmentdata->length);
$statementdata = json_decode(json_encode($statement));
$this->assertObjectHasProperty('attachments', $statementdata);
$this->assertNotEmpty($statementdata->attachments);
$this->assertCount(1, $statementdata->attachments);
// Now try to send an invalid attachments.
$this->expectException(xapi_exception::class);
$data->attachments = 'Invalid data';
$statement = statement::create_from_data($data);
}
/**
* Test all getters into a not set statement.
*
* @dataProvider invalid_gets_provider
* @param string $method the method to test
* @param bool $exception if an exception is expected
*/
public function test_invalid_gets(string $method, bool $exception): void {
$statement = new statement();
if ($exception) {
$this->expectException(xapi_exception::class);
}
$result = $statement->$method();
$this->assertNull($result);
}
/**
* Data provider for the text_invalid_gets.
*
* @return array
*/
public function invalid_gets_provider(): array {
return [
'Method get_user on empty statement' => ['get_user', true],
'Method get_all_users on empty statement' => ['get_all_users', true],
'Method get_group on empty statement' => ['get_group', true],
'Method get_verb_id on empty statement' => ['get_verb_id', true],
'Method get_activity_id on empty statement' => ['get_activity_id', true],
'Method get_actor on empty statement' => ['get_actor', false],
'Method get_verb on empty statement' => ['get_verb', false],
'Method get_object on empty statement' => ['get_object', false],
'Method get_context on empty statement' => ['get_context', false],
'Method get_result on empty statement' => ['get_result', false],
'Method get_timestamp on empty statement' => ['get_timestamp', false],
'Method get_stored on empty statement' => ['get_stored', false],
'Method get_authority on empty statement' => ['get_authority', false],
'Method get_version on empty statement' => ['get_version', false],
'Method get_attachments on empty statement' => ['get_attachments', false],
];
}
/**
* Try to get a user from a group statement.
*/
public function test_invalid_get_user(): void {
$this->resetAfterTest();
// Create one course with a group.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user->id));
// Our statement.
$statement = new statement();
// Populate statement.
$statement->set_actor(item_group::create_from_group($group));
$statement->set_verb(item_verb::create_from_id('cook'));
$statement->set_object(item_activity::create_from_id('paella'));
$this->expectException(xapi_exception::class);
$statement->get_user();
}
/**
* Try to get a group from an agent statement.
*/
public function test_invalid_get_group(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
// Our statement.
$statement = new statement();
// Populate statement.
$statement->set_actor(item_agent::create_from_user($user));
$statement->set_verb(item_verb::create_from_id('cook'));
$statement->set_object(item_activity::create_from_id('paella'));
$this->expectException(xapi_exception::class);
$statement->get_group();
}
/**
* Try to get activity Id from a statement with agent object.
*/
public function test_invalid_get_activity_id(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
// Our statement.
$statement = new statement();
// Populate statement with and agent object.
$statement->set_actor(item_agent::create_from_user($user));
$statement->set_verb(item_verb::create_from_id('cook'));
$statement->set_object(item_agent::create_from_user($user));
$this->expectException(xapi_exception::class);
$statement->get_activity_id();
}
/**
* Test for invalid structures.
*
* @dataProvider invalid_data_provider
* @param bool $useuser if use user into statement
* @param bool $userverb if use verb into statement
* @param bool $useobject if use object into statement
*/
public function test_invalid_data(bool $useuser, bool $userverb, bool $useobject): void {
$data = new stdClass();
if ($useuser) {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$data->actor = item_agent::create_from_user($user);
}
if ($userverb) {
$data->verb = item_verb::create_from_id('cook');
}
if ($useobject) {
$data->object = item_activity::create_from_id('paella');
}
$this->expectException(xapi_exception::class);
$statement = statement::create_from_data($data);
}
/**
* Data provider for the test_invalid_data tests.
*
* @return array
*/
public function invalid_data_provider(): array {
return [
'No actor, no verb, no object' => [false, false, false],
'No actor, verb, no object' => [false, true, false],
'No actor, no verb, object' => [false, false, true],
'No actor, verb, object' => [false, true, true],
'Actor, no verb, no object' => [true, false, false],
'Actor, verb, no object' => [true, true, false],
'Actor, no verb, object' => [true, false, true],
];
}
/**
* Test minify statement.
*/
public function test_minify(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Our statement.
$statement = new statement();
// Populate statement.
$statement->set_actor(item_agent::create_from_user($user));
$statement->set_verb(item_verb::create_from_id('cook'));
$statement->set_object(item_activity::create_from_id('paella'));
$statement->set_result($this->get_valid_item('result'));
$statement->set_context($this->get_valid_item('context'));
$statement->set_authority($this->get_valid_item('authority'));
$statement->add_attachment($this->get_valid_item('attachment'));
$statement->set_version('Example');
$statement->set_timestamp('Example');
$statement->set_stored('Example');
$min = $statement->minify();
// Check calculated fields.
$this->assertCount(6, $min);
$this->assertArrayNotHasKey('actor', $min);
$this->assertArrayHasKey('verb', $min);
$this->assertArrayHasKey('object', $min);
$this->assertArrayHasKey('context', $min);
$this->assertArrayHasKey('result', $min);
$this->assertArrayNotHasKey('timestamp', $min);
$this->assertArrayNotHasKey('stored', $min);
$this->assertArrayHasKey('authority', $min);
$this->assertArrayNotHasKey('version', $min);
$this->assertArrayHasKey('attachments', $min);
}
}