2014-08-18 22:49:26 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-22 15:31:00 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Test\Integration\Model;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
|
|
|
use MediaWiki\Extension\Notifications\Model\Notification;
|
|
|
|
use MediaWiki\Extension\Notifications\Model\TargetPage;
|
2024-02-22 15:31:00 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2023-08-19 07:48:53 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2023-08-06 20:48:58 +00:00
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
|
|
|
use Wikimedia\Rdbms\LBFactory;
|
2024-04-27 21:37:18 +00:00
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2022-11-02 21:34:17 +00:00
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Model\Notification
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
class NotificationTest extends MediaWikiIntegrationTestCase {
|
2014-08-18 22:49:26 +00:00
|
|
|
|
|
|
|
public function testNewFromRow() {
|
|
|
|
$row = $this->mockNotificationRow() + $this->mockEventRow();
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
$notif = Notification::newFromRow( (object)$row );
|
|
|
|
$this->assertInstanceOf( Notification::class, $notif );
|
2014-08-18 22:49:26 +00:00
|
|
|
// getReadTimestamp() should return null
|
|
|
|
$this->assertNull( $notif->getReadTimestamp() );
|
|
|
|
$this->assertEquals(
|
|
|
|
$notif->getTimestamp(),
|
|
|
|
wfTimestamp( TS_MW, $row['notification_timestamp'] )
|
|
|
|
);
|
2022-11-02 21:34:17 +00:00
|
|
|
$this->assertInstanceOf( Event::class, $notif->getEvent() );
|
2015-03-16 15:47:13 +00:00
|
|
|
$this->assertNull( $notif->getTargetPages() );
|
2014-08-18 22:49:26 +00:00
|
|
|
|
|
|
|
// Provide a read timestamp
|
|
|
|
$row['notification_read_timestamp'] = time() + 1000;
|
2022-11-02 21:34:17 +00:00
|
|
|
$notif = Notification::newFromRow( (object)$row );
|
2014-08-18 22:49:26 +00:00
|
|
|
// getReadTimestamp() should return the timestamp in MW format
|
|
|
|
$this->assertEquals(
|
|
|
|
$notif->getReadTimestamp(),
|
|
|
|
wfTimestamp( TS_MW, $row['notification_read_timestamp'] )
|
|
|
|
);
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
$notif = Notification::newFromRow( (object)$row, [
|
|
|
|
TargetPage::newFromRow( (object)$this->mockTargetPageRow() )
|
2016-12-05 18:51:07 +00:00
|
|
|
] );
|
2018-06-17 16:59:03 +00:00
|
|
|
$this->assertNotEmpty( $notif->getTargetPages() );
|
2015-03-16 15:47:13 +00:00
|
|
|
foreach ( $notif->getTargetPages() as $targetPage ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$this->assertInstanceOf( TargetPage::class, $targetPage );
|
2015-03-16 15:47:13 +00:00
|
|
|
}
|
2014-08-18 22:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewFromRowWithException() {
|
2024-04-27 21:37:18 +00:00
|
|
|
$queryBuilder = $this->createMock( SelectQueryBuilder::class );
|
|
|
|
$queryBuilder->method( $this->logicalOr( 'select', 'from', 'where', 'caller' ) )->willReturnSelf();
|
|
|
|
$queryBuilder->method( 'fetchRow' )->willReturn( false );
|
|
|
|
$db = $this->createMock( IDatabase::class );
|
|
|
|
$db->method( 'newSelectQueryBuilder' )->willReturn( $queryBuilder );
|
2023-08-06 20:48:58 +00:00
|
|
|
$lb = $this->createMock( ILoadBalancer::class );
|
2024-04-27 21:37:18 +00:00
|
|
|
$lb->method( 'getConnection' )->willReturn( $db );
|
2023-08-06 20:48:58 +00:00
|
|
|
$lbFactory = $this->createMock( LBFactory::class );
|
|
|
|
$lbFactory->method( 'getExternalLB' )->willReturn( $lb );
|
|
|
|
$this->setService( 'DBLoadBalancer', $lb );
|
|
|
|
$this->setService( 'DBLoadBalancerFactory', $lbFactory );
|
2014-08-18 22:49:26 +00:00
|
|
|
$row = $this->mockNotificationRow();
|
|
|
|
// Provide an invalid event id
|
|
|
|
$row['notification_event'] = -1;
|
2023-06-09 00:21:09 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2022-11-02 21:34:17 +00:00
|
|
|
Notification::newFromRow( (object)$row );
|
2014-08-18 22:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock a notification row from database
|
|
|
|
*/
|
2024-03-15 08:27:36 +00:00
|
|
|
protected function mockNotificationRow(): array {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2014-08-18 22:49:26 +00:00
|
|
|
'notification_user' => 1,
|
|
|
|
'notification_event' => 1,
|
|
|
|
'notification_timestamp' => time(),
|
2022-01-05 19:08:47 +00:00
|
|
|
'notification_read_timestamp' => null,
|
2014-08-18 22:49:26 +00:00
|
|
|
'notification_bundle_hash' => 'testhash',
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-08-18 22:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock an event row from database
|
|
|
|
*/
|
2024-03-15 08:27:36 +00:00
|
|
|
protected function mockEventRow(): array {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2014-08-18 22:49:26 +00:00
|
|
|
'event_id' => 1,
|
|
|
|
'event_type' => 'test_event',
|
|
|
|
'event_variant' => '',
|
|
|
|
'event_extra' => '',
|
|
|
|
'event_page_id' => '',
|
|
|
|
'event_agent_id' => '',
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_agent_ip' => '',
|
|
|
|
'event_deleted' => 0,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-08-18 22:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock a target page row
|
|
|
|
*/
|
2024-03-15 08:27:36 +00:00
|
|
|
protected function mockTargetPageRow(): array {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2014-08-18 22:49:26 +00:00
|
|
|
'etp_page' => 2,
|
|
|
|
'etp_event' => 1
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-08-18 22:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|