| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\Unit; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Notifications\Bundler; |
| 6 |
use MediaWiki\Extension\Notifications\Model\Notification; |
| 7 |
use MediaWikiUnitTestCase; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class BundlerTest extends MediaWikiUnitTestCase { |
| 13 |
|
| 14 |
public function testBundle() { |
| 15 |
$read = true; |
| 16 |
$unread = false; |
| 17 |
$n1 = $this->createNotificationForBundling( 'bundle_hash_1', 'timestamp_4', $read ); |
| 18 |
$n2 = $this->createNotificationForBundling( 'bundle_hash_1', 'timestamp_1', $read ); |
| 19 |
$n3 = $this->createNotificationForBundling( 'bundle_hash_2', 'timestamp_3', $unread ); |
| 20 |
$n4 = $this->createNotificationForBundling( 'bundle_hash_2', 'timestamp_2', $unread ); |
| 21 |
$n5 = $this->createNotificationForBundling( 'bundle_hash_2', 'timestamp_5', $read ); |
| 22 |
$notifications = [ $n1, $n2, $n3, $n4, $n5 ]; |
| 23 |
|
| 24 |
$bundler = new Bundler(); |
| 25 |
$bundledNotifications = $bundler->bundle( $notifications ); |
| 26 |
|
| 27 |
$this->assertCount( 4, $bundledNotifications ); |
| 28 |
$this->assertSame( $n5, $bundledNotifications[0] ); |
| 29 |
$this->assertSame( $n1, $bundledNotifications[1] ); |
| 30 |
$this->assertSame( $n3, $bundledNotifications[2] ); |
| 31 |
$this->assertCount( 1, $bundledNotifications[2]->getBundledNotifications() ); |
| 32 |
$this->assertSame( $n4, $bundledNotifications[2]->getBundledNotifications()[0] ); |
| 33 |
$this->assertSame( $n2, $bundledNotifications[3] ); |
| 34 |
} |
| 35 |
|
| 36 |
private function createNotificationForBundling( $bundleHash, $timestamp, $readStatus ) { |
| 37 |
$mock = $this->getMockBuilder( Notification::class ) |
| 38 |
->disableOriginalConstructor() |
| 39 |
->onlyMethods( [ |
| 40 |
'getBundlingKey', |
| 41 |
'getSortingKey', |
| 42 |
'canBeBundled', |
| 43 |
] ) |
| 44 |
->getMock(); |
| 45 |
|
| 46 |
$mock->method( 'getBundlingKey' )->willReturn( $bundleHash ); |
| 47 |
$mock->method( 'getSortingKey' )->willReturn( $timestamp ); |
| 48 |
$mock->method( 'canBeBundled' )->willReturn( !$readStatus ); |
| 49 |
|
| 50 |
return $mock; |
| 51 |
} |
| 52 |
|
| 53 |
} |