Exemplo n.º 1
0
 /**
  * Runs the bare test sequence and log any changes in global state or database.
  * @return void
  */
 public final function runBare()
 {
     global $DB;
     try {
         parent::runBare();
     } catch (Exception $e) {
         // cleanup after failed expectation
         phpunit_util::reset_all_data();
         throw $e;
     }
     if ($DB->is_transaction_started()) {
         phpunit_util::reset_all_data();
         throw new coding_exception('basic_testcase ' . $this->getName() . ' is not supposed to use database transactions!');
     }
     phpunit_util::reset_all_data(true);
 }
Exemplo n.º 2
0
 /**
  * Runs the bare test sequence and log any changes in global state or database.
  * @return void
  */
 public final function runBare()
 {
     global $DB;
     try {
         parent::runBare();
     } catch (Exception $ex) {
         $e = $ex;
     } catch (Throwable $ex) {
         // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
         $e = $ex;
     }
     if (isset($e)) {
         // cleanup after failed expectation
         phpunit_util::reset_all_data();
         throw $e;
     }
     if ($DB->is_transaction_started()) {
         phpunit_util::reset_all_data();
         throw new coding_exception('basic_testcase ' . $this->getName() . ' is not supposed to use database transactions!');
     }
     phpunit_util::reset_all_data(true);
 }
Exemplo n.º 3
0
 /**
  * Called during bootstrap only!
  * @internal
  * @static
  * @return void
  */
 public static function bootstrap_init()
 {
     global $CFG, $SITE, $DB;
     // backup the globals
     self::$globals['_SERVER'] = $_SERVER;
     self::$globals['CFG'] = clone $CFG;
     self::$globals['SITE'] = clone $SITE;
     self::$globals['DB'] = $DB;
     // refresh data in all tables, clear caches, etc.
     phpunit_util::reset_all_data();
 }
 public static function tearDownAfterClass()
 {
     if (self::$extradb) {
         self::$extradb->dispose();
         self::$extradb = null;
     }
     phpunit_util::reset_all_data(null);
     parent::tearDownAfterClass();
 }
Exemplo n.º 5
0
 /**
  * Reset all database tables, restore global state and clear caches and optionally purge dataroot dir.
  * @static
  * @return void
  */
 public static function resetAllData() {
     phpunit_util::reset_all_data();
 }
Exemplo n.º 6
0
 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);
     // _GET change.
     $_GET['__somethingthatwillnotnormallybepresent__'] = 'yy';
     phpunit_util::reset_all_data(true);
     $this->assertEquals(array(), $_GET);
     // _POST change.
     $_POST['__somethingthatwillnotnormallybepresent2__'] = 'yy';
     phpunit_util::reset_all_data(true);
     $this->assertEquals(array(), $_POST);
     // _FILES change.
     $_FILES['__somethingthatwillnotnormallybepresent3__'] = 'yy';
     phpunit_util::reset_all_data(true);
     $this->assertEquals(array(), $_FILES);
     // _REQUEST change.
     $_REQUEST['__somethingthatwillnotnormallybepresent4__'] = 'yy';
     phpunit_util::reset_all_data(true);
     $this->assertEquals(array(), $_REQUEST);
     // Silent changes.
     $_SERVER['xx'] = 'yy';
     phpunit_util::reset_all_data(true);
     $this->assertFalse(isset($_SERVER['xx']));
     // COURSE change.
     $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);
     }
 }
Exemplo n.º 7
0
 /**
  * Reset all database tables, restore global state and clear caches and optionally purge dataroot dir.
  *
  * @param bool $detectchanges
  *      true  - changes in global state and database are reported as errors
  *      false - no errors reported
  *      null  - only critical problems are reported as errors
  * @return void
  */
 public static function resetAllData($detectchanges = false) {
     phpunit_util::reset_all_data($detectchanges);
 }
Exemplo n.º 8
0
 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);
     }
 }
