diff --git a/lib/ddl/tests/ddl_test.php b/lib/ddl/tests/ddl_test.php index 9f7385a6a4a..9c7cf43ff5f 100644 --- a/lib/ddl/tests/ddl_test.php +++ b/lib/ddl/tests/ddl_test.php @@ -31,9 +31,6 @@ class ddl_testcase extends database_driver_testcase { private $records= array(); protected function setUp() { - //global $CFG; - //require_once($CFG->libdir . '/adminlib.php'); - parent::setUp(); $table = new xmldb_table('test_table0'); @@ -131,6 +128,8 @@ class ddl_testcase extends database_driver_testcase { * Fill the given test table with some records, as far as * DDL behaviour must be tested both with real data and * with empty tables + * @param string $tablename + * @return int count of records */ private function fill_deftable($tablename) { $DB = $this->tdb; // do not use global $DB! diff --git a/lib/phpunit/lib.php b/lib/phpunit/lib.php index 640e868eba1..58e80f01e35 100644 --- a/lib/phpunit/lib.php +++ b/lib/phpunit/lib.php @@ -945,10 +945,10 @@ class basic_testcase extends PHPUnit_Framework_TestCase { */ class advanced_testcase extends PHPUnit_Framework_TestCase { /** @var bool automatically reset everything? null means log changes */ - protected $resetAfterTest; + private $resetAfterTest; /** @var moodle_transaction */ - protected $testdbtransaction; + private $testdbtransaction; /** * Constructs a test case with the given name. @@ -1133,7 +1133,7 @@ class advanced_testcase extends PHPUnit_Framework_TestCase { */ class database_driver_testcase extends PHPUnit_Framework_TestCase { /** @var moodle_database connection to extra database */ - protected static $extradb = null; + private static $extradb = null; /** @var moodle_database used in these tests*/ protected $tdb; diff --git a/lib/tests/phpunit_test.php b/lib/tests/phpunit_test.php index e651bbccc11..2910cbad412 100644 --- a/lib/tests/phpunit_test.php +++ b/lib/tests/phpunit_test.php @@ -192,7 +192,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase { $this->assertSame($_SESSION['USER'], $USER); } - public function test_database_reset_repeated() { + public function test_database_reset() { global $DB; $this->resetAfterTest(true); @@ -255,6 +255,66 @@ class core_phpunit_advanced_testcase extends advanced_testcase { $this->assertEquals(2, $DB->count_records('user')); } + public function test_change_detection() { + global $DB, $CFG, $COURSE, $SITE, $USER; + + $this->preventResetByRollback(); + phpunit_util::reset_all_data(true); + + // database change + $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2))); + $DB->set_field('user', 'confirmed', 0, array('id'=>2)); + try { + phpunit_util::reset_all_data(true); + } catch (Exception $e) { + $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e); + } + $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2))); + + // config change + $CFG->xx = 'yy'; + unset($CFG->admin); + $CFG->rolesactive = 0; + try { + phpunit_util::reset_all_data(true); + } catch (Exception $e) { + $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e); + $this->assertContains('xx', $e->getMessage()); + $this->assertContains('admin', $e->getMessage()); + $this->assertContains('rolesactive', $e->getMessage()); + } + $this->assertFalse(isset($CFG->xx)); + $this->assertTrue(isset($CFG->admin)); + $this->assertEquals(1, $CFG->rolesactive); + + //silent changes + $_SERVER['xx'] = 'yy'; + phpunit_util::reset_all_data(true); + $this->assertFalse(isset($_SERVER['xx'])); + + // COURSE + $SITE->id = 10; + $COURSE = new stdClass(); + $COURSE->id = 7; + try { + phpunit_util::reset_all_data(true); + } catch (Exception $e) { + $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e); + $this->assertEquals(1, $SITE->id); + $this->assertSame($SITE, $COURSE); + $this->assertSame($SITE, $COURSE); + } + + // USER change + $this->setUser(2); + try { + phpunit_util::reset_all_data(true); + } catch (Exception $e) { + $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e); + $this->assertEquals(0, $USER->id); + } + } + public function test_getDataGenerator() { $generator = $this->getDataGenerator(); $this->assertInstanceOf('phpunit_data_generator', $generator);