From a23969a5fa8c7ceab03377ec5cf00b517635359a Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=ADctor=20D=C3=A9niz=20Falc=C3=B3n?= Date: Wed, 5 Feb 2020 15:22:12 +0000 Subject: [PATCH] MDL-67192 core_h5p: get site UUID from H5P using the API --- h5p/classes/core.php | 63 ++++++++++++++++--- h5p/tests/h5p_core_test.php | 25 +++++++- lib/tests/fixtures/testable_core_h5p.php | 16 +++-- lib/tests/h5p_get_content_types_task_test.php | 2 +- 4 files changed, 89 insertions(+), 17 deletions(-) diff --git a/h5p/classes/core.php b/h5p/classes/core.php index 660396d0de0..631e0e24b2b 100644 --- a/h5p/classes/core.php +++ b/h5p/classes/core.php @@ -164,7 +164,7 @@ class core extends \H5PCore { */ 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; } @@ -184,7 +184,7 @@ class core extends \H5PCore { 'machineName' => $type->id, 'majorVersion' => $type->version->major, 'minorVersion' => $type->version->minor, - 'patchVersion' => $type->version->patch + 'patchVersion' => $type->version->patch, ]; $shoulddownload = true; @@ -256,14 +256,21 @@ class core extends \H5PCore { /** * 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); } @@ -275,9 +282,11 @@ class core extends \H5PCore { * - 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)) { @@ -293,6 +302,43 @@ class core extends \H5PCore { 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. * @@ -308,5 +354,4 @@ class core extends \H5PCore { } return true; } - } diff --git a/h5p/tests/h5p_core_test.php b/h5p/tests/h5p_core_test.php index 27d8882f10d..648b64e99b4 100644 --- a/h5p/tests/h5p_core_test.php +++ b/h5p/tests/h5p_core_test.php @@ -36,7 +36,7 @@ defined('MOODLE_INTERNAL') || die(); * * @runTestsInSeparateProcesses */ -class h5p_core_test extends \advanced_testcase { +class h5p_core_testcase extends \advanced_testcase { protected function setup() { global $CFG; @@ -147,4 +147,27 @@ class h5p_core_test extends \advanced_testcase { $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); + } } diff --git a/lib/tests/fixtures/testable_core_h5p.php b/lib/tests/fixtures/testable_core_h5p.php index 1914c4f5379..282ca89e693 100644 --- a/lib/tests/fixtures/testable_core_h5p.php +++ b/lib/tests/fixtures/testable_core_h5p.php @@ -83,18 +83,22 @@ class h5p_test_core extends core { /** * 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); diff --git a/lib/tests/h5p_get_content_types_task_test.php b/lib/tests/h5p_get_content_types_task_test.php index 3ba655d7388..21dd1850d92 100644 --- a/lib/tests/h5p_get_content_types_task_test.php +++ b/lib/tests/h5p_get_content_types_task_test.php @@ -36,7 +36,7 @@ defined('MOODLE_INTERNAL') || die(); * * @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; -- 2.43.0