Sbisson has uploaded a new change for review. https://gerrit.wikimedia.org/r/315702
Change subject: [WIP] phpunit API tests for get notifications ...................................................................... [WIP] phpunit API tests for get notifications Change-Id: Ie3a79b4158b20736d8932582d9698f93213b07d0 --- M tests/phpunit/api/ApiEchoNotificationsTest.php 1 file changed, 123 insertions(+), 47 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo refs/changes/02/315702/1 diff --git a/tests/phpunit/api/ApiEchoNotificationsTest.php b/tests/phpunit/api/ApiEchoNotificationsTest.php index 3ce3726..7114c73 100644 --- a/tests/phpunit/api/ApiEchoNotificationsTest.php +++ b/tests/phpunit/api/ApiEchoNotificationsTest.php @@ -7,62 +7,138 @@ */ class ApiEchoNotificationsTest extends ApiTestCase { - public function testWithSectionGrouping() { - // Grouping by section - $data = $this->doApiRequest( array( - 'action' => 'query', - 'meta' => 'notifications', - 'notsections' => 'alert|message', - 'notgroupbysection' => 1, - 'notlimit' => 10, - 'notprop' => 'list|count' ) ); + protected $notifications = []; - $this->assertArrayHasKey( 'query', $data[0] ); - $this->assertArrayHasKey( 'notifications', $data[0]['query'] ); + public function setUp() { + parent::setUp(); - $result = $data[0]['query']['notifications']; + $this->tablesUsed = [ 'echo_event', 'echo_notification' ]; - // General count - $this->assertArrayHasKey( 'count', $result ); - $this->assertArrayHasKey( 'rawcount', $result ); + self::$users['NotifUser'] = $notifUser = new TestUser( 'NotifUser' ); + RequestContext::getMain()->setTitle( Title::newFromText( 'Whatever' ) ); - // Alert - $this->assertArrayHasKey( 'alert', $result ); - $alert = $result['alert']; - $this->assertArrayHasKey( 'list', $alert ); - $this->assertArrayHasKey( 'continue', $alert ); - $this->assertArrayHasKey( 'rawcount', $alert ); - $this->assertArrayHasKey( 'count', $alert ); + global $wgEchoNotifications; + $wgEchoNotifications['unit-test'] = [ + 'section' => 'alert', + 'user-locators' => [ + function () use ( $notifUser ) { return [ $notifUser->getUser() ]; } + ] + ]; - // Message - $this->assertArrayHasKey( 'message', $result ); - $message = $result['message']; - $this->assertArrayHasKey( 'list', $message ); - $this->assertArrayHasKey( 'continue', $message ); - $this->assertArrayHasKey( 'rawcount', $message ); - $this->assertArrayHasKey( 'count', $message ); + $this->doLogin( 'NotifUser' ); + + $this->notifications['read1'] = EchoEvent::create( [ + 'type' => 'unit-test', + 'agent' => self::$users['sysop']->getUser(), + ] ); + + $this->notifications['read2'] = EchoEvent::create( [ + 'type' => 'unit-test', + 'agent' => self::$users['sysop']->getUser(), + ] ); + + $this->doApiRequestWithToken( [ + 'action' => 'echomarkread', + 'all' => true, + ], [ 'wsToken' => 'token' ] ); + + $this->notifications['unread1'] = EchoEvent::create( [ + 'type' => 'unit-test', + 'agent' => self::$users['sysop']->getUser(), + ] ); } - public function testWithoutSectionGrouping() { - $data = $this->doApiRequest( array( + public function testUnreadCount() { + // unread alert count should be 1 + $response = $this->getNotifications( [ + 'notsections' => 'alert', + 'notprop' => 'count', + ] ); + $this->assertEquals( 1, $response['count'] ); + $this->assertEquals( 1, $response['rawcount'] ); + + // unread message count should be 0 + $response = $this->getNotifications( [ + 'notsections' => 'message', + 'notprop' => 'count', + ] ); + $this->assertEquals( 0, $response['count'] ); + $this->assertEquals( 0, $response['rawcount'] ); + + // unread count for both with section grouping + $response = $this->getNotifications( [ + 'notsections' => 'alert|message', + 'notgroupbysection' => 1, + 'notprop' => 'count', + ] ); + $this->assertEquals( 1, $response['count'] ); + $this->assertEquals( 1, $response['rawcount'] ); + $this->assertEquals( 1, $response['alert']['count'] ); + $this->assertEquals( 1, $response['alert']['rawcount'] ); + $this->assertEquals( 0, $response['message']['count'] ); + $this->assertEquals( 0, $response['message']['rawcount'] ); + } + + public function testFilter() { + // get only unread + $response = $this->getNotifications( [ + 'notsections' => 'alert|message', + 'notfilter' => '!read', + ] ); + $this->assertCount( 1, $response['list'] ); + $this->assertEquals( $this->notifications['unread1']->getId(), reset( $response['list'] )['id'] ); + + // get only read + $response = $this->getNotifications( [ + 'notsections' => 'alert|message', + 'notfilter' => 'read', + ] ); + $this->assertCount( 2, $response['list'] ); + $expectedNotificationIds = [ + $this->notifications['read1']->getId(), + $this->notifications['read2']->getId(), + ]; + $actualNotificationIds = array_map( function ( $notif ) { + return $notif['id']; + }, $response['list'] ); + $this->assertArrayEquals( $expectedNotificationIds, $actualNotificationIds ); + + // get both read and unread + $response = $this->getNotifications( [ + 'notsections' => 'alert|message', + 'notfilter' => '!read|read', + ] ); + $this->assertCount( 3, $response['list'] ); + } + + public function testLimitAndContinue() { + // first "page" + $response = $this->getNotifications( [ + 'notunreadfirst' => 1, + 'notlimit' => 1, + ] ); + $this->assertCount( 1, $response['list'] ); + // most recent notification is 'unread1' + $this->assertEquals( $this->notifications['unread1']->getId(), $response['list'][0]['id'] ); + $continue = $response['continue']; + + // second "page" + $response = $this->getNotifications( [ + 'notunreadfirst' => 1, + 'notlimit' => 1, + 'notcontinue' => $continue, + ] ); + $this->assertCount( 1, $response['list'] ); + // second most recent notification is 'read2' + $this->assertEquals( $this->notifications['read2']->getId(), $response['list'][0]['id'] ); + } + + protected function getNotifications( $params = [] ) { + $data = $this->doApiRequest( [ 'action' => 'query', 'meta' => 'notifications', - 'notsections' => 'alert|message', - 'notlimit' => 10, - 'notprop' => 'list|count' ) ); - - $this->assertArrayHasKey( 'query', $data[0] ); - $this->assertArrayHasKey( 'notifications', $data[0]['query'] ); - - $result = $data[0]['query']['notifications']; - - $this->assertArrayHasKey( 'count', $result ); - $this->assertArrayHasKey( 'rawcount', $result ); - $this->assertArrayHasKey( 'list', $result ); - $this->assertArrayHasKey( 'continue', $result ); - - $this->assertTrue( !isset( $result['alert'] ) ); - $this->assertTrue( !isset( $result['message'] ) ); + ] + $params ); + return $data[0]['query']['notifications']; } } -- To view, visit https://gerrit.wikimedia.org/r/315702 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie3a79b4158b20736d8932582d9698f93213b07d0 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Echo Gerrit-Branch: master Gerrit-Owner: Sbisson <sbis...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits