mediawiki-extensions-Echo/tests/phpunit/api/Push/ApiEchoPushSubscriptionsCreateTest.php
Dmitry Brant 6c5a88107c Turn push notification token list into a circular buffer.
At the moment we support a maximum of 10 tokens per user for subscribing
to push notifications, stored as a basic list that runs out when the
limit is reached.  There may, however, be some edge cases where an app
registers a token and then forgets to unregister it (and repeats this 10
times), after which time it will be unable to register any new token.

This changes the token list to behave more like a circular buffer, by
simply deleting the oldest token before inserting the new one. This way
an app could register a new token even in the rare case of forgetting
the previous ten.

Change-Id: I387de63460882e4e56d1aa6db1f78d73a0495208
2021-07-29 17:22:57 -04:00

93 lines
2.8 KiB
PHP

<?php
use EchoPush\Utils;
/**
* @group medium
* @group API
* @group Database
* @covers \EchoPush\Api\ApiEchoPushSubscriptionsCreate
*/
class ApiEchoPushSubscriptionsCreateTest extends ApiTestCase {
/** @var User */
private $user;
public function setUp(): void {
parent::setUp();
$this->setMwGlobals( [
'wgEchoEnablePush' => true,
'wgEchoPushMaxSubscriptionsPerUser' => 2
] );
$this->tablesUsed[] = 'echo_push_subscription';
$this->tablesUsed[] = 'echo_push_provider';
$this->user = $this->getTestUser()->getUser();
$this->createTestData();
}
public function testApiCreateSubscription(): void {
// Before max subscriptions reached
$params = [
'action' => 'echopushsubscriptions',
'command' => 'create',
'provider' => 'fcm',
'providertoken' => 'ABC123',
];
$result = $this->doApiRequestWithToken( $params, null, $this->user );
$this->assertEquals( 'Success', $result[0]['create']['result'] );
// Make sure it's possible to register a new token even when limit is reached
$params['providertoken'] = 'DEF456';
$result = $this->doApiRequestWithToken( $params, null, $this->user );
$this->assertEquals( 'Success', $result[0]['create']['result'] );
// Explicitly verify that the oldest token was removed
$subscriptionManager = EchoServices::getInstance()->getPushSubscriptionManager();
$subscriptions = $subscriptionManager->getSubscriptionsForUser( Utils::getPushUserId( $this->user ) );
foreach ( $subscriptions as $subscription ) {
$this->assertNotEquals( 'XYZ789', $subscription->getToken() );
}
}
public function testApiCreateSubscriptionTokenExists(): void {
$params = [
'action' => 'echopushsubscriptions',
'command' => 'create',
'provider' => 'fcm',
'providertoken' => 'XYZ789',
];
$this->expectException( ApiUsageException::class );
$this->doApiRequestWithToken( $params, null, $this->user );
}
public function testApiCreateApnsSubscriptionWithTopic(): void {
$params = [
'action' => 'echopushsubscriptions',
'command' => 'create',
'provider' => 'apns',
'providertoken' => 'ABC123',
'topic' => 'test',
];
$result = $this->doApiRequestWithToken( $params, null, $this->user );
$this->assertEquals( 'Success', $result[0]['create']['result'] );
}
public function testApiCreateApnsSubscriptionWithoutTopic(): void {
$params = [
'action' => 'echopushsubscriptions',
'command' => 'create',
'provider' => 'apns',
'providertoken' => 'DEF456',
];
$this->expectException( ApiUsageException::class );
$this->doApiRequestWithToken( $params, null, $this->user );
}
private function createTestData(): void {
$subscriptionManager = EchoServices::getInstance()->getPushSubscriptionManager();
$userId = Utils::getPushUserId( $this->user );
$subscriptionManager->create( 'fcm', 'XYZ789', $userId );
}
}