MDL-86204 core: Add environment tests for dev dependencies

This commit is contained in:
Andrew Nicols
2025-08-25 14:33:19 +08:00
parent dbc8a08e68
commit a4744bc5fe
5 changed files with 395 additions and 0 deletions
+4
View File
@@ -5021,6 +5021,10 @@
</PHP_SETTING>
</PHP_SETTINGS>
<CUSTOM_CHECKS>
<CUSTOM_CHECK function="\core\environment::check_composer_dependencies_installed" level="optional" />
<CUSTOM_CHECK function="\core\environment::check_composer_developer_dependencies_not_installed" level="optional" />
<CUSTOM_CHECK function="\core\environment::check_composer_dependencies_optimised" level="optional" />
</CUSTOM_CHECK>
<CUSTOM_CHECK file="lib/upgradelib.php" function="check_database_storage_engine" level="required">
<FEEDBACK>
<ON_ERROR message="unsupporteddbstorageengine" />
+4
View File
@@ -168,6 +168,10 @@ $string['commonactivitysettings'] = 'Common activity settings';
$string['commonfiltersettings'] = 'Common filter settings';
$string['commonsettings'] = 'Common settings';
$string['componentinstalled'] = 'Component installed';
$string['composernotfound'] = 'Composer dependencies were not found. Please ensure that the "composer install --no-dev --classmap-authoritative" command has been run in the Moodle root directory. If you are not using Composer, please ensure that the vendor directory exists and contains the necessary files.';
$string['composerdeveloperdependenciesinstalled'] = 'Composer Developer dependencies are installed. Please ensure that the "composer install --no-dev --classmap-authoritative" command has been run in the Moodle root directory.';
$string['composeroptimisedindevmode'] = 'The Composer autoloader is currently running optimised whilst Moodle is in developer mode. This can cause issues in some cases. You may wish to run "composer install" without additional arguments.';
$string['composernotoptimised'] = 'Moodle is running in production mode whilst the Composer autoloader is not optimised. You may wish to run "composer install --no-dev --classmap-authoritative" to optimise the autoloader.';
$string['computedfromlogs'] = 'Computed from logs since {$a}.';
$string['condifmodeditdefaults'] = 'Default values are used in the settings form when creating a new activity or resource.';
$string['confeditorhidebuttons'] = 'Select the buttons that should be hidden in the HTML editor.';
+163
View File
@@ -0,0 +1,163 @@
<?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;
/**
* Class environment
*
* @package core
* @copyright Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class environment {
/**
* Ensure that Composer dependencies are installed and the necessary files are present.
*
* @param \environment_results $result
* @return \environment_results|null
*/
public static function check_composer_dependencies_installed(\environment_results $result): ?\environment_results {
// Check if the composer vendor directory exists.
$vendorpath = static::get_vendor_path();
if (!is_dir($vendorpath)) {
$result->setInfo('Composer vendor directory not found');
$result->setFeedbackStr('composernotfound');
return $result;
}
// Check if the composer autoload file exists.
$autoloadpath = "{$vendorpath}/autoload.php";
if (!is_file($autoloadpath)) {
$result->setInfo('Composer autoload file not found');
$result->setFeedbackStr('composernotfound');
return $result;
}
// Check if the installed.php file exists in the composer directory.
$installedpath = "{$vendorpath}/composer/installed.php";
if (!is_file($installedpath)) {
$result->setInfo('Composer installed data not found');
$result->setFeedbackStr('composernotfound');
return $result;
}
return null;
}
/**
* Ensure that Composer developer dependencies are not installed.
*
* @param \environment_results $result
* @return \environment_results|null
*/
public static function check_composer_developer_dependencies_not_installed(
\environment_results $result
): ?\environment_results {
if (static::is_developer_mode_enabled()) {
$result->setInfo('Developer mode is enabled, skipping check for developer dependencies');
return null; // Skip this check in developer mode.
}
$vendorpath = static::get_vendor_path();
if (!is_dir($vendorpath)) {
return null; // No vendor directory, so no developer dependencies to check.
}
// Check if the installed.php file exists in the composer directory.
$installedpath = "{$vendorpath}/composer/installed.php";
if (!is_file($installedpath)) {
return null; // No installed file, so no developer dependencies to check.
}
// Check if developer dependencies have been installed too.
$installed = include($installedpath);
if (is_array($installed) && array_key_exists('root', $installed)) {
if ($installed['root']['dev']) {
$result->setInfo('Composer Developer dependencies are installed');
$result->setFeedbackStr('composerdeveloperdependenciesinstalled');
return $result;
}
}
return null;
}
/**
* Ensure that Composer developer dependencies are optimised .
*
* @param \environment_results $result
* @return \environment_results|null
* @codeCoverageIgnore
*/
public static function check_composer_dependencies_optimised(
\environment_results $result
): ?\environment_results {
$vendorpath = static::get_vendor_path();
if (!is_dir($vendorpath)) {
return null; // No vendor directory, so no developer dependencies to check.
}
$autoloader = require("{$vendorpath}/autoload.php");
if (static::is_developer_mode_enabled()) {
if ($autoloader->isClassMapAuthoritative()) {
$result->setInfo('Composer autoloader is optimised');
$result->setFeedbackStr('composeroptimisedindevmode');
return $result;
}
$result->setInfo('Developer mode is enabled, optimiser is correctly disabled.');
return null;
}
if ($autoloader->isClassMapAuthoritative()) {
$result->setInfo('Autoloader is correctly optimised.');
return null;
}
$result->setInfo('Composer autoloader is not optimised');
$result->setFeedbackStr('composernotoptimised');
return $result;
}
/**
* Get the path to the Composer vendor directory.
*
* @return string
*/
protected static function get_vendor_path(): string {
global $CFG;
// Return the path to the vendor directory.
return "{$CFG->root}/vendor";
}
/**
* Check if developer mode is enabled.
*
* @return bool
*/
protected static function is_developer_mode_enabled(): bool {
global $CFG;
return !empty($CFG->debugdeveloper);
}
}
+68
View File
@@ -0,0 +1,68 @@
<?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\tests;
/**
* Test helper for the \core\environment class.
*
* @package core
* @copyright Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class environment extends \core\environment {
/** @var string|null The path to the vendor dir if modified */
protected static ?string $vendorpath;
/** @var bool|null Whether developer mode is enabled, or defer to $CFG */
protected static ?bool $devmode = null;
/**
* Set the developer mode for testing purposes.
*
* @param bool $enabled
*/
public static function set_developer_mode(bool $enabled): void {
self::$devmode = $enabled;
}
/**
* Set the vendor path for testing purposes.
*
* @param string $path
*/
public static function set_vendor_path(string $path): void {
self::$vendorpath = $path;
}
#[\Override]
protected static function get_vendor_path(): string {
if (self::$vendorpath) {
return self::$vendorpath;
}
return parent::get_vendor_path();
}
#[\Override]
protected static function is_developer_mode_enabled(): bool {
if (self::$devmode !== null) {
return self::$devmode;
}
return parent::is_developer_mode_enabled();
}
}
+156
View File
@@ -0,0 +1,156 @@
<?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;
use core\tests\environment as environment_tester;
/**
* Tests for the \core\environment class.
*
* @package core
* @category test
* @copyright Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\PHPUnit\Framework\Attributes\CoversClass(environment::class)]
final class environment_test extends \advanced_testcase {
#[\PHPUnit\Framework\Attributes\DataProvider('composer_error_states_provider')]
public function test_composer_not_installed_cases(
array $fs = [],
string $expectedfeedback = 'composernotfound'
): void {
\org\bovigo\vfs\vfsStream::setup('root', null, $fs);
environment_tester::set_vendor_path(\org\bovigo\vfs\vfsStream::url('root/vendor'));
$result = new \environment_results('custom_check');
$result = environment_tester::check_composer_dependencies_installed($result);
$this->assertEquals($expectedfeedback, $result->getFeedbackStr());
// Check that the developer dependencies tests do not error in these conditions.
$result = new \environment_results('custom_check');
$result = environment_tester::check_composer_developer_dependencies_not_installed($result);
$this->assertNull($result);
}
/**
* Data provider for test_composer_not_installed_cases.
*
* @return \Generator
*/
public static function composer_error_states_provider(): \Generator {
yield 'composer vendor directory not found' => [
'fs' => [],
'expectedfeedback' => 'composernotfound',
];
yield 'composer autoload file not found' => [
'fs' => [
'vendor' => [],
],
'expectedfeedback' => 'composernotfound',
];
yield 'composer installed data not found' => [
'fs' => [
'vendor' => [
'autoload.php' => '',
],
],
'expectedfeedback' => 'composernotfound',
];
}
public function test_composer_installed(): void {
\org\bovigo\vfs\vfsStream::setup('root', null, [
'vendor' => [
'autoload.php' => '',
'composer' => [
'installed.php' => '',
],
],
]);
environment_tester::set_vendor_path(\org\bovigo\vfs\vfsStream::url('root/vendor'));
$result = new \environment_results('custom_check');
$result = environment_tester::check_composer_dependencies_installed($result);
$this->assertNull($result);
}
public function test_composer_dev_installed(): void {
\org\bovigo\vfs\vfsStream::setup('root', null, [
'vendor' => [
'autoload.php' => '',
'composer' => [
'installed.php' => '<?php return ["root" => ["dev" => true]];',
],
],
]);
environment_tester::set_vendor_path(\org\bovigo\vfs\vfsStream::url('root/vendor'));
environment_tester::set_developer_mode(false);
$result = new \environment_results('custom_check');
$result = environment_tester::check_composer_developer_dependencies_not_installed($result);
$this->assertEquals('composerdeveloperdependenciesinstalled', $result->getFeedbackStr());
// Check that the dependencies tests do not error in these conditions.
$result = new \environment_results('custom_check');
$result = environment_tester::check_composer_dependencies_installed($result);
$this->assertNull($result);
}
public function test_composer_dev_installed_with_developer_mode(): void {
\org\bovigo\vfs\vfsStream::setup('root', null, [
'vendor' => [
'autoload.php' => '',
'composer' => [
'installed.php' => '<?php return ["root" => ["dev" => true]];',
],
],
]);
environment_tester::set_vendor_path(\org\bovigo\vfs\vfsStream::url('root/vendor'));
environment_tester::set_developer_mode(true);
$result = new \environment_results('custom_check');
$result = environment_tester::check_composer_developer_dependencies_not_installed($result);
$this->assertNull($result);
// Check that the dependencies tests do not error in these conditions.
$result = new \environment_results('custom_check');
$result = environment_tester::check_composer_dependencies_installed($result);
$this->assertNull($result);
}
public function test_composer_dev_not_installed(): void {
\org\bovigo\vfs\vfsStream::setup('root', null, [
'vendor' => [
'autoload.php' => '',
'composer' => [
'installed.php' => '<?php return ["root" => ["dev" => false]];',
],
],
]);
environment_tester::set_vendor_path(\org\bovigo\vfs\vfsStream::url('root/vendor'));
$result = new \environment_results('custom_check');
$result = environment_tester::check_composer_developer_dependencies_not_installed($result);
$this->assertNull($result);
// Check that the dependencies tests do not error in these conditions.
$result = new \environment_results('custom_check');
$result = environment_tester::check_composer_dependencies_installed($result);
$this->assertNull($result);
}
}