aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTimo Tijhof <krinklemail@gmail.com>2019-10-11 19:46:28 +0100
committerTimo Tijhof <krinklemail@gmail.com>2019-10-25 02:07:21 +0100
commit63ddc8c436e372307a08bf613748fafffed6289d (patch)
tree576fad4679db57159661c8d4b65b1ee097a05ca4
parente8a58a3356426d3fd173993337b18e968d5c3e86 (diff)
downloadmediawikicore-63ddc8c436e372307a08bf613748fafffed6289d.tar.gz
mediawikicore-63ddc8c436e372307a08bf613748fafffed6289d.zip
tests: Replace RandomImageGenerator dictionary logic with rand+time
The main purpose of this class is to generate filenames during tests. The reason it doesn't use fixed filenames is not because it wants to be a fuzzy test, but because it wants to allow running against a development wiki (outside CI) multiple times where one test may've failed and unable to clean up after itself, or where you may've done something manually and thus a conflicting file exists in the local wiki from before the DB was cloned for testing. Using dictionary based random names is needlessly complex, and has the additional downside of actually not avoiding conflicts very well. Replace all this by using a timestamp instead, which is much more stable over time and basically will never conflict with itself unless two tests were run on the same machine and started in the same second (or if the clock went backwards/broken). That seems unlikely enough to not need to support magically. But to counter-act it a bit still, also add a 3-char random hex in front of it. This also helps with file listings so that when you run it three times, it's easier to see the files generated by the same run close to each other (by not having subsequent tests start with a mostly identical prefix). Bug: T222416 Change-Id: Iec1d89324ff2fa53d1712796157adaf4ed34bdfe
-rw-r--r--tests/phpunit/includes/api/RandomImageGenerator.php110
-rw-r--r--tests/phpunit/includes/api/generateRandomImages.php1
2 files changed, 10 insertions, 101 deletions
diff --git a/tests/phpunit/includes/api/RandomImageGenerator.php b/tests/phpunit/includes/api/RandomImageGenerator.php
index 201a055aeb3a..44c59b7ce4d8 100644
--- a/tests/phpunit/includes/api/RandomImageGenerator.php
+++ b/tests/phpunit/includes/api/RandomImageGenerator.php
@@ -30,7 +30,6 @@ use MediaWiki\Shell\Shell;
* Can fetch a random image, or also write a number of them to disk with random filenames.
*/
class RandomImageGenerator {
- private $dictionaryFile;
private $minWidth = 400;
private $maxWidth = 800;
private $minHeight = 400;
@@ -72,33 +71,13 @@ class RandomImageGenerator {
];
public function __construct( $options = [] ) {
- foreach ( [ 'dictionaryFile', 'minWidth', 'minHeight',
+ foreach ( [ 'minWidth', 'minHeight',
'maxWidth', 'maxHeight', 'shapesToDraw' ] as $property
) {
if ( isset( $options[$property] ) ) {
$this->$property = $options[$property];
}
}
-
- // find the dictionary file, to generate random names
- if ( !isset( $this->dictionaryFile ) ) {
- foreach (
- [
- '/usr/share/dict/words',
- '/usr/dict/words',
- __DIR__ . '/words.txt'
- ] as $dictionaryFile
- ) {
- if ( is_file( $dictionaryFile ) && is_readable( $dictionaryFile ) ) {
- $this->dictionaryFile = $dictionaryFile;
- break;
- }
- }
- }
- if ( !isset( $this->dictionaryFile ) ) {
- throw new Exception( "RandomImageGenerator: dictionary file not "
- . "found or not specified properly" );
- }
}
/**
@@ -149,8 +128,9 @@ class RandomImageGenerator {
}
/**
- * Return a number of randomly-generated filenames
- * Each filename uses two words randomly drawn from the dictionary, like elephantine_spatula.jpg
+ * Return a number of randomly-generated filenames.
+ *
+ * Each filename uses follows the pattern "hex_timestamp_1.jpg".
*
* @param int $number Number of filenames to generate
* @param string $extension Optional, defaults to 'jpg'
@@ -162,13 +142,13 @@ class RandomImageGenerator {
$dir = getcwd();
}
$filenames = [];
- foreach ( $this->getRandomWordPairs( $number ) as $pair ) {
- $basename = $pair[0] . '_' . $pair[1];
- if ( !is_null( $extension ) ) {
- $basename .= '.' . $extension;
+ $prefix = wfRandomString( 3 ) . '_' . gmdate( 'YmdHis' ) . '_';
+ foreach ( range( 1, $number ) as $offset ) {
+ $filename = $prefix . $offset;
+ if ( $extension !== null ) {
+ $filename .= '.' . $extension;
}
- $basename = preg_replace( '/\s+/', '', $basename );
- $filenames[] = "$dir/$basename";
+ $filenames[] = "$dir/$filename";
}
return $filenames;
@@ -432,74 +412,4 @@ class RandomImageGenerator {
return 'rgb(' . implode( ', ', $components ) . ')';
}
-
- /**
- * Get an array of random pairs of random words, like
- * [ [ 'foo', 'bar' ], [ 'quux', 'baz' ] ];
- *
- * @param int $number Number of pairs
- * @return array Two-element arrays
- */
- private function getRandomWordPairs( $number ) {
- $lines = $this->getRandomLines( $number * 2 );
- // construct pairs of words
- $pairs = [];
- $count = count( $lines );
- for ( $i = 0; $i < $count; $i += 2 ) {
- $pairs[] = [ $lines[$i], $lines[$i + 1] ];
- }
-
- return $pairs;
- }
-
- /**
- * Return N random lines from a file
- *
- * Will throw exception if the file could not be read or if it had fewer lines than requested.
- *
- * @param int $number_desired Number of lines desired
- *
- * @throws Exception
- * @return array Array of exactly n elements, drawn randomly from lines the file
- */
- private function getRandomLines( $number_desired ) {
- $filepath = $this->dictionaryFile;
-
- // initialize array of lines
- $lines = [];
- for ( $i = 0; $i < $number_desired; $i++ ) {
- $lines[] = null;
- }
-
- /*
- * This algorithm obtains N random lines from a file in one single pass.
- * It does this by replacing elements of a fixed-size array of lines,
- * less and less frequently as it reads the file.
- */
- $fh = fopen( $filepath, "r" );
- if ( !$fh ) {
- throw new Exception( "couldn't open $filepath" );
- }
- $line_number = 0;
- while ( !feof( $fh ) ) {
- $line = fgets( $fh );
- if ( $line !== false ) {
- $line_number++;
- $line = trim( $line );
- $index = mt_rand( 0, $line_number - 1 );
- if ( $index < $number_desired ) {
- if ( $line_number <= $number_desired ) {
- $lines[$line_number - 1] = $lines[$index];
- }
- $lines[$index] = $line;
- }
- }
- }
- fclose( $fh );
- if ( $line_number < $number_desired ) {
- throw new Exception( "not enough lines in $filepath" );
- }
-
- return $lines;
- }
}
diff --git a/tests/phpunit/includes/api/generateRandomImages.php b/tests/phpunit/includes/api/generateRandomImages.php
index 9e7ef99b8abe..afc9ca007226 100644
--- a/tests/phpunit/includes/api/generateRandomImages.php
+++ b/tests/phpunit/includes/api/generateRandomImages.php
@@ -17,7 +17,6 @@ class GenerateRandomImages extends Maintenance {
public function execute() {
$getOptSpec = [
- 'dictionaryFile::',
'minWidth::',
'maxWidth::',
'minHeight::',