From 0161a2016747718dc3620a4d0a4a04c849dabdd5 Mon Sep 17 00:00:00 2001 From: Eugene Venter Date: Mon, 20 Oct 2014 10:50:58 +1300 Subject: [PATCH] MDL-47502 test_statslib_cron_daily: fix provider Fiddling with the database in the data provider causes "out of sync" errors when running suite tests. --- lib/tests/statslib_test.php | 78 +++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 43 deletions(-) diff --git a/lib/tests/statslib_test.php b/lib/tests/statslib_test.php index d88824e8fe0..ba0117e92ad 100644 --- a/lib/tests/statslib_test.php +++ b/lib/tests/statslib_test.php @@ -50,43 +50,13 @@ class core_statslib_testcase extends advanced_testcase { /** @var array The replacements to be used when loading XML files **/ protected $replacements = null; - /** - * Set up the database for tests. - * - * This function is needed so that daily_log_provider has the before-test set up from setUp() - */ - protected function set_up_db() { - global $DB; - - if ($DB->record_exists('user', array('username' => 'user1'))) { - return; - } - - $datagen = self::getDataGenerator(); - - $user1 = $datagen->create_user(array('username'=>'user1')); - $user2 = $datagen->create_user(array('username'=>'user2')); - - $course1 = $datagen->create_course(array('shortname'=>'course1')); - - $success = enrol_try_internal_enrol($course1->id, $user1->id, 5); - - if (!$success) { - trigger_error('User enrollment failed', E_USER_ERROR); - } - - $context = context_system::instance(); - role_assign(self::STID, $user2->id, $context->id); - - $this->generate_replacement_list(); - } /** * Setup function * - Allow changes to CFG->debug for testing purposes. */ protected function setUp() { - global $CFG; + global $CFG, $DB; parent::setUp(); // Settings to force statistic to run during testing. @@ -107,8 +77,30 @@ class core_statslib_testcase extends advanced_testcase { $CFG->statsruntimestarthour = $shour; $CFG->statsruntimestartminute = 0; - $this->set_up_db(); + if ($DB->record_exists('user', array('username' => 'user1'))) { + return; + } + // Set up the database. + $datagen = self::getDataGenerator(); + + $user1 = $datagen->create_user(array('username'=>'user1')); + $user2 = $datagen->create_user(array('username'=>'user2')); + + $course1 = $datagen->create_course(array('shortname'=>'course1')); + + $success = enrol_try_internal_enrol($course1->id, $user1->id, 5); + + if (!$success) { + trigger_error('User enrollment failed', E_USER_ERROR); + } + + $context = context_system::instance(); + role_assign(self::STID, $user2->id, $context->id); + + $this->generate_replacement_list(); + + // Reset between tests. $this->resetAfterTest(); } @@ -226,21 +218,18 @@ class core_statslib_testcase extends advanced_testcase { /** * Provides the log data for test_statslib_cron_daily. + * + * @return array of fixture XML log file names. */ public function daily_log_provider() { - global $CFG, $DB; + $logfiles = array(); + $fileno = array('00', '01', '02', '03', '04', '05', '06', '07', '08'); - $this->set_up_db(); - - $tests = array('00', '01', '02', '03', '04', '05', '06', '07', '08'); - - $dataset = array(); - - foreach ($tests as $test) { - $dataset[] = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test{$test}.xml"); + foreach ($fileno as $no) { + $logfiles[] = array("statslib-test{$no}.xml"); } - return $dataset; + return $logfiles; } /** @@ -691,9 +680,12 @@ class core_statslib_testcase extends advanced_testcase { * @depends test_statslib_temp_table_fill * @dataProvider daily_log_provider */ - public function test_statslib_cron_daily($logs, $stats) { + public function test_statslib_cron_daily($xmlfile) { global $CFG, $DB; + $dataset = $this->load_xml_data_file(__DIR__."/fixtures/{$xmlfile}"); + + list($logs, $stats) = $dataset; $this->prepare_db($logs, array('log')); // Stats cron daily uses mtrace, turn on buffering to silence output.