mediawiki-extensions-Echo/tests/phpunit/api/Push/ApiEchoPushSubscriptionsCreateTest.php
Michael Holloway 15d48278be Create and enforce a config setting for max subscriptions per user
Creates a EchoPushMaxSubscriptionsPerUser config setting (default: 0)
that controls the maximum number of subscriptions a user may associate
with the user's central user ID.

The setting is enforced in EchoPush\SubscriptionManager::create().

To allow creating push subscriptions for development, set
$wgEchoPushMaxSubscriptionsPerUser to a positive integer value in
LocalSettings.php:

 $wgEchoPushMaxSubscriptionsPerUser = 10;

Bug: T259150
Change-Id: Ib97b6b6cbb8161dd75dad92c54b4fe4fff80c421
2020-08-12 17:58:11 -04:00

60 lines
1.6 KiB
PHP

<?php
/**
* @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'] );
// After max subscriptions reached
$params['providertoken'] = 'DEF456';
$this->expectException( ApiUsageException::class );
$this->doApiRequestWithToken( $params, null, $this->user );
}
public function testApiCreateSubscriptionTokenExists(): void {
$params = [
'action' => 'echopushsubscriptions',
'command' => 'create',
'provider' => 'fcm',
'providertoken' => 'XYZ789',
];
$this->expectException( ApiUsageException::class );
$this->doApiRequestWithToken( $params, null, $this->user );
}
private function createTestData(): void {
$subscriptionManager = EchoServices::getInstance()->getPushSubscriptionManager();
$subscriptionManager->create( $this->user, 'fcm', 'XYZ789' );
}
}