Addshore has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/351645 )

Change subject: Add some Integration tests for the SpecialPage
......................................................................

Add some Integration tests for the SpecialPage

This uses hamcrest to test the functionality of the
SpecialPage mocking real requests.

Also in this change ht HTML output on the special page is fixed,
where previously some elements were in h2 tags that are not allowed.

Change-Id: I6991bc94837e69c8784ed8a63a8fda83d6ff4a86
---
M src/Html/ImportPreviewPage.php
A tests/SpecialImportFileIntegrationTest.php
2 files changed, 245 insertions(+), 54 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/FileImporter 
refs/changes/45/351645/1

diff --git a/src/Html/ImportPreviewPage.php b/src/Html/ImportPreviewPage.php
index 8929543..11a0f1c 100644
--- a/src/Html/ImportPreviewPage.php
+++ b/src/Html/ImportPreviewPage.php
@@ -67,34 +67,34 @@
                Html::rawElement(
                        'h2',
                        [],
-                       $this->title->getPrefixedText() .
-                       Html::openElement(
-                               'form',
-                               [
-                                       'class' => 'mw-importfile-rightAlign',
-                                       'action' => 
$this->specialPage->getPageTitle()->getLocalURL(),
-                                       'method' => 'GET',
-                               ]
-                       ) .
-                       Html::element(
-                               'input',
-                               [
-                                       'type' => 'hidden',
-                                       'name' => 'action',
-                                       'value' => 'edittitle',
-                               ]
-                       ) .
-                       $importIdentityFormSnippet .
-                       new ButtonInputWidget(
-                               [
-                                       'classes' => [ 
'mw-importfile-edittitle' ],
-                                       'label' => ( new Message( 
'fileimporter-edittitle' ) )->plain(),
-                                       'type' => 'submit',
-                                       'flags' => [ 'progressive' ],
-                               ]
-                       ) .
-                       Html::closeElement( 'form' )
+                       $this->title->getPrefixedText()
                ) .
