[MediaWiki-commits] [Gerrit] tests: Clean up use of mt_rand() - change (mediawiki/core)

2015-12-06 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: tests: Clean up use of mt_rand()
..


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
---
M includes/GlobalFunctions.php
M tests/phpunit/includes/api/query/ApiQueryTest.php
M tests/phpunit/includes/filebackend/FileBackendTest.php
M tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
M tests/phpunit/suites/UploadFromUrlTestSuite.php
5 files changed, 16 insertions(+), 20 deletions(-)

Approvals:
  Aaron Schulz: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index 43b936b..4a3fceb 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -374,20 +374,22 @@
  * 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 61b992b..eb1ad26 100644
--- a/tests/phpunit/includes/api/query/ApiQueryTest.php
+++ b/tests/phpunit/includes/api/query/ApiQueryTest.php
@@ -61,7 +61,7 @@
public function testTitlesAreRejectedIfInvalid() {
$title = false;
while ( !$title || Title::newFromText( $title )->exists() ) {
-   $title = md5( mt_rand( 0, 1 ) + rand( 0, 999000 ) );
+   $title = md5( mt_rand( 0, 10 ) );
}
 
$data = $this->doApiRequest( array(
diff --git a/tests/phpunit/includes/filebackend/FileBackendTest.php 
b/tests/phpunit/includes/filebackend/FileBackendTest.php
index 9bef8e0..e115d17 100644
--- a/tests/phpunit/includes/filebackend/FileBackendTest.php
+++ b/tests/phpunit/includes/filebackend/FileBackendTest.php
@@ -18,7 +18,6 @@
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 @@
'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 c839bc4..ee0cf2d 100644
--- a/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
+++ b/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
@@ -11,7 +11,7 @@
 
$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 e867250..80893da 100644
--- a/tests/phpunit/suites/UploadFromUrlTestSuite.php
+++ b/tests/phpunit/suites/UploadFromUrlTestSuite.php
@@ -21,6 +21,7 @@
$wgParserCacheType, $wgNamespaceAliases, 
$wgNamespaceProtection,
$parserMemc;
 
+   $tmpDir = $this->getNewTempDirectory();
$tmpGlobals 

[MediaWiki-commits] [Gerrit] tests: Clean up use of mt_rand() - change (mediawiki/core)

2015-12-03 Thread Krinkle (Code Review)
Krinkle has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/256674

Change subject: tests: Clean up use of mt_rand()
..

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
---
M includes/GlobalFunctions.php
M tests/phpunit/includes/api/query/ApiQueryTest.php
M tests/phpunit/includes/filebackend/FileBackendTest.php
M tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
M tests/phpunit/suites/UploadFromUrlTestSuite.php
5 files changed, 16 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/74/256674/1

diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index 43b936b..4a3fceb 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -374,20 +374,22 @@
  * 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 61b992b..eb1ad26 100644
--- a/tests/phpunit/includes/api/query/ApiQueryTest.php
+++ b/tests/phpunit/includes/api/query/ApiQueryTest.php
@@ -61,7 +61,7 @@
public function testTitlesAreRejectedIfInvalid() {
$title = false;
while ( !$title || Title::newFromText( $title )->exists() ) {
-   $title = md5( mt_rand( 0, 1 ) + rand( 0, 999000 ) );
+   $title = md5( mt_rand( 0, 10 ) );
}
 
$data = $this->doApiRequest( array(
diff --git a/tests/phpunit/includes/filebackend/FileBackendTest.php 
b/tests/phpunit/includes/filebackend/FileBackendTest.php
index 9bef8e0..e115d17 100644
--- a/tests/phpunit/includes/filebackend/FileBackendTest.php
+++ b/tests/phpunit/includes/filebackend/FileBackendTest.php
@@ -18,7 +18,6 @@
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 @@
'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 c839bc4..ee0cf2d 100644
--- a/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
+++ b/tests/phpunit/includes/filerepo/MigrateFileRepoLayoutTest.php
@@ -11,7 +11,7 @@
 
$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 e867250..80893da 100644
--- a/tests/phpunit/suites/UploadFromUrlTestSuite.php
+++ b/tests/phpunit/suites/UploadFromUrlTestSuite.php
@@ -21,6 +21,7 @@
$wgParserCacheType, $wgNamespaceAliases, 
$wgNamespaceProtection,
$parserMemc;
 
+   $tmpDir =