From: Umherirrender Date: Fri, 16 Mar 2018 17:40:38 +0000 (+0100) Subject: Rename ApiTestCaseUpload X-Git-Tag: 1.31.0-rc.0~361^2 X-Git-Url: http://git.cyclocoop.org/%24self?a=commitdiff_plain;h=040063a4ba7bc9775ebf5efd393b2076c59345a7;p=lhc%2Fweb%2Fwiklou.git Rename ApiTestCaseUpload Having *TestCase at end of the class name makes it easier to detect it as TestCase class Change-Id: Ic7ae7328e729c0f2d97afcf7c5be793d6a8df58f --- diff --git a/tests/common/TestsAutoLoader.php b/tests/common/TestsAutoLoader.php index a777153b04..b994f8a1b5 100644 --- a/tests/common/TestsAutoLoader.php +++ b/tests/common/TestsAutoLoader.php @@ -77,6 +77,7 @@ $wgAutoloadClasses += [ 'ApiTestCase' => "$testDir/phpunit/includes/api/ApiTestCase.php", 'ApiTestCaseUpload' => "$testDir/phpunit/includes/api/ApiTestCaseUpload.php", 'ApiTestContext' => "$testDir/phpunit/includes/api/ApiTestContext.php", + 'ApiUploadTestCase' => "$testDir/phpunit/includes/api/ApiUploadTestCase.php", 'MockApi' => "$testDir/phpunit/includes/api/MockApi.php", 'MockApiQueryBase' => "$testDir/phpunit/includes/api/MockApiQueryBase.php", 'UserWrapper' => "$testDir/phpunit/includes/api/UserWrapper.php", diff --git a/tests/phpunit/includes/api/ApiTestCaseUpload.php b/tests/phpunit/includes/api/ApiTestCaseUpload.php index f15da2ee91..3670fad846 100644 --- a/tests/phpunit/includes/api/ApiTestCaseUpload.php +++ b/tests/phpunit/includes/api/ApiTestCaseUpload.php @@ -1,153 +1,8 @@ setMwGlobals( [ - 'wgEnableUploads' => true, - 'wgEnableAPI' => true, - ] ); - - $this->clearFakeUploads(); - } - - /** - * Helper function -- remove files and associated articles by Title - * - * @param Title $title Title to be removed - * - * @return bool - */ - public function deleteFileByTitle( $title ) { - if ( $title->exists() ) { - $file = wfFindFile( $title, [ 'ignoreRedirect' => true ] ); - $noOldArchive = ""; // yes this really needs to be set this way - $comment = "removing for test"; - $restrictDeletedVersions = false; - $status = FileDeleteForm::doDelete( - $title, - $file, - $noOldArchive, - $comment, - $restrictDeletedVersions - ); - - if ( !$status->isGood() ) { - return false; - } - - $page = WikiPage::factory( $title ); - $page->doDeleteArticle( "removing for test" ); - - // see if it now doesn't exist; reload - $title = Title::newFromText( $title->getText(), NS_FILE ); - } - - return !( $title && $title instanceof Title && $title->exists() ); - } - - /** - * Helper function -- remove files and associated articles with a particular filename - * - * @param string $fileName Filename to be removed - * - * @return bool - */ - public function deleteFileByFileName( $fileName ) { - return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) ); - } - - /** - * Helper function -- given a file on the filesystem, find matching - * content in the db (and associated articles) and remove them. - * - * @param string $filePath Path to file on the filesystem - * - * @return bool - */ - public function deleteFileByContent( $filePath ) { - $hash = FSFile::getSha1Base36FromPath( $filePath ); - $dupes = RepoGroup::singleton()->findBySha1( $hash ); - $success = true; - foreach ( $dupes as $dupe ) { - $success &= $this->deleteFileByTitle( $dupe->getTitle() ); - } - - return $success; - } - - /** - * Fake an upload by dumping the file into temp space, and adding info to $_FILES. - * (This is what PHP would normally do). - * - * @param string $fieldName Name this would have in the upload form - * @param string $fileName Name to title this - * @param string $type MIME type - * @param string $filePath Path where to find file contents - * - * @throws Exception - * @return bool - */ - function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) { - $tmpName = $this->getNewTempFile(); - if ( !file_exists( $filePath ) ) { - throw new Exception( "$filePath doesn't exist!" ); - } - - if ( !copy( $filePath, $tmpName ) ) { - throw new Exception( "couldn't copy $filePath to $tmpName" ); - } - - clearstatcache(); - $size = filesize( $tmpName ); - if ( $size === false ) { - throw new Exception( "couldn't stat $tmpName" ); - } - - $_FILES[$fieldName] = [ - 'name' => $fileName, - 'type' => $type, - 'tmp_name' => $tmpName, - 'size' => $size, - 'error' => null - ]; - - return true; - } - - function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) { - $tmpName = $this->getNewTempFile(); - // copy the chunk data to temp location: - if ( !file_put_contents( $tmpName, $chunkData ) ) { - throw new Exception( "couldn't copy chunk data to $tmpName" ); - } - - clearstatcache(); - $size = filesize( $tmpName ); - if ( $size === false ) { - throw new Exception( "couldn't stat $tmpName" ); - } - - $_FILES[$fieldName] = [ - 'name' => $fileName, - 'type' => $type, - 'tmp_name' => $tmpName, - 'size' => $size, - 'error' => null - ]; - } - - /** - * Remove traces of previous fake uploads - */ - function clearFakeUploads() { - $_FILES = []; - } } diff --git a/tests/phpunit/includes/api/ApiUploadTest.php b/tests/phpunit/includes/api/ApiUploadTest.php index 345f19675c..41c9aed46c 100644 --- a/tests/phpunit/includes/api/ApiUploadTest.php +++ b/tests/phpunit/includes/api/ApiUploadTest.php @@ -22,7 +22,7 @@ * * @covers ApiUpload */ -class ApiUploadTest extends ApiTestCaseUpload { +class ApiUploadTest extends ApiUploadTestCase { /** * Testing login * XXX this is a funny way of getting session context diff --git a/tests/phpunit/includes/api/ApiUploadTestCase.php b/tests/phpunit/includes/api/ApiUploadTestCase.php new file mode 100644 index 0000000000..3c7efd5768 --- /dev/null +++ b/tests/phpunit/includes/api/ApiUploadTestCase.php @@ -0,0 +1,153 @@ +setMwGlobals( [ + 'wgEnableUploads' => true, + 'wgEnableAPI' => true, + ] ); + + $this->clearFakeUploads(); + } + + /** + * Helper function -- remove files and associated articles by Title + * + * @param Title $title Title to be removed + * + * @return bool + */ + public function deleteFileByTitle( $title ) { + if ( $title->exists() ) { + $file = wfFindFile( $title, [ 'ignoreRedirect' => true ] ); + $noOldArchive = ""; // yes this really needs to be set this way + $comment = "removing for test"; + $restrictDeletedVersions = false; + $status = FileDeleteForm::doDelete( + $title, + $file, + $noOldArchive, + $comment, + $restrictDeletedVersions + ); + + if ( !$status->isGood() ) { + return false; + } + + $page = WikiPage::factory( $title ); + $page->doDeleteArticle( "removing for test" ); + + // see if it now doesn't exist; reload + $title = Title::newFromText( $title->getText(), NS_FILE ); + } + + return !( $title && $title instanceof Title && $title->exists() ); + } + + /** + * Helper function -- remove files and associated articles with a particular filename + * + * @param string $fileName Filename to be removed + * + * @return bool + */ + public function deleteFileByFileName( $fileName ) { + return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) ); + } + + /** + * Helper function -- given a file on the filesystem, find matching + * content in the db (and associated articles) and remove them. + * + * @param string $filePath Path to file on the filesystem + * + * @return bool + */ + public function deleteFileByContent( $filePath ) { + $hash = FSFile::getSha1Base36FromPath( $filePath ); + $dupes = RepoGroup::singleton()->findBySha1( $hash ); + $success = true; + foreach ( $dupes as $dupe ) { + $success &= $this->deleteFileByTitle( $dupe->getTitle() ); + } + + return $success; + } + + /** + * Fake an upload by dumping the file into temp space, and adding info to $_FILES. + * (This is what PHP would normally do). + * + * @param string $fieldName Name this would have in the upload form + * @param string $fileName Name to title this + * @param string $type MIME type + * @param string $filePath Path where to find file contents + * + * @throws Exception + * @return bool + */ + function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) { + $tmpName = $this->getNewTempFile(); + if ( !file_exists( $filePath ) ) { + throw new Exception( "$filePath doesn't exist!" ); + } + + if ( !copy( $filePath, $tmpName ) ) { + throw new Exception( "couldn't copy $filePath to $tmpName" ); + } + + clearstatcache(); + $size = filesize( $tmpName ); + if ( $size === false ) { + throw new Exception( "couldn't stat $tmpName" ); + } + + $_FILES[$fieldName] = [ + 'name' => $fileName, + 'type' => $type, + 'tmp_name' => $tmpName, + 'size' => $size, + 'error' => null + ]; + + return true; + } + + function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) { + $tmpName = $this->getNewTempFile(); + // copy the chunk data to temp location: + if ( !file_put_contents( $tmpName, $chunkData ) ) { + throw new Exception( "couldn't copy chunk data to $tmpName" ); + } + + clearstatcache(); + $size = filesize( $tmpName ); + if ( $size === false ) { + throw new Exception( "couldn't stat $tmpName" ); + } + + $_FILES[$fieldName] = [ + 'name' => $fileName, + 'type' => $type, + 'tmp_name' => $tmpName, + 'size' => $size, + 'error' => null + ]; + } + + /** + * Remove traces of previous fake uploads + */ + function clearFakeUploads() { + $_FILES = []; + } +}