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',
],
];
}
}