Matěj Suchánek has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/356374 )

Change subject: Fix default notification preference
......................................................................

Fix default notification preference

Making it default preference was wrong because default preferences
are fallback for missing preferences, whereas it was missing for
all users when the feature was deployed.

Bug: T166657
Change-Id: I5a475b35c5b8392f799b4ff8f64de684b9833544
---
M client/WikibaseClient.hooks.php
M client/includes/Hooks/EchoNotificationsHandlers.php
M client/tests/phpunit/includes/Hooks/EchoNotificationsHandlersTest.php
3 files changed, 60 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/74/356374/1

diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index 646bbc0..850a46e 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -458,7 +458,7 @@
 
                // These hooks should only be run if we use the Echo extension
                if ( class_exists( EchoEvent::class ) ) {
-                       $wgHooks['UserGetDefaultOptions'][] = 
EchoNotificationsHandlers::class . '::onUserGetDefaultOptions';
+                       $wgHooks['LocalUserCreated'][] = 
EchoNotificationsHandlers::class . '::onLocalUserCreated';
                        $wgHooks['WikibaseHandleChange'][] = 
EchoNotificationsHandlers::class . '::onWikibaseHandleChange';
                }
 
diff --git a/client/includes/Hooks/EchoNotificationsHandlers.php 
b/client/includes/Hooks/EchoNotificationsHandlers.php
index 35842dc..3c0ff36 100644
--- a/client/includes/Hooks/EchoNotificationsHandlers.php
+++ b/client/includes/Hooks/EchoNotificationsHandlers.php
@@ -165,22 +165,24 @@
        }
 
        /**
-        * Handler for UserGetDefaultOptions hook
-        * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/UserGetDefaultOptions
-        *
-        * @param bool[] &$defaultOptions Array of preference keys and their 
default values.
+        * Handler for LocalUserCreated hook.
+        * @see https://www.mediawiki.org/wiki/Manual:Hooks/LocalUserCreated
+        * @param User $user User object that was created.
+        * @param bool $autocreated True when account was auto-created
         */
-       public static function onUserGetDefaultOptions( array &$defaultOptions 
) {
+       public static function onLocalUserCreated( User $user, $autocreated ) {
                $self = self::newFromGlobalState();
-               $self->doUserGetDefaultOptions( $defaultOptions );
+               $self->doLocalUserCreated( $user, $autocreated );
        }
 
        /**
-        * @param bool[] &$defaultOptions
+        * @param User $user
+        * @param bool $autocreated
         */
-       public function doUserGetDefaultOptions( array &$defaultOptions ) {
+       public function doLocalUserCreated( User $user, $autocreated ) {
                if ( $this->sendEchoNotification === true ) {
-                       
$defaultOptions['echo-subscriptions-web-wikibase-action'] = true;
+                       $user->setOption( 
'echo-subscriptions-web-wikibase-action', true );
+                       $user->saveSettings();
                }
        }
 
diff --git 
a/client/tests/phpunit/includes/Hooks/EchoNotificationsHandlersTest.php 
b/client/tests/phpunit/includes/Hooks/EchoNotificationsHandlersTest.php
index c120e44..6594d11 100644
--- a/client/tests/phpunit/includes/Hooks/EchoNotificationsHandlersTest.php
+++ b/client/tests/phpunit/includes/Hooks/EchoNotificationsHandlersTest.php
@@ -5,6 +5,7 @@
 use EchoEvent;
 use MediaWikiTestCase;
 use Title;
+use User;
 use Wikibase\ChangeRow;
 use Wikibase\Client\Hooks\EchoNotificationsHandlers;
 use Wikibase\Client\RepoLinker;
@@ -199,7 +200,7 @@
                $this->assertSame( $register, isset( 
$notifications[$handlers::NOTIFICATION_TYPE] ) );
                $this->assertSame( $register, isset( 
$categories['wikibase-action'] ) );
                $this->assertSame( $register, isset( 
$icons[$handlers::NOTIFICATION_TYPE] ) );
-
+               
                if ( $register ) {
                        if ( isset( $expectedIcon['path'] ) ) {
                                $this->assertSame(
@@ -219,4 +220,50 @@
                }
        }
 
+       public function localUserCreatedProvider() {
+               return [
+                       'disabled no auto' => [
+                               'enabled' => false,
+                               'times' => 0,
+                               'auto' => false,
+                       ],
+                       'disabled auto' => [
+                               'enabled' => false,
+                               'times' => 0,
+                               'auto' => true,
+                       ],
+                       'enabled no auto' => [
+                               'enabled' => true,
+                               'times' => 1,
+                               'auto' => false,
+                       ],
+                       'enabled auto' => [
+                               'enabled' => true,
+                               'times' => 1,
+                               'auto' => true,
+                       ]
+               ];
+       }
+
+       /**
+        * @dataProvider localUserCreatedProvider
+        */
+       public function testLocalUserCreated( $enabled, $times, $auto ) {
+               $handlers = new EchoNotificationsHandlers(
+                       $this->repoLinker,
+                       'enwiki',
+                       $enabled,
+                       '',
+                       'repoSiteName'
+               );
+
+               $user = $this->createMock( User::class );
+               $user->expects( $this->exactly( $times ) )
+                       ->method( 'setOption' );
+               $user->expects( $this->exactly( $times ) )
+                       ->method( 'saveSettings' );
+
+               $handlers->doLocalUserCreated( $user, $auto );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5a475b35c5b8392f799b4ff8f64de684b9833544
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Matěj Suchánek <[email protected]>

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

Reply via email to