moodle/lib/tests/update_code_manager_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

189 lines
8.9 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();
global $CFG;
require_once(__DIR__.'/fixtures/testable_update_code_manager.php');
/**
* Tests for \core\update\code_manager features.
*
* @package core_plugin
* @category test
* @copyright 2015 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class update_code_manager_test extends \advanced_testcase {
public function test_get_remote_plugin_zip() {
$codeman = new \core\update\testable_code_manager();
$this->assertFalse($codeman->get_remote_plugin_zip('ftp://not.support.ed/', 'doesnotmatter'));
$this->assertDebuggingCalled('Error fetching plugin ZIP: unsupported transport protocol: ftp://not.support.ed/');
$this->assertEquals(0, $codeman->downloadscounter);
$this->assertFalse($codeman->get_remote_plugin_zip('http://first/', ''));
$this->assertDebuggingCalled('Error fetching plugin ZIP: md5 mismatch.');
$this->assertEquals(1, $codeman->downloadscounter);
$this->assertNotFalse($codeman->get_remote_plugin_zip('http://first/', md5('http://first/')));
$this->assertEquals(2, $codeman->downloadscounter);
$this->assertNotFalse($codeman->get_remote_plugin_zip('http://two/', md5('http://two/')));
$this->assertEquals(3, $codeman->downloadscounter);
$this->assertNotFalse($codeman->get_remote_plugin_zip('http://first/', md5('http://first/')));
$this->assertEquals(3, $codeman->downloadscounter);
}
public function test_get_remote_plugin_zip_corrupted_cache() {
$temproot = make_request_directory();
$codeman = new \core\update\testable_code_manager(null, $temproot);
file_put_contents($temproot.'/distfiles/'.md5('http://valid/').'.zip', 'http://invalid/');
// Even if the cache file is already there, its name does not match its
// actual content. It must be removed and re-downaloaded.
$returned = $codeman->get_remote_plugin_zip('http://valid/', md5('http://valid/'));
$this->assertEquals(basename($returned), md5('http://valid/').'.zip');
$this->assertEquals(file_get_contents($returned), 'http://valid/');
}
public function test_unzip_plugin_file() {
$codeman = new \core\update\testable_code_manager();
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
$targetdir = make_request_directory();
mkdir($targetdir.'/aaa_another');
$files = $codeman->unzip_plugin_file($zipfilepath, $targetdir);
$this->assertIsArray($files);
$this->assertCount(4, $files);
$this->assertSame(true, $files['invalid-root/']);
$this->assertSame(true, $files['invalid-root/lang/']);
$this->assertSame(true, $files['invalid-root/lang/en/']);
$this->assertSame(true, $files['invalid-root/lang/en/fixed_root.php']);
foreach ($files as $file => $status) {
if (substr($file, -1) === '/') {
$this->assertTrue(is_dir($targetdir.'/'.$file));
} else {
$this->assertTrue(is_file($targetdir.'/'.$file));
}
}
$files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'fixed_root');
$this->assertIsArray($files);
$this->assertCount(4, $files);
$this->assertSame(true, $files['fixed_root/']);
$this->assertSame(true, $files['fixed_root/lang/']);
$this->assertSame(true, $files['fixed_root/lang/en/']);
$this->assertSame(true, $files['fixed_root/lang/en/fixed_root.php']);
foreach ($files as $file => $status) {
if (substr($file, -1) === '/') {
$this->assertTrue(is_dir($targetdir.'/'.$file));
} else {
$this->assertTrue(is_file($targetdir.'/'.$file));
}
}
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
$files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'bar');
}
public function test_unzip_plugin_file_multidir() {
$codeman = new \core\update\testable_code_manager();
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/multidir.zip';
$targetdir = make_request_directory();
// Attempting to rename the root folder if there are multiple ones should lead to exception.
$this->expectException(\moodle_exception::class);
$files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'foo');
}
public function test_get_plugin_zip_root_dir() {
$codeman = new \core\update\testable_code_manager();
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
$this->assertEquals('invalid-root', $codeman->get_plugin_zip_root_dir($zipfilepath));
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
$this->assertEquals('bar', $codeman->get_plugin_zip_root_dir($zipfilepath));
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/multidir.zip';
$this->assertSame(false, $codeman->get_plugin_zip_root_dir($zipfilepath));
}
public function test_list_plugin_folder_files() {
$fixtures = __DIR__.'/fixtures/update_validator/plugindir';
$codeman = new \core\update\testable_code_manager();
$files = $codeman->list_plugin_folder_files($fixtures.'/foobar');
$this->assertIsArray($files);
$this->assertEquals(6, count($files));
$fixtures = str_replace(DIRECTORY_SEPARATOR, '/', $fixtures);
$this->assertEquals($files['foobar/'], $fixtures.'/foobar');
$this->assertEquals($files['foobar/lang/en/local_foobar.php'], $fixtures.'/foobar/lang/en/local_foobar.php');
}
public function test_zip_plugin_folder() {
$fixtures = __DIR__.'/fixtures/update_validator/plugindir';
$storage = make_request_directory();
$codeman = new \core\update\testable_code_manager();
$codeman->zip_plugin_folder($fixtures.'/foobar', $storage.'/foobar.zip');
$this->assertTrue(file_exists($storage.'/foobar.zip'));
$fp = get_file_packer('application/zip');
$zipfiles = $fp->list_files($storage.'/foobar.zip');
$this->assertNotEmpty($zipfiles);
foreach ($zipfiles as $zipfile) {
if ($zipfile->is_directory) {
$this->assertTrue(is_dir($fixtures.'/'.$zipfile->pathname));
} else {
$this->assertTrue(file_exists($fixtures.'/'.$zipfile->pathname));
}
}
}
public function test_archiving_plugin_version() {
$fixtures = __DIR__.'/fixtures/update_validator/plugindir';
$codeman = new \core\update\testable_code_manager();
$this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 0));
$this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', null));
$this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', '', 2015100900));
$this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar-does-not-exist', 'local_foobar', 2013031900));
$this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
$this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
$this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 2013031900, true));
$this->assertNotFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
$this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
$this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', '2013031900')));
$this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
$this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031901));
$this->assertFalse($codeman->get_archived_plugin_version('', 2013031901));
$this->assertFalse($codeman->get_archived_plugin_version('local_foobar', ''));
$this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', '2013031900'));
$this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
}
}