With the introduction of a REST API into MediaWiki core, we're going to want to share parameter validation logic rather than having similar code in both the Action API and the REST API. This abstracts out parameter validation logic as a library. There will be at least two follow-up patches: * One to add calls in the REST API, plus the interface for the REST API to do body validation. Should be reasonably straightforward. * One to adjust the Action API to use this. That'll be much less straightforward, as the Action API needs some MediaWiki-specific types (which the REST API might use too in the future) and needs to override the defaults on some of the library's checks (to maintain back-compat). Bug: T142080 Bug: T223239 Change-Id: I5c0cc3a8d686ace97596df5832c450a6a50f902c Depends-On: Iea05dc439688871c574c639e617765ae88a75ff7
82 lines
2 KiB
PHP
82 lines
2 KiB
PHP
<?php
|
|
|
|
namespace Wikimedia\ParamValidator\Util;
|
|
|
|
use RecursiveDirectoryIterator;
|
|
use RecursiveIteratorIterator;
|
|
use Wikimedia\AtEase\AtEase;
|
|
|
|
class UploadedFileTestBase extends \PHPUnit\Framework\TestCase {
|
|
|
|
/** @var string|null */
|
|
protected static $tmpdir;
|
|
|
|
public static function setUpBeforeClass() {
|
|
parent::setUpBeforeClass();
|
|
|
|
// Create a temporary directory for this test's files.
|
|
self::$tmpdir = null;
|
|
$base = sys_get_temp_dir() . DIRECTORY_SEPARATOR .
|
|
'phpunit-ParamValidator-UploadedFileTest-' . time() . '-' . getmypid() . '-';
|
|
for ( $i = 0; $i < 10000; $i++ ) {
|
|
$dir = $base . sprintf( '%04d', $i );
|
|
if ( AtEase::quietCall( 'mkdir', $dir, 0700, false ) === true ) {
|
|
self::$tmpdir = $dir;
|
|
break;
|
|
}
|
|
}
|
|
if ( self::$tmpdir === null ) {
|
|
self::fail( "Could not create temporary directory '{$base}XXXX'" );
|
|
}
|
|
}
|
|
|
|
public static function tearDownAfterClass() {
|
|
parent::tearDownAfterClass();
|
|
|
|
// Clean up temporary directory.
|
|
if ( self::$tmpdir !== null ) {
|
|
$iter = new RecursiveIteratorIterator(
|
|
new RecursiveDirectoryIterator( self::$tmpdir, RecursiveDirectoryIterator::SKIP_DOTS ),
|
|
RecursiveIteratorIterator::CHILD_FIRST
|
|
);
|
|
foreach ( $iter as $file ) {
|
|
if ( $file->isDir() ) {
|
|
rmdir( $file->getRealPath() );
|
|
} else {
|
|
unlink( $file->getRealPath() );
|
|
}
|
|
}
|
|
rmdir( self::$tmpdir );
|
|
self::$tmpdir = null;
|
|
}
|
|
}
|
|
|
|
protected static function assertTmpdir() {
|
|
if ( self::$tmpdir === null || !is_dir( self::$tmpdir ) ) {
|
|
self::fail( 'No temporary directory for ' . static::class );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $prefix For tempnam()
|
|
* @param string $content Contents of the file
|
|
* @return string Filename
|
|
*/
|
|
protected function makeTemp( $prefix, $content = 'foobar' ) {
|
|
self::assertTmpdir();
|
|
|
|
$filename = tempnam( self::$tmpdir, $prefix );
|
|
if ( $filename === false ) {
|
|
self::fail( 'Failed to create temporary file' );
|
|
}
|
|
|
|
self::assertSame(
|
|
strlen( $content ),
|
|
file_put_contents( $filename, $content ),
|
|
'Writing test temporary file'
|
|
);
|
|
|
|
return $filename;
|
|
}
|
|
|
|
}
|