From d22835216f72501a58f3811a41f2834e4e5e543a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Mon, 5 Oct 2015 15:19:09 +0200 Subject: [PATCH] MDL-49329 admin: Clean up core_plugin unit tests As a first step for removing the \core\update\deployer and mdeploy.php, this patch fixes existing tests. --- lib/tests/fixtures/testable_update_api.php | 4 +- .../testable_update_checker.php} | 59 ++++--------------- lib/tests/update_api_test.php | 4 +- lib/tests/update_checker_test.php | 54 +++++++++-------- lib/tests/update_code_manager_test.php | 2 +- 5 files changed, 47 insertions(+), 76 deletions(-) rename lib/tests/{update_deployer_test.php => fixtures/testable_update_checker.php} (75%) diff --git a/lib/tests/fixtures/testable_update_api.php b/lib/tests/fixtures/testable_update_api.php index 62ef79e12a7..3cfef1a9e13 100644 --- a/lib/tests/fixtures/testable_update_api.php +++ b/lib/tests/fixtures/testable_update_api.php @@ -15,7 +15,9 @@ // along with Moodle. If not, see . /** - * @package core + * Provides \core\update\testable_api class. + * + * @package core_plugin * @subpackage fixtures * @category test * @copyright 2015 David Mudrak diff --git a/lib/tests/update_deployer_test.php b/lib/tests/fixtures/testable_update_checker.php similarity index 75% rename from lib/tests/update_deployer_test.php rename to lib/tests/fixtures/testable_update_checker.php index 9232492d631..4a29d34f15c 100644 --- a/lib/tests/update_deployer_test.php +++ b/lib/tests/fixtures/testable_update_checker.php @@ -14,50 +14,24 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace core\update; + /** - * Unit tests for the update deployer. + * Provides \core\update\testable_checker class. * - * @package core - * @category phpunit - * @copyright 2012 David Mudrak - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package core_plugin + * @subpackage fixtures + * @category test + * @copyright 2012, 2015 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); - -/** - * Test cases for {@link \core\update\deployer} class. - */ -class core_update_deployer_testcase extends advanced_testcase { - - public function test_magic_setters() { - $deployer = testable_available_update_deployer::instance(); - $value = new moodle_url('/'); - $deployer->set_returnurl($value); - $this->assertSame($deployer->get_returnurl(), $value); - } - - public function test_prepare_authorization() { - global $CFG; - - $deployer = testable_available_update_deployer::instance(); - list($passfile, $password) = $deployer->prepare_authorization(); - $filename = $CFG->phpunit_dataroot.'/mdeploy/auth/'.$passfile; - $this->assertFileExists($filename); - $stored = file($filename, FILE_IGNORE_NEW_LINES); - $this->assertCount(2, $stored); - $this->assertGreaterThan(23, strlen($stored[0])); - $this->assertSame($stored[0], $password); - $this->assertLessThan(60, time() - (int)$stored[1]); - } -} - - /** * Modified version of {@link \core\update\checker} suitable for testing. */ -class testable_available_update_checker extends \core\update\checker { +class testable_checker extends checker { /** @var replaces the default DB table storage for the fetched response */ protected $fakeresponsestorage; @@ -69,7 +43,7 @@ class testable_available_update_checker extends \core\update\checker { /** * Factory method for this class. * - * @return testable_available_update_checker the singleton instance + * @return \core\update\testable_checker the singleton instance */ public static function instance() { global $CFG; @@ -188,19 +162,12 @@ class testable_available_update_checker extends \core\update\checker { } protected function cron_execute() { - throw new testable_available_update_checker_cron_executed('Cron executed!'); + throw new testable_checker_cron_executed('Cron executed!'); } } - /** - * Exception used to detect {@link \core\update\checker::cron_execute()} calls. + * Exception used to detect {@link checker::cron_execute()} calls. */ -class testable_available_update_checker_cron_executed extends Exception { -} - -/** - * Modified {@link \core\update\deployer} suitable for testing purposes. - */ -class testable_available_update_deployer extends \core\update\deployer { +class testable_checker_cron_executed extends \Exception { } diff --git a/lib/tests/update_api_test.php b/lib/tests/update_api_test.php index f6661dfd559..49b76653808 100644 --- a/lib/tests/update_api_test.php +++ b/lib/tests/update_api_test.php @@ -15,9 +15,9 @@ // along with Moodle. If not, see . /** - * Provides core_update_checker_testcase class. + * Provides core_update_api_testcase class. * - * @package core + * @package core_plugin * @category test * @copyright 2015 David Mudrak * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later diff --git a/lib/tests/update_checker_test.php b/lib/tests/update_checker_test.php index 95877b7e5a2..4139f18890a 100644 --- a/lib/tests/update_checker_test.php +++ b/lib/tests/update_checker_test.php @@ -17,17 +17,19 @@ /** * Unit tests for the update checker. * - * @package core - * @category phpunit - * @copyright 2012 David Mudrak + * @package core_plugin + * @category test + * @copyright 2012, 2015 David Mudrak * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); -global $CFG; -require_once(__DIR__.'/update_deployer_test.php'); +use core\update\testable_checker; +use core\update\testable_checker_cron_executed; +global $CFG; +require_once(__DIR__.'/fixtures/testable_update_checker.php'); /** * Tests of the basic API of the available update checker. @@ -35,7 +37,7 @@ require_once(__DIR__.'/update_deployer_test.php'); class core_update_checker_testcase extends advanced_testcase { public function test_core_available_update() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $this->assertInstanceOf('\core\update\checker', $provider); $provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array()); @@ -55,10 +57,10 @@ class core_update_checker_testcase extends advanced_testcase { * If there are no fetched data yet, the first cron should fetch them. */ public function test_cron_initial_fetch() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $provider->fakerecentfetch = null; $provider->fakecurrenttimestamp = -1; - $this->setExpectedException('testable_available_update_checker_cron_executed'); + $this->setExpectedException('\core\update\testable_checker_cron_executed'); $provider->cron(); } @@ -66,7 +68,7 @@ class core_update_checker_testcase extends advanced_testcase { * If there is a fresh fetch available, no cron execution is expected. */ public function test_cron_has_fresh_fetch() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $provider->fakerecentfetch = time() - 23 * HOURSECS; // Fetched 23 hours ago. $provider->fakecurrenttimestamp = -1; $provider->cron(); @@ -77,20 +79,20 @@ class core_update_checker_testcase extends advanced_testcase { * If there is an outdated fetch, the cron execution is expected. */ public function test_cron_has_outdated_fetch() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $provider->fakerecentfetch = time() - 49 * HOURSECS; // Fetched 49 hours ago. $provider->fakecurrenttimestamp = -1; - $this->setExpectedException('testable_available_update_checker_cron_executed'); + $this->setExpectedException('\core\update\testable_checker_cron_executed'); $provider->cron(); } /** * The first cron after 01:42 AM today should fetch the data. * - * @see testable_available_update_checker::cron_execution_offset() + * @see testable_checker::cron_execution_offset() */ public function test_cron_offset_execution_not_yet() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $provider->fakecurrenttimestamp = mktime(1, 40, 02); // 01:40:02 AM today $provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS; $provider->cron(); @@ -101,10 +103,10 @@ class core_update_checker_testcase extends advanced_testcase { * The first cron after 01:42 AM today should fetch the data and then * it is supposed to wait next 24 hours. * - * @see testable_available_update_checker::cron_execution_offset() + * @see testable_checker::cron_execution_offset() */ public function test_cron_offset_execution() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); // The cron at 01:45 should fetch the data. $provider->fakecurrenttimestamp = mktime(1, 45, 02); // 01:45:02 AM today @@ -112,7 +114,7 @@ class core_update_checker_testcase extends advanced_testcase { $executed = false; try { $provider->cron(); - } catch (testable_available_update_checker_cron_executed $e) { + } catch (testable_checker_cron_executed $e) { $executed = true; } $this->assertTrue($executed, 'Cron should be executed at 01:45:02 but it was not.'); @@ -123,7 +125,7 @@ class core_update_checker_testcase extends advanced_testcase { $executed = false; try { $provider->cron(); - } catch (testable_available_update_checker_cron_executed $e) { + } catch (testable_checker_cron_executed $e) { $executed = true; } $this->assertFalse($executed, 'Cron should not be executed at 06:45:03 but it was.'); @@ -133,14 +135,14 @@ class core_update_checker_testcase extends advanced_testcase { $executed = false; try { $provider->cron(); - } catch (testable_available_update_checker_cron_executed $e) { + } catch (testable_checker_cron_executed $e) { $executed = true; } $this->assertTrue($executed, 'Cron should be executed the next night but it was not.'); } public function test_compare_responses_both_empty() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $old = array(); $new = array(); $cmp = $provider->compare_responses($old, $new); @@ -149,7 +151,7 @@ class core_update_checker_testcase extends advanced_testcase { } public function test_compare_responses_old_empty() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $old = array(); $new = array( 'updates' => array( @@ -168,7 +170,7 @@ class core_update_checker_testcase extends advanced_testcase { } public function test_compare_responses_no_change() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $old = $new = array( 'updates' => array( 'core' => array( @@ -192,7 +194,7 @@ class core_update_checker_testcase extends advanced_testcase { } public function test_compare_responses_new_and_missing_update() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $old = array( 'updates' => array( 'core' => array( @@ -228,7 +230,7 @@ class core_update_checker_testcase extends advanced_testcase { } public function test_compare_responses_modified_update() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $old = array( 'updates' => array( 'mod_foo' => array( @@ -256,7 +258,7 @@ class core_update_checker_testcase extends advanced_testcase { } public function test_compare_responses_invalid_format() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $broken = array( 'status' => 'ERROR' // No 'updates' key here. ); @@ -265,7 +267,7 @@ class core_update_checker_testcase extends advanced_testcase { } public function test_is_same_release_explicit() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120323)')); $this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120330)')); $this->assertFalse($provider->is_same_release('2.3dev (Build: 20120529)', '2.3 (Build: 20120601)')); @@ -279,7 +281,7 @@ class core_update_checker_testcase extends advanced_testcase { } public function test_is_same_release_implicit() { - $provider = testable_available_update_checker::instance(); + $provider = testable_checker::instance(); $provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array()); $this->assertTrue($provider->is_same_release('2.3.2')); $this->assertTrue($provider->is_same_release('2.3.2+')); diff --git a/lib/tests/update_code_manager_test.php b/lib/tests/update_code_manager_test.php index d47b37c6a37..fa61144732f 100644 --- a/lib/tests/update_code_manager_test.php +++ b/lib/tests/update_code_manager_test.php @@ -17,7 +17,7 @@ /** * Provides core_update_code_manager_testcase class. * - * @package core + * @package core_plugin * @category test * @copyright 2015 David Mudrak * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later