From fbab4fecef8dcd4010d5c81736cccb565bf218da Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Thu, 3 Dec 2015 13:32:04 +0000 Subject: [PATCH] tests: Clean up use of mt_rand() * ApiQueryTest: One random is enough. * FileBackendTest: More consistent and idiomatic via wfRandomString() * MigrateFileRepoLayoutTest: Use getNewTempDirectory(). Similar to what FileBackendTest used already. * UploadFromUrlTestSuite: Use getNewTempDirectory(). Change-Id: I772de2134be41506d8ed08367be8c18f354bfc72 --- includes/GlobalFunctions.php | 12 +++++++----- .../phpunit/includes/api/query/ApiQueryTest.php | 2 +- .../includes/filebackend/FileBackendTest.php | 3 +-- .../filerepo/MigrateFileRepoLayoutTest.php | 2 +- tests/phpunit/suites/UploadFromUrlTestSuite.php | 17 ++++++----------- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 43b936b54b..4a3fceb2c5 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -374,20 +374,22 @@ function wfObjectToArray( $objOrArray, $recursive = true ) { * not likely to give duplicate values for any realistic * number of articles. * + * @note This is designed for use in relation to Special:RandomPage + * and the page_random database field. + * * @return string */ function wfRandom() { - # The maximum random value is "only" 2^31-1, so get two random - # values to reduce the chance of dupes + // The maximum random value is "only" 2^31-1, so get two random + // values to reduce the chance of dupes $max = mt_getrandmax() + 1; $rand = number_format( ( mt_rand() * $max + mt_rand() ) / $max / $max, 12, '.', '' ); - return $rand; } /** - * Get a random string containing a number of pseudo-random hex - * characters. + * Get a random string containing a number of pseudo-random hex characters. + * * @note This is not secure, if you are trying to generate some sort * of token please use MWCryptRand instead. * diff --git a/tests/phpunit/includes/api/query/ApiQueryTest.php b/tests/phpunit/includes/api/query/ApiQueryTest.php index 61b992bae3..eb1ad26802 100644 --- a/tests/phpunit/includes/api/query/ApiQueryTest.php +++ b/tests/phpunit/includes/api/query/ApiQueryTest.php @@ -61,7 +61,7 @@ class ApiQueryTest extends ApiTestCase { public function testTitlesAreRejectedIfInvalid() { $title = false; while ( !$title || Title::newFromText( $title )->exists() ) { - $title = md5( mt_rand( 0, 10000 ) + rand( 0, 999000 ) ); + $title = md5( mt_rand( 0, 100000 ) ); } $data = $this->doApiRequest( array( diff --git a/tests/phpunit/includes/filebackend/FileBackendTest.php b/tests/phpunit/includes/filebackend/FileBackendTest.php index 9bef8e0577..e115d17c15 100644 --- a/tests/phpunit/includes/filebackend/FileBackendTest.php +++ b/tests/phpunit/includes/filebackend/FileBackendTest.php @@ -18,7 +18,6 @@ class FileBackendTest extends MediaWikiTestCase { protected function setUp() { global $wgFileBackends; parent::setUp(); - $uniqueId = time() . '-' . mt_rand(); $tmpDir = $this->getNewTempDirectory(); if ( $this->getCliArg( 'use-filebackend' ) ) { if ( self::$backendToUse ) { @@ -58,7 +57,7 @@ class FileBackendTest extends MediaWikiTestCase { 'name' => 'localtesting', 'lockManager' => LockManagerGroup::singleton()->get( 'fsLockManager' ), 'parallelize' => 'implicit', - 'wikiId' => wfWikiId() . $uniqueId, + 'wikiId' => wfWikiId() . wfRandomString(), 'backends' => array( array( 'name' => 'localmultitesting1', diff --git a/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php b/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php index c839bc42cf..ee0cf2d559 100644 --- a/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php +++ b/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php @@ -11,7 +11,7 @@ class MigrateFileRepoLayoutTest extends MediaWikiTestCase { $filename = 'Foo.png'; - $this->tmpPrefix = wfTempDir() . '/migratefilelayout-test-' . time() . '-' . mt_rand(); + $this->tmpPrefix = $this->getNewTempDirectory(); $backend = new FSFileBackend( array( 'name' => 'local-migratefilerepolayouttest', diff --git a/tests/phpunit/suites/UploadFromUrlTestSuite.php b/tests/phpunit/suites/UploadFromUrlTestSuite.php index e8672501d2..80893dab41 100644 --- a/tests/phpunit/suites/UploadFromUrlTestSuite.php +++ b/tests/phpunit/suites/UploadFromUrlTestSuite.php @@ -21,6 +21,7 @@ class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite { $wgParserCacheType, $wgNamespaceAliases, $wgNamespaceProtection, $parserMemc; + $tmpDir = $this->getNewTempDirectory(); $tmpGlobals = array(); $tmpGlobals['wgScript'] = '/index.php'; @@ -38,10 +39,10 @@ class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite { 'name' => 'local-backend', 'wikiId' => wfWikiId(), 'containerPaths' => array( - 'local-public' => wfTempDir() . '/test-repo/public', - 'local-thumb' => wfTempDir() . '/test-repo/thumb', - 'local-temp' => wfTempDir() . '/test-repo/temp', - 'local-deleted' => wfTempDir() . '/test-repo/delete', + 'local-public' => "{$tmpDir}/test-repo/public", + 'local-thumb' => "{$tmpDir}/test-repo/thumb", + 'local-temp' => "{$tmpDir}/test-repo/temp", + 'local-deleted' => "{$tmpDir}/test-repo/delete", ) ) ), ); @@ -176,17 +177,11 @@ class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite { return $dir; } } else { - $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images"; + $dir = $this->getNewTempDirectory(); } wfDebug( "Creating upload directory $dir\n" ); - if ( file_exists( $dir ) ) { - wfDebug( "Already exists!\n" ); - - return $dir; - } - wfMkdirParents( $dir . '/3/3a', null, __METHOD__ ); copy( "$IP/tests/phpunit/data/upload/headbg.jpg", "$dir/3/3a/Foobar.jpg" ); -- 2.20.1