+               Html::openElement(
+                       'form',
+                       [
+                               'class' => 'mw-importfile-rightAlign',
+                               'action' => 
$this->specialPage->getPageTitle()->getLocalURL(),
+                               'method' => 'GET',
+                       ]
+               ) .
+               Html::element(
+                       'input',
+                       [
+                               'type' => 'hidden',
+                               'name' => 'action',
+                               'value' => 'edittitle',
+                       ]
+               ) .
+               $importIdentityFormSnippet .
+               new ButtonInputWidget(
+                       [
+                               'classes' => [ 'mw-importfile-edittitle' ],
+                               'label' => ( new Message( 
'fileimporter-edittitle' ) )->plain(),
+                               'type' => 'submit',
+                               'flags' => [ 'progressive' ],
+                       ]
+               ) .
+               Html::closeElement( 'form' ) .
                Linker::makeExternalImage(
                        $importDetails->getImageDisplayUrl(),
                        $this->title->getPrefixedText()
@@ -102,34 +102,34 @@
                Html::rawElement(
                        'h2',
                        [],
-                       ( new Message( 'fileimporter-heading-fileinfo' ) 
)->plain() .
-                       Html::openElement(
-                               'form',
-                               [
-                                       'class' => 'mw-importfile-rightAlign',
-                                       'action' => 
$this->specialPage->getPageTitle()->getLocalURL(),
-                                       'method' => 'GET',
-                               ]
-                       ) .
-                       Html::element(
-                               'input',
-                               [
-                                       'type' => 'hidden',
-                                       'name' => 'action',
-                                       'value' => 'editinfo',
-                               ]
-                       ) .
-                       $importIdentityFormSnippet .
-                       new ButtonInputWidget(
-                               [
-                                       'classes' => [ 'mw-importfile-editinfo' 
],
-                                       'label' => ( new Message( 
'fileimporter-editinfo' ) )->plain(),
-                                       'type' => 'submit',
-                                       'flags' => [ 'progressive' ],
-                               ]
-                       ) .
-                       Html::closeElement( 'form' )
+                       ( new Message( 'fileimporter-heading-fileinfo' ) 
)->plain()
                ) .
+               Html::openElement(
+                       'form',
+                       [
+                               'class' => 'mw-importfile-rightAlign',
+                               'action' => 
$this->specialPage->getPageTitle()->getLocalURL(),
+                               'method' => 'GET',
+                       ]
+               ) .
+               Html::element(
+                       'input',
+                       [
+                               'type' => 'hidden',
+                               'name' => 'action',
+                               'value' => 'editinfo',
+                       ]
+               ) .
+               $importIdentityFormSnippet .
+               new ButtonInputWidget(
+                       [
+                               'classes' => [ 'mw-importfile-editinfo' ],
+                               'label' => ( new Message( 
'fileimporter-editinfo' ) )->plain(),
+                               'type' => 'submit',
+                               'flags' => [ 'progressive' ],
+                       ]
+               ) .
+               Html::closeElement( 'form' ) .
                Html::rawElement(
                        'div',
                        [ 'class' => 'mw-importfile-parsedContent' ],
diff --git a/tests/SpecialImportFileIntegrationTest.php 
b/tests/SpecialImportFileIntegrationTest.php
new file mode 100644
index 0000000..40541fe
--- /dev/null
+++ b/tests/SpecialImportFileIntegrationTest.php
@@ -0,0 +1,191 @@
+<?php
+
+use FileImporter\MediaWiki\SiteTableSiteLookup;
+use FileImporter\SpecialImportFile;
+
+/**
+ * This test makes some calls to https://commons.wikimedia.org
+ *
+ * @group Database
+ */
+class SpecialImportFileIntegrationTest extends SpecialPageTestBase {
+
+       public function setUp() {
+               parent::setUp();
+
+               $commonsSite = $this->getMockSite( 'commonswiki', 
'commons.wikimedia.org' );
+               $hashSiteStore = new HashSiteStore( [ $commonsSite ] );
+               $siteTableSiteLookup = new SiteTableSiteLookup( $hashSiteStore 
);
+               $this->setService( 'FileImporterMediaWikiSiteTableSiteLookup', 
$siteTableSiteLookup );
+       }
+
+       private function getMockSite( $globalId, $domain ) {
+               $mockSite = $this->getMock( Site::class );
+               $mockSite->expects( $this->any() )
+                       ->method( 'getGlobalId' )
+                       ->will( $this->returnValue( $globalId ) );
+               $mockSite->expects( $this->any() )
+                       ->method( 'getDomain' )
+                       ->will( $this->returnValue( $domain ) );
+               $mockSite->expects( $this->any() )
+                       ->method( 'getNavigationIds' )
+                       ->will( $this->returnValue( [] ) );
+               return $mockSite;
+       }
+
+       /**
+        * Returns a new instance of the special page under test.
+        *
+        * @return SpecialPage
+        */
+       protected function newSpecialPage() {
+               return new SpecialImportFile();
+       }
+
+       public function provideTestData() {
+               return [
+                       'User(no groups / anon), Request(Blank GET), 
Expect(Groups required)' => [
+                               new FauxRequest(),
+                               new User(),
+                               [
+                                       'name' => PermissionsError::class,
+                                       'message' => 'The action you have 
requested is limited to users in one of the groups',
+                               ],
+                               function(){}
+                       ],
+                       'User(uploader), Request(Blank GET), Expect(Input 
Form)' => [
+                               new FauxRequest(),
+                               [ 'sysop' ],
+                               null,
+                               function( $html ) {
+                                       $this->assertInitialInputFormPreset( 
$html );
+                               }
+                       ],
+                       'User(uploader), Request(Valid clientUrl), 
Expect(Preview Form)' => [
+                               new FauxRequest( [
+                                       'clientUrl' => 
'https://commons.wikimedia.org/wiki/File:Chicken_In_Snow.JPG'
+                               ] ),
+                               [ 'sysop' ],
+                               null,
+                               function( $html ) {
+                                       $this->assertPreviewPage(
+                                               $html,
+                                               
'https://commons.wikimedia.org/wiki/File:Chicken_In_Snow.JPG',
+                                               'File:Chicken_In_Snow.JPG'
+                                       );
+                               }
+                       ],
+                       'User(uploader), Request(Invalid clientUrl), 
Expect(Error & Input Form)' => [
+                               new FauxRequest( [
+                                       'clientUrl' => 
'https://test.wikimedia.org/wiki/File:AnyFile.JPG'
+                               ] ),
+                               [ 'sysop' ],
+                               null,
+                               function( $html ) {
+                                       $this->assertInitialInputFormPreset( 
$html );
+                                       $this->assertWarningBox( $html, 'Can\'t 
import the given URL' );
+                               }
+                       ],
+               ];
+       }
+
+       private function assertInitialInputFormPreset( $html ) {
+               assertThat(
+                       $html,
+                       is( htmlPiece( havingChild(
+                                               both( withTagName( 'form' ) )
+                                                       ->andAlso( 
withAttribute( 'action' ) )
+                                                       ->andAlso( 
withAttribute( 'method' )->havingValue( 'GET' ) )
+                                                       ->andAlso( havingChild(
+                                                                       both( 
withTagName( 'input' ) )
+                                                                               
->andAlso( withAttribute( 'type' )->havingValue( 'url' ) )
+                                                                               
->andAlso( withAttribute( 'name' )->havingValue( 'clientUrl' ) )
+                                                       ) )
+                                                       ->andAlso( havingChild(
+                                                                       both( 
withTagName( 'button' ) )
+                                                                               
->andAlso( withAttribute( 'type' )->havingValue( 'submit' ) )
+                                                       ) )
+                       ) ) )
+               );
+       }
+
+       private function assertWarningBox( $html, $text ) {
+               assertThat(
+                       $html,
+                       is( htmlPiece( havingChild(
+                               both( withTagName( 'div' ) )
+                                       ->andAlso( withClass( 'warningbox' ) )
+                                       ->andAlso( havingChild(
+                                               both( withTagName( 'p' ) )
+                                                       ->andAlso( 
havingTextContents( $text ) )
+                                       ) )
+                       ) ) )
+               );
+       }
+
+       private function assertPreviewPage( $html, $clientUrl, $intendedTitle ) 
{
+               assertThat(
+                       $html,
+                       is( htmlPiece( havingChild(
+                               both( withTagName( 'form' ) )
+                                       ->andAlso( withAttribute( 'action' ) )
+                                       ->andAlso( withAttribute( 'method' 
)->havingValue( 'POST' ) )
+                                       ->andAlso( havingChild( 
$this->thatIsHiddenInputField( 'clientUrl', $clientUrl ) ) )
+                                       ->andAlso( havingChild( 
$this->thatIsHiddenInputField( 'intendedTitle', $intendedTitle ) ) )
+                                       ->andAlso( havingChild( 
$this->thatIsHiddenInputField( 'importDetailsHash' ) ) )
+                                       ->andAlso( havingChild( 
$this->thatIsHiddenInputField( 'token' ) ) )
+                                       ->andAlso( havingChild(
+                                               both( withTagName( 'button' ) )
+                                                       ->andAlso( 
withAttribute( 'type' )->havingValue( 'submit' ) )
+                                       ) )
+                       ) ) )
+               );
+       }
+
+       private function thatIsHiddenInputField( $name, $value = null ) {
+               $valueMatcher = withAttribute( 'value' );
+               if ( $value !== null ) {
+                       $valueMatcher->havingValue( $value );
+               }
+               return both( withTagName( 'input' ) )
+                       ->andAlso( withAttribute( 'type' )->havingValue( 
'hidden' ) )
+                       ->andAlso( withAttribute( 'name' )->havingValue( $name 
) )
+                       ->andAlso( $valueMatcher );
+       }
+
+       /**
+        * @dataProvider provideTestData
+        */
+       public function testSpecialPageExecutionWithVariousInputs(
+               $request,
+               $userOrGroups,
+               $expectedExceptionDetails = null,
+               $htmlAssertionCallable
+       ) {
+               if ( $expectedExceptionDetails ) {
+                       $this->setExpectedException(
+                               $expectedExceptionDetails['name'],
+                               $expectedExceptionDetails['message']
+                       );
+               }
+
+               if ( is_array( $userOrGroups ) ) {
+                       $user = $this->getTestUser( $userOrGroups )->getUser();
+               } else {
+                       $user = $userOrGroups;
+               }
+
+               /** @var string $html */
+               /** @var WebResponse $response */
+               list( $html, $response ) = $this->executeSpecialPage(
+                       '',
+                       $request,
+                       'en',
+                       $user
+               );
+
+               $htmlAssertionCallable( $html );
+               $this->assertTrue( true ); // assertion to avoid phpunit 
showing hamcrest test as risky
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/351645
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6991bc94837e69c8784ed8a63a8fda83d6ff4a86
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/FileImporter
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to