Phuedx has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/254402

Change subject: [WIP] Validate and handle a survey's platform
......................................................................

[WIP] Validate and handle a survey's platform

Changes:
* Add Survey#getPlatform and #setPlatform so as not to introduce another
  constructor parameter
* Make SurveyFactory validate and set the platform if it's given in the
  survey spec
* Include the platform in Survey#toArray so that it's sent to the client

Change-Id: I99ffb2c0db33d3ccdd72041dd598fa759f7428d3
---
M includes/Survey.php
M includes/SurveyFactory.php
A tests/phpunit/SurveyFactoryPlatformTest.php
3 files changed, 164 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/QuickSurveys 
refs/changes/02/254402/1

diff --git a/includes/Survey.php b/includes/Survey.php
index 06c413a..bc625ef 100644
--- a/includes/Survey.php
+++ b/includes/Survey.php
@@ -4,6 +4,18 @@
 
 abstract class Survey
 {
+       const PLATFORM_ALL = true;
+       const PLATFORM_DESKTOP = 'desktop';
+       const PLATFORM_MOBILE = 'mobile';
+
+       const PLATFORM_MODE_STABLE = 'stable';
+       const PLATFORM_MODE_BETA = 'beta';
+
+       public static $VALID_MOBILE_PLATFORMS = array(
+               self::PLATFORM_MODE_STABLE,
+               self::PLATFORM_MODE_BETA,
+       );
+
        /**
         * @var string The friendly name of the survey
         */
@@ -29,6 +41,11 @@
         */
        private $coverage;
 
+       /**
+        * @var array|null The platforms that the survey can be displayed on
+        */
+       private $platform;
+
        public function __construct(
                $name,
                $question,
@@ -41,6 +58,7 @@
                $this->description = $description;
                $this->isEnabled = $isEnabled;
                $this->coverage = $coverage;
+               $this->platform = self::PLATFORM_ALL;
        }
 
        // --------
@@ -80,6 +98,7 @@
                        'description' => $this->description,
                        'module' => $this->getResourceLoaderModuleName(),
                        'coverage' => $this->coverage,
+                       'platform' => $this->platform,
                );
        }
 
@@ -91,4 +110,43 @@
        public function isEnabled() {
                return $this->isEnabled;
        }
+
+       /**
+        * Sets the platforms that the survey can be displayed on
+        *
+        * Platforms are represented by a map of platform name, e.g. 'mobile', 
to:
+        *  <ul>
+        *    <li>a boolean value indicating whether the platform is enabled or 
not<li>
+        *    <li>
+        *      a set of modes that the platform can be in, e.g. 'beta', that 
the survey is
+        *      enabled in
+        *    </li>
+        *  </ul>
+        *
+        * For example, a survey configured as follows would be disabled on 
desktop and enabled on
+        * mobile beta:
+        *
+        * <code><pre>
+        * <?php
+        * $platform = array(
+        *   Survey::PLATFORM_MOBILE => array(
+        *     Survey::PLATFORM_MODE_BETA,
+        *   ),
+        * );
+        * </pre></code>
+        *
+        * @param null|array $platform
+        */
+       public function setPlatform( $platform ) {
+               $this->platform = $platform;
+       }
+
+       /**
+        * Gets the survey's platform configuration
+        *
+        * @return null|array
+        */
+       public function getPlatform() {
+               return $this->platform;
+       }
 }
diff --git a/includes/SurveyFactory.php b/includes/SurveyFactory.php
index a546a39..1e61739 100644
--- a/includes/SurveyFactory.php
+++ b/includes/SurveyFactory.php
@@ -53,11 +53,41 @@
                        $spec['enabled'] = false;
                }
 
