Skip to content

Commit

Permalink
MDL-46193 phpunit: Use random starting value on initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Russell Smith committed Jul 4, 2014
1 parent 4de51c2 commit 529495f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/phpunit/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,13 @@ public static function build_config_file() {
$suites .= $suite;
}
}
// Start a sequence between 100000 and 199000 to ensure each call to init produces
// different ids in the database. This reduces the risk that hard coded values will
// end up being placed in phpunit or behat test code.
$sequencestart = 100000 + mt_rand(0, 99) * 1000;

$data = preg_replace('|<!--@plugin_suites_start@-->.*<!--@plugin_suites_end@-->|s', $suites, $data, 1);
$data = preg_replace('|<!--@PHPUNIT_SEQUENCE_START@-->|s', $sequencestart, $data, 1);

$result = false;
if (is_writable($CFG->dirroot)) {
Expand Down
6 changes: 5 additions & 1 deletion lib/testing/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ public static function reset_all_database_sequences(array $empties = null) {
// To reduce the chance of the coding error, we start sequences at different values where possible.
// In a attempt to avoid tables with existing id's we start at a high number.
// Reset the value each time all database sequences are reset.
self::$sequencenextstartingid = 100000;
if (defined('PHPUNIT_SEQUENCE_START')) {
self::$sequencenextstartingid = PHPUNIT_SEQUENCE_START;
} else {
self::$sequencenextstartingid = 100000;
}

$dbfamily = $DB->get_dbfamily();
if ($dbfamily === 'postgres') {
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<php>
<!--<const name="PHPUNIT_LONGTEST" value="1"/> uncomment to execute also slow or otherwise expensive tests-->
<const name="PHPUNIT_SEQUENCE_START" value="<!--@PHPUNIT_SEQUENCE_START@-->"/>

<!--Following constants instruct tests to fetch external test files from alternative location or skip tests if empty, clone https://github.com/moodlehq/moodle-exttests to local web server-->
<!--<const name="TEST_EXTERNAL_FILES_HTTP_URL" value="http://download.moodle.org/unittest"/> uncomment and alter to fetch external test files from alternative location-->
Expand Down

0 comments on commit 529495f

Please sign in to comment.