moodle/lib/tests/update_checker_test.php
Eloy Lafuente (stronk7) 83b490a594 MDL-75111 phpunit: Move tests to use correct names and ns (take#4)
Applied the following changes to various testcase classes:

- Namespaced with component[\level2-API]
- Moved to level2-API subdirectory when required.
- Fixed incorrect use statements with leading backslash.
- Remove file phpdoc block
- Remove MOODLE_INTERNAL if not needed.
- Changed code to point to global scope when needed.
- Fix some relative paths and comments here and there.
- All them passing individually.
- Complete runs passing too.

Special mention to:

- The following task tests have been moved within the level2 directory:
  - \core\adhoc_task_test => \core\task\adhoc_task_test
  - \core\scheduled_task_test => \core\task\scheduled_task_test
  - \core\calendar_cron_task_test => \core\task\calendar_cron_task_test
  - \core\h5p_get_content_types_task_test => \core\task\h5p_get_content_types_task_test
  - \core\task_database_logger_test => \core\task\database_logger_test
  - \core\task_logging_test => \core\task\logging_test

- The following event tests have been moved within level2 directory:
  - \core\event_context_locked_test => \core\event\context_locked_test
  - \core\event_deprecated_test => \core\event\deprecated_test
  - \core\event_grade_deleted_test => \core\event\grade_deleted_test
  - \core\event_profile_field_test => \core\event\profile_field_test
  - \core\event_unknown_logged_test => \core\event\unknown_logged_test
  - \core\event_user_graded_test => \core\event\user_graded_test
  - \core\event_user_password_updated_test => \core\event\user_password_updated_test

- The following output tests have been moved within level2 directory:
  - \core\mustache_template_finder_test => \core\output\mustache_template_finder_test
  - \core\mustache_template_source_loader_test => \core\output\mustache_template_source_loader_test
  - \core\output_mustache_helper_collection_test => \core\output\mustache_helper_collection_test

- The following tests have been moved to their correct tests directories:
  - lib/tests/time_splittings_test.php => analytics/tests/time_splittings_test.php

- All the classes and tests under lib/filebrowser and lib/filestorage
  belong to core, not to core_files. Some day we should move
  them to their correct subsystem.
- All the classes and tests under lib/grade belong to core, not
  to core_grades. Some day we should move them to their correct
  subsystem.
- The core_grades_external class and its \core\grades_external_test
  unit test should belong to the grades subsystem or, alternatively,
  to \core\external, they both should be moved together.
- The core_grading_external class and its \core\grading_external_test
  unit test should belong to the grading subsystem or, alternatively,
  to \core\external, they both should be moved together.
- The \core\message\message and \core\message\inbound (may be others)
  classes, and their associated tests should go to the core_message
  subsystem.
- The core_user class, and its associated tests should go to the
  core_user subsystem.
- The \core\update namespace is plain wrong (update is not valid API)
  and needs action 1) create it or 2) move elsewhere.
2022-08-26 16:34:20 +02:00

290 lines
11 KiB
PHP

