Addshore has uploaded a new change for review. https://gerrit.wikimedia.org/r/78515
Change subject: Make and use a method for asserting html elements ...................................................................... Make and use a method for asserting html elements This should make creating simple tests for special pages a bit nicer! Change-Id: I297a4431dd9ce42bc80ab12d17f77a51f65cad66 --- M lib/tests/phpunit/specials/SpecialPageTestBase.php M repo/tests/phpunit/includes/specials/SpecialItemByTitleTest.php M repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php M repo/tests/phpunit/includes/specials/SpecialNewItemTest.php M repo/tests/phpunit/includes/specials/SpecialNewPropertyTest.php 5 files changed, 130 insertions(+), 28 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/15/78515/1 diff --git a/lib/tests/phpunit/specials/SpecialPageTestBase.php b/lib/tests/phpunit/specials/SpecialPageTestBase.php index 5ec8067..6e0b310 100644 --- a/lib/tests/phpunit/specials/SpecialPageTestBase.php +++ b/lib/tests/phpunit/specials/SpecialPageTestBase.php @@ -29,6 +29,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > * @author Daniel Kinzler + * @author Adam Shorland */ abstract class SpecialPageTestBase extends \MediaWikiTestCase { @@ -53,10 +54,6 @@ parent::tearDown(); } - - //public function testConstructor() { - //$this->assertInstanceOf( 'SpecialPage', new \SpecialItemDisambiguation() ); - //} /** * Returns a new instance of the special page under test. @@ -126,4 +123,19 @@ return array( $text, $response ); } + /** + * Uses regex to assert that the given tag exists in a string + * @param $text string to check + * @param $tag string name of tag + * @param $elements array list of html element and value + */ + protected function assertHasHtmlTagWithElements( $text, $tag, $elements ){ + $parts = array(); + foreach( $elements as $parm => $value ){ + $parts[] = preg_quote("{$parm}=\"{$value}\""); + } + $this->assertRegExp( '/'.$tag.'([^<]*?('.implode( '|', $parts ).')[^>]*?){'.count( $parts ).'}>/' , + $text, "Missing {$tag} tag in text" ); + } + } diff --git a/repo/tests/phpunit/includes/specials/SpecialItemByTitleTest.php b/repo/tests/phpunit/includes/specials/SpecialItemByTitleTest.php index c9492b3..e2d2c31 100644 --- a/repo/tests/phpunit/includes/specials/SpecialItemByTitleTest.php +++ b/repo/tests/phpunit/includes/specials/SpecialItemByTitleTest.php @@ -33,6 +33,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > * @author Daniel Kinzler + * @author Adam Shorland */ class SpecialItemByTitleTest extends SpecialPageTestBase { @@ -41,14 +42,35 @@ } public function testExecute() { - //TODO: Actually verify that the output is correct. - // Currently this just tests that there is no fatal error. + //TODO: Verify that more of the output is correct. + + $expectedInputs = array( + 'site' => array( + 'id' => 'wb-itembytitle-sitename', + 'name' => 'site' ), + 'pagename' => array( + 'id' => 'pagename', + 'class' => 'wb-input-text', + 'name' => 'page' ), + 'submit' => array( + 'id' => 'wb-itembytitle-submit', + 'class' => 'wb-input-button', + 'type' => 'submit', + 'name' => 'submit' ), + ); list( $output, ) = $this->executeSpecialPage( '' ); - $this->assertTrue( true, 'Calling execute without any subpage value' ); + // -- Make sure the special page loads with expected input fields ---- + foreach( $expectedInputs as $expected ){ + $this->assertHasHtmlTagWithElements( $output, 'input', $expected ); + } - list( $output, ) = $this->executeSpecialPage( 'en/oHai' ); - $this->assertTrue( true, 'Calling execute with a subpage value' ); //TODO: assert output + list( $output, ) = $this->executeSpecialPage( 'SiteText/PageText' ); + // -- Make sure the subpage values have been passed to the correct input fields ---- + $this->assertHasHtmlTagWithElements( $output, 'input', + array_merge( $expectedInputs['site'], array( 'value' => 'SiteText' ) ) ); + $this->assertHasHtmlTagWithElements( $output, 'input', + array_merge( $expectedInputs['pagename'], array( 'value' => 'PageText' ) ) ); } } \ No newline at end of file diff --git a/repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php b/repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php index 634170a..a219ade 100644 --- a/repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php +++ b/repo/tests/phpunit/includes/specials/SpecialItemDisambiguationTest.php @@ -33,6 +33,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > * @author Daniel Kinzler + * @author Adam Shorland */ class SpecialItemDisambiguationTest extends SpecialPageTestBase { @@ -41,14 +42,36 @@ } public function testExecute() { - //TODO: Actually verify that the output is correct. - // Currently this just tests that there is no fatal error. + //TODO: Verify that more of the output is correct. + + $expectedInputs = array( + 'language' => array( + 'id' => 'wb-itemdisambiguation-languagename', + 'class' => 'wb-input-text', + 'name' => 'language' ), + 'label' => array( + 'id' => 'labelname', + 'class' => 'wb-input-text', + 'name' => 'label' ), + 'submit' => array( + 'id' => 'wb-itembytitle-submit', + 'class' => 'wb-input-button', + 'type' => 'submit', + 'name' => 'submit' ), + ); list( $output, ) = $this->executeSpecialPage( '' ); - $this->assertTrue( true, 'Calling execute without any subpage value' ); + // -- Make sure the special page loads with expected input fields ---- + foreach( $expectedInputs as $expected ){ + $this->assertHasHtmlTagWithElements( $output, 'input', $expected ); + } - list( $output, ) = $this->executeSpecialPage( 'en/oHai' ); - $this->assertTrue( true, 'Calling execute with a subpage value' ); //TODO: assert output + list( $output, ) = $this->executeSpecialPage( 'LangText/LabelText' ); + // -- Make sure the subpage values have been passed to the correct input fields ---- + $this->assertHasHtmlTagWithElements( $output, 'input', + array_merge( $expectedInputs['language'], array( 'value' => 'LangText' ) ) ); + $this->assertHasHtmlTagWithElements( $output, 'input', + array_merge( $expectedInputs['label'], array( 'value' => 'LabelText' ) ) ); } } diff --git a/repo/tests/phpunit/includes/specials/SpecialNewItemTest.php b/repo/tests/phpunit/includes/specials/SpecialNewItemTest.php index 371ca4e..4ca5e6d 100644 --- a/repo/tests/phpunit/includes/specials/SpecialNewItemTest.php +++ b/repo/tests/phpunit/includes/specials/SpecialNewItemTest.php @@ -33,6 +33,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > * @author Daniel Kinzler + * @author Adam Shorland */ class SpecialNewItemTest extends SpecialPageTestBase { @@ -41,16 +42,38 @@ } public function testExecute() { - //TODO: Actually verify that the output is correct. - // Currently this just tests that there is no fatal error, - // and that the restriction handling is working and doesn't - // block. That is, the default should let the user execute - // the page. - + //TODO: Verify that more of the output is correct. //TODO: Verify that item creation works via a faux post request + $expectedInputs = array( + 'label' => array( + 'id' => 'wb-newentity-label', + 'class' => 'wb-input', + 'name' => 'label' ), + 'description' => array( + 'id' => 'wb-newentity-description', + 'class' => 'wb-input', + 'name' => 'description' ), + 'submit' => array( + 'id' => 'wb-newentity-submit', + 'class' => 'wb-button', + 'type' => 'submit', + 'name' => 'submit' ), + ); + list( $output, ) = $this->executeSpecialPage( '' ); - $this->assertTrue( true, 'Calling execute without any subpage value' ); + // -- Make sure the special page loads with expected input fields ---- + foreach( $expectedInputs as $expected ){ + $this->assertHasHtmlTagWithElements( $output, 'input', $expected ); + } + + list( $output, ) = $this->executeSpecialPage( 'LabelText/DescriptionText' ); + // -- Make sure the subpage values have been passed to the correct input fields ---- + $this->assertHasHtmlTagWithElements( $output, 'input', + array_merge( $expectedInputs['label'], array( 'value' => 'LabelText' ) ) ); + $this->assertHasHtmlTagWithElements( $output, 'input', + array_merge( $expectedInputs['description'], array( 'value' => 'DescriptionText' ) ) ); + } } \ No newline at end of file diff --git a/repo/tests/phpunit/includes/specials/SpecialNewPropertyTest.php b/repo/tests/phpunit/includes/specials/SpecialNewPropertyTest.php index 5063ab1..27533a0 100644 --- a/repo/tests/phpunit/includes/specials/SpecialNewPropertyTest.php +++ b/repo/tests/phpunit/includes/specials/SpecialNewPropertyTest.php @@ -32,6 +32,7 @@ * * @licence GNU GPL v2+ * @author John Erling Blad < [email protected] > + * @author Adam Shorland */ class SpecialNewPropertyTest extends SpecialPageTestBase { @@ -40,16 +41,37 @@ } public function testExecute() { - //TODO: Actually verify that the output is correct. - // Currently this just tests that there is no fatal error, - // and that the restriction handling is working and doesn't - // block. That is, the default should let the user execute - // the page. - + //TODO: Verify that more of the output is correct. //TODO: Verify that item creation works via a faux post request + $expectedInputs = array( + 'label' => array( + 'id' => 'wb-newentity-label', + 'class' => 'wb-input', + 'name' => 'label' ), + 'description' => array( + 'id' => 'wb-newentity-description', + 'class' => 'wb-input', + 'name' => 'description' ), + 'submit' => array( + 'id' => 'wb-newentity-submit', + 'class' => 'wb-button', + 'type' => 'submit', + 'name' => 'submit' ), + ); + list( $output, ) = $this->executeSpecialPage( '' ); - $this->assertTrue( true, 'Calling execute without any subpage value' ); + // -- Make sure the special page loads with expected input fields ---- + foreach( $expectedInputs as $expected ){ + $this->assertHasHtmlTagWithElements( $output, 'input', $expected ); + } + + list( $output, ) = $this->executeSpecialPage( 'LabelText/DescriptionText' ); + // -- Make sure the subpage values have been passed to the correct input fields ---- + $this->assertHasHtmlTagWithElements( $output, 'input', + array_merge( $expectedInputs['label'], array( 'value' => 'LabelText' ) ) ); + $this->assertHasHtmlTagWithElements( $output, 'input', + array_merge( $expectedInputs['description'], array( 'value' => 'DescriptionText' ) ) ); } } \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/78515 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I297a4431dd9ce42bc80ab12d17f77a51f65cad66 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Addshore <[email protected]> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
