MDL-67673 phpunit: Move tests to use new phpunit_dataset
- Make advanced_testcase old methods to use new ones internally. - Fix advanced_testcase, statslib, mod/quiz and mod/data tests. Originally MDL-64600
This commit is contained in:
+20
-42
@@ -95,28 +95,15 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
/**
|
||||
* Function to setup database.
|
||||
*
|
||||
* @param array $dataset An array of tables including the log table.
|
||||
* @param array $tables
|
||||
* @param phpunit_dataset $dataset Containing all the information loaded from fixtures.
|
||||
* @param array $filter Tables to be sent to database.
|
||||
*/
|
||||
protected function prepare_db($dataset, $tables) {
|
||||
global $DB;
|
||||
|
||||
foreach ($tables as $tablename) {
|
||||
$DB->delete_records($tablename);
|
||||
|
||||
foreach ($dataset as $name => $table) {
|
||||
|
||||
if ($tablename == $name) {
|
||||
|
||||
$rows = $table->getRowCount();
|
||||
|
||||
for ($i = 0; $i < $rows; $i++) {
|
||||
$row = $table->getRow($i);
|
||||
|
||||
$DB->insert_record($tablename, $row, false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
$dataset->to_database([$tablename]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,25 +169,19 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
* Load dataset from XML file.
|
||||
*
|
||||
* @param string $file The name of the file to load
|
||||
* @return array
|
||||
* @return phpunit_dataset
|
||||
*/
|
||||
protected function load_xml_data_file($file) {
|
||||
static $replacements = null;
|
||||
|
||||
$raw = $this->createXMLDataSet($file);
|
||||
$clean = new PHPUnit\DbUnit\DataSet\ReplacementDataSet($raw);
|
||||
$xml = file_get_contents($file);
|
||||
|
||||
// Apply all the replacements straight in xml.
|
||||
foreach ($this->replacements as $placeholder => $value) {
|
||||
$clean->addFullReplacement($placeholder, $value);
|
||||
$placeholder = preg_quote($placeholder, '/');
|
||||
$xml = preg_replace('/' . $placeholder . '/', $value, $xml);
|
||||
}
|
||||
|
||||
$logs = new PHPUnit\DbUnit\DataSet\Filter($clean);
|
||||
$logs->addIncludeTables(array('log'));
|
||||
|
||||
$stats = new PHPUnit\DbUnit\DataSet\Filter($clean);
|
||||
$stats->addIncludeTables(array('stats_daily', 'stats_user_daily'));
|
||||
|
||||
return array($logs, $stats);
|
||||
return $this->dataset_from_string($xml, 'xml');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,7 +243,7 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
foreach ($expected as $type => $table) {
|
||||
$records = $DB->get_records($type);
|
||||
|
||||
$rows = $table->getRowCount();
|
||||
$rows = count($table);
|
||||
|
||||
$message = 'Incorrect number of results returned for '. $type;
|
||||
|
||||
@@ -273,7 +254,7 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
$this->assertCount($rows, $records, $message);
|
||||
|
||||
for ($i = 0; $i < $rows; $i++) {
|
||||
$row = $table->getRow($i);
|
||||
$row = $table[$i];
|
||||
$found = 0;
|
||||
|
||||
foreach ($records as $key => $record) {
|
||||
@@ -334,7 +315,7 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
// Note: within 3 days of a DST change - -3 days != 3 * 24 hours (it may be more or less).
|
||||
$this->assertLessThanOrEqual(1, stats_get_start_from('daily') - strtotime('-3 days', time()), 'All start time');
|
||||
|
||||
$this->prepare_db($dataset[0], array('log'));
|
||||
$this->prepare_db($dataset, array('log'));
|
||||
$records = $DB->get_records('log');
|
||||
|
||||
$this->assertEquals($day + 14410, stats_get_start_from('daily'), 'Log entry start');
|
||||
@@ -345,7 +326,7 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
$CFG->statsfirstrun = 14515200;
|
||||
$this->assertLessThanOrEqual(1, stats_get_start_from('daily') - (time() - (14515200)), 'Specified start time');
|
||||
|
||||
$this->prepare_db($dataset[1], array('stats_daily'));
|
||||
$this->prepare_db($dataset, array('stats_daily'));
|
||||
$this->assertEquals($day + DAYSECS, stats_get_start_from('daily'), 'Daily stats start time');
|
||||
|
||||
// New log stores.
|
||||
@@ -587,8 +568,7 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
global $CFG, $DB, $USER;
|
||||
|
||||
$dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test09.xml");
|
||||
|
||||
$this->prepare_db($dataset[0], array('log'));
|
||||
$this->prepare_db($dataset, array('log'));
|
||||
|
||||
// This nonsense needs to be rewritten.
|
||||
$date = new DateTime('now', core_date::get_server_timezone_object());
|
||||
@@ -689,8 +669,7 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
public function test_statslib_temp_table_setup() {
|
||||
global $DB;
|
||||
|
||||
$logs = array();
|
||||
$this->prepare_db($logs, array('log'));
|
||||
$DB->delete_records('log');
|
||||
|
||||
stats_temp_table_create();
|
||||
stats_temp_table_setup();
|
||||
@@ -749,9 +728,8 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
global $CFG, $DB;
|
||||
|
||||
$dataset = $this->load_xml_data_file(__DIR__."/fixtures/{$xmlfile}");
|
||||
|
||||
list($logs, $stats) = $dataset;
|
||||
$this->prepare_db($logs, array('log'));
|
||||
$stats = $this->prepare_db($dataset, array('log'));
|
||||
$stats = $dataset->get_rows(['stats_daily', 'stats_user_daily']);
|
||||
|
||||
// Stats cron daily uses mtrace, turn on buffering to silence output.
|
||||
ob_start();
|
||||
@@ -780,8 +758,8 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
$gr = get_guest_role();
|
||||
|
||||
$dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test10.xml");
|
||||
|
||||
$this->prepare_db($dataset[0], array('log'));
|
||||
$this->prepare_db($dataset, array('log'));
|
||||
$stats = $dataset->get_rows(['stats_user_daily']);
|
||||
|
||||
// Stats cron daily uses mtrace, turn on buffering to silence output.
|
||||
ob_start();
|
||||
@@ -789,6 +767,6 @@ class core_statslib_testcase extends advanced_testcase {
|
||||
$output = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
$this->verify_stats($dataset[1], $output);
|
||||
$this->verify_stats($dataset, $output);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user