-               if ( $spec['type'] === 'internal' ) {
-                       return self::factoryInternal( $spec );
+               $survey = $spec['type'] === 'internal'
+                       ? self::factoryInternal( $spec )
+                       : self::factoryExternal( $spec );
+
+               if ( isset ( $spec['platform'] ) ) {
+                       if ( empty( $spec['platform'] ) ) {
+                               throw new InvalidArgumentException(
+                                       "The \"{$name}\" survey doesn't have 
any platforms specified."
+                               );
+                       }
+
+                       if ( isset ( $spec['platform'][Survey::PLATFORM_MOBILE] 
) ) {
+                               $mobilePlatforms = 
$spec['platform'][Survey::PLATFORM_MOBILE];
+
+                               if (
+                                       !$mobilePlatforms ||
+                                       array_diff(
+                                               $mobilePlatforms,
+                                               array_intersect(
+                                                       
Survey::$VALID_MOBILE_PLATFORMS,
+                                                       $mobilePlatforms
+                                               )
+                                       )
+                               ) {
+                                       throw new InvalidArgumentException(
+                                               "The \"{$name}\" survey doesn't 
have a valid mobile platform. " .
+                                               "Specify 'stable', 'beta', or 
both."
+                                       );
+                               }
+                       }
+
+                       $survey->setPlatform( $spec['platform'] );
                }
 
-               return self::factoryExternal( $spec );
+               return $survey;
        }
 
        private static function factoryExternal( $spec ) {
diff --git a/tests/phpunit/SurveyFactoryPlatformTest.php 
b/tests/phpunit/SurveyFactoryPlatformTest.php
new file mode 100644
index 0000000..d73b8be
--- /dev/null
+++ b/tests/phpunit/SurveyFactoryPlatformTest.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace Tests\QuickSurveys;
+
+use PHPUnit_Framework_TestCase;
+use QuickSurveys\SurveyFactory;
+use QuickSurveys\Survey;
+
+class SurveyFactoryPlatformTest extends PHPUnit_Framework_TestCase
+{
+       private $baseSurveyConfig = array(
+               'name' => 'test',
+               'type' => 'external',
+               'question' => 'Do you like writing unit tests?',
+               'description' => 'A survey for (potential) developers of the 
QuickSurveys extension.',
+               'enabled' => true,
+               'coverage' => 1,
+               'link' => '//example.org/test-external-survey',
+               'privacyPolicy' => 
'ext-quicksurveys-test-external-survey-privacy-policy',
+       );
+
+       public function testItShouldSetThePlatformToAllByDefault() {
+               $survey = SurveyFactory::factory( $this->baseSurveyConfig );
+
+               $this->assertEquals( Survey::PLATFORM_ALL, 
$survey->getPlatform() );
+       }
+
+       public function testItShouldSetThePlatform() {
+               $platform = array(
+                       Survey::PLATFORM_MOBILE => array(
+                               Survey::PLATFORM_MODE_BETA,
+                       ),
+               );
+
+               $survey = SurveyFactory::factory( $this->baseSurveyConfig + 
array(
+                       'platform' => $platform,
+               ) );
+
+               $this->assertEquals( $platform, $survey->getPlatform() );
+       }
+
+       /**
+        * @expectedException InvalidArgumentException
+        * @expectedExceptionMessage The "test" survey doesn't have any 
platforms specified.
+        */
+       public function testItShouldThrowWhenPlatformIsEmpty() {
+               SurveyFactory::factory( $this->baseSurveyConfig + array(
+                       'platform' => array(),
+               ) );
+       }
+
+       /**
+        * @dataProvider provideInvalidMobilePlatform
+        *
+        * @expectedException InvalidArgumentException
+        * @expectedExceptionMessage The "test" survey doesn't have a valid 
mobile platform.
+        *  Specify 'stable', 'beta', or both.
+        */
+       public function testItShouldThrowWhenMobilePlatformIsInvalid( 
$mobilePlatform ) {
+               SurveyFactory::factory( $this->baseSurveyConfig + array(
+                       'platform' => array(
+                               Survey::PLATFORM_MOBILE => $mobilePlatform,
+                       ),
+               ) );
+       }
+
+       public function provideInvalidMobilePlatform() {
+               return array(
+                       array( array() ),
+                       array( array( 'alpha' ) ),
+               );
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I99ffb2c0db33d3ccdd72041dd598fa759f7428d3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/QuickSurveys
Gerrit-Branch: dev
Gerrit-Owner: Phuedx <[email protected]>

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

Reply via email to