mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-32323 use new loadDataSet in mod_data and fix typos in last commit
This commit is contained in:
parent
e396e8ed5f
commit
c691274bf9
@ -1144,7 +1144,7 @@ abstract class advanced_testcase extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new CvsDataSet from the given array of csv files . (absolute path.)
|
||||
* Creates a new CsvDataSet from the given array of csv files. (absolute paths.)
|
||||
*
|
||||
* @param array $files array tablename=>cvsfile
|
||||
* @param string $delimiter
|
||||
@ -1152,7 +1152,7 @@ abstract class advanced_testcase extends PHPUnit_Framework_TestCase {
|
||||
* @param string $escape
|
||||
* @return PHPUnit_Extensions_Database_DataSet_CsvDataSet
|
||||
*/
|
||||
protected function createCvsDataSet($files, $delimiter = ',', $enclosure = '"', $escape = '"') {
|
||||
protected function createCsvDataSet($files, $delimiter = ',', $enclosure = '"', $escape = '"') {
|
||||
$dataSet = new PHPUnit_Extensions_Database_DataSet_CsvDataSet($delimiter, $enclosure, $escape);
|
||||
foreach($files as $table=>$file) {
|
||||
$dataSet->addTable($table, $file);
|
||||
|
@ -362,7 +362,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
|
||||
$this->assertEquals('john.doe', $user5->username);
|
||||
$this->assertEquals('jane.doe', $user7->username);
|
||||
|
||||
$dataset = $this->createCvsDataSet(array('user'=>__DIR__.'/fixtures/sample_dataset.csv'));
|
||||
$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')));
|
||||
|
@ -90,12 +90,6 @@ class data_advanced_search_sql_test extends advanced_testcase {
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Set up data for the test database.
|
||||
$tablenames = array(
|
||||
'data_fields',
|
||||
'data_records',
|
||||
'data_content',
|
||||
);
|
||||
|
||||
// we already have 2 users, we need 98 more - let's ignore the fact that guest can not post anywhere
|
||||
for($i=3;$i<=100;$i++) {
|
||||
@ -107,16 +101,15 @@ class data_advanced_search_sql_test extends advanced_testcase {
|
||||
$data = $this->getDataGenerator()->create_module('data', array('course'=>$course->id));
|
||||
$this->recorddata = $data;
|
||||
|
||||
foreach ($tablenames as $tablename) {
|
||||
$filename = __DIR__. '/fixtures/test_' . $tablename . '.csv';
|
||||
if (file_exists($filename)) {
|
||||
$file = file_get_contents($filename);
|
||||
}
|
||||
$this->insert_data_from_csv($file, $tablename);
|
||||
}
|
||||
// Set up data for the test database.
|
||||
$files = array(
|
||||
'data_fields' => __DIR__.'/fixtures/test_data_fields.csv',
|
||||
'data_records' => __DIR__.'/fixtures/test_data_records.csv',
|
||||
'data_content' => __DIR__.'/fixtures/test_data_content.csv',
|
||||
);
|
||||
$this->loadDataSet($this->createCsvDataSet($files));
|
||||
|
||||
// Create the search array which contains our advanced search criteria.
|
||||
|
||||
$fieldinfo = array('0' => new stdClass(),
|
||||
'1' => new stdClass(),
|
||||
'2' => new stdClass(),
|
||||
@ -201,29 +194,4 @@ class data_advanced_search_sql_test extends advanced_testcase {
|
||||
$records = $DB->get_records_sql($html['sql'], $allparams);
|
||||
$this->assertEquals($records, $this->finalrecord);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts data from a csv file into the data module table specified.
|
||||
*
|
||||
* @param string $file comma seperated value file
|
||||
* @param string $tablename name of the table for the data to be inserted into.
|
||||
*/
|
||||
function insert_data_from_csv($file, $tablename) {
|
||||
global $DB;
|
||||
$iid = csv_import_reader::get_new_iid('moddata');
|
||||
$csvdata = new csv_import_reader($iid, 'moddata');
|
||||
$fielddata = $csvdata->load_csv_content($file, 'utf-8', 'comma');
|
||||
$columns = $csvdata->get_columns();
|
||||
$columncount = count($columns);
|
||||
$csvdata->init();
|
||||
$fieldinfo = array();
|
||||
for ($j = 0; $j < $fielddata; $j++) {
|
||||
$thing = $csvdata->next();
|
||||
$fieldinfo[$j] = new stdClass();
|
||||
for ($i = 0; $i < $columncount; $i++) {
|
||||
$fieldinfo[$j]->$columns[$i] = $thing[$i];
|
||||
}
|
||||
$DB->insert_record($tablename, $fieldinfo[$j], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user