diff --git a/tests/grade_me_test.php b/tests/grade_me_test.php index e3b6a86..c6843c1 100644 --- a/tests/grade_me_test.php +++ b/tests/grade_me_test.php @@ -52,8 +52,9 @@ class block_grade_me_testcase extends advanced_testcase { */ protected function create_grade_me_data($file) { // Read the datafile and get the table names. - $dataset = $this->createXMLDataSet(__DIR__ . '/fixtures/' . $file); - $names = array_flip($dataset->getTableNames()); + $dataset = $this->dataset_from_files([__DIR__ . '/fixtures/' . $file]); + $datasetrows = $dataset->get_rows(); + $names = array_keys($datasetrows); // Generate Data. $generator = $this->getDataGenerator(); @@ -64,12 +65,11 @@ protected function create_grade_me_data($file) { $gradeables = array('assign', 'assignment', 'forum', 'glossary', 'quiz'); foreach ($gradeables as $gradeable) { - if (array_key_exists($gradeable, $names)) { + if (in_array($gradeable, $names)) { $pgen = $generator->get_plugin_generator("mod_{$gradeable}"); - $table = $dataset->getTable($gradeable); - $rows = $table->getRowCount(); - for ($row = 0; $row < $rows; $row += 1) { - $fields = $table->getRow($row); + $gradeablerows = $datasetrows[$gradeable]; + for ($row = 0; $row < count($gradeablerows); $row += 1) { + $fields = $gradeablerows[$row]; unset($fields['id']); $fields['course'] = $courses[$fields['course']]->id; $instance = $pgen->create_instance($fields); @@ -151,7 +151,7 @@ protected function create_grade_me_data($file) { foreach ($overrides as $field => $override) { foreach ($override['tables'] as $tablename) { // Skip tables that aren't in the dataset. - if (array_key_exists($tablename, $names)) { + if (in_array($tablename, $names)) { if (!array_key_exists($tablename, $tables)) { $tables[$tablename] = array($field => array()); } @@ -162,26 +162,31 @@ protected function create_grade_me_data($file) { // Perform the overrides. foreach ($tables as $tablename => $translations) { - $table = $dataset->getTable($tablename); - $rows = $table->getRowCount(); foreach ($translations as $column => $values) { foreach ($values as $value) { $list = $value['list']; $field = $value['field']; - for ($row = 0; $row < $rows; $row += 1) { - $index = $table->getValue($row, $column); + $tablerows = $datasetrows[$tablename]; + for ($row = 0; $row < count($tablerows); $row += 1) { + $index = $tablerows[$row][$column]; if (isset(${$list}[$index])) { - $table->setValue($row, $column, ${$list}[$index]->$field); + $datasetrows[$tablename][$row][$column] = ${$list}[$index]->$field; } } } } } - // Load the data. - $filtered = new \PHPUnit\DbUnit\DataSet\Filter($dataset); - $filtered->addExcludeTables($excludes); - $this->loadDataSet($filtered); + // Remove any empty tables (otherwise dataset_from_array breaks). + foreach (array_keys($datasetrows) as $tablename) { + if (empty($datasetrows[$tablename])) { + unset($datasetrows[$tablename]); + } + } + + // Load back in the modified dataset and send to the db. + $finaldataset = $this->dataset_from_array($datasetrows); + $finaldataset->to_database(); // Return the generated users and courses because the tests often need them for result calculations. return array($users, $courses, $plugins); @@ -756,7 +761,7 @@ public function provider_get_content_single_user() { $matches = array( 1 => '/Go to assign/', 2 => '|mod/assign/view.php|', - 3 => '/action=grade&userid=[user0]/', + 3 => '/action=grader&userid=[user0]/', 5 => '/testassignment3/', 6 => '/testassignment4/', ); @@ -829,7 +834,7 @@ public function test_get_content_single_user($plugin, $expectedvalues) { foreach ($expectedvalues as $expected) { $match = str_replace('[user0]', $users[0]->id, $expected); - $this->assertRegExp($match, $content->text); + $this->assertMatchesRegularExpression($match, $content->text); } } @@ -854,8 +859,8 @@ public function provider_get_content_multiple_user() { $matches = array( 1 => '/Go to assign/', 2 => '|mod/assign/view.php|', - 3 => '/action=grade&userid=[user0]/', - 4 => '/action=grade&userid=[user1]/', + 3 => '/action=grader&userid=[user0]/', + 4 => '/action=grader&userid=[user1]/', 5 => '/testassignment3/', 6 => '/testassignment4/' ); @@ -957,7 +962,7 @@ public function test_get_content_multiple_user($plugin, $expectedvalues) { foreach ($expectedvalues as $expected) { $match = str_replace('[user0]', $users[0]->id, $expected); $match = str_replace('[user1]', $users[1]->id, $match); - $this->assertRegExp($match, $content->text); + $this->assertMatchesRegularExpression($match, $content->text); } } @@ -982,7 +987,7 @@ public function test_tree_uses_correct_forum_discussion_id() { $this->assertFalse(empty($gradeables), 'Expected results not found.'); $actual = block_grade_me_tree($gradeables); - $this->assertRegExp('/mod\/forum\/discuss.php\?d=100\#p1/', $actual); + $this->assertMatchesRegularExpression('/mod\/forum\/discuss.php\?d=100\#p1/', $actual); } /**