first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
<?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/>.
/**
* Frozen clock for testing purposes.
*
* @package core
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @property-read \DateTimeImmutable $time The current time of the clock
*/
class frozen_clock implements \core\clock {
/** @var DateTimeImmutable The next time of the clock */
public DateTimeImmutable $time;
/**
* Create a new instance of the frozen clock.
*
* @param null|int $time The initial time to use. If not specified, the current time is used.
*/
public function __construct(
?int $time = null,
) {
if ($time) {
$this->time = new \DateTimeImmutable("@{$time}");
} else {
$this->time = new \DateTimeImmutable();
}
}
public function now(): \DateTimeImmutable {
return $this->time;
}
public function time(): int {
return $this->time->getTimestamp();
}
/**
* Set the time of the clock.
*
* @param int $time
*/
public function set_to(int $time): void {
$this->time = new \DateTimeImmutable("@{$time}");
}
/**
* Bump the time by a number of seconds.
*
* @param int $seconds
*/
public function bump(int $seconds = 1): void {
$this->time = $this->time->modify("+{$seconds} seconds");
}
}
@@ -0,0 +1,67 @@
<?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/>.
/**
* Incrementing clock for testing purposes.
*
* @package core
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @property-read int $time The current time of the clock
*/
class incrementing_clock implements \core\clock {
/** @var int The next time of the clock */
public int $time;
/**
* Create a new instance of the incrementing clock.
*
* @param null|int $starttime The initial time to use. If not specified, the current time is used.
*/
public function __construct(
?int $starttime = null,
) {
$this->time = $starttime ?? time();
}
public function now(): \DateTimeImmutable {
return new \DateTimeImmutable('@' . $this->time++);
}
public function time(): int {
return $this->now()->getTimestamp();
}
/**
* Set the time of the clock.
*
* @param int $time
*/
public function set_to(int $time): void {
$this->time = $time;
}
/**
* Bump the time by a number of seconds.
*
* Note: The act of fetching the time will also bump the time by one second.
*
* @param int $seconds
*/
public function bump(int $seconds = 1): void {
$this->time += $seconds;
}
}
+116
View File
@@ -0,0 +1,116 @@
<?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/>.
/**
* Nasty strings to use in tests.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Nasty strings manager.
*
* Responds to nasty strings requests with a random string of the list
* to try with different combinations in different places.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class nasty_strings {
/**
* List of different strings to fill fields and assert against them
*
* Non of these strings can be a part of another one, this would not be good
* when using more one string at the same time and asserting results.
*
* @static
* @var array
*/
protected static $strings = array(
'< > & &lt; &gt; &amp; \' \\" \ \'$@NULL@$ @@TEST@@ \\\" \\ , ; : . 日本語­% %%',
'&amp; \' \\" \ \'$@NULL@$ < > & &lt; &gt; @@TEST@@ \\\" \\ , ; : . 日本語­% %%',
'< > & &lt; &gt; &amp; \' \\" \ \\\" \\ , ; : . \'$@NULL@$ @@TEST@@ 日本語­% %%',
'< > & &lt; &gt; &amp; \' \\" \ \'$@NULL@$ 日本語­% %%@@TEST@@ \. \\" \\ , ; :',
'< > & &lt; &gt; \\\" \\ , ; : . 日本語&amp; \' \\" \ \'$@NULL@$ @@TEST@@­% %%',
'\' \\" \ \'$@NULL@$ @@TEST@@ < > & &lt; &gt; &amp; \\\" \\ , ; : . 日本語­% %%',
'\\\" \\ , ; : . 日本語­% < > & &lt; &gt; &amp; \' \\" \ \'$@NULL@$ @@TEST@@ %%',
'< > & &lt; &gt; &amp; \' \\" \ \'$@NULL@$ 日本語­% %% @@TEST@@ \\\" \\ . , ; :',
'. 日本語&amp; \' \\" < > & &lt; &gt; \\ , ; : \ \'$@NULL@$ \\\" @@TEST@@­% %%',
'&amp; \' \\" \ < > & &lt; &gt; \\\" \\ , ; : . 日本語\'$@NULL@$ @@TEST@@­% %%',
);
/**
* Already used nasty strings.
*
* This array will be cleaned before each scenario.
*
* @static
* @var array
*/
protected static $usedstrings = array();
/**
* Returns a nasty string and stores the key mapping.
*
* @static
* @param string $key The key
* @return string
*/
public static function get($key) {
// If have been used during the this tests return it.
if (isset(self::$usedstrings[$key])) {
return self::$strings[self::$usedstrings[$key]];
}
// Getting non-used random string.
do {
$index = self::random_index();
} while (in_array($index, self::$usedstrings));
// Mark the string as already used.
self::$usedstrings[$key] = $index;
return self::$strings[$index];
}
/**
* Resets the used strings var.
* @static
* @return void
*/
public static function reset_used_strings() {
self::$usedstrings = array();
}
/**
* Returns a random index.
* @static
* @return int
*/
protected static function random_index() {
return mt_rand(0, count(self::$strings) - 1);
}
}
+121
View File
@@ -0,0 +1,121 @@
<?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/>.
/**
* Tests lock
*
* @package core
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../lib.php');
/**
* Tests lock to prevent concurrent executions of the same test suite
*
* @package core
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class test_lock {
/**
* @var array Array of resource used for prevention of parallel test execution
*/
protected static $lockhandles = array();
/**
* Prevent parallel test execution - this can not work in Moodle because we modify database and dataroot.
*
* Note: do not call manually!
*
* @internal
* @static
* @param string $framework phpunit|behat
* @param string $lockfilesuffix A sub-type used by the framework
* @return void
*/
public static function acquire(string $framework, string $lockfilesuffix = '') {
global $CFG;
$datarootpath = $CFG->{$framework . '_dataroot'} . '/' . $framework;
$lockfile = "{$datarootpath}/lock{$lockfilesuffix}";
if (!file_exists($datarootpath)) {
// Dataroot not initialised yet.
return;
}
if (!file_exists($lockfile)) {
file_put_contents($lockfile, 'This file prevents concurrent execution of Moodle ' . $framework . ' tests');
testing_fix_file_permissions($lockfile);
}
$lockhandlename = self::get_lock_handle_name($framework, $lockfilesuffix);
if (self::$lockhandles[$lockhandlename] = fopen($lockfile, 'r')) {
$wouldblock = null;
$locked = flock(self::$lockhandles[$lockhandlename], (LOCK_EX | LOCK_NB), $wouldblock);
if (!$locked) {
if ($wouldblock) {
echo "Waiting for other test execution to complete...\n";
}
$locked = flock(self::$lockhandles[$lockhandlename], LOCK_EX);
}
if (!$locked) {
fclose(self::$lockhandles[$lockhandlename]);
self::$lockhandles[$lockhandlename] = null;
}
}
register_shutdown_function(['test_lock', 'release'], $framework, $lockfilesuffix);
}
/**
* Note: do not call manually!
* @internal
* @static
* @param string $framework phpunit|behat
* @param string $lockfilesuffix A sub-type used by the framework
* @return void
*/
public static function release(string $framework, string $lockfilesuffix = '') {
$lockhandlename = self::get_lock_handle_name($framework, $lockfilesuffix);
if (self::$lockhandles[$lockhandlename]) {
flock(self::$lockhandles[$lockhandlename], LOCK_UN);
fclose(self::$lockhandles[$lockhandlename]);
self::$lockhandles[$lockhandlename] = null;
}
}
/**
* Get the name of the lock handle stored in the class.
*
* @param string $framework
* @param string $lockfilesuffix
* @return string
*/
protected static function get_lock_handle_name(string $framework, string $lockfilesuffix): string {
$lockhandlepieces = [$framework];
if (!empty($lockfilesuffix)) {
$lockhandlepieces[] = $lockfilesuffix;
}
return implode('%', $lockhandlepieces);
}
}
+207
View File
@@ -0,0 +1,207 @@
<?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/>.
/**
* Tests finder
*
* @package core
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Finds components and plugins with tests
*
* @package core
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tests_finder {
/**
* Returns all the components with tests of the specified type
* @param string $testtype The kind of test we are looking for
* @return array
*/
public static function get_components_with_tests($testtype) {
// Get all the components
$components = self::get_all_plugins_with_tests($testtype) + self::get_all_subsystems_with_tests($testtype);
// Get all the directories having tests
$directories = self::get_all_directories_with_tests($testtype);
// Find any directory not covered by proper components
$remaining = array_diff($directories, $components);
// Add them to the list of components
$components += $remaining;
return $components;
}
/**
* Returns all the plugins having tests
* @param string $testtype The kind of test we are looking for
* @return array all the plugins having tests
*/
private static function get_all_plugins_with_tests($testtype) {
$pluginswithtests = array();
$plugintypes = core_component::get_plugin_types();
ksort($plugintypes);
foreach ($plugintypes as $type => $unused) {
$plugs = core_component::get_plugin_list($type);
ksort($plugs);
foreach ($plugs as $plug => $fullplug) {
// Look for tests recursively
if (self::directory_has_tests($fullplug, $testtype)) {
$pluginswithtests[$type . '_' . $plug] = $fullplug;
}
}
}
return $pluginswithtests;
}
/**
* Returns all the subsystems having tests
*
* Note we are hacking here the list of subsystems
* to cover some well-known subsystems that are not properly
* returned by the {@link get_core_subsystems()} function.
*
* @param string $testtype The kind of test we are looking for
* @return array all the subsystems having tests
*/
private static function get_all_subsystems_with_tests($testtype) {
global $CFG;
$subsystemswithtests = array();
$subsystems = core_component::get_core_subsystems();
// Hack the list a bit to cover some well-known ones
$subsystems['backup'] = $CFG->dirroot.'/backup';
$subsystems['db-dml'] = $CFG->dirroot.'/lib/dml';
$subsystems['db-ddl'] = $CFG->dirroot.'/lib/ddl';
ksort($subsystems);
foreach ($subsystems as $subsys => $fullsubsys) {
if ($fullsubsys === null) {
continue;
}
if (!is_dir($fullsubsys)) {
continue;
}
// Look for tests recursively
if (self::directory_has_tests($fullsubsys, $testtype)) {
$subsystemswithtests['core_' . $subsys] = $fullsubsys;
}
}
return $subsystemswithtests;
}
/**
* Returns all the directories having tests
*
* @param string $testtype The kind of test we are looking for
* @return array all directories having tests
*/
private static function get_all_directories_with_tests($testtype) {
global $CFG;
// List of directories to exclude from test file searching.
$excludedir = array('node_modules', 'vendor');
// Get first level directories in which tests should be searched.
$directoriestosearch = array();
$alldirs = glob($CFG->dirroot . DIRECTORY_SEPARATOR . '*' , GLOB_ONLYDIR);
foreach ($alldirs as $dir) {
if (!in_array(basename($dir), $excludedir) && (filetype($dir) != 'link')) {
$directoriestosearch[] = $dir;
}
}
// Search for tests in valid directories.
$dirs = array();
foreach ($directoriestosearch as $dir) {
$dirite = new RecursiveDirectoryIterator($dir);
$iteite = new RecursiveIteratorIterator($dirite);
$regexp = self::get_regexp($testtype);
$regite = new RegexIterator($iteite, $regexp);
foreach ($regite as $path => $element) {
$key = dirname(dirname($path));
$value = trim(str_replace(DIRECTORY_SEPARATOR, '_', str_replace($CFG->dirroot, '', $key)), '_');
$dirs[$key] = $value;
}
}
ksort($dirs);
return array_flip($dirs);
}
/**
* Returns if a given directory has tests (recursively)
*
* @param string $dir full path to the directory to look for phpunit tests
* @param string $testtype phpunit|behat
* @return bool if a given directory has tests (true) or no (false)
*/
private static function directory_has_tests($dir, $testtype) {
if (!is_dir($dir)) {
return false;
}
$dirite = new RecursiveDirectoryIterator($dir);
$iteite = new RecursiveIteratorIterator($dirite);
$regexp = self::get_regexp($testtype);
$regite = new RegexIterator($iteite, $regexp);
$regite->rewind();
if ($regite->valid()) {
return true;
}
return false;
}
/**
* Returns the regular expression to match by the test files
* @param string $testtype
* @return string
*/
private static function get_regexp($testtype) {
$sep = preg_quote(DIRECTORY_SEPARATOR, '|');
switch ($testtype) {
case 'phpunit':
$regexp = '|'.$sep.'tests'.$sep.'.*_test\.php$|';
break;
case 'features':
$regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'.*\.feature$|';
break;
case 'stepsdefinitions':
$regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'behat_.*\.php$|';
break;
case 'behat':
$regexp = '!'.$sep.'tests'.$sep.'behat'.$sep.'(.*\.feature)|(behat_.*\.php)$!';
break;
}
return $regexp;
}
}
File diff suppressed because it is too large Load Diff