MarkTraceur has uploaded a new change for review.

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


Change subject: Add auto-enroll framework - with nesting and tests
......................................................................

Add auto-enroll framework - with nesting and tests

Auto-enrollment is now tested, and has LAYERS.

VisualEditor can use this to launch multiple features and one
overarching experimental one.

Change-Id: I8f3b5c0b64e2d399f9d0912df9aeae6e6c14f92c
---
M BetaFeaturesHooks.php
A tests/AutoEnrollmentTest.php
2 files changed, 174 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BetaFeatures 
refs/changes/29/84929/1

diff --git a/BetaFeaturesHooks.php b/BetaFeaturesHooks.php
index 5ff8cbb..c656772 100644
--- a/BetaFeaturesHooks.php
+++ b/BetaFeaturesHooks.php
@@ -148,6 +148,15 @@
 
                $counts = self::getUserCounts( array_keys( $betaPrefs ) );
 
+               $autoEnrollAll = $user->getOption( 'beta-feature-auto-enroll' ) 
=== HTMLFeatureField::OPTION_ENABLED;
+               $autoEnroll = array();
+
+               foreach ( $betaPrefs as $key => $info ) {
+                       if ( isset( $info['auto-enrollment'] ) ) {
+                               $autoEnroll[$info['auto-enrollment']] = $key;
+                       }
+               }
+
                foreach ( $betaPrefs as $key => $info ) {
                        $opt = array(
                                'class' => 'HTMLFeatureField',
@@ -183,15 +192,26 @@
                        $prefs[$key] = $opt;
 
                        $currentValue = $user->getOption( $key );
+
+                       $autoEnrollForThisPref = false;
+
+                       if ( isset( $info['group'] ) && isset( 
$autoEnroll[$info['group']] ) ) {
+                               $autoEnrollForThisPref = $user->getOption( 
$autoEnroll[$info['group']] ) === HTMLFeatureField::OPTION_ENABLED;
+                       }
+
+                       $autoEnrollHere = $autoEnrollAll === true || 
$autoEnrollForThisPref === true;
+
                        if ( $currentValue !== HTMLFeatureField::OPTION_ENABLED 
&&
                                        $currentValue !== 
HTMLFeatureField::OPTION_DISABLED &&
-                                       $user->getOption( 
'beta-feature-auto-enroll' ) === HTMLFeatureField::OPTION_ENABLED ) {
+                                       $autoEnrollHere === true ) {
                                // We haven't seen this before, and the user 
has auto-enroll enabled!
                                // Set the option to true.
                                $user->setOption( $key, 
HTMLFeatureField::OPTION_ENABLED );
                        }
                }
 
+               $user->saveSettings();
+
                return true;
        }
 
