From: addshore Date: Thu, 24 Mar 2016 14:00:41 +0000 (+0000) Subject: Factor SpecilaPageExecutor out of SpecialPageTestBase X-Git-Tag: 1.31.0-rc.0~7490^2 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=049fb94979e6d79e4adbbeb4c9c968a67a478426;p=lhc%2Fweb%2Fwiklou.git Factor SpecilaPageExecutor out of SpecialPageTestBase Change-Id: I3c8ae980db5ba3b34bd6664f400bc3b5cdf587c0 --- diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 76a6335456..c8b466145d 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -105,6 +105,7 @@ $wgAutoloadClasses += [ # tests/phpunit/includes/specials 'SpecialPageTestBase' => "$testDir/phpunit/includes/specials/SpecialPageTestBase.php", + 'SpecialPageExecutor' => "$testDir/phpunit/includes/specials/SpecialPageExecutor.php", # tests/phpunit/languages 'LanguageClassesTestCase' => "$testDir/phpunit/languages/LanguageClassesTestCase.php", diff --git a/tests/phpunit/includes/specials/SpecialPageExecutor.php b/tests/phpunit/includes/specials/SpecialPageExecutor.php new file mode 100644 index 0000000000..2f7b7678ca --- /dev/null +++ b/tests/phpunit/includes/specials/SpecialPageExecutor.php @@ -0,0 +1,129 @@ +newContext( $request, $language, $user ); + + $output = new OutputPage( $context ); + $context->setOutput( $output ); + + $page->setContext( $context ); + $output->setTitle( $page->getPageTitle() ); + + $html = $this->getHTMLFromSpecialPage( $page, $subPage ); + $response = $context->getRequest()->response(); + + if ( $response instanceof FauxResponse ) { + $code = $response->getStatusCode(); + + if ( $code > 0 ) { + $response->header( 'Status: ' . $code . ' ' . HttpStatus::getMessage( $code ) ); + } + } + + return [ $html, $response ]; + } + + /** + * @param WebRequest|null $request + * @param Language|string|null $language + * @param User|null $user + * + * @return DerivativeContext + */ + private function newContext( + WebRequest $request = null, + $language = null, + User $user = null + ) { + $context = new DerivativeContext( RequestContext::getMain() ); + + $context->setRequest( $request ?: new FauxRequest() ); + + if ( $language !== null ) { + $context->setLanguage( $language ); + } + + if ( $user !== null ) { + $context->setUser( $user ); + } + + $this->setEditTokenFromUser( $context ); + + return $context; + } + + /** + * If we are trying to edit and no token is set, supply one. + * + * @param DerivativeContext $context + */ + private function setEditTokenFromUser( DerivativeContext $context ) { + $request = $context->getRequest(); + + // Edits via GET are a security issue and should not succeed. On the other hand, not all + // POST requests are edits, but should ignore unused parameters. + if ( !$request->getCheck( 'wpEditToken' ) && $request->wasPosted() ) { + $request->setVal( 'wpEditToken', $context->getUser()->getEditToken() ); + } + } + + /** + * @param SpecialPage $page + * @param string $subPage + * + * @throws Exception + * @return string HTML + */ + private function getHTMLFromSpecialPage( SpecialPage $page, $subPage ) { + ob_start(); + + try { + $page->execute( $subPage ); + + $output = $page->getOutput(); + + if ( $output->getRedirect() !== '' ) { + $output->output(); + $html = ob_get_contents(); + } elseif ( $output->isDisabled() ) { + $html = ob_get_contents(); + } else { + $html = $output->getHTML(); + } + } catch ( Exception $ex ) { + ob_end_clean(); + + // Re-throw exception after "finally" handling because PHP 5.3 doesn't have "finally". + throw $ex; + } + + ob_end_clean(); + + return $html; + } + +} diff --git a/tests/phpunit/includes/specials/SpecialPageTestBase.php b/tests/phpunit/includes/specials/SpecialPageTestBase.php index 48baa8e79e..2f091d5d06 100644 --- a/tests/phpunit/includes/specials/SpecialPageTestBase.php +++ b/tests/phpunit/includes/specials/SpecialPageTestBase.php @@ -60,106 +60,13 @@ abstract class SpecialPageTestBase extends MediaWikiTestCase { $language = null, User $user = null ) { - $context = $this->newContext( $request, $language, $user ); - - $output = new OutputPage( $context ); - $context->setOutput( $output ); - - $page = $this->newSpecialPage(); - $page->setContext( $context ); - $output->setTitle( $page->getPageTitle() ); - - $html = $this->getHTMLFromSpecialPage( $page, $subPage ); - $response = $context->getRequest()->response(); - - if ( $response instanceof FauxResponse ) { - $code = $response->getStatusCode(); - - if ( $code > 0 ) { - $response->header( 'Status: ' . $code . ' ' . HttpStatus::getMessage( $code ) ); - } - } - - return [ $html, $response ]; - } - - /** - * @param WebRequest|null $request - * @param Language|string|null $language - * @param User|null $user - * - * @return DerivativeContext - */ - private function newContext( - WebRequest $request = null, - $language = null, - User $user = null - ) { - $context = new DerivativeContext( RequestContext::getMain() ); - - $context->setRequest( $request ?: new FauxRequest() ); - - if ( $language !== null ) { - $context->setLanguage( $language ); - } - - if ( $user !== null ) { - $context->setUser( $user ); - } - - $this->setEditTokenFromUser( $context ); - - return $context; - } - - /** - * If we are trying to edit and no token is set, supply one. - * - * @param DerivativeContext $context - */ - private function setEditTokenFromUser( DerivativeContext $context ) { - $request = $context->getRequest(); - - // Edits via GET are a security issue and should not succeed. On the other hand, not all - // POST requests are edits, but should ignore unused parameters. - if ( !$request->getCheck( 'wpEditToken' ) && $request->wasPosted() ) { - $request->setVal( 'wpEditToken', $context->getUser()->getEditToken() ); - } - } - - /** - * @param SpecialPage $page - * @param string $subPage - * - * @throws Exception - * @return string HTML - */ - private function getHTMLFromSpecialPage( SpecialPage $page, $subPage ) { - ob_start(); - - try { - $page->execute( $subPage ); - - $output = $page->getOutput(); - - if ( $output->getRedirect() !== '' ) { - $output->output(); - $html = ob_get_contents(); - } elseif ( $output->isDisabled() ) { - $html = ob_get_contents(); - } else { - $html = $output->getHTML(); - } - } catch ( Exception $ex ) { - ob_end_clean(); - - // Re-throw exception after "finally" handling because PHP 5.3 doesn't have "finally". - throw $ex; - } - - ob_end_clean(); - - return $html; + return ( new SpecialPageExecutor() )->executeSpecialPage( + $this->newSpecialPage(), + $subPage, + $request, + $language, + $user + ); } }