Aude has uploaded a new change for review. https://gerrit.wikimedia.org/r/242877
Change subject: Add some tests for GeoDataHooks, including onLinksUpdate ...................................................................... Add some tests for GeoDataHooks, including onLinksUpdate This adds tests for some of the hooks in GeoDataHooks, including onLinksUpdate (though doesn't yet test extracting file metadata, nor Cirrus hooks). Also added tests for onArticleDeleteComplete and onParserFirstCallInit hooks. Change-Id: Ib48fc6c861788f0268b31817f2a014d9662cb03c --- A tests/GeoDataHooksTest.php 1 file changed, 135 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GeoData refs/changes/77/242877/1 diff --git a/tests/GeoDataHooksTest.php b/tests/GeoDataHooksTest.php new file mode 100644 index 0000000..7936910 --- /dev/null +++ b/tests/GeoDataHooksTest.php @@ -0,0 +1,135 @@ +<?php + +/** + * @group GeoData + * @group Database + */ +class GeoDataHooksTest extends MediaWikiTestCase { + + public function testOnParserFirstCallInit() { + $parserConfig = array( 'class' => 'Parser' ); + $parser = new Parser( $parserConfig ); + + GeoDataHooks::onParserFirstCallInit( $parser ); + + $this->assertEquals( array( 'coordinates' ), $parser->getFunctionHooks() ); + } + + public function testOnArticleDeleteComplete() { + $page = 'Help:GeoDataArticleDeleteTestPage'; + $text = '{{#coordinates:35.014167|135.7475}}'; + + $pageData = $this->insertPage( $page, $text ); + + $rows = $this->selectGeoTagsRows( $pageData['id'] ); + + // sanity check + $this->assertEquals( $pageData['id'], $rows[0]->gt_page_id, 'geo tags added' ); + + GeoDataHooks::onArticleDeleteComplete( + Article::newFromId( $pageData['id'] ), + User::newFromName( 'UTSysop' ), + 'delete!', + $pageData['id'] + ); + + $this->assertEquals( + array(), + $this->selectGeoTagsRows( $pageData['id'] ), + 'geo tags deleted' + ); + } + + public function testOnLinksUpdate() { + $coordsOutput = new CoordinatesOutput(); + $coordsOutput->addPrimary( + $this->makeCoord( 44.112, -87.913, true ) + ); + + $coordsOutput->addSecondary( + $this->makeCoord( 30.0123, 40.456, false ) + ); + + $linksUpdate = $this->newLinksUpdate( 'Help:GeoDataLinksUpdateTestPage', $coordsOutput ); + + $expected = array( + $this->makeGeoTagsRow( '30.01230000', '40.45600000', 0, $linksUpdate->mId ), + $this->makeGeoTagsRow( '44.11200000', '-87.91300000', 1, $linksUpdate->mId ) + ); + + GeoDataHooks::onLinksUpdate( $linksUpdate ); + + $this->assertEquals( $expected, $this->selectGeoTagsRows( $linksUpdate->mId ) ); + } + + private function newLinksUpdate( $titleText, $coordinatesOutput ) { + $pageData = $this->insertPage( $titleText, 'data!' ); + + $parserOutput = new ParserOutput(); + $parserOutput->geoData = $coordinatesOutput; + + return new LinksUpdate( + Title::newFromText( $titleText ), + $parserOutput + ); + } + + private function makeCoord( $lat, $lon, $primary ) { + $primaryRow = array( + 'gt_lat' => $lat, + 'gt_lon' => $lon, + 'gt_globe' => 'earth', + 'gt_primary' => $primary, + 'gt_dim' => 1000 + ); + + return Coord::newFromRow( (object)$primaryRow ); + } + + private function makeGeoTagsRow( $lat, $lon, $primary, $pageId ) { + $row = array( + 'gt_page_id' => $pageId, + 'gt_globe' => 'earth', + 'gt_primary' => $primary, + 'gt_lat' => $lat, + 'gt_lon' => $lon, + 'gt_dim' => '1000', + 'gt_type' => NULL, + 'gt_name' => NULL, + 'gt_country' => NULL, + 'gt_region' => NULL, + ); + + return (object)$row; + } + + private function selectGeoTagsRows( $pageId ) { + // Use master since select comes right after inserting a page. + $dbw = wfGetDB( DB_MASTER ); + + $res = $dbw->select( + 'geo_tags', + array( '*' ), + array( + 'gt_page_id' => $pageId, + ), + __METHOD__ + ); + + return $this->extractRows( $res ); + } + + private function extractRows( $res ) { + $rows = array(); + + while ( ( $row = $res->fetchObject() ) ) { + // We do not need to be concerned about what exact row id the geo tags have. + unset( $row->gt_id ); + + $rows[] = $row; + } + + return $rows; + } + +} -- To view, visit https://gerrit.wikimedia.org/r/242877 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib48fc6c861788f0268b31817f2a014d9662cb03c Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/GeoData Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits