Skip to content

Commit

Permalink
MDL-20438 A set of unittests to illustrate the update checking integr…
Browse files Browse the repository at this point in the history
…ation in the plugin_manager
  • Loading branch information
mudrd8mz committed Mar 30, 2012
1 parent dd119e2 commit 2b47947
Showing 1 changed file with 141 additions and 3 deletions.
144 changes: 141 additions & 3 deletions lib/simpletest/testpluginlib.php
Expand Up @@ -91,18 +91,87 @@ public function get_plugins($disablecache=false) {
'mod' => array(
'foo' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/mod', 'foo',
$CFG->dirroot.'/mod/foo', 'testable_plugininfo_mod'),
'bar' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/bar', 'bar',
$CFG->dirroot.'/mod/bar', 'testable_plugininfo_mod'),
'buz' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/buz', 'buz',
$CFG->dirroot.'/mod/buz', 'testable_plugininfo_mod'),
)
);

$checker = testable_available_update_checker::instance();
$this->pluginsinfo['mod']['foo']->check_available_update($checker);
$this->pluginsinfo['mod']['bar']->check_available_update($checker);
$this->pluginsinfo['mod']['buz']->check_available_update($checker);

return $this->pluginsinfo;
}
}


/**
* Test cases for the pluginlib API
*
* These are basic tests to document the basic API of the plugin manager.
* Modified version of {@link available_update_checker} suitable for testing
*/
class testable_available_update_checker extends available_update_checker {

/**
* Factory method for this class
*
* @return testable_available_update_checker the singleton instance
*/
public static function instance() {
global $CFG;

if (is_null(self::$singletoninstance)) {
self::$singletoninstance = new self();
}
return self::$singletoninstance;
}

/**
* Do not load config in this testable subclass
*/
protected function load_config() {
}

/**
* Do not fetch anything in this testable subclass
*/
public function fetch() {
}

/**
* Here we simulate read access to the fetched remote statuses
*/
public function get_update_info($component) {
if ($component === 'mod_foo') {
// no update available
return (object)array(
'version' => 2012030500,
);

} else if ($component === 'mod_bar') {
// there is an update available
return (object)array(
'version' => 2012030501,
);

} else {
// nothing known to us
return null;
}
}

/**
* Makes the method public so we can test it
*/
public function merge_components_info(stdClass $old, stdClass $new, $timegenerated=null) {
return parent::merge_components_info($old, $new, $timegenerated);
}
}


/**
* Tests of the basic API of the plugin manager
*/
class plugin_manager_test extends UnitTestCase {

Expand All @@ -124,4 +193,73 @@ public function test_get_status() {
$modfoo = $plugins['mod']['foo'];
$this->assertEqual($modfoo->get_status(), plugin_manager::PLUGIN_STATUS_UPGRADE);
}

public function test_available_update() {
$pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins();
$this->assertFalse($plugins['mod']['foo']->available_update());
$this->assertNull($plugins['mod']['buz']->available_update());
$this->assertIsA($plugins['mod']['bar']->available_update(), 'stdClass');
$this->assertEqual($plugins['mod']['bar']->available_update()->version, 2012030501);
}
}


/**
* Tests of the basic API of the available update checker
*/
class available_update_checker_test extends UnitTestCase {

public function test_core_available_update() {
$provider = testable_available_update_checker::instance();
$this->assertTrue($provider instanceof available_update_checker);
}

public function test_merge_components_info() {
$old = (object)array(
'2.2' => (object)array(
'core' => (object)array(
'version' => 2011120501.11,
'release' => '2.2.1+ (Build: 20120301)',
'maturity' => MATURITY_STABLE,
),
'mod_foo' => (object)array(
'version' => 2011010100,
),
'mod_bar' => (object)array(
'version' => 2011020200,
)
)
);
$new = (object)array(
'2.2' => (object)array(
'core' => (object)array(
'version' => 2011120501.12,
'release' => '2.2.1+ (Build: 20120302)',
'maturity' => MATURITY_STABLE,
),
'mod_bar' => (object)array(
'version' => 2011020201,
),
),
'2.3' => (object)array(
'core' => (object)array(
'version' => 2012030100.00,
'release' => '2.3dev (Build: 20120301)',
'maturity' => MATURITY_ALPHA,
),
'mod_foo' => (object)array(
'version' => 2012010200,
)
)
);
$checker = testable_available_update_checker::instance();
$now = time();
$merged = $checker->merge_components_info($old, $new, $now);
$this->assertEqual($merged->{2.2}->core->version, 2011120501.12); // from $new
$this->assertEqual($merged->{2.2}->mod_bar->version, 2011020201); // from $new
$this->assertEqual($merged->{2.2}->mod_foo->version, 2011010100); // from $old
$this->assertEqual($merged->{2.3}->core->version, 2012030100.00); // from $new
$this->assertFalse(isset($merged->{2.3}->mod_bar));
}
}

0 comments on commit 2b47947

Please sign in to comment.