Exemplo n.º 9
0
$CFG->jsrev = 1;
// load test case stub classes and other stuff
require_once "{$CFG->dirroot}/lib/phpunit/lib.php";
// finish moodle init
define('ABORT_AFTER_CONFIG_CANCEL', true);
require "{$CFG->dirroot}/lib/setup.php";
raise_memory_limit(MEMORY_EXTRA);
if (defined('PHPUNIT_CLI_UTIL')) {
    // all other tests are done in the CLI scripts...
    return;
}
if (!phpunit_util::is_testing_ready()) {
    phpunit_bootstrap_error('Database is not initialised to run unit tests, please use "php admin/tool/phpunit/cli/util.php --install"', 132);
}
// refresh data in all tables, clear caches, etc.
phpunit_util::reset_all_data();
// store fresh globals
phpunit_util::init_globals();
//=========================================================
/**
 * Print error and stop execution
 * @param string $text An error message to display
 * @param int $errorcode The error code (see docblock for detailed list)
 * @return void stops code execution with error code
 */
function phpunit_bootstrap_error($text, $errorcode = 1)
{
    fwrite(STDERR, $text . "\n");
    exit($errorcode);
}
/**
Exemplo n.º 10
0
 public function test_locale_reset()
 {
     global $CFG;
     $this->resetAfterTest();
     // If this fails \phpunit_util::reset_all_data() must be updated.
     $this->assertSame('en_AU.UTF-8', get_string('locale', 'langconfig'));
     $this->assertSame('English_Australia.1252', get_string('localewin', 'langconfig'));
     if ($CFG->ostype === 'WINDOWS') {
         $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
         setlocale(LC_TIME, 'English_USA.1252');
     } else {
         $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
         setlocale(LC_TIME, 'en_US.UTF-8');
     }
     try {
         phpunit_util::reset_all_data(true);
     } catch (Exception $e) {
         $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
     }
     if ($CFG->ostype === 'WINDOWS') {
         $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
     } else {
         $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
     }
     if ($CFG->ostype === 'WINDOWS') {
         $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
         setlocale(LC_TIME, 'English_USA.1252');
     } else {
         $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
         setlocale(LC_TIME, 'en_US.UTF-8');
     }
     phpunit_util::reset_all_data(false);
     if ($CFG->ostype === 'WINDOWS') {
         $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
     } else {
         $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
     }
 }
Exemplo n.º 11
0
 /**
  * Runs the bare test sequence.
  * @return void
  */
 public function runBare()
 {
     global $CFG, $USER, $DB;
     $dbwrites = $DB->perf_get_writes();
     parent::runBare();
     $oldcfg = phpunit_util::get_global_backup('CFG');
     foreach ($CFG as $k => $v) {
         if (!property_exists($oldcfg, $k)) {
             unset($CFG->{$k});
             error_log('warning: unexpected new $CFG->' . $k . ' value in testcase: ' . get_class($this) . '->' . $this->getName(true));
         } else {
             if ($oldcfg->{$k} !== $CFG->{$k}) {
                 $CFG->{$k} = $oldcfg->{$k};
                 error_log('warning: unexpected change of $CFG->' . $k . ' value in testcase: ' . get_class($this) . '->' . $this->getName(true));
             }
         }
         unset($oldcfg->{$k});
     }
     if ($oldcfg) {
         foreach ($oldcfg as $k => $v) {
             $CFG->{$k} = $v;
             error_log('warning: unexpected removal of $CFG->' . $k . ' in testcase: ' . get_class($this) . '->' . $this->getName(true));
         }
     }
     if ($USER->id != 0) {
         error_log('warning: unexpected change of $USER in testcase: ' . get_class($this) . '->' . $this->getName(true));
         $USER = new stdClass();
         $USER->id = 0;
     }
     if ($dbwrites != $DB->perf_get_writes()) {
         //TODO: find out what was changed exactly
         error_log('warning: unexpected database modification, resetting DB state in testcase: ' . get_class($this) . '->' . $this->getName(true));
         phpunit_util::reset_all_data();
     }
     //TODO: somehow find out if there are changes in dataroot
 }