Debian Codegraph @

extensions/Echo/tests/phpunit/Model/NotificationTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Test\Integration\Model;
4
5 use InvalidArgumentException;
6 use MediaWiki\Extension\Notifications\Model\Event;
7 use MediaWiki\Extension\Notifications\Model\Notification;
8 use MediaWiki\Extension\Notifications\Model\TargetPage;
9 use MediaWikiIntegrationTestCase;
10 use Wikimedia\Rdbms\IDatabase;
11 use Wikimedia\Rdbms\ILoadBalancer;
12 use Wikimedia\Rdbms\LBFactory;
13 use Wikimedia\Rdbms\SelectQueryBuilder;
14
15 /**
16 * @covers \MediaWiki\Extension\Notifications\Model\Notification
17 */
18 class NotificationTest extends MediaWikiIntegrationTestCase {
19
20 public function testNewFromRow() {
21 $row = $this->mockNotificationRow() + $this->mockEventRow();
22
23 $notif = Notification::newFromRow( (object)$row );
24 $this->assertInstanceOf( Notification::class, $notif );
25 // getReadTimestamp() should return null
26 $this->assertNull( $notif->getReadTimestamp() );
27 $this->assertEquals(
28 $notif->getTimestamp(),
29 wfTimestamp( TS_MW, $row['notification_timestamp'] )
30 );
31 $this->assertInstanceOf( Event::class, $notif->getEvent() );
32 $this->assertNull( $notif->getTargetPages() );
33
34 // Provide a read timestamp
35 $row['notification_read_timestamp'] = time() + 1000;
36 $notif = Notification::newFromRow( (object)$row );
37 // getReadTimestamp() should return the timestamp in MW format
38 $this->assertEquals(
39 $notif->getReadTimestamp(),
40 wfTimestamp( TS_MW, $row['notification_read_timestamp'] )
41 );
42
43 $notif = Notification::newFromRow( (object)$row, [
44 TargetPage::newFromRow( (object)$this->mockTargetPageRow() )
45 ] );
46 $this->assertNotEmpty( $notif->getTargetPages() );
47 foreach ( $notif->getTargetPages() as $targetPage ) {
48 $this->assertInstanceOf( TargetPage::class, $targetPage );
49 }
50 }
51
52 public function testNewFromRowWithException() {
53 $queryBuilder = $this->createMock( SelectQueryBuilder::class );
54 $queryBuilder->method( $this->logicalOr( 'select', 'from', 'where', 'caller' ) )->willReturnSelf();
55 $queryBuilder->method( 'fetchRow' )->willReturn( false );
56 $db = $this->createMock( IDatabase::class );
57 $db->method( 'newSelectQueryBuilder' )->willReturn( $queryBuilder );
58 $lb = $this->createMock( ILoadBalancer::class );
59 $lb->method( 'getConnection' )->willReturn( $db );
60 $lbFactory = $this->createMock( LBFactory::class );
61 $lbFactory->method( 'getExternalLB' )->willReturn( $lb );
62 $this->setService( 'DBLoadBalancer', $lb );
63 $this->setService( 'DBLoadBalancerFactory', $lbFactory );
64 $row = $this->mockNotificationRow();
65 // Provide an invalid event id
66 $row['notification_event'] = -1;
67 $this->expectException( InvalidArgumentException::class );
68 Notification::newFromRow( (object)$row );
69 }
70
71 /**
72 * Mock a notification row from database
73 */
74 protected function mockNotificationRow(): array {
75 return [
76 'notification_user' => 1,
77 'notification_event' => 1,
78 'notification_timestamp' => time(),
79 'notification_read_timestamp' => null,
80 'notification_bundle_hash' => 'testhash',
81 ];
82 }
83
84 /**
85 * Mock an event row from database
86 */
87 protected function mockEventRow(): array {
88 return [
89 'event_id' => 1,
90 'event_type' => 'test_event',
91 'event_variant' => '',
92 'event_extra' => '',
93 'event_page_id' => '',
94 'event_agent_id' => '',
95 'event_agent_ip' => '',
96 'event_deleted' => 0,
97 ];
98 }
99
100 /**
101 * Mock a target page row
102 */
103 protected function mockTargetPageRow(): array {
104 return [
105 'etp_page' => 2,
106 'etp_event' => 1
107 ];
108 }
109
110 }