X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FMediaWikiTestCase.php;h=329f0ba12df6719a131da75387cbd269e62ead36;hb=e83ca504052654a2b9decdf6208fec335aa43e7a;hp=fd0cea1023c2ca00c415c6d094bffc16ab5fd24f;hpb=b2ebc4452456a1ce1b228fe10bda5161469b646e;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index fd0cea1023..329f0ba12d 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -472,7 +472,17 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { * @return string Absolute name of the temporary file */ protected function getNewTempFile() { - $fileName = tempnam( wfTempDir(), 'MW_PHPUnit_' . static::class . '_' ); + $fileName = tempnam( + wfTempDir(), + // Avoid backslashes here as they result in inconsistent results + // between Windows and other OS, as well as between functions + // that try to normalise these in one or both directions. + // For example, tempnam rejects directory separators in the prefix which + // means it rejects any namespaced class on Windows. + // And then there is, wfMkdirParents which normalises paths always + // whereas most other PHP and MW functions do not. + 'MW_PHPUnit_' . strtr( static::class, [ '\\' => '_' ] ) . '_' + ); $this->tmpFiles[] = $fileName; return $fileName; @@ -489,14 +499,15 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { * @return string Absolute name of the temporary directory */ protected function getNewTempDirectory() { - // Starting of with a temporary /file/. + // Starting of with a temporary *file*. $fileName = $this->getNewTempFile(); - // Converting the temporary /file/ to a /directory/ + // Converting the temporary file to a *directory*. // The following is not atomic, but at least we now have a single place, - // where temporary directory creation is bundled and can be improved + // where temporary directory creation is bundled and can be improved. unlink( $fileName ); - $this->assertTrue( wfMkdirParents( $fileName ) ); + // If this fails for some reason, PHP will warn and fail the test. + mkdir( $fileName, 0777, /* recursive = */ true ); return $fileName; } @@ -2233,18 +2244,19 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { } // NOTE: prefer content namespaces + $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo(); $namespaces = array_unique( array_merge( - MWNamespace::getContentNamespaces(), + $nsInfo->getContentNamespaces(), [ NS_MAIN, NS_HELP, NS_PROJECT ], // prefer these - MWNamespace::getValidNamespaces() + $nsInfo->getValidNamespaces() ) ); $namespaces = array_diff( $namespaces, [ NS_FILE, NS_CATEGORY, NS_MEDIAWIKI, NS_USER // don't mess with magic namespaces ] ); - $talk = array_filter( $namespaces, function ( $ns ) { - return MWNamespace::isTalk( $ns ); + $talk = array_filter( $namespaces, function ( $ns ) use ( $nsInfo ) { + return $nsInfo->isTalk( $ns ); } ); // prefer non-talk pages @@ -2355,7 +2367,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { if ( $createIfMissing ) { if ( !file_exists( $fileName ) ) { file_put_contents( $fileName, $actualData ); - $this->markTestSkipped( 'Data file $fileName does not exist' ); + $this->markTestSkipped( "Data file $fileName does not exist" ); } } else { self::assertFileExists( $fileName ); @@ -2369,7 +2381,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { * @param string $text Content of the page * @param string $summary Optional summary string for the revision * @param int $defaultNs Optional namespace id - * @return array Array as returned by WikiPage::doEditContent() + * @return Status Object as returned by WikiPage::doEditContent() * @throws MWException If this test cases's needsDB() method doesn't return true. * Test cases can use "@group Database" to enable database test support, * or list the tables under testing in $this->tablesUsed, or override the @@ -2408,4 +2420,18 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { 'comment' => $comment, ] ); } + + /** + * Returns a PHPUnit constraint that matches anything other than a fixed set of values. This can + * be used to whitelist values, e.g. + * $mock->expects( $this->never() )->method( $this->anythingBut( 'foo', 'bar' ) ); + * which will throw if any unexpected method is called. + * + * @param mixed ...$values Values that are not matched + */ + protected function anythingBut( ...$values ) { + return $this->logicalNot( $this->logicalOr( + ...array_map( [ $this, 'matches' ], $values ) + ) ); + } }