Mwalker has submitted this change and it was merged.

Change subject: unit test fixtures for CentralNotice
......................................................................


unit test fixtures for CentralNotice

Change-Id: I118bbd6f435d30865b309a674614034666ebbb45
---
M CentralNotice.php
A tests/ApiAllocationsTest.php
M tests/CentralNoticeTest.php
A tests/CentralNoticeTestFixtures.php
A tests/ComparisonUtil.php
5 files changed, 199 insertions(+), 0 deletions(-)

Approvals:
  Mwalker: Looks good to me, approved



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..9752736
--- /dev/null
+++ b/tests/ApiAllocationsTest.php
@@ -0,0 +1,79 @@
+<?php
+
+require_once 'CentralNoticeTestFixtures.php';
+require_once 'ComparisonUtil.php';
+
+/**
+ * @group CentralNotice
+ * @group medium
+ * @group Database
+ *
+ * This is a little sloppy, it is testing both the api and the allocations 
algorithms
+ */
+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() {
+               parent::tearDown();
+       }
+
+       public function testEqualAllocations() {
+               // Campaign has two banners, with default parameters
+               $this->cnFixtures->addFixtures( array(
+                       'campaigns' => array(
+                               array(
+                                       'banners' => array(
+                                               array(),
+                                               array()
+                                       ),
+                               ),
+                       ),
+               ) );
+               $expected = array(
+                       'centralnoticeallocations' => array(
+                               'banners' => array(
+                                       array (
+                                               'name' => 
$this->cnFixtures->spec['campaigns'][0]['banners'][0]['name'],
+                                               'fundraising' => 1,
+                                               'campaign' => 
$this->cnFixtures->spec['campaigns'][0]['name'],
+                                               'bucket' => 0,
+                                               'allocation' => .5,
+                                       ),
+                                       array (
+                                               'name' => 
$this->cnFixtures->spec['campaigns'][0]['banners'][1]['name'],
+                                               'fundraising' => 1,
+                                               'campaign' => 
$this->cnFixtures->spec['campaigns'][0]['name'],
+                                               'bucket' => 0,
+                                               'allocation' => .5,
+                                       ),
+                               ),
+                       ),
+               );
+
+               $ret = $this->doApiRequest( array(
+            'action' => 'centralnoticeallocations',
+               ) );
+               $this->assertTrue( ComparisonUtil::assertSuperset( $ret[0], 
$expected ) );
+       }
+
+       //TODO:
+       //function testInvalid() {
+
+       //function testFilters() {
+
+       //function testUnderallocation() {
+       // * driven below 1 / total_weight -> 1
+       // * 1/4 edge?
+       // * an underallocation -> in a predictable order
+       //function testKnownUnequal() {
+       //function testPriorities() {
+       //function test() {
+}
diff --git a/tests/CentralNoticeTest.php b/tests/CentralNoticeTest.php
index ce2ba22..69bd02e 100644
--- a/tests/CentralNoticeTest.php
+++ b/tests/CentralNoticeTest.php
@@ -2,6 +2,7 @@
 
 /**
  * @group Fundraising
+ * @group Database
  */
 class CentralNoticeTest extends PHPUnit_Framework_TestCase {
        /**
diff --git a/tests/CentralNoticeTestFixtures.php 
b/tests/CentralNoticeTestFixtures.php
new file mode 100644
index 0000000..3d9e749
--- /dev/null
+++ b/tests/CentralNoticeTestFixtures.php
@@ -0,0 +1,82 @@
+<?php
+
+class CentralNoticeTestFixtures {
+       public $spec = array();
+       protected $user;
+
+       // Use exactly the api defaults where available
+       static public $defaultCampaign;
+       static public $defaultBanner;
+       
+       function __construct( $user ) {
+               $this->user = $user;
+
+               static::$defaultCampaign = array(
+                       'enabled' => 1,
+                       // inclusive comparison is used, so this does not cause 
a race condition.
+                       'startTs' => wfTimestamp( TS_MW ),
+                       'projects' => array( 
ApiCentralNoticeAllocations::DEFAULT_PROJECT ),
+                       'project_languages' => array( 
ApiCentralNoticeAllocations::DEFAULT_LANGUAGE ),
+                       'geotargeted' => 1,
+                       'geo_countries' => array( 
ApiCentralNoticeAllocations::DEFAULT_COUNTRY ),
+                       'banners' => array(),
+               );
+               static::$defaultBanner = array(
+                       'body' => 'testing',
+                       'displayAnon' => 
ApiCentralNoticeAllocations::DEFAULT_ANONYMOUS === 'true',
+                       'displayAccount' => 
ApiCentralNoticeAllocations::DEFAULT_ANONYMOUS === 'false',
+                       'fundraising' => 1,
+                       'autolink' => 0,
+                       'landingPages' => 'JA1, JA2',
+                       'campaign_z_index' => 0,
+                       'weight' => 25,
+               );
+       }
+
+       function addFixtures( $spec ) {
+               foreach ( $spec['campaigns'] as $campaignSpec ) {
+                       $campaign = $campaignSpec + static::$defaultCampaign + 
array(
+                               'name' => 'TestCampaign_' . rand(),
+                       );
+
+                       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 + static::$defaultBanner 
+ array(
+                                       'name' => 'TestBanner_' . rand(),
+                               );
+
+                               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['campaigns'][] = $campaign;
+               }
+       }
+}
diff --git a/tests/ComparisonUtil.php b/tests/ComparisonUtil.php
new file mode 100644
index 0000000..a2d686c
--- /dev/null
+++ b/tests/ComparisonUtil.php
@@ -0,0 +1,36 @@
+<?php
+
+class ComparisonUtil {
+       /**
+        * Check that all elements of $inner match in $super, recursively.
+        */
+       static public function assertSuperset( $super, $inner, $path = array() 
) {
+               $expected_value = static::array_dereference( $inner, $path );
+               if ( is_array( $expected_value ) ) {
+                       foreach ( array_keys( $expected_value ) as $key ) {
+                               $inner_path = $path;
+                               $inner_path[] = $key;
+                               self::assertSuperset( $super, $inner, 
$inner_path );
+                       }
+               } else {
+                       $actual_value = static::array_dereference( $super, 
$path );
+                       if ( $expected_value !== $actual_value ) {
+                               throw new Exception( "Non-match at " . implode( 
".", $path ) );
+                       }
+               }
+               return true;
+       }
+
+       static protected function array_dereference( $root, $path ) {
+               $cur_path = array();
+               while ( count( $path ) ) {
+                       $key = array_shift( $path );
+                       $cur_path[] = $key;
+                       if ( !is_array( $root ) or !array_key_exists( $key, 
$root ) ) {
+                               throw new Exception( "Missing value for key " . 
implode( ".", $cur_path ) );
+                       }
+                       $root = $root[$key];
+               }
+               return $root;
+       }
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I118bbd6f435d30865b309a674614034666ebbb45
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Adamw <awi...@wikimedia.org>
Gerrit-Reviewer: Adamw <awi...@wikimedia.org>
Gerrit-Reviewer: Mwalker <mwal...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to