tests: Clean up use of mt_rand()
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 3 Dec 2015 13:32:04 +0000 (13:32 +0000)
committerKrinkle <krinklemail@gmail.com>
Thu, 3 Dec 2015 13:45:55 +0000 (13:45 +0000)
* 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
tests/phpunit/includes/api/query/ApiQueryTest.php
tests/phpunit/includes/filebackend/FileBackendTest.php
tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
tests/phpunit/suites/UploadFromUrlTestSuite.php

index 43b936b..4a3fceb 100644 (file)
@@ -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.
  *
index 61b992b..eb1ad26 100644 (file)
@@ -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(
index 9bef8e0..e115d17 100644 (file)
@@ -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',
index c839bc4..ee0cf2d 100644 (file)
@@ -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',
index e867250..80893da 100644 (file)
@@ -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" );