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 be396eddf0
commit 1f13dff1a7
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 * @return PHPUnit\DbUnit\DataSet\FlatXmlDataSet
*/ */
protected function createFlatXMLDataSet($xmlFile) { protected function createFlatXMLDataSet($xmlFile) {
// TODO: MDL-67673 - removed
return new PHPUnit\DbUnit\DataSet\FlatXmlDataSet($xmlFile); return new PHPUnit\DbUnit\DataSet\FlatXmlDataSet($xmlFile);
} }
@ -163,24 +164,22 @@ abstract class advanced_testcase extends base_testcase {
* @return PHPUnit\DbUnit\DataSet\XmlDataSet * @return PHPUnit\DbUnit\DataSet\XmlDataSet
*/ */
protected function createXMLDataSet($xmlFile) { 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.) * Creates a new CsvDataSet from the given array of csv files. (absolute paths.)
* *
* @param array $files array tablename=>cvsfile * @param array $files array tablename=>cvsfile
* @param string $delimiter * @param string $delimiter unused
* @param string $enclosure * @param string $enclosure unused
* @param string $escape * @param string $escape unused
* @return PHPUnit\DbUnit\DataSet\CsvDataSet * @return phpunit_dataset
*/ */
protected function createCsvDataSet($files, $delimiter = ',', $enclosure = '"', $escape = '"') { protected function createCsvDataSet($files, $delimiter = ',', $enclosure = '"', $escape = '"') {
$dataSet = new PHPUnit\DbUnit\DataSet\CsvDataSet($delimiter, $enclosure, $escape); // TODO: MDL-67673 - deprecate this (debugging...)
foreach($files as $table=>$file) { return $this->dataset_from_files($files);
$dataSet->addTable($table, $file);
}
return $dataSet;
} }
/** /**
@ -190,7 +189,8 @@ abstract class advanced_testcase extends base_testcase {
* @return phpunit_ArrayDataSet * @return phpunit_ArrayDataSet
*/ */
protected function createArrayDataSet(array $data) { 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 * Note: it is usually better to use data generators
* *
* @param PHPUnit\DbUnit\DataSet\IDataSet $dataset * @param phpunit_dataset $dataset
* @return void * @return void
*/ */
protected function loadDataSet(PHPUnit\DbUnit\DataSet\IDataSet $dataset) { protected function loadDataSet(phpunit_dataset $dataset) {
global $DB; // 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); * Creates a new dataset from string (CSV or XML).
$metadata = $dataset->getTableMetaData($tablename); *
$columns = $metadata->getColumns(); * @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) { * Creates a new dataset from PHP array.
$doimport = in_array('id', $columns); *
} * @param array $data array of tables, see {@see phpunit_dataset::from_array()} for supported formats.
* @return phpunit_dataset
for($r=0; $r<$table->getRowCount(); $r++) { */
$record = $table->getRow($r); protected function dataset_from_array(array $data) {
if ($doimport) { $dataset = new phpunit_dataset();
$DB->import_record($tablename, $record); $dataset->from_array($data);
} else { return $dataset;
$DB->insert_record($tablename, $record);
}
}
if ($doimport) {
$DB->get_manager()->reset_sequence(new xmldb_table($tablename));
}
}
} }
/** /**

View file

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

View file

@ -95,28 +95,15 @@ class core_statslib_testcase extends advanced_testcase {
/** /**
* Function to setup database. * Function to setup database.
* *
* @param array $dataset An array of tables including the log table. * @param phpunit_dataset $dataset Containing all the information loaded from fixtures.
* @param array $tables * @param array $filter Tables to be sent to database.
*/ */
protected function prepare_db($dataset, $tables) { protected function prepare_db($dataset, $tables) {
global $DB; global $DB;
foreach ($tables as $tablename) { foreach ($tables as $tablename) {
$DB->delete_records($tablename); $DB->delete_records($tablename);
$dataset->to_database([$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);
}
}
}
} }
} }
@ -182,25 +169,19 @@ class core_statslib_testcase extends advanced_testcase {
* Load dataset from XML file. * Load dataset from XML file.
* *
* @param string $file The name of the file to load * @param string $file The name of the file to load
* @return array * @return phpunit_dataset
*/ */
protected function load_xml_data_file($file) { protected function load_xml_data_file($file) {
static $replacements = null;
$raw = $this->createXMLDataSet($file); $xml = file_get_contents($file);
$clean = new PHPUnit\DbUnit\DataSet\ReplacementDataSet($raw);
// Apply all the replacements straight in xml.
foreach ($this->replacements as $placeholder => $value) { 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); return $this->dataset_from_string($xml, 'xml');
$logs->addIncludeTables(array('log'));
$stats = new PHPUnit\DbUnit\DataSet\Filter($clean);
$stats->addIncludeTables(array('stats_daily', 'stats_user_daily'));
return array($logs, $stats);
} }
/** /**
@ -262,7 +243,7 @@ class core_statslib_testcase extends advanced_testcase {
foreach ($expected as $type => $table) { foreach ($expected as $type => $table) {
$records = $DB->get_records($type); $records = $DB->get_records($type);
$rows = $table->getRowCount(); $rows = count($table);
$message = 'Incorrect number of results returned for '. $type; $message = 'Incorrect number of results returned for '. $type;
@ -273,7 +254,7 @@ class core_statslib_testcase extends advanced_testcase {
$this->assertCount($rows, $records, $message); $this->assertCount($rows, $records, $message);
for ($i = 0; $i < $rows; $i++) { for ($i = 0; $i < $rows; $i++) {
$row = $table->getRow($i); $row = $table[$i];
$found = 0; $found = 0;
foreach ($records as $key => $record) { 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). // 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->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'); $records = $DB->get_records('log');
$this->assertEquals($day + 14410, stats_get_start_from('daily'), 'Log entry start'); $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; $CFG->statsfirstrun = 14515200;
$this->assertLessThanOrEqual(1, stats_get_start_from('daily') - (time() - (14515200)), 'Specified start time'); $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'); $this->assertEquals($day + DAYSECS, stats_get_start_from('daily'), 'Daily stats start time');
// New log stores. // New log stores.
@ -587,8 +568,7 @@ class core_statslib_testcase extends advanced_testcase {
global $CFG, $DB, $USER; global $CFG, $DB, $USER;
$dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test09.xml"); $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test09.xml");
$this->prepare_db($dataset, array('log'));
$this->prepare_db($dataset[0], array('log'));
// This nonsense needs to be rewritten. // This nonsense needs to be rewritten.
$date = new DateTime('now', core_date::get_server_timezone_object()); $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() { public function test_statslib_temp_table_setup() {
global $DB; global $DB;
$logs = array(); $DB->delete_records('log');
$this->prepare_db($logs, array('log'));
stats_temp_table_create(); stats_temp_table_create();
stats_temp_table_setup(); stats_temp_table_setup();
@ -749,9 +728,8 @@ class core_statslib_testcase extends advanced_testcase {
global $CFG, $DB; global $CFG, $DB;
$dataset = $this->load_xml_data_file(__DIR__."/fixtures/{$xmlfile}"); $dataset = $this->load_xml_data_file(__DIR__."/fixtures/{$xmlfile}");
$stats = $this->prepare_db($dataset, array('log'));
list($logs, $stats) = $dataset; $stats = $dataset->get_rows(['stats_daily', 'stats_user_daily']);
$this->prepare_db($logs, array('log'));
// Stats cron daily uses mtrace, turn on buffering to silence output. // Stats cron daily uses mtrace, turn on buffering to silence output.
ob_start(); ob_start();
@ -780,8 +758,8 @@ class core_statslib_testcase extends advanced_testcase {
$gr = get_guest_role(); $gr = get_guest_role();
$dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test10.xml"); $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test10.xml");
$this->prepare_db($dataset, array('log'));
$this->prepare_db($dataset[0], array('log')); $stats = $dataset->get_rows(['stats_user_daily']);
// Stats cron daily uses mtrace, turn on buffering to silence output. // Stats cron daily uses mtrace, turn on buffering to silence output.
ob_start(); ob_start();
@ -789,6 +767,6 @@ class core_statslib_testcase extends advanced_testcase {
$output = ob_get_contents(); $output = ob_get_contents();
ob_end_clean(); 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_records' => __DIR__.'/fixtures/test_data_records.csv',
'data_content' => __DIR__.'/fixtures/test_data_content.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. // 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_fields} SET dataid = ?', array($data->id));
$DB->execute('UPDATE {data_records} 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. * 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 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", * @param array $csvdata of data read from csv file "questionsXX.csv", "stepsXX.csv" and "responsesXX.csv".
* "stepsXX.csv" and "responsesXX.csv".
* @dataProvider get_data_for_walkthrough * @dataProvider get_data_for_walkthrough
*/ */
public function test_walkthrough_from_csv($quizsettings, $csvdata) { 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']); $quizattemptids = $this->walkthrough_attempts($csvdata['steps']);
for ($rowno = 0; $rowno < $csvdata['responses']->getRowCount(); $rowno++) { foreach ($csvdata['responses'] as $responsesfromcsv) {
$responsesfromcsv = $csvdata['responses']->getRow($rowno);
$responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv); $responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv);
if (!isset($quizattemptids[$responses['quizattempt']])) { 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. * 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", * @param array $csvdata data read from csv file "questionsXX.csv", "stepsXX.csv" and "resultsXX.csv".
* "stepsXX.csv" and "resultsXX.csv".
* @dataProvider get_data_for_walkthrough * @dataProvider get_data_for_walkthrough
*/ */
public function test_walkthrough_from_csv($quizsettings, $csvdata) { 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. * 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. * @param $questionstats \core_question\statistics\questions\all_calculated_for_qubaid_condition Calculated stats.
*/ */
protected function check_question_stats($qstats, $questionstats) { protected function check_question_stats($qstats, $questionstats) {
for ($rowno = 0; $rowno < $qstats->getRowCount(); $rowno++) { foreach ($qstats as $slotqstats) {
$slotqstats = $qstats->getRow($rowno);
foreach ($slotqstats as $statname => $slotqstat) { foreach ($slotqstats as $statname => $slotqstat) {
if (!in_array($statname, array('slot', 'subqname')) && $slotqstat !== '') { if (!in_array($statname, array('slot', 'subqname')) && $slotqstat !== '') {
$this->assert_stat_equals($slotqstat, $this->assert_stat_equals($slotqstat,
@ -230,8 +228,7 @@ class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkth
* @param $whichtries * @param $whichtries
*/ */
protected function check_response_counts($responsecounts, $qubaids, $questions, $whichtries) { protected function check_response_counts($responsecounts, $qubaids, $questions, $whichtries) {
for ($rowno = 0; $rowno < $responsecounts->getRowCount(); $rowno++) { foreach ($responsecounts as $expected) {
$expected = $responsecounts->getRow($rowno);
$defaultsforexpected = array('randq' => '', 'variant' => '1', 'subpart' => '1'); $defaultsforexpected = array('randq' => '', 'variant' => '1', 'subpart' => '1');
foreach ($defaultsforexpected as $key => $expecteddefault) { foreach ($defaultsforexpected as $key => $expecteddefault) {
if (!isset($expected[$key])) { 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/. * 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 $whichattempts
* @param string $whichtries * @param string $whichtries
* @param \core\dml\sql_join $groupstudentsjoins * @param \core\dml\sql_join $groupstudentsjoins

View file

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