<?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/>.
namespace core;
defined('MOODLE_INTERNAL') || die();
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.
*
* @package core
* @category test
* @copyright 2012, 2015 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class update_checker_test extends \advanced_testcase {
public function test_core_available_update() {
$provider = testable_checker::instance();
$this->assertInstanceOf('\core\update\checker', $provider);
$provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array());
$updates = $provider->get_update_info('core');
$this->assertCount(2, $updates);
$provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
$updates = $provider->get_update_info('core');
$this->assertCount(1, $updates);
$provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
$updates = $provider->get_update_info('core', array('minmaturity' => MATURITY_STABLE));
$this->assertNull($updates);
}
/**
* If there are no fetched data yet, the first cron should fetch them.
*/
public function test_cron_initial_fetch() {
$provider = testable_checker::instance();
$provider->fakerecentfetch = null;
$provider->fakecurrenttimestamp = -1;
$this->expectException(\core\update\testable_checker_cron_executed::class);
$provider->cron();
}
/**
* If there is a fresh fetch available, no cron execution is expected.
*/
public function test_cron_has_fresh_fetch() {
$provider = testable_checker::instance();
$provider->fakerecentfetch = time() - 23 * HOURSECS; // Fetched 23 hours ago.
$provider->fakecurrenttimestamp = -1;
$provider->cron();
$this->assertTrue(true); // We should get here with no exception thrown.
}
/**
* If there is an outdated fetch, the cron execution is expected.
*/
public function test_cron_has_outdated_fetch() {
$provider = testable_checker::instance();
$provider->fakerecentfetch = time() - 49 * HOURSECS; // Fetched 49 hours ago.
$provider->fakecurrenttimestamp = -1;
$this->expectException(\core\update\testable_checker_cron_executed::class);
$provider->cron();
}
/**
* The first cron after 01:42 AM today should fetch the data.
*
* @see testable_checker::cron_execution_offset()
*/
public function test_cron_offset_execution_not_yet() {
$provider = testable_checker::instance();
$provider->fakecurrenttimestamp = mktime(1, 40, 02); // 01:40:02 AM today
$provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS;
$provider->cron();
$this->assertTrue(true); // We should get here with no exception thrown.
}
/**
* The first cron after 01:42 AM today should fetch the data and then
* it is supposed to wait next 24 hours.
*
* @see testable_checker::cron_execution_offset()
*/
public function test_cron_offset_execution() {
$provider = testable_checker::instance();
// The cron at 01:45 should fetch the data.
$provider->fakecurrenttimestamp = mktime(1, 45, 02); // 01:45:02 AM today
$provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS - 1;
$executed = false;
try {
$provider->cron();
} catch (testable_checker_cron_executed $e) {
$executed = true;
}
$this->assertTrue($executed, 'Cron should be executed at 01:45:02 but it was not.');
// Another cron at 06:45 should still consider data as fresh enough.
$provider->fakerecentfetch = $provider->fakecurrenttimestamp;
$provider->fakecurrenttimestamp = mktime(6, 45, 03); // 06:45:03 AM
$executed = false;
try {
$provider->cron();
} catch (testable_checker_cron_executed $e) {
$executed = true;
}
$this->assertFalse($executed, 'Cron should not be executed at 06:45:03 but it was.');
// The next scheduled execution should happen the next day.
$provider->fakecurrenttimestamp = $provider->fakerecentfetch + 24 * HOURSECS + 1;
$executed = false;
try {
$provider->cron();
} 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_checker::instance();
$old = array();
$new = array();
$cmp = $provider->compare_responses($old, $new);
$this->assertIsArray($cmp);
$this->assertEmpty($cmp);
}
public function test_compare_responses_old_empty() {
$provider = testable_checker::instance();
$old = array();
$new = array(
'updates' => array(
'core' => array(
array(
'version' => 2012060103
)
)
)
);
$cmp = $provider->compare_responses($old, $new);
$this->assertIsArray($cmp);
$this->assertNotEmpty($cmp);
$this->assertTrue(isset($cmp['core'][0]['version']));
$this->assertEquals(2012060103, $cmp['core'][0]['version']);
}
public function test_compare_responses_no_change() {
$provider = testable_checker::instance();
$old = $new = array(
'updates' => array(
'core' => array(
array(
'version' => 2012060104
),
array(
'version' => 2012120100
)
),
'mod_foo' => array(
array(
'version' => 2011010101
)
)
)
);
$cmp = $provider->compare_responses($old, $new);
$this->assertIsArray($cmp);
$this->assertEmpty($cmp);
}
public function test_compare_responses_new_and_missing_update() {
$provider = testable_checker::instance();
$old = array(
'updates' => array(
'core' => array(
array(
'version' => 2012060104
)
),
'mod_foo' => array(
array(
'version' => 2011010101
)
)
)
);
$new = array(
'updates' => array(
'core' => array(
array(
'version' => 2012060104
),
array(
'version' => 2012120100
)
)
)
);
$cmp = $provider->compare_responses($old, $new);
$this->assertIsArray($cmp);
$this->assertNotEmpty($cmp);
$this->assertCount(1, $cmp);
$this->assertCount(1, $cmp['core']);
$this->assertEquals(2012120100, $cmp['core'][0]['version']);
}
public function test_compare_responses_modified_update() {
$provider = testable_checker::instance();
$old = array(
'updates' => array(
'mod_foo' => array(
array(
'version' => 2011010101
)
)
)
);
$new = array(
'updates' => array(
'mod_foo' => array(
array(
'version' => 2011010102
)
)
)
);
$cmp = $provider->compare_responses($old, $new);
$this->assertIsArray($cmp);
$this->assertNotEmpty($cmp);
$this->assertCount(1, $cmp);
$this->assertCount(1, $cmp['mod_foo']);
$this->assertEquals(2011010102, $cmp['mod_foo'][0]['version']);
}
public function test_compare_responses_invalid_format() {
$provider = testable_checker::instance();
$broken = array(
'status' => 'ERROR' // No 'updates' key here.
);
$this->expectException(\core\update\checker_exception::class);
$cmp = $provider->compare_responses($broken, $broken);
}
public function test_is_same_release_explicit() {
$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)'));
$this->assertFalse($provider->is_same_release('2.3dev', '2.3 dev'));
$this->assertFalse($provider->is_same_release('2.3.1', '2.3'));
$this->assertFalse($provider->is_same_release('2.3.1', '2.3.2'));
$this->assertTrue($provider->is_same_release('2.3.2+', '2.3.2')); // Yes, really!
$this->assertTrue($provider->is_same_release('2.3.2 (Build: 123456)', '2.3.2+ (Build: 123457)'));
$this->assertFalse($provider->is_same_release('3.0 Community Edition', '3.0 Enterprise Edition'));
$this->assertTrue($provider->is_same_release('3.0 Community Edition', '3.0 Community Edition (Build: 20290101)'));
}
public function test_is_same_release_implicit() {
$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+'));
$this->assertTrue($provider->is_same_release('2.3.2+ (Build: 20121013)'));
$this->assertFalse($provider->is_same_release('2.4dev (Build: 20121012)'));
}
}