mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 10:26:40 +02:00
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:
parent
be396eddf0
commit
1f13dff1a7
7 changed files with 139 additions and 126 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue