Merge branch 'MDL-43266_master-last-fixes' of git://github.com/dmonllao/moodle

Conflicts:
	lib/phpunit/classes/util.php
This commit is contained in:
Eloy Lafuente (stronk7) 2014-03-13 19:12:25 +01:00
commit 1700bec3c6
3 changed files with 161 additions and 13 deletions

View file

@ -85,6 +85,10 @@ class behat_util extends testing_util {
install_cli_database($options, false); install_cli_database($options, false);
// We need to keep the installed dataroot filedir files.
// So each time we reset the dataroot before running a test, the default files are still installed.
self::save_original_data_files();
$frontpagesummary = new admin_setting_special_frontpagedesc(); $frontpagesummary = new admin_setting_special_frontpagedesc();
$frontpagesummary->write_setting(self::BEHATSITENAME); $frontpagesummary->write_setting(self::BEHATSITENAME);

4
lib/phpunit/classes/util.php Normal file → Executable file
View file

@ -387,6 +387,10 @@ class phpunit_util extends testing_util {
// Disable all logging for performance and sanity reasons. // Disable all logging for performance and sanity reasons.
set_config('enabled_stores', '', 'tool_log'); set_config('enabled_stores', '', 'tool_log');
// We need to keep the installed dataroot filedir files.
// So each time we reset the dataroot before running a test, the default files are still installed.
self::save_original_data_files();
// install timezone info // install timezone info
$timezones = get_records_csv($CFG->libdir.'/timezone.txt', 'timezone'); $timezones = get_records_csv($CFG->libdir.'/timezone.txt', 'timezone');
update_timezone_records($timezones); update_timezone_records($timezones);

166
lib/testing/classes/util.php Normal file → Executable file
View file

@ -34,6 +34,11 @@
*/ */
abstract class testing_util { abstract class testing_util {
/**
* @var string dataroot (likely to be $CFG->dataroot).
*/
private static $dataroot = null;
/** /**
* @var int last value of db writes counter, used for db resetting * @var int last value of db writes counter, used for db resetting
*/ */
@ -64,6 +69,53 @@ abstract class testing_util {
*/ */
protected static $sequencenames = null; protected static $sequencenames = null;
/**
* @var string name of the json file where we store the list of dataroot files to not reset during reset_dataroot.
*/
private static $originaldatafilesjson = 'originaldatafiles.json';
/**
* @var boolean set to true once $originaldatafilesjson file is created.
*/
private static $originaldatafilesjsonadded = false;
/**
* Return the name of the JSON file containing the init filenames.
*
* @static
* @return string
*/
public static function get_originaldatafilesjson() {
return self::$originaldatafilesjson;
}
/**
* Return the dataroot. It's useful when mocking the dataroot when unit testing this class itself.
*
* @static
* @return string the dataroot.
*/
public static function get_dataroot() {
global $CFG;
// By default it's the test framework dataroot.
if (empty(self::$dataroot)) {
self::$dataroot = $CFG->dataroot;
}
return self::$dataroot;
}
/**
* Set the dataroot. It's useful when mocking the dataroot when unit testing this class itself.
*
* @param string $dataroot the dataroot of the test framework.
* @static
*/
public static function set_dataroot($dataroot) {
self::$dataroot = $dataroot;
}
/** /**
* Returns the testing framework name * Returns the testing framework name
* @static * @static
@ -99,7 +151,7 @@ abstract class testing_util {
$framework = self::get_framework(); $framework = self::get_framework();
if (!file_exists($CFG->dataroot . '/' . $framework . 'testdir.txt')) { if (!file_exists(self::get_dataroot() . '/' . $framework . 'testdir.txt')) {
// this is already tested in bootstrap script, // this is already tested in bootstrap script,
// but anyway presence of this file means the dataroot is for testing // but anyway presence of this file means the dataroot is for testing
return false; return false;
@ -128,7 +180,7 @@ abstract class testing_util {
$framework = self::get_framework(); $framework = self::get_framework();
$datarootpath = $CFG->dataroot . '/' . $framework; $datarootpath = self::get_dataroot() . '/' . $framework;
if (!file_exists($datarootpath . '/tabledata.ser') or !file_exists($datarootpath . '/tablestructure.ser')) { if (!file_exists($datarootpath . '/tabledata.ser') or !file_exists($datarootpath . '/tablestructure.ser')) {
return false; return false;
} }
@ -178,12 +230,12 @@ abstract class testing_util {
} }
} }
$data = serialize($data); $data = serialize($data);
$datafile = $CFG->dataroot . '/' . $framework . '/tabledata.ser'; $datafile = self::get_dataroot() . '/' . $framework . '/tabledata.ser';
file_put_contents($datafile, $data); file_put_contents($datafile, $data);
testing_fix_file_permissions($datafile); testing_fix_file_permissions($datafile);
$structure = serialize($structure); $structure = serialize($structure);
$structurefile = $CFG->dataroot . '/' . $framework . '/tablestructure.ser'; $structurefile = self::get_dataroot() . '/' . $framework . '/tablestructure.ser';
file_put_contents($structurefile, $structure); file_put_contents($structurefile, $structure);
testing_fix_file_permissions($structurefile); testing_fix_file_permissions($structurefile);
} }
@ -201,7 +253,7 @@ abstract class testing_util {
set_config($framework . 'test', $hash); set_config($framework . 'test', $hash);
// hash all plugin versions - helps with very fast detection of db structure changes // hash all plugin versions - helps with very fast detection of db structure changes
$hashfile = $CFG->dataroot . '/' . $framework . '/versionshash.txt'; $hashfile = self::get_dataroot() . '/' . $framework . '/versionshash.txt';
file_put_contents($hashfile, $hash); file_put_contents($hashfile, $hash);
testing_fix_file_permissions($hashfile); testing_fix_file_permissions($hashfile);
} }
@ -216,7 +268,7 @@ abstract class testing_util {
$framework = self::get_framework(); $framework = self::get_framework();
$datafile = $CFG->dataroot . '/' . $framework . '/tabledata.ser'; $datafile = self::get_dataroot() . '/' . $framework . '/tabledata.ser';
if (!file_exists($datafile)) { if (!file_exists($datafile)) {
// Not initialised yet. // Not initialised yet.
return array(); return array();
@ -244,7 +296,7 @@ abstract class testing_util {
$framework = self::get_framework(); $framework = self::get_framework();
$structurefile = $CFG->dataroot . '/' . $framework . '/tablestructure.ser'; $structurefile = self::get_dataroot() . '/' . $framework . '/tablestructure.ser';
if (!file_exists($structurefile)) { if (!file_exists($structurefile)) {
// Not initialised yet. // Not initialised yet.
return array(); return array();
@ -584,18 +636,39 @@ abstract class testing_util {
$childclassname = self::get_framework() . '_util'; $childclassname = self::get_framework() . '_util';
$handle = opendir($CFG->dataroot); // Do not delete automatically installed files.
self::skip_original_data_files($childclassname);
// Clean up the dataroot folder.
$handle = opendir(self::get_dataroot());
while (false !== ($item = readdir($handle))) { while (false !== ($item = readdir($handle))) {
if (in_array($item, $childclassname::$datarootskiponreset)) { if (in_array($item, $childclassname::$datarootskiponreset)) {
continue; continue;
} }
if (is_dir("$CFG->dataroot/$item")) { if (is_dir(self::get_dataroot()."/$item")) {
remove_dir("$CFG->dataroot/$item", false); remove_dir(self::get_dataroot()."/$item", false);
} else { } else {
unlink("$CFG->dataroot/$item"); unlink(self::get_dataroot()."/$item");
} }
} }
closedir($handle); closedir($handle);
// Clean up the dataroot/filedir folder.
if (file_exists(self::get_dataroot() . '/filedir')) {
$handle = opendir(self::get_dataroot() . '/filedir');
while (false !== ($item = readdir($handle))) {
if (in_array('filedir/' . $item, $childclassname::$datarootskiponreset)) {
continue;
}
if (is_dir(self::get_dataroot()."/filedir/$item")) {
remove_dir(self::get_dataroot()."/filedir/$item", false);
} else {
unlink(self::get_dataroot()."/filedir/$item");
}
}
closedir($handle);
}
make_temp_directory(''); make_temp_directory('');
make_cache_directory(''); make_cache_directory('');
make_localcache_directory(''); make_localcache_directory('');
@ -730,17 +803,84 @@ abstract class testing_util {
$framework = self::get_framework(); $framework = self::get_framework();
$childclassname = $framework . '_util'; $childclassname = $framework . '_util';
$files = scandir($CFG->dataroot . '/' . $framework); $files = scandir(self::get_dataroot() . '/' . $framework);
foreach ($files as $file) { foreach ($files as $file) {
if (in_array($file, $childclassname::$datarootskipondrop)) { if (in_array($file, $childclassname::$datarootskipondrop)) {
continue; continue;
} }
$path = $CFG->dataroot . '/' . $framework . '/' . $file; $path = self::get_dataroot() . '/' . $framework . '/' . $file;
if (is_dir($path)) { if (is_dir($path)) {
remove_dir($path, false); remove_dir($path, false);
} else { } else {
unlink($path); unlink($path);
} }
} }
$jsonfilepath = self::get_dataroot() . '/' . self::$originaldatafilesjson;
if (file_exists($jsonfilepath)) {
// Delete the json file.
unlink($jsonfilepath);
// Delete the dataroot filedir.
remove_dir(self::get_dataroot() . '/filedir', false);
}
}
/**
* Skip the original dataroot files to not been reset.
*
* @static
* @param string $utilclassname the util class name..
*/
protected static function skip_original_data_files($utilclassname) {
$jsonfilepath = self::get_dataroot() . '/' . self::$originaldatafilesjson;
if (file_exists($jsonfilepath)) {
$listfiles = file_get_contents($jsonfilepath);
// Mark each files as to not be reset.
if (!empty($listfiles) && !self::$originaldatafilesjsonadded) {
$originaldatarootfiles = json_decode($listfiles);
// Keep the json file. Only drop_dataroot() should delete it.
$originaldatarootfiles[] = self::$originaldatafilesjson;
$utilclassname::$datarootskiponreset = array_merge($utilclassname::$datarootskiponreset,
$originaldatarootfiles);
self::$originaldatafilesjsonadded = true;
}
}
}
/**
* Save the list of the original dataroot files into a json file.
*/
protected static function save_original_data_files() {
global $CFG;
$jsonfilepath = self::get_dataroot() . '/' . self::$originaldatafilesjson;
// Save the original dataroot files if not done (only executed the first time).
if (!file_exists($jsonfilepath)) {
$listfiles = array();
$listfiles['filedir/.'] = 'filedir/.';
$listfiles['filedir/..'] = 'filedir/..';
$filedir = self::get_dataroot() . '/filedir';
if (file_exists($filedir)) {
$directory = new RecursiveDirectoryIterator($filedir);
foreach (new RecursiveIteratorIterator($directory) as $file) {
if ($file->isDir()) {
$key = substr($file->getPath(), strlen(self::get_dataroot() . '/'));
} else {
$key = substr($file->getPathName(), strlen(self::get_dataroot() . '/'));
}
$listfiles[$key] = $key;
}
}
// Save the file list in a JSON file.
$fp = fopen($jsonfilepath, 'w');
fwrite($fp, json_encode($listfiles));
fclose($fp);
}
} }
} }