diff --git a/lib/classes/plugin_manager.php b/lib/classes/plugin_manager.php index 0d60610731e..860af8a3b22 100644 --- a/lib/classes/plugin_manager.php +++ b/lib/classes/plugin_manager.php @@ -1010,7 +1010,7 @@ class core_plugin_manager { * @param string $component plugin frankenstyle name * @param string|int $version ANY_VERSION or the version number * @param bool $exactmatch false if "given version or higher" is requested - * @return stdClass|bool false or data object + * @return \core\update\remote_info|bool */ public function get_remote_plugin_info($component, $version, $exactmatch) { @@ -1075,7 +1075,7 @@ class core_plugin_manager { * fulfill the requirements of all plugins, if possible. * * @param bool $availableonly return only available missing dependencies - * @return array of stdClass|bool indexed by the component name + * @return array of \core\update\remote_info|bool indexed by the component name */ public function missing_dependencies($availableonly=false) { @@ -1296,7 +1296,7 @@ class core_plugin_manager { * release candidates but less mature than explicit stable (this should be * pretty rare case). * - * @return array (string)component => (\core\update\info)info + * @return array (string)component => (\core\update\remote_info)remoteinfo */ public function available_updates() { diff --git a/lib/classes/update/api.php b/lib/classes/update/api.php index 3cb5cd4c292..2901da2503d 100644 --- a/lib/classes/update/api.php +++ b/lib/classes/update/api.php @@ -90,7 +90,7 @@ class api { * * @param string $component frankenstyle name of the plugin * @param int $version plugin version as declared via $plugin->version in its version.php - * @return stdClass|bool + * @return \core\update\remote_info|bool */ public function get_plugin_info($component, $version) { @@ -123,7 +123,7 @@ class api { * @param string $component frankenstyle name of the plugin * @param string|int $reqversion minimal required version of the plugin, defaults to ANY_VERSION * @param int $branch moodle core branch such as 29, 30, 31 etc, defaults to $CFG->branch - * @return stdClass|bool false or data object + * @return \core\update\remote_info|bool */ public function find_plugin($component, $reqversion=ANY_VERSION, $branch=null) { global $CFG; @@ -155,7 +155,7 @@ class api { * provided by the pluginfo.php version this client works with (self::APIVER). * * @param stdClass $data - * @return stdClass|bool false if data are not valid, original data otherwise + * @return \core\update\remote_info|bool false if data are not valid, original data otherwise */ public function validate_pluginfo_format($data) { @@ -163,6 +163,8 @@ class api { return false; } + $output = new remote_info(); + $rootproperties = array('id' => 1, 'name' => 1, 'component' => 1, 'source' => 0, 'doc' => 0, 'bugs' => 0, 'discussion' => 0, 'version' => 0); foreach ($rootproperties as $property => $required) { @@ -172,6 +174,7 @@ class api { if ($required and empty($data->$property)) { return false; } + $output->$property = $data->$property; } if (!empty($data->version)) { @@ -208,14 +211,14 @@ class api { } } - return $data; + return $output; } /** * Calls the pluginfo.php end-point with given parameters. * * @param array $params - * @return stdClass|bool false or data object + * @return \core\update\remote_info|bool */ protected function call_pluginfo_service(array $params) { diff --git a/lib/classes/update/remote_info.php b/lib/classes/update/remote_info.php new file mode 100644 index 00000000000..ca85fea3893 --- /dev/null +++ b/lib/classes/update/remote_info.php @@ -0,0 +1,45 @@ +. + +/** + * Provides \core\update\remote_info class. + * + * @package core_plugin + * @copyright 2015 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\update; +use stdClass; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Thin wrapper for data structures returned by {@link api::get_plugin_info()} + * + * Given that the API client returns instances of this class instead of pure + * objects allows us to have proper type hinting / declarations in method + * signatures. The validation of the data structure is happening in the API + * client so the rest of the code can simply rely on the class type. + * + * We extend the stdClass explicitly so that it can be eventually used in + * methods signatures, too (not recommended). + * + * @copyright 2015 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class remote_info extends stdClass { +} diff --git a/lib/tests/plugin_manager_test.php b/lib/tests/plugin_manager_test.php index ae2b20c371b..917bd240caf 100644 --- a/lib/tests/plugin_manager_test.php +++ b/lib/tests/plugin_manager_test.php @@ -297,7 +297,9 @@ class core_plugin_manager_testcase extends advanced_testcase { 'testable_plugininfo_base', $pluginman); $foobar->versiondb = 2015092900; $foobar->versiondisk = 2015092900; + $pluginman->inject_testable_plugininfo('foo', 'bar', $foobar); + $washere = false; foreach ($pluginman->get_plugins() as $type => $infos) { foreach ($infos as $name => $plugin) { $updates = $plugin->available_updates(); @@ -307,6 +309,7 @@ class core_plugin_manager_testcase extends advanced_testcase { $this->assertTrue(is_array($updates)); $this->assertEquals(3, count($updates)); foreach ($updates as $update) { + $washere = true; $this->assertInstanceOf('\core\update\info', $update); $this->assertEquals($update->component, $plugin->component); $this->assertTrue($update->version > $plugin->versiondb); @@ -314,6 +317,7 @@ class core_plugin_manager_testcase extends advanced_testcase { } } } + $this->assertTrue($washere); } public function test_some_plugins_updatable_none() { @@ -347,6 +351,7 @@ class core_plugin_manager_testcase extends advanced_testcase { $this->assertTrue(is_array($updates)); $this->assertEquals(1, count($updates)); $update = $updates['foo_bar']; + $this->assertInstanceOf('\core\update\remote_info', $update); $this->assertEquals('foo_bar', $update->component); $this->assertEquals(2015100400, $update->version->version); } @@ -472,10 +477,12 @@ class core_plugin_manager_testcase extends advanced_testcase { $one->dependencies = array('foo_bar' => ANY_VERSION); $misdeps = $pluginman->missing_dependencies(); + $this->assertInstanceOf('\core\update\remote_info', $misdeps['foo_bar']); $this->assertEquals(2015100400, $misdeps['foo_bar']->version->version); $two->dependencies = array('foo_bar' => 2015100500); $misdeps = $pluginman->missing_dependencies(); + $this->assertInstanceOf('\core\update\remote_info', $misdeps['foo_bar']); $this->assertEquals(2015100500, $misdeps['foo_bar']->version->version); } } diff --git a/lib/tests/update_api_test.php b/lib/tests/update_api_test.php index 49b76653808..32cfc7622a4 100644 --- a/lib/tests/update_api_test.php +++ b/lib/tests/update_api_test.php @@ -68,12 +68,14 @@ class core_update_api_testcase extends advanced_testcase { // The plugin is known but there is no such version. $info = $client->get_plugin_info('foo_bar', 2014010100); + $this->assertInstanceOf('\core\update\remote_info', $info); $this->assertFalse($info->version); // Both plugin and the version are available. foreach (array(2015093000 => MATURITY_STABLE, 2015100400 => MATURITY_STABLE, 2015100500 => MATURITY_BETA) as $version => $maturity) { $info = $client->get_plugin_info('foo_bar', $version); + $this->assertInstanceOf('\core\update\remote_info', $info); $this->assertNotEmpty($info->version); $this->assertEquals($maturity, $info->version->maturity); } @@ -96,15 +98,18 @@ class core_update_api_testcase extends advanced_testcase { // Both plugin and the version are available. Of the two available // stable versions, the more recent one is returned. $info = $client->find_plugin('foo_bar', 2015093000); + $this->assertInstanceOf('\core\update\remote_info', $info); $this->assertEquals(2015100400, $info->version->version); // If any version is required, the most recent most mature one is // returned. $info = $client->find_plugin('foo_bar', ANY_VERSION); + $this->assertInstanceOf('\core\update\remote_info', $info); $this->assertEquals(2015100400, $info->version->version); // Less matured versions are returned if needed. $info = $client->find_plugin('foo_bar', 2015100500); + $this->assertInstanceOf('\core\update\remote_info', $info); $this->assertEquals(2015100500, $info->version->version); } @@ -118,19 +123,20 @@ class core_update_api_testcase extends advanced_testcase { $json = '{"id":127,"name":"Course contents","component":"block_course_contents","source":"https:\/\/github.com\/mudrd8mz\/moodle-block_course_contents","doc":"http:\/\/docs.moodle.org\/20\/en\/Course_contents_block","bugs":"https:\/\/github.com\/mudrd8mz\/moodle-block_course_contents\/issues","discussion":null,"version":{"id":8100,"version":"2015030300","release":"3.0","maturity":200,"downloadurl":"https:\/\/moodle.org\/plugins\/download.php\/8100\/block_course_contents_moodle29_2015030300.zip","downloadmd5":"8d8ae64822f38d278420776f8b42eaa5","vcssystem":"git","vcssystemother":null,"vcsrepositoryurl":"https:\/\/github.com\/mudrd8mz\/moodle-block_course_contents","vcsbranch":"master","vcstag":"v3.0","supportedmoodles":[{"version":2014041100,"release":"2.7"},{"version":2014101000,"release":"2.8"},{"version":2015041700,"release":"2.9"}]}}'; $data = json_decode($json); - $this->assertSame($data, $client->validate_pluginfo_format($data)); + $this->assertInstanceOf('\core\update\remote_info', $client->validate_pluginfo_format($data)); + $this->assertEquals(json_encode($data), json_encode($client->validate_pluginfo_format($data))); // All properties must be present; unset($data->version); $this->assertFalse($client->validate_pluginfo_format($data)); $data->version = false; - $this->assertSame($data, $client->validate_pluginfo_format($data)); + $this->assertEquals(json_encode($data), json_encode($client->validate_pluginfo_format($data))); // Some properties may be empty. $data = json_decode($json); $data->version->release = null; - $this->assertSame($data, $client->validate_pluginfo_format($data)); + $this->assertEquals(json_encode($data), json_encode($client->validate_pluginfo_format($data))); // Some properties must not be empty. $data = json_decode($json);