Adamw has uploaded a new change for review. https://gerrit.wikimedia.org/r/54516
Change subject: unit test fixtures for CentralNotice ...................................................................... unit test fixtures for CentralNotice Change-Id: I118bbd6f435d30865b309a674614034666ebbb45 --- M CentralNotice.php A tests/ApiAllocationsTest.php A tests/CentralNoticeTestFixtures.php 3 files changed, 168 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralNotice refs/changes/16/54516/1 diff --git a/CentralNotice.php b/CentralNotice.php index 8cdca57..21022cf 100644 --- a/CentralNotice.php +++ b/CentralNotice.php @@ -577,6 +577,7 @@ * @return bool */ function efCentralNoticeUnitTests( &$files ) { + $files[ ] = __DIR__ . '/tests/ApiAllocationsTest.php'; $files[ ] = __DIR__ . '/tests/CentralNoticeTest.php'; return true; } diff --git a/tests/ApiAllocationsTest.php b/tests/ApiAllocationsTest.php new file mode 100644 index 0000000..848939f --- /dev/null +++ b/tests/ApiAllocationsTest.php @@ -0,0 +1,87 @@ +<?php + +require_once 'CentralNoticeTestFixtures.php'; + +/** + * @group CentralNotice + * @group medium + */ +class ApiAllocationsTest extends ApiTestCase { + protected $cnFixtures; + + protected function setUp() { + parent::setUp(); + + $this->userUser = self::$users['uploader']->user; + + $this->cnFixtures = new CentralNoticeTestFixtures( $this->userUser ); + } + + protected function tearDown() { + $this->cnFixtures->dropFixtures(); + + parent::tearDown(); + } + + public function testEqualAllocations() { + // XXX this is wicked unclear, and defaults are not that cool. + $this->cnFixtures->addFixtures( array( + array( + 'banners' => array( + array(), array() + ), + ), + ) ); + + $ret = $this->doApiRequest( array( + 'action' => 'centralnoticeallocations', + 'project' => 'wikipedia', + 'language' => 'en', + 'country' => 'US', + 'anonymous' => '1', + 'bucket' => '0', + 'minimal' => '0', + ) ); + //XXX order is not deterministic + $expected = array( + 'centralnoticeallocations' => array( + 'banners' => array( + array ( + 'name' => $this->cnFixtures->spec[0]['banners'][0]['name'], + 'fundraising' => 1, + 'campaign' => $this->cnFixtures->spec[0]['name'], + 'bucket' => 0, + 'allocation' => .5, + ), + array ( + 'name' => $this->cnFixtures->spec[0]['banners'][1]['name'], + 'fundraising' => 1, + 'campaign' => $this->cnFixtures->spec[0]['name'], + 'bucket' => 0, + 'allocation' => .5, + ), + ), + ), + ); + $this->assertEquals( + $expected, + $ret[0] + ); + } + + //function testInvalid() { + //function testCountryFilter() { +} + +class CentralNoticeFixtures { + public $campaigns; + public $banners; + + function newBanner( $overrides = array() ) { + return $banner; + } + + function newCampaign( $overrides = array() ) { + return $campaign; + } +} diff --git a/tests/CentralNoticeTestFixtures.php b/tests/CentralNoticeTestFixtures.php new file mode 100644 index 0000000..b6c2e02 --- /dev/null +++ b/tests/CentralNoticeTestFixtures.php @@ -0,0 +1,80 @@ +<?php + +class CentralNoticeTestFixtures { + public $spec = array(); + protected $user; + + function __construct( $user ) { + $this->user = $user; + } + + function addFixtures( $spec ) { + foreach ( $spec as $campaignSpec ) { + $campaign = $campaignSpec + array( + 'name' => 'TestCampaign_' . rand(), + 'enabled' => 1, + 'startTs' => wfTimestamp( TS_MW ), + 'projects' => array( 'wikipedia', 'wikibooks' ), + 'project_languages' => array( 'en', 'de' ), + 'geotargeted' => 1, + 'geo_countries' => array( 'US', 'AF' ), + 'banners' => array(), + ); + Campaign::addCampaign( + $campaign['name'], + $campaign['enabled'], + $campaign['startTs'], + $campaign['projects'], + $campaign['project_languages'], + $campaign['geotargeted'], + $campaign['geo_countries'], + $this->user + ); + + $banners = array(); + foreach ( $campaign['banners'] as $bannerSpec ) { + $banner = $bannerSpec + array( + 'name' => 'TestBanner_' . rand(), + 'body' => 'testing', + 'displayAnon' => 1, + 'displayAccount' => 1, + 'fundraising' => 1, + 'autolink' => 0, + 'landingPages' => 'JA1, JA2', + 'campaign_z_index' => 0, + 'weight' => 25, + ); + Banner::addTemplate( + $banner['name'], + $banner['body'], + $this->user, + $banner['displayAnon'], + $banner['displayAccount'], + $banner['fundraising'], + $banner['autolink'], + $banner['landingPages'] + ); + Campaign::addTemplateTo( + $campaign['name'], + $banner['name'], + $banner['weight'] + ); + + $banners[] = $banner; + } + $campaign['banners'] = $banners; + + $this->spec[] = $campaign; + } + } + + function dropFixtures() { + foreach ( $this->spec as $campaign ) { + foreach ( $campaign['banners'] as $banner ) { + Campaign::removeTemplateFor( $campaign['name'], $banner['name'] ); + Banner::removeTemplate ( $banner['name'], $this->user ); + } + Campaign::removeCampaign( $campaign['name'], $this->user ); + } + } +} -- To view, visit https://gerrit.wikimedia.org/r/54516 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I118bbd6f435d30865b309a674614034666ebbb45 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/CentralNotice Gerrit-Branch: master Gerrit-Owner: Adamw <awi...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits