Commit 4f18a4e6 authored by David Mudrák's avatar David Mudrák
Browse files

MDL-49329 admin: Introduce new \core\update\remote_info class

This is just a thin wrapper for normal objects, allowing us to have
explicit type hinting / declarations in method signatures.
parent 44371554
...@@ -1010,7 +1010,7 @@ class core_plugin_manager { ...@@ -1010,7 +1010,7 @@ class core_plugin_manager {
* @param string $component plugin frankenstyle name * @param string $component plugin frankenstyle name
* @param string|int $version ANY_VERSION or the version number * @param string|int $version ANY_VERSION or the version number
* @param bool $exactmatch false if "given version or higher" is requested * @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) { public function get_remote_plugin_info($component, $version, $exactmatch) {
...@@ -1075,7 +1075,7 @@ class core_plugin_manager { ...@@ -1075,7 +1075,7 @@ class core_plugin_manager {
* fulfill the requirements of all plugins, if possible. * fulfill the requirements of all plugins, if possible.
* *
* @param bool $availableonly return only available missing dependencies * @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) { public function missing_dependencies($availableonly=false) {
...@@ -1296,7 +1296,7 @@ class core_plugin_manager { ...@@ -1296,7 +1296,7 @@ class core_plugin_manager {
* release candidates but less mature than explicit stable (this should be * release candidates but less mature than explicit stable (this should be
* pretty rare case). * pretty rare case).
* *
* @return array (string)component => (\core\update\info)info * @return array (string)component => (\core\update\remote_info)remoteinfo
*/ */
public function available_updates() { public function available_updates() {
......
...@@ -90,7 +90,7 @@ class api { ...@@ -90,7 +90,7 @@ class api {
* *
* @param string $component frankenstyle name of the plugin * @param string $component frankenstyle name of the plugin
* @param int $version plugin version as declared via $plugin->version in its version.php * @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) { public function get_plugin_info($component, $version) {
...@@ -123,7 +123,7 @@ class api { ...@@ -123,7 +123,7 @@ class api {
* @param string $component frankenstyle name of the plugin * @param string $component frankenstyle name of the plugin
* @param string|int $reqversion minimal required version of the plugin, defaults to ANY_VERSION * @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 * @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) { public function find_plugin($component, $reqversion=ANY_VERSION, $branch=null) {
global $CFG; global $CFG;
...@@ -155,7 +155,7 @@ class api { ...@@ -155,7 +155,7 @@ class api {
* provided by the pluginfo.php version this client works with (self::APIVER). * provided by the pluginfo.php version this client works with (self::APIVER).
* *
* @param stdClass $data * @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) { public function validate_pluginfo_format($data) {
...@@ -163,6 +163,8 @@ class api { ...@@ -163,6 +163,8 @@ class api {
return false; return false;
} }
$output = new remote_info();
$rootproperties = array('id' => 1, 'name' => 1, 'component' => 1, 'source' => 0, 'doc' => 0, $rootproperties = array('id' => 1, 'name' => 1, 'component' => 1, 'source' => 0, 'doc' => 0,
'bugs' => 0, 'discussion' => 0, 'version' => 0); 'bugs' => 0, 'discussion' => 0, 'version' => 0);
foreach ($rootproperties as $property => $required) { foreach ($rootproperties as $property => $required) {
...@@ -172,6 +174,7 @@ class api { ...@@ -172,6 +174,7 @@ class api {
if ($required and empty($data->$property)) { if ($required and empty($data->$property)) {
return false; return false;
} }
$output->$property = $data->$property;
} }
if (!empty($data->version)) { if (!empty($data->version)) {
...@@ -208,14 +211,14 @@ class api { ...@@ -208,14 +211,14 @@ class api {
} }
} }
return $data; return $output;
} }
/** /**
* Calls the pluginfo.php end-point with given parameters. * Calls the pluginfo.php end-point with given parameters.
* *
* @param array $params * @param array $params
* @return stdClass|bool false or data object * @return \core\update\remote_info|bool
*/ */
protected function call_pluginfo_service(array $params) { protected function call_pluginfo_service(array $params) {
......
<?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/>.
/**
* Provides \core\update\remote_info class.
*
* @package core_plugin
* @copyright 2015 David Mudrak <david@moodle.com>
* @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 <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class remote_info extends stdClass {
}
...@@ -297,7 +297,9 @@ class core_plugin_manager_testcase extends advanced_testcase { ...@@ -297,7 +297,9 @@ class core_plugin_manager_testcase extends advanced_testcase {
'testable_plugininfo_base', $pluginman); 'testable_plugininfo_base', $pluginman);
$foobar->versiondb = 2015092900; $foobar->versiondb = 2015092900;
$foobar->versiondisk = 2015092900; $foobar->versiondisk = 2015092900;
$pluginman->inject_testable_plugininfo('foo', 'bar', $foobar);
$washere = false;
foreach ($pluginman->get_plugins() as $type => $infos) { foreach ($pluginman->get_plugins() as $type => $infos) {
foreach ($infos as $name => $plugin) { foreach ($infos as $name => $plugin) {
$updates = $plugin->available_updates(); $updates = $plugin->available_updates();
...@@ -307,6 +309,7 @@ class core_plugin_manager_testcase extends advanced_testcase { ...@@ -307,6 +309,7 @@ class core_plugin_manager_testcase extends advanced_testcase {
$this->assertTrue(is_array($updates)); $this->assertTrue(is_array($updates));
$this->assertEquals(3, count($updates)); $this->assertEquals(3, count($updates));
foreach ($updates as $update) { foreach ($updates as $update) {
$washere = true;
$this->assertInstanceOf('\core\update\info', $update); $this->assertInstanceOf('\core\update\info', $update);
$this->assertEquals($update->component, $plugin->component); $this->assertEquals($update->component, $plugin->component);
$this->assertTrue($update->version > $plugin->versiondb); $this->assertTrue($update->version > $plugin->versiondb);
...@@ -314,6 +317,7 @@ class core_plugin_manager_testcase extends advanced_testcase { ...@@ -314,6 +317,7 @@ class core_plugin_manager_testcase extends advanced_testcase {
} }
} }
} }
$this->assertTrue($washere);
} }
public function test_some_plugins_updatable_none() { public function test_some_plugins_updatable_none() {
...@@ -347,6 +351,7 @@ class core_plugin_manager_testcase extends advanced_testcase { ...@@ -347,6 +351,7 @@ class core_plugin_manager_testcase extends advanced_testcase {
$this->assertTrue(is_array($updates)); $this->assertTrue(is_array($updates));
$this->assertEquals(1, count($updates)); $this->assertEquals(1, count($updates));
$update = $updates['foo_bar']; $update = $updates['foo_bar'];
$this->assertInstanceOf('\core\update\remote_info', $update);
$this->assertEquals('foo_bar', $update->component); $this->assertEquals('foo_bar', $update->component);
$this->assertEquals(2015100400, $update->version->version); $this->assertEquals(2015100400, $update->version->version);
} }
...@@ -472,10 +477,12 @@ class core_plugin_manager_testcase extends advanced_testcase { ...@@ -472,10 +477,12 @@ class core_plugin_manager_testcase extends advanced_testcase {
$one->dependencies = array('foo_bar' => ANY_VERSION); $one->dependencies = array('foo_bar' => ANY_VERSION);
$misdeps = $pluginman->missing_dependencies(); $misdeps = $pluginman->missing_dependencies();
$this->assertInstanceOf('\core\update\remote_info', $misdeps['foo_bar']);
$this->assertEquals(2015100400, $misdeps['foo_bar']->version->version); $this->assertEquals(2015100400, $misdeps['foo_bar']->version->version);
$two->dependencies = array('foo_bar' => 2015100500); $two->dependencies = array('foo_bar' => 2015100500);
$misdeps = $pluginman->missing_dependencies(); $misdeps = $pluginman->missing_dependencies();
$this->assertInstanceOf('\core\update\remote_info', $misdeps['foo_bar']);
$this->assertEquals(2015100500, $misdeps['foo_bar']->version->version); $this->assertEquals(2015100500, $misdeps['foo_bar']->version->version);
} }
} }
...@@ -68,12 +68,14 @@ class core_update_api_testcase extends advanced_testcase { ...@@ -68,12 +68,14 @@ class core_update_api_testcase extends advanced_testcase {
// The plugin is known but there is no such version. // The plugin is known but there is no such version.
$info = $client->get_plugin_info('foo_bar', 2014010100); $info = $client->get_plugin_info('foo_bar', 2014010100);
$this->assertInstanceOf('\core\update\remote_info', $info);
$this->assertFalse($info->version); $this->assertFalse($info->version);
// Both plugin and the version are available. // Both plugin and the version are available.
foreach (array(2015093000 => MATURITY_STABLE, 2015100400 => MATURITY_STABLE, foreach (array(2015093000 => MATURITY_STABLE, 2015100400 => MATURITY_STABLE,
2015100500 => MATURITY_BETA) as $version => $maturity) { 2015100500 => MATURITY_BETA) as $version => $maturity) {
$info = $client->get_plugin_info('foo_bar', $version); $info = $client->get_plugin_info('foo_bar', $version);
$this->assertInstanceOf('\core\update\remote_info', $info);
$this->assertNotEmpty($info->version); $this->assertNotEmpty($info->version);
$this->assertEquals($maturity, $info->version->maturity); $this->assertEquals($maturity, $info->version->maturity);
} }
...@@ -96,15 +98,18 @@ class core_update_api_testcase extends advanced_testcase { ...@@ -96,15 +98,18 @@ class core_update_api_testcase extends advanced_testcase {
// Both plugin and the version are available. Of the two available // Both plugin and the version are available. Of the two available
// stable versions, the more recent one is returned. // stable versions, the more recent one is returned.
$info = $client->find_plugin('foo_bar', 2015093000); $info = $client->find_plugin('foo_bar', 2015093000);
$this->assertInstanceOf('\core\update\remote_info', $info);
$this->assertEquals(2015100400, $info->version->version); $this->assertEquals(2015100400, $info->version->version);
// If any version is required, the most recent most mature one is // If any version is required, the most recent most mature one is
// returned. // returned.
$info = $client->find_plugin('foo_bar', ANY_VERSION); $info = $client->find_plugin('foo_bar', ANY_VERSION);
$this->assertInstanceOf('\core\update\remote_info', $info);
$this->assertEquals(2015100400, $info->version->version); $this->assertEquals(2015100400, $info->version->version);
// Less matured versions are returned if needed. // Less matured versions are returned if needed.
$info = $client->find_plugin('foo_bar', 2015100500); $info = $client->find_plugin('foo_bar', 2015100500);
$this->assertInstanceOf('\core\update\remote_info', $info);
$this->assertEquals(2015100500, $info->version->version); $this->assertEquals(2015100500, $info->version->version);
} }
...@@ -118,19 +123,20 @@ class core_update_api_testcase extends advanced_testcase { ...@@ -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"}]}}'; $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); $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; // All properties must be present;
unset($data->version); unset($data->version);
$this->assertFalse($client->validate_pluginfo_format($data)); $this->assertFalse($client->validate_pluginfo_format($data));
$data->version = false; $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. // Some properties may be empty.
$data = json_decode($json); $data = json_decode($json);
$data->version->release = null; $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. // Some properties must not be empty.
$data = json_decode($json); $data = json_decode($json);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment