MarkTraceur has uploaded a new change for review.
https://gerrit.wikimedia.org/r/83051
Change subject: Add unit tests for the hooks
......................................................................
Add unit tests for the hooks
Tests whether the hooks run (trivial) and whether they run correctly
and raise appropriate errors.
Change-Id: Ib5c9ff41fbd145c0ae3de6b3729c6a673345e98a
---
M BetaFeatures.php
M BetaFeaturesHooks.php
A tests/HooksRunTest.php
A tests/PreferenceHandlingTest.php
4 files changed, 168 insertions(+), 1 deletion(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BetaFeatures
refs/changes/51/83051/1
diff --git a/BetaFeatures.php b/BetaFeatures.php
index 02ed3e7..0a5392b 100644
--- a/BetaFeatures.php
+++ b/BetaFeatures.php
@@ -32,6 +32,7 @@
$wgHooks['GetPreferences'][] = 'BetaFeaturesHooks::getPreferences';
$wgHooks['PersonalUrls'][] = 'BetaFeaturesHooks::getBetaFeaturesLink';
$wgHooks['GetBetaFeaturePreferences'][] =
'BetaFeaturesHooks::getAutoEnrollPreference';
+$wgHooks['UnitTestsList'][] = 'BetaFeaturesHooks::getUnitTestsList';
$moduleInfo = array(
'localBasePath' => __DIR__,
diff --git a/BetaFeaturesHooks.php b/BetaFeaturesHooks.php
index c542ef6..a45a0aa 100644
--- a/BetaFeaturesHooks.php
+++ b/BetaFeaturesHooks.php
@@ -21,6 +21,9 @@
* @copyright Copyright © 2013, Mark Holmquist
*/
+class BetaFeaturesMissingFieldException extends Exception {
+}
+
class BetaFeaturesHooks {
static function getPreferences( $user, &$prefs ) {
@@ -57,7 +60,7 @@
// we got from the
GetBetaFeaturePreferences hook.
// Don't add this feature to the form.
$complete = false;
- throw new Exception( "The field
{$field} was missing from the beta feature {$key}." );
+ throw new
BetaFeaturesMissingFieldException( "The field {$field} was missing from the
beta feature {$key}." );
}
}
@@ -106,4 +109,10 @@
return true;
}
+
+ static function getUnitTestsList( &$files ) {
+ $testDir = __DIR__ . '/tests';
+ $files = array_merge( $files, glob( "$testDir/*Test.php" ) );
+ return true;
+ }
}
diff --git a/tests/HooksRunTest.php b/tests/HooksRunTest.php
new file mode 100644
index 0000000..85bc8bd
--- /dev/null
+++ b/tests/HooksRunTest.php
@@ -0,0 +1,70 @@
+<?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 HooksRunTest extends MediaWikiTestCase {
+
+ // Key for testing preference
+ const testPrefKey = 'unittest';
+
+ // Structure of testing preference
+ static $testPref = array(
+ 'label-message' => 'nullish',
+ 'desc-message' => 'nullish',
+ 'info-link' =>
'https://mediawiki.org/wiki/Extension:BetaFeatures',
+ 'discussion-link' =>
'https://mediawiki.org/wiki/Extension_talk:BetaFeatures',
+ );
+
+ static function nullHook( $user, &$betaPrefs ) {
+ return true;
+ }
+
+ static function hookThatRegistersPreference( $user, &$betaPrefs ) {
+ $betaPrefs[self::testPrefKey] = self::$testPref;
+ return true;
+ }
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->user = new User;
+ $this->user->addGroup( 'unittesters' );
+ }
+
+ public function testCanRegisterHooks() {
+ global $wgHooks;
+ $this->assertArrayHasKey( 'GetBetaFeaturePreferences',
$wgHooks, 'Hook array does not exist...' );
+ $wgHooks['GetBetaFeaturePreferences'] = array(
'HooksRunTest::nullHook' );
+ // There's no reason for this to not work
+ $this->assertGreaterThan( 0, count(
$wgHooks['GetBetaFeaturePreferences'] ), 'Hook did not get registered.' );
+ }
+
+ public function testHooksRun() {
+ global $wgHooks;
+
+ $wgHooks['GetBetaFeaturePreferences'] = array(
'HooksRunTest::hookThatRegistersPreference' );
+ $prefs = array();
+ wfRunHooks( 'GetBetaFeaturePreferences', array( $this->user,
&$prefs ) );
+ $this->assertArrayHasKey( self::testPrefKey, $prefs, 'Hook did
not run' );
+ $this->assertEquals( $prefs[self::testPrefKey],
self::$testPref, 'The returned preference was not the same as what we
registered.' );
+ }
+}
diff --git a/tests/PreferenceHandlingTest.php b/tests/PreferenceHandlingTest.php
new file mode 100644
index 0000000..68a6476
--- /dev/null
+++ b/tests/PreferenceHandlingTest.php
@@ -0,0 +1,87 @@
+<?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 PreferenceHandlingTest extends MediaWikiTestCase {
+
+ const testPrefKey = 'unittest';
+
+ static function preferenceListing() {
+ $invalidPref = array(
+ 'screenshot' => 'google it bro',
+ );
+
+ $validPref = array(
+ 'label-message' => 'soup-label',
+ 'desc-message' => 'something-something-desc-side',
+ 'info-link' =>
'https://mediawiki.org/wiki/Extension:BetaFeatures/Testing',
+ 'discussion-link' =>
'https://mediawiki.org/wiki/Extension_talk:BetaFeatures/Testing',
+ );
+
+ $validPrefPostHook = $validPref;
+ $validPrefPostHook['class'] = 'HTMLFeatureField';
+ $validPrefPostHook['section'] = 'betafeatures';
+
+ return array(
+ array( 'Invalid preference should cause an error',
$invalidPref, null ),
+ array( 'Totally valid preference should get set
accurately', $validPref, $validPrefPostHook ),
+ );
+ }
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->user = new User;
+ $this->user->addGroup( 'unittesters' );
+ }
+
+ /**
+ * @dataProvider preferenceListing
+ */
+ public function testHandlingOfPreferences( $msg, $pref, $expected ) {
+ global $wgHooks;
+ $prefkey = self::testPrefKey;
+ $prefs = array();
+ $wgHooks['GetBetaFeaturePreferences'] = array( function (
$user, &$prefs ) use ( $pref, $prefkey ) {
+ $prefs[$prefkey] = $pref;
+ return true;
+ } );
+
+ try {
+ wfRunHooks( 'GetPreferences', array( $this->user,
&$prefs ) );
+ } catch ( BetaFeaturesMissingFieldException $e ) {
+ if ( $expected === null ) {
+ $this->assertEquals(
'BetaFeaturesMissingFieldException', get_class( $e ) );
+ return;
+ } else {
+ throw $e;
+ }
+ }
+
+ if ( $expected === null ) {
+ $this->fail( $msg );
+ } else {
+ $this->assertArrayHasKey( $prefkey, $prefs );
+ $this->assertEquals( $expected, $prefs[$prefkey] );
+ }
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/83051
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib5c9ff41fbd145c0ae3de6b3729c6a673345e98a
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