Skip to content

Commit

Permalink
Merge branch 'MDL-67192-master' of git://github.com/vmdef/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Feb 10, 2020
2 parents 4e90332 + fe9bb50 commit 6ec4ed6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 18 deletions.
65 changes: 55 additions & 10 deletions h5p/classes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static function get_scripts(): array {
*/
public function fetch_latest_content_types(): ?\stdClass {

$contenttypes = self::get_latest_content_types();
$contenttypes = $this->get_latest_content_types();
if (!empty($contenttypes->error)) {
return $contenttypes;
}
Expand All @@ -184,7 +184,7 @@ public function fetch_latest_content_types(): ?\stdClass {
'machineName' => $type->id,
'majorVersion' => $type->version->major,
'minorVersion' => $type->version->minor,
'patchVersion' => $type->version->patch
'patchVersion' => $type->version->patch,
];

$shoulddownload = true;
Expand All @@ -197,7 +197,7 @@ public function fetch_latest_content_types(): ?\stdClass {
if ($shoulddownload) {
$installed['id'] = $this->fetch_content_type($library);
if ($installed['id']) {
$installed['name'] = $librarykey = \H5PCore::libraryToString($library);
$installed['name'] = \H5PCore::libraryToString($library);
$typesinstalled[] = $installed;
}
}
Expand Down Expand Up @@ -256,14 +256,21 @@ public function fetch_content_type(array $library): ?int {
/**
* Get H5P endpoints.
*
* If $library is null, moodle_url is the endpoint of the latest version of the H5P content types. If library is the
* machine name of a content type, moodle_url is the endpoint to download the content type.
* If $endpoint = 'content' and $library is null, moodle_url is the endpoint of the latest version of the H5P content
* types; however, if $library is the machine name of a content type, moodle_url is the endpoint to download the content type.
* The SITES endpoint ($endpoint = 'site') may be use to get a site UUID or send site data.
*
* @param string|null $library The machineName of the library whose endpoint is requested.
* @param string $endpoint The endpoint required. Valid values: "site", "content".
* @return moodle_url The endpoint moodle_url object.
*/
public function get_api_endpoint(?string $library): moodle_url {
$h5purl = \H5PHubEndpoints::createURL(\H5PHubEndpoints::CONTENT_TYPES ) . $library;
public function get_api_endpoint(?string $library = null, string $endpoint = 'content'): moodle_url {
if ($endpoint == 'site') {
$h5purl = \H5PHubEndpoints::createURL(\H5PHubEndpoints::SITES );
} else if ($endpoint == 'content') {
$h5purl = \H5PHubEndpoints::createURL(\H5PHubEndpoints::CONTENT_TYPES ) . $library;
}

return new moodle_url($h5purl);
}

Expand All @@ -275,9 +282,11 @@ public function get_api_endpoint(?string $library): moodle_url {
* - array contentTypes: an object for each H5P content type with its information
*/
public function get_latest_content_types(): \stdClass {
$siteuuid = $this->get_site_uuid() ?? md5($CFG->wwwroot);
$postdata = ['uuid' => $siteuuid];

// Get the latest content-types json.
$postdata = ['uuid' => 'foo'];
$endpoint = $this->get_api_endpoint(null);
$endpoint = $this->get_api_endpoint();
$request = download_file_content($endpoint, null, $postdata, true);

if (!empty($request->error) || $request->status != '200' || empty($request->results)) {
Expand All @@ -293,6 +302,43 @@ public function get_latest_content_types(): \stdClass {
return $contenttypes;
}

/**
* Get the site UUID. If site UUID is not defined, try to register the site.
*
* return $string The site UUID, null if it is not set.
*/
public function get_site_uuid(): ?string {
// Check if the site_uuid is already set.
$siteuuid = get_config('core_h5p', 'site_uuid');

if (!$siteuuid) {
$siteuuid = $this->register_site();
}

return $siteuuid;
}

/**
* Get H5P generated site UUID.
*
* return ?string Returns H5P generated site UUID, null if can't get it.
*/
private function register_site(): ?string {
$endpoint = $this->get_api_endpoint(null, 'site');
$siteuuid = download_file_content($endpoint, null, '');

// Successful UUID retrieval from H5P.
if ($siteuuid) {
$json = json_decode($siteuuid);
if (isset($json->uuid)) {
set_config('site_uuid', $json->uuid, 'core_h5p');
return $json->uuid;
}
}

return null;
}

/**
* Checks that the required H5P core API version or higher is installed.
*
Expand All @@ -308,5 +354,4 @@ public function is_required_core_api($coreapi): bool {
}
return true;
}

}
25 changes: 24 additions & 1 deletion h5p/tests/h5p_core_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @runTestsInSeparateProcesses
*/
class h5p_core_test extends \advanced_testcase {
class h5p_core_testcase extends \advanced_testcase {

protected function setup() {
global $CFG;
Expand Down Expand Up @@ -147,4 +147,27 @@ public function test_fetch_latest_content_types(): void {
$this->assertEquals($numcontenttypes, count($contentfiles));
$this->assertCount(0, $result->typesinstalled);
}

/**
* Test that if site_uuid is not set, the site is registered and site_uuid is set.
*
*/
public function test_get_site_uuid(): void {
$this->resetAfterTest(true);

if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}

// Check that site_uuid does not have a value.
$this->assertFalse(get_config('core_h5p', 'site_uuid'));

$siteuuid = $this->core->get_site_uuid();

$this->assertSame($siteuuid, get_config('core_h5p', 'site_uuid'));

// Check that after a new request the site_uuid remains the same.
$siteuuid2 = $this->core->get_site_uuid();
$this->assertEquals( $siteuuid, $siteuuid2);
}
}
16 changes: 10 additions & 6 deletions lib/tests/fixtures/testable_core_h5p.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,22 @@ public function set_endpoint($url): void {
/**
* Get the URL of the test endpoints instead of the H5P ones.
*
* If $library is null, moodle_url is the endpoint of the json test file with the H5P content types definition. If library is
* the machine name of a content type, moodle_url is the test URL for downloading the content type file.
* If $endpoint = 'content' and $library is null, moodle_url is the endpoint of the latest version of the H5P content
* types; however, if $library is the machine name of a content type, moodle_url is the endpoint to download the content type.
* The SITES endpoint ($endpoint = 'site') may be use to get a site UUID or send site data.
*
* @param string|null $library The filename of the H5P content type file in external.
* @return \moodle_url The moodle_url of the file in external.
* @param string|null $library The machineName of the library whose endpoint is requested.
* @param string $endpoint The endpoint required. Valid values: "site", "content".
* @return \moodle_url The endpoint moodle_url object.
*/
public function get_api_endpoint(?string $library): \moodle_url {
public function get_api_endpoint(?string $library = null, string $endpoint = 'content'): \moodle_url {

if ($library) {
$h5purl = $this->endpoint . '/' . $library . '.h5p';
} else if ($endpoint == 'content') {
$h5purl = $this->endpoint . '/h5pcontenttypes.json';
} else {
$h5purl = $h5purl = $this->endpoint . '/h5pcontenttypes.json';
$h5purl = $this->endpoint . '/h5puuid.json';
}

return new \moodle_url($h5purl);
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/h5p_get_content_types_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @runTestsInSeparateProcesses
*/
class h5p_get_content_types_task_test extends advanced_testcase {
class h5p_get_content_types_task_testcase extends advanced_testcase {

protected function setup() {
global $CFG;
Expand Down

0 comments on commit 6ec4ed6

Please sign in to comment.