aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/Notification/NotificationBatchTest.php
diff options
context:
space:
mode:
authorPiotr Miazga <pmiazga@wikimedia.org>2025-03-06 14:48:12 +0100
committerPiotr Miazga <pmiazga@wikimedia.org>2025-03-21 10:50:14 +0100
commitf1e88be974500910c79ad0dd9a2cf4a454d24b4d (patch)
tree681a41dd81136fb3609b071d2ccce8ad5a436af7 /tests/phpunit/unit/includes/Notification/NotificationBatchTest.php
parent35bbe148a98cb4f119524cbbf52dd0e96ec0f4dc (diff)
downloadmediawikicore-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/unit/includes/Notification/NotificationBatchTest.php')
-rw-r--r--tests/phpunit/unit/includes/Notification/NotificationBatchTest.php94
1 files changed, 94 insertions, 0 deletions
diff --git a/tests/phpunit/unit/includes/Notification/NotificationBatchTest.php b/tests/phpunit/unit/includes/Notification/NotificationBatchTest.php
new file mode 100644
index 000000000000..4cda67741215
--- /dev/null
+++ b/tests/phpunit/unit/includes/Notification/NotificationBatchTest.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace MediaWiki\Tests\Notification;
+
+use MediaWiki\Notification\Notification;
+use MediaWiki\Notification\NotificationEnvelope;
+use MediaWiki\Notification\NotificationsBatch;
+use MediaWiki\Notification\RecipientSet;
+use MediaWikiUnitTestCase;
+use stdClass;
+
+/**
+ * @covers \MediaWiki\Notification\NotificationsBatch
+ * @covers \MediaWiki\Notification\NotificationEnvelope
+ */
+class NotificationBatchTest extends MediaWikiUnitTestCase {
+
+ public function testAdds() {
+ $first = new NotificationEnvelope( new Notification( 'first' ), new RecipientSet( [] ) );
+ $second = new NotificationEnvelope( new Notification( 'second' ), new RecipientSet( [] ) );
+
+ $batch = new NotificationsBatch( $first );
+ $batch->add( $second );
+
+ $this->assertCount( 2, $batch );
+ $envelopesArray = iterator_to_array( $batch );
+ $this->assertSame( $first, $envelopesArray[0] );
+ $this->assertSame( $second, $envelopesArray[1] );
+ }
+
+ public function testRemoveWhenExists() {
+ $first = new NotificationEnvelope( new Notification( 'first' ), new RecipientSet( [] ) );
+ $second = new NotificationEnvelope( new Notification( 'second' ), new RecipientSet( [] ) );
+ $third = new NotificationEnvelope( new Notification( 'third' ), new RecipientSet( [] ) );
+ $last = new NotificationEnvelope( new Notification( 'fourth' ), new RecipientSet( [] ) );
+
+ $batch = new NotificationsBatch( $first, $second, $third, $last );
+ $this->assertCount( 4, $batch );
+
+ $batch->remove( $second );
+ $this->assertCount( 3, $batch );
+ $envelopesArray = iterator_to_array( $batch );
+ $this->assertSame( $first, $envelopesArray[0] );
+ $this->assertSame( $third, $envelopesArray[1] );
+ $this->assertSame( $last, $envelopesArray[2] );
+
+ $batch->remove( $first );
+ $this->assertCount( 2, $batch );
+ $envelopesArray = iterator_to_array( $batch );
+ $this->assertSame( $third, $envelopesArray[0] );
+ $this->assertSame( $last, $envelopesArray[1] );
+
+ $batch->remove( $last );
+ $this->assertCount( 1, $batch );
+ $envelopesArray = iterator_to_array( $batch );
+ $this->assertSame( $third, $envelopesArray[0] );
+ }
+
+ public function testRemovesWhenNotExists() {
+ $first = new NotificationEnvelope( new Notification( 'first' ), new RecipientSet( [] ) );
+ $second = new NotificationEnvelope( new Notification( 'second' ), new RecipientSet( [] ) );
+ $other = new NotificationEnvelope( new Notification( 'third' ), new RecipientSet( [] ) );
+
+ $batch = new NotificationsBatch( $first, $second );
+ $batch->remove( $other );
+
+ $this->assertCount( 2, $batch );
+ $envelopesArray = iterator_to_array( $batch );
+ $this->assertSame( $first, $envelopesArray[0] );
+ $this->assertSame( $second, $envelopesArray[1] );
+ }
+
+ public function testFilters() {
+ $first = new NotificationEnvelope( new Notification( 'first' ), new RecipientSet( [] ) );
+ $second = new NotificationEnvelope( new Notification( 'second' ), new RecipientSet( [] ) );
+
+ $filterMock = $this->getMockBuilder( stdClass::class )
+ ->addMethods( [ '__invoke' ] )
+ ->getMock();
+
+ $filterMock->expects( $this->exactly( 2 ) )
+ ->method( '__invoke' )
+ ->willReturnCallback( static function ( NotificationEnvelope $envelope ) use ( $first ) {
+ return $envelope->equals( $first );
+ } );
+
+ $batch = new NotificationsBatch( $first, $second );
+ $batch->filter( $filterMock );
+ $this->assertCount( 1, $batch );
+ $envelopesArray = iterator_to_array( $batch );
+ $this->assertSame( $first, $envelopesArray[0] );
+ }
+
+}