aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/Notification/MiddlewareChainTest.php
blob: c133727d84cba9bee65956ef3d716ba56946e111 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php

namespace MediaWiki\Tests\Notification;

use MediaWiki\Notification\MiddlewareChain;
use MediaWiki\Notification\Notification;
use MediaWiki\Notification\NotificationEnvelope;
use MediaWiki\Notification\NotificationMiddlewareInterface;
use MediaWiki\Notification\NotificationsBatch;
use MediaWiki\Notification\RecipientSet;
use MediaWiki\User\UserIdentity;
use MediaWikiUnitTestCase;

/**
 * @covers \MediaWiki\Notification\MiddlewareChain
 */
class MiddlewareChainTest extends MediaWikiUnitTestCase {

	private function getSut( array $middlewares ): MiddlewareChain {
		$specs = [];
		foreach ( $middlewares as $middleware ) {
			$specs[] = [ 'factory' => static fn () => $middleware ];
		}
		return new MiddlewareChain( $this->createSimpleObjectFactory(), $specs );
	}

	public function testEmptyMiddlewareChainReturnsOriginalBatch() {
		$userIdentity = $this->createMock( UserIdentity::class );
		$notificationToSend = new Notification( 'test', [] );
		$recipients = new RecipientSet( [ $userIdentity ] );

		$envelopes = new NotificationsBatch(
			new NotificationEnvelope( $notificationToSend, $recipients )
		);

		$sut = $this->getSut( [] );
		$this->assertSame( $envelopes, $sut->process( $envelopes ) );
	}

	public function testExecutesInOrderAndModifiesBatch() {
		$userIdentity = $this->createMock( UserIdentity::class );
		$notificationToSend = new Notification( 'test', [] );
		$recipients = new RecipientSet( [ $userIdentity ] );

		$noOpMiddleware = $this->createMock( NotificationMiddlewareInterface::class );
		$noOpMiddleware->expects( $this->exactly( 2 ) )
			->method( 'handle' )
			->willReturnCallback( function ( NotificationsBatch $batch, callable $next ) {
				$this->assertCount( 1, $batch );
				$next();
			} );
		$emptyMiddleware = $this->createMock( NotificationMiddlewareInterface::class );
		$emptyMiddleware->expects( $this->once() )
			->method( 'handle' )
			->willReturnCallback( static function ( NotificationsBatch $batch, callable $next ) {
				foreach ( $batch as $envelope ) {
					$batch->remove( $envelope );
				}
				$next();
			} );
		$makeSureBatchIsEmptyMiddleware = $this->createMock( NotificationMiddlewareInterface::class );
		$makeSureBatchIsEmptyMiddleware->expects( $this->once() )
			->method( 'handle' )
			->willReturnCallback( function ( NotificationsBatch $batch, callable $next ) {
				$this->assertCount( 0, $batch );
				$next();
			} );

		$middlewareChain = $this->getSut( [
			$noOpMiddleware, $noOpMiddleware, $emptyMiddleware, $makeSureBatchIsEmptyMiddleware,
		] );

		$result = $middlewareChain->process( new NotificationsBatch(
			new NotificationEnvelope( $notificationToSend, $recipients )
		) );
		$this->assertCount( 0, $result );
	}

	public function testMiddlewareDoesntCallNext() {
		$userIdentity = $this->createMock( UserIdentity::class );
		$keptNotification = new Notification( 'test', [] );
		$recipients = new RecipientSet( [ $userIdentity ] );

		$suppressWelcomeMiddleware = $this->createMock( NotificationMiddlewareInterface::class );
		$suppressWelcomeMiddleware->expects( $this->once() )
			->method( 'handle' )
			->willReturnCallback( static function ( NotificationsBatch $batch, callable $next ) {
				// no op, but also don't call next
			} );

		$middlewareChain = $this->getSut( [
			$suppressWelcomeMiddleware,
		] );

		$batch = $middlewareChain->process(
			new NotificationsBatch(
				new NotificationEnvelope( $keptNotification, $recipients )
			)
		);
		$this->assertCount( 1, $batch );
		$envelopes = iterator_to_array( $batch );
		$this->assertSame( $keptNotification, $envelopes[0]->getNotification() );
	}

	public function testSupressNotification() {
		$userIdentity = $this->createMock( UserIdentity::class );
		$keptNotification = new Notification( 'test', [] );
		$removedNotification = new Notification( 'welcome', [] );
		$recipients = new RecipientSet( [ $userIdentity ] );

		$suppressWelcomeMiddleware = $this->createMock( NotificationMiddlewareInterface::class );
		$suppressWelcomeMiddleware->expects( $this->once() )
			->method( 'handle' )
			->willReturnCallback( static function ( NotificationsBatch $batch, callable $next ) {
				foreach ( $batch as $envelope ) {
					if ( $envelope->getNotification()->getType() === 'welcome' ) {
						$batch->remove( $envelope );
					}
				}
				$next();
			} );

		$middlewareChain = $this->getSut( [
			$suppressWelcomeMiddleware,
		] );

		$batch = $middlewareChain->process( new NotificationsBatch(
			new NotificationEnvelope( $keptNotification, $recipients ),
			new NotificationEnvelope( $removedNotification, $recipients )
		) );
		$this->assertCount( 1, $batch );
		foreach ( $batch as $envelope ) {
			$this->assertSame( $keptNotification, $envelope->getNotification() );
		}
	}

	/**
	 * 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 testBadMiddlewareTriesToSendNotificationInsteadOfInjectingToBatch() {
		$userIdentity = $this->createMock( UserIdentity::class );
		$keptNotification = new Notification( 'test', [] );
		$recipients = new RecipientSet( [ $userIdentity ] );

		$suppressWelcomeMiddleware = $this->createMock( NotificationMiddlewareInterface::class );
		$suppressWelcomeMiddleware->expects( $this->once() )
			->method( 'handle' )
			->willReturnCallback( static function ( NotificationsBatch $batch, callable $next ) {
				// no op, but also don't call next
			} );

		$middlewareChain = $this->getSut( [
			$suppressWelcomeMiddleware,
		] );

		$batch = $middlewareChain->process(
			new NotificationsBatch(
				new NotificationEnvelope( $keptNotification, $recipients )
			)
		);
		$this->assertCount( 1, $batch );
		$envelopes = iterator_to_array( $batch );
		$this->assertSame( $keptNotification, $envelopes[0]->getNotification() );
	}

}