diff options
author | Piotr Miazga <pmiazga@wikimedia.org> | 2025-03-06 14:48:12 +0100 |
---|---|---|
committer | Piotr Miazga <pmiazga@wikimedia.org> | 2025-03-21 10:50:14 +0100 |
commit | f1e88be974500910c79ad0dd9a2cf4a454d24b4d (patch) | |
tree | 681a41dd81136fb3609b071d2ccce8ad5a436af7 /tests/phpunit/integration/includes/Notification/MiddlewareChainTest.php | |
parent | 35bbe148a98cb4f119524cbbf52dd0e96ec0f4dc (diff) | |
download | mediawikicore-f1e88be974500910c79ad0dd9a2cf4a454d24b4d.tar.gz mediawikicore-f1e88be974500910c79ad0dd9a2cf4a454d24b4d.zip |
notifications: Introduce Notification Middleware and NotificationEnvelope
To allow ourselves esier processing/modifying Notifications lets
introduce an idea of NotificationsEnvelope which represents a
Notification being sent and list of recipients.
The middleware approach will allow us to modify the Notification
behaviour by letting extensions to inject/modify the Notifications.
Each Middleware will retrieve a list of Envelopes MediaWiki wants to
send. Middlewares should iterate over envelopes and decide if those
want to add/remove/replace Notifications and/or Recipients.
Bug: T387996
Change-Id: Ib3ee35c75b2f4dcfdc516b9259a852dc73c4a778
Diffstat (limited to 'tests/phpunit/integration/includes/Notification/MiddlewareChainTest.php')
-rw-r--r-- | tests/phpunit/integration/includes/Notification/MiddlewareChainTest.php | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/phpunit/integration/includes/Notification/MiddlewareChainTest.php b/tests/phpunit/integration/includes/Notification/MiddlewareChainTest.php new file mode 100644 index 000000000000..4fda5639b16b --- /dev/null +++ b/tests/phpunit/integration/includes/Notification/MiddlewareChainTest.php @@ -0,0 +1,74 @@ +<?php +/** + * This program 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. + * + * This program 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +namespace MediaWiki\Tests\Integration\Notification; + +use MediaWiki\MediaWikiServices; +use MediaWiki\Notification\MiddlewareException; +use MediaWiki\Notification\Notification; +use MediaWiki\Notification\NotificationMiddlewareInterface; +use MediaWiki\Notification\RecipientSet; +use MediaWiki\Registration\ExtensionRegistry; +use MediaWiki\User\UserIdentity; +use MediaWikiIntegrationTestCase; +use Wikimedia\ScopedCallback; + +/** + * @group Mail + * @covers \MediaWiki\Notification\MiddlewareChain + */ +class MiddlewareChainTest extends MediaWikiIntegrationTestCase { + + /** + * Test case when Middleware calls NotificationService::notify() to inject new notification + * This can cause endless loops where Middleware triggers a notification, that triggers the + * middleware again. + * + * @return void + */ + public function testMiddlewareCannotTriggerNotificationService() { + $this->expectException( MiddlewareException::class ); + + $scope = ExtensionRegistry::getInstance()->setAttributeForTest( + 'NotificationMiddleware', [ + [ + "factory" => static function () { + return new class implements NotificationMiddlewareInterface { + public function handle( $batch, callable $next ): void { + MediaWikiServices::getInstance() + ->getNotificationService() + ->notify( + new Notification( "bad" ), new RecipientSet( [] ) + ); + $next(); + } + }; + }, + ], + ] + ); + $sut = MediaWikiServices::getInstance()->getNotificationService(); + $user = $this->createMock( UserIdentity::class ); + $sut->notify( + new Notification( "good" ), new RecipientSet( [ $user ] ) + ); + ScopedCallback::consume( $scope ); + } +} |