mediawiki-extensions-Echo/tests/phpunit/Api/Push/ApiEchoPushSubscriptionsCreateTest.php
Fomafix e03f7c3e82 Use overrideConfigValue/overrideConfigValues instead of setMwGlobals
Also use MainConfigNames.

Change-Id: I80d6b3ef9d7b857e35d8f93f424db1cfca1301f6
2024-07-26 12:47:09 +00:00

94 lines
2.9 KiB
PHP

<?php
use MediaWiki\Extension\Notifications\Push\Utils;
use MediaWiki\Extension\Notifications\Services;
use MediaWiki\Tests\Api\ApiTestCase;
use MediaWiki\User\User;
/**
* @group medium
* @group API
* @group Database
* @covers \MediaWiki\Extension\Notifications\Push\Api\ApiEchoPushSubscriptionsCreate
*/
class ApiEchoPushSubscriptionsCreateTest extends ApiTestCase {
/** @var User */
private $user;
protected function setUp(): void {
parent::setUp();
$this->overrideConfigValues( [
'EchoEnablePush' => true,
'EchoPushMaxSubscriptionsPerUser' => 2,
] );
$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 = Services::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 = Services::getInstance()->getPushSubscriptionManager();
$userId = Utils::getPushUserId( $this->user );
$subscriptionManager->create( 'fcm', 'XYZ789', $userId );
}
}