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:
Eloy Lafuente (stronk7) 2020-10-05 00:37:34 +02:00
parent ed103545dd
commit 8183def69e
7 changed files with 139 additions and 126 deletions

View File

@ -153,6 +153,7 @@ abstract class advanced_testcase extends base_testcase {
* @return PHPUnit\DbUnit\DataSet\FlatXmlDataSet
*/
protected function createFlatXMLDataSet($xmlFile) {
// TODO: MDL-67673 - removed
return new PHPUnit\DbUnit\DataSet\FlatXmlDataSet($xmlFile);
}
@ -163,24 +164,22 @@ abstract class advanced_testcase extends base_testcase {
* @return PHPUnit\DbUnit\DataSet\XmlDataSet
*/
protected function createXMLDataSet($xmlFile) {
return new PHPUnit\DbUnit\DataSet\XmlDataSet($xmlFile);
// TODO: MDL-67673 - deprecate this (debugging...)
return $this->dataset_from_files([$xmlFile]);
}
/**
* Creates a new CsvDataSet from the given array of csv files. (absolute paths.)
*
* @param array $files array tablename=>cvsfile
* @param string $delimiter
* @param string $enclosure
* @param string $escape
* @return PHPUnit\DbUnit\DataSet\CsvDataSet
* @param string $delimiter unused
* @param string $enclosure unused
* @param string $escape unused
* @return phpunit_dataset
*/
protected function createCsvDataSet($files, $delimiter = ',', $enclosure = '"', $escape = '"') {
$dataSet = new PHPUnit\DbUnit\DataSet\CsvDataSet($delimiter, $enclosure, $escape);
foreach($files as $table=>$file) {
$dataSet->addTable($table, $file);
}
return $dataSet;
// TODO: MDL-67673 - deprecate this (debugging...)
return $this->dataset_from_files($files);
}
/**
@ -190,7 +189,8 @@ abstract class advanced_testcase extends base_testcase {
* @return phpunit_ArrayDataSet
*/
protected function createArrayDataSet(array $data) {
return new phpunit_ArrayDataSet($data);
// TODO: MDL-67673 - deprecate this (debugging...)
return $this->dataset_from_array($data);
}
/**
@ -198,36 +198,59 @@ abstract class advanced_testcase extends base_testcase {
*
* Note: it is usually better to use data generators
*
* @param PHPUnit\DbUnit\DataSet\IDataSet $dataset
* @param phpunit_dataset $dataset
* @return void
*/
protected function loadDataSet(PHPUnit\DbUnit\DataSet\IDataSet $dataset) {
global $DB;
protected function loadDataSet(phpunit_dataset $dataset) {
// TODO: MDL-67673 - deprecate this (debugging...)
$dataset->to_database();
}
$structure = phpunit_util::get_tablestructure();
/**
* Creates a new dataset from CVS/XML files.
*
* This method accepts an array of full paths to CSV or XML files to be loaded
* into the dataset. For CSV files, the name of the table which the file belongs
* to needs to be specified. Example:
*
* $fullpaths = [
* '/path/to/users.xml',
* 'course' => '/path/to/courses.csv',
* ];
*
* @param array $files full paths to CSV or XML files to load.
* @return phpunit_dataset
*/
protected function dataset_from_files(array $files) {
// We ignore $delimiter, $enclosure and $escape, use the default ones in your fixtures.
$dataset = new phpunit_dataset();
$dataset->from_files($files);
return $dataset;
}
foreach($dataset->getTableNames() as $tablename) {
$table = $dataset->getTable($tablename);
$metadata = $dataset->getTableMetaData($tablename);
$columns = $metadata->getColumns();
/**
* Creates a new dataset from string (CSV or XML).
*
* @param string $content contents (CSV or XML) to load.
* @param string $type format of the content to be loaded (csv or xml).
* @param string $table name of the table which the file belongs to (only for CSV files).
*/
protected function dataset_from_string(string $content, string $type, ?string $table = null) {
$dataset = new phpunit_dataset();
$dataset->from_string($content, $type, $table);
return $dataset;
}
$doimport = false;
if (isset($structure[$tablename]['id']) and $structure[$tablename]['id']->auto_increment) {
$doimport = in_array('id', $columns);
}
for($r=0; $r<$table->getRowCount(); $r++) {
$record = $table->getRow($r);
if ($doimport) {
$DB->import_record($tablename, $record);
} else {
$DB->insert_record($tablename, $record);
}
}
if ($doimport) {
$DB->get_manager()->reset_sequence(new xmldb_table($tablename));
}
}
/**
* Creates a new dataset from PHP array.
*
* @param array $data array of tables, see {@see phpunit_dataset::from_array()} for supported formats.
* @return phpunit_dataset
*/
protected function dataset_from_array(array $data) {
$dataset = new phpunit_dataset();
$dataset->from_array($data);
return $dataset;
}
/**

View File

@ -318,26 +318,42 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->assertFalse($DB->get_record('user', array('id'=>9999)));
}
public function test_load_dataset() {
public function test_load_data_dataset_xml() {
global $DB;
$this->resetAfterTest();
$this->assertFalse($DB->record_exists('user', array('id'=>5)));
$this->assertFalse($DB->record_exists('user', array('id'=>7)));
$this->assertFalse($DB->record_exists('user', array('id' => 5)));
$this->assertFalse($DB->record_exists('user', array('id' => 7)));
$dataset = $this->createXMLDataSet(__DIR__.'/fixtures/sample_dataset.xml');
$this->loadDataSet($dataset);
$this->assertTrue($DB->record_exists('user', array('id'=>5)));
$this->assertTrue($DB->record_exists('user', array('id'=>7)));
$user5 = $DB->get_record('user', array('id'=>5));
$user7 = $DB->get_record('user', array('id'=>7));
$this->assertSame('john.doe', $user5->username);
$this->assertSame('jane.doe', $user7->username);
$this->assertTrue($DB->record_exists('user', array('id' => 5)));
$this->assertTrue($DB->record_exists('user', array('id' => 7)));
$user5 = $DB->get_record('user', array('id' => 5));
$user7 = $DB->get_record('user', array('id' => 7));
$this->assertSame('bozka.novakova', $user5->username);
$this->assertSame('pepa.novak', $user7->username);
$dataset = $this->createCsvDataSet(array('user'=>__DIR__.'/fixtures/sample_dataset.csv'));
}
public function test_load_dataset_csv() {
global $DB;
$this->resetAfterTest();
$this->assertFalse($DB->record_exists('user', array('id' => 8)));
$this->assertFalse($DB->record_exists('user', array('id' => 9)));
$dataset = $this->createCsvDataSet(array('user' => __DIR__.'/fixtures/sample_dataset.csv'));
$this->loadDataSet($dataset);
$this->assertEquals(8, $DB->get_field('user', 'id', array('username'=>'pepa.novak')));
$this->assertEquals(9, $DB->get_field('user', 'id', array('username'=>'bozka.novakova')));
$this->assertEquals(5, $DB->get_field('user', 'id', array('username' => 'bozka.novakova')));
$this->assertEquals(7, $DB->get_field('user', 'id', array('username' => 'pepa.novak')));
}
public function test_load_dataset_array() {
global $DB;
$this->resetAfterTest();
$data = array(
'user' => array(
@ -346,21 +362,24 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
array('low.secret', 'low@example.com'),
),
);
$this->assertFalse($DB->record_exists('user', array('email' => 'top@example.com')));
$this->assertFalse($DB->record_exists('user', array('email' => 'low@example.com')));
$dataset = $this->createArrayDataSet($data);
$this->loadDataSet($dataset);
$this->assertTrue($DB->record_exists('user', array('email'=>'top@example.com')));
$this->assertTrue($DB->record_exists('user', array('email'=>'low@example.com')));
$this->assertTrue($DB->record_exists('user', array('email' => 'top@example.com')));
$this->assertTrue($DB->record_exists('user', array('email' => 'low@example.com')));
$data = array(
'user' => array(
array('username'=>'noidea', 'email'=>'noidea@example.com'),
array('username'=>'onemore', 'email'=>'onemore@example.com'),
array('username' => 'noidea', 'email' => 'noidea@example.com'),
array('username' => 'onemore', 'email' => 'onemore@example.com'),
),
);
$dataset = $this->createArrayDataSet($data);
$this->loadDataSet($dataset);
$this->assertTrue($DB->record_exists('user', array('username'=>'noidea')));
$this->assertTrue($DB->record_exists('user', array('username'=>'onemore')));
$this->assertTrue($DB->record_exists('user', array('username' => 'noidea')));
$this->assertTrue($DB->record_exists('user', array('username' => 'onemore')));
}
public function test_assert_time_current() {

View File

@ -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);
}
}

View File

@ -164,7 +164,7 @@ class mod_data_search_test extends advanced_testcase {
'data_records' => __DIR__.'/fixtures/test_data_records.csv',
'data_content' => __DIR__.'/fixtures/test_data_content.csv',
);
$this->loadDataSet($this->createCsvDataSet($files));
$this->dataset_from_files($files)->to_database();
// Set dataid to the correct value now the data has been inserted by csv file.
$DB->execute('UPDATE {data_fields} SET dataid = ?', array($data->id));
$DB->execute('UPDATE {data_records} SET dataid = ?', array($data->id));

View File

@ -53,8 +53,7 @@ class quiz_report_responses_from_steps_testcase extends mod_quiz_attempt_walkthr
* Create a quiz add questions to it, walk through quiz attempts and then check results.
*
* @param array $quizsettings settings to override default settings for quiz created by generator. Taken from quizzes.csv.
* @param PHPUnit\DbUnit\DataSet\ITable[] $csvdata of data read from csv file "questionsXX.csv",
* "stepsXX.csv" and "responsesXX.csv".
* @param array $csvdata of data read from csv file "questionsXX.csv", "stepsXX.csv" and "responsesXX.csv".
* @dataProvider get_data_for_walkthrough
*/
public function test_walkthrough_from_csv($quizsettings, $csvdata) {
@ -66,8 +65,7 @@ class quiz_report_responses_from_steps_testcase extends mod_quiz_attempt_walkthr
$quizattemptids = $this->walkthrough_attempts($csvdata['steps']);
for ($rowno = 0; $rowno < $csvdata['responses']->getRowCount(); $rowno++) {
$responsesfromcsv = $csvdata['responses']->getRow($rowno);
foreach ($csvdata['responses'] as $responsesfromcsv) {
$responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv);
if (!isset($quizattemptids[$responses['quizattempt']])) {

View File

@ -66,8 +66,7 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
/**
* Create a quiz add questions to it, walk through quiz attempts and then check results.
*
* @param PHPUnit\DbUnit\DataSet\ITable[] of data read from csv file "questionsXX.csv",
* "stepsXX.csv" and "resultsXX.csv".
* @param array $csvdata data read from csv file "questionsXX.csv", "stepsXX.csv" and "resultsXX.csv".
* @dataProvider get_data_for_walkthrough
*/
public function test_walkthrough_from_csv($quizsettings, $csvdata) {
@ -89,12 +88,11 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
/**
* Check actual question stats are the same as that found in csv file.
*
* @param $qstats PHPUnit\DbUnit\DataSet\ITable data from csv file.
* @param $qstats array data from csv file.
* @param $questionstats \core_question\statistics\questions\all_calculated_for_qubaid_condition Calculated stats.
*/
protected function check_question_stats($qstats, $questionstats) {
for ($rowno = 0; $rowno < $qstats->getRowCount(); $rowno++) {
$slotqstats = $qstats->getRow($rowno);
foreach ($qstats as $slotqstats) {
foreach ($slotqstats as $statname => $slotqstat) {
if (!in_array($statname, array('slot', 'subqname')) && $slotqstat !== '') {
$this->assert_stat_equals($slotqstat,
@ -230,8 +228,7 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
* @param $whichtries
*/
protected function check_response_counts($responsecounts, $qubaids, $questions, $whichtries) {
for ($rowno = 0; $rowno < $responsecounts->getRowCount(); $rowno++) {
$expected = $responsecounts->getRow($rowno);
foreach ($responsecounts as $expected) {
$defaultsforexpected = array('randq' => '', 'variant' => '1', 'subpart' => '1');
foreach ($defaultsforexpected as $key => $expecteddefault) {
if (!isset($expected[$key])) {
@ -355,7 +352,7 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
/**
* Check the question stats and the response counts used in the statistics report. If the appropriate files exist in fixtures/.
*
* @param PHPUnit\DbUnit\DataSet\ITable[] $csvdata Data loaded from csv files for this test.
* @param array $csvdata Data loaded from csv files for this test.
* @param string $whichattempts
* @param string $whichtries
* @param \core\dml\sql_join $groupstudentsjoins

View File

@ -57,8 +57,7 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
* directory.
*
* @param array $quizsettings of settings read from csv file quizzes.csv
* @param PHPUnit\DbUnit\DataSet\ITable[] $csvdata of data read from csv file "questionsXX.csv",
* "stepsXX.csv" and "resultsXX.csv".
* @param array $csvdata of data read from csv file "questionsXX.csv", "stepsXX.csv" and "resultsXX.csv".
* @dataProvider get_data_for_walkthrough
*/
public function test_walkthrough_from_csv($quizsettings, $csvdata) {
@ -77,8 +76,8 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
$slots = array();
$qidsbycat = array();
$sumofgrades = 0;
for ($rowno = 0; $rowno < $qs->getRowCount(); $rowno++) {
$q = $this->explode_dot_separated_keys_to_make_subindexs($qs->getRow($rowno));
foreach ($qs as $qsrow) {
$q = $this->explode_dot_separated_keys_to_make_subindexs($qsrow);
$catname = array('name' => $q['cat']);
if (!$cat = $DB->get_record('question_categories', array('name' => $q['cat']))) {
@ -146,7 +145,7 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
* Create quiz, simulate attempts and check results (if resultsXX.csv exists).
*
* @param array $quizsettings Quiz overrides for this quiz.
* @param PHPUnit\DbUnit\DataSet\ITable[] $csvdata Data loaded from csv files for this test.
* @param array $csvdata Data loaded from csv files for this test.
*/
protected function create_quiz_simulate_attempts_and_check_results($quizsettings, $csvdata) {
$this->resetAfterTest(true);
@ -177,11 +176,11 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
*
* @param string $setname
* @param string $test
* @return PHPUnit\DbUnit\DataSet\ITable
* @return array
*/
protected function load_csv_data_file($setname, $test='') {
$files = array($setname => $this->get_full_path_of_csv_file($setname, $test));
return $this->createCsvDataSet($files)->getTable($setname);
return $this->dataset_from_files($files)->get_rows([$setname]);
}
/**
@ -217,14 +216,13 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
* test_walkthrough_from_csv.
*/
public function get_data_for_walkthrough() {
$quizzes = $this->load_csv_data_file('quizzes');
$quizzes = $this->load_csv_data_file('quizzes')['quizzes'];
$datasets = array();
for ($rowno = 0; $rowno < $quizzes->getRowCount(); $rowno++) {
$quizsettings = $quizzes->getRow($rowno);
foreach ($quizzes as $quizsettings) {
$dataset = array();
foreach ($this->files as $file) {
if (file_exists($this->get_full_path_of_csv_file($file, $quizsettings['testnumber']))) {
$dataset[$file] = $this->load_csv_data_file($file, $quizsettings['testnumber']);
$dataset[$file] = $this->load_csv_data_file($file, $quizsettings['testnumber'])[$file];
}
}
$datasets[] = array($quizsettings, $dataset);
@ -233,15 +231,15 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
}
/**
* @param $steps PHPUnit\DbUnit\DataSet\ITable the step data from the csv file.
* @param $steps array the step data from the csv file.
* @return array attempt no as in csv file => the id of the quiz_attempt as stored in the db.
*/
protected function walkthrough_attempts($steps) {
global $DB;
$attemptids = array();
for ($rowno = 0; $rowno < $steps->getRowCount(); $rowno++) {
foreach ($steps as $steprow) {
$step = $this->explode_dot_separated_keys_to_make_subindexs($steps->getRow($rowno));
$step = $this->explode_dot_separated_keys_to_make_subindexs($steprow);
// Find existing user or make a new user to do the quiz.
$username = array('firstname' => $step['firstname'],
'lastname' => $step['lastname']);
@ -294,12 +292,12 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
}
/**
* @param $results PHPUnit\DbUnit\DataSet\ITable the results data from the csv file.
* @param $results array the results data from the csv file.
* @param $attemptids array attempt no as in csv file => the id of the quiz_attempt as stored in the db.
*/
protected function check_attempts_results($results, $attemptids) {
for ($rowno = 0; $rowno < $results->getRowCount(); $rowno++) {
$result = $this->explode_dot_separated_keys_to_make_subindexs($results->getRow($rowno));
foreach ($results as $resultrow) {
$result = $this->explode_dot_separated_keys_to_make_subindexs($resultrow);
// Re-load quiz attempt data.
$attemptobj = quiz_attempt::create($attemptids[$result['quizattempt']]);
$this->check_attempt_results($result, $attemptobj);