diff --git a/tests/AutoEnrollmentTest.php b/tests/AutoEnrollmentTest.php
new file mode 100644
index 0000000..83e8ceb
--- /dev/null
+++ b/tests/AutoEnrollmentTest.php
@@ -0,0 +1,153 @@
+<?php
+/*
+ * This file is part of the MediaWiki extension BetaFeatures.
+ *
+ * BetaFeatures is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * BetaFeatures is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with BetaFeatures.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @file
+ * @ingroup extensions
+ * @author Mark Holmquist <[email protected]>
+ * @copyright Copyright © 2013, Mark Holmquist
+ */
+
+class AutoEnrollmentTest extends MediaWikiTestCase {
+
+       // Structure of testing preference
+       static $testPrefs = array(
+               'unittest-all' => array(
+                       'label-message' => 'nullish',
+                       'desc-message' => 'nullish',
+                       'info-link' => 
'https://mediawiki.org/wiki/Extension:BetaFeatures',
+                       'discussion-link' => 
'https://mediawiki.org/wiki/Extension_talk:BetaFeatures',
+                       'auto-enrollment' => 'unittest',
+               ),
+
+               'unittest-ft1' => array(
+                       'label-message' => 'something else',
+                       'desc-message' => 'something even differenter',
+                       'info-link' => 
'https://mediawiki.org/wiki/Extension:BetaFeatures/Auto-enrollment',
+                       'discussion-link' => 
'https://mediawiki.org/wiki/Extension_talk:BetaFeatures/Auto-enrollment',
+                       'group' => 'unittest',
+                       'auto-enrollment' => 'unittest2',
+               ),
+
+               'unittest-ft2' => array(
+                       'label-message' => 'something even more else',
+                       'desc-message' => 'something even more differenter',
+                       'info-link' => 
'https://mediawiki.org/wiki/Extension:BetaFeatures/Auto-enrollment2',
+                       'discussion-link' => 
'https://mediawiki.org/wiki/Extension_talk:BetaFeatures/Auto-enrollment2',
+                       'group' => 'unittest2',
+               ),
+       );
+
+       static function hookThatRegistersPreference( $user, &$betaPrefs ) {
+               foreach ( self::$testPrefs as $key => $testPref ) {
+                       $betaPrefs[$key] = $testPref;
+               }
+
+               return true;
+       }
+
+       static function getTestData() {
+               return array(
+                       array(
+                               null,
+                               null,
+                               'unittest-ft1',
+                               null,
+                               'Hooks set the preference though auto-enroll 
was not set.'
+                       ),
+
+                       array(
+                               'beta-feature-auto-enroll',
+                               HTMLFeatureField::OPTION_ENABLED,
+                               'unittest-ft1',
+                               HTMLFeatureField::OPTION_ENABLED,
+                               'Hooks did not set the preference though global 
auto-enroll was set.',
+                       ),
+
+                       array(
+                               null,
+                               null,
+                               'unittest-ft1',
+                               null,
+                               'Hooks set the preference though group 
auto-enroll was not set.'
+                       ),
+
+                       array(
+                               'unittest-all',
+                               HTMLFeatureField::OPTION_ENABLED,
+                               'unittest-ft1',
+                               HTMLFeatureField::OPTION_ENABLED,
+                               'Hooks did not set the preference though group 
auto-enroll was set.',
+                       ),
+
+                       array(
+                               null,
+                               null,
+                               'unittest-ft2',
+                               null,
+                               'Hooks set the preference though no auto-enroll 
was set.'
+                       ),
+
+                       array(
+                               'unittest-all',
+                               HTMLFeatureField::OPTION_ENABLED,
+                               'unittest-ft2',
+                               HTMLFeatureField::OPTION_ENABLED,
+                               'Hooks did not set the preference though 
grandparent group auto-enroll was set.',
+                       ),
+
+                       array(
+                               'beta-feature-auto-enroll',
+                               HTMLFeatureField::OPTION_ENABLED,
+                               'unittest-ft2',
+                               HTMLFeatureField::OPTION_ENABLED,
+                               'Hooks did not set the preference though global 
auto-enroll was set.',
+                       ),
+               );
+       }
+
+       protected function setUp() {
+               global $wgHooks;
+
+               parent::setUp();
+
+               $wgHooks['GetBetaFeaturePreferences'] = array( 
'AutoEnrollmentTest::hookThatRegistersPreference' );
+
+               $this->user = new User;
+               $this->user->addGroup( 'unittesters' );
+       }
+
+       /**
+        * @dataProvider getTestData
+        */
+       public function testAutoEnroll( $set, $setVal, $check, $expected, $msg 
) {
+               $user = $this->user;
+               $prefs = array();
+
+               if ( $set !== null ) {
+                       $user->setOption( $set, $setVal );
+               }
+
+               wfRunHooks( 'GetPreferences', array( $user, &$prefs ) );
+
+               $value = $user->getOption( $check );
+               if ( $expected === null ) {
+                       $this->assertNull( $value, $msg );
+               } else {
+                       $this->assertEquals( $expected, $value, $msg );
+               }
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8f3b5c0b64e2d399f9d0912df9aeae6e6c14f92c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BetaFeatures
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <[email protected]>

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

Reply via email to