2020-05-15 17:19:03 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-22 15:31:00 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Test\API;
|
|
|
|
|
|
|
|
use MediaWiki\Api\ApiUsageException;
|
2022-04-08 00:38:27 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Push\Utils;
|
2022-11-13 07:53:42 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Services;
|
2024-06-12 18:31:47 +00:00
|
|
|
use MediaWiki\Tests\Api\ApiTestCase;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2020-08-11 20:34:21 +00:00
|
|
|
|
2020-05-15 17:19:03 +00:00
|
|
|
/**
|
|
|
|
* @group medium
|
|
|
|
* @group API
|
|
|
|
* @group Database
|
2022-04-08 00:47:34 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Push\Api\ApiEchoPushSubscriptionsCreate
|
2020-05-15 17:19:03 +00:00
|
|
|
*/
|
|
|
|
class ApiEchoPushSubscriptionsCreateTest extends ApiTestCase {
|
|
|
|
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
2022-09-29 13:41:35 +00:00
|
|
|
protected function setUp(): void {
|
2020-05-15 17:19:03 +00:00
|
|
|
parent::setUp();
|
2024-07-26 12:45:47 +00:00
|
|
|
$this->overrideConfigValues( [
|
|
|
|
'EchoEnablePush' => true,
|
|
|
|
'EchoPushMaxSubscriptionsPerUser' => 2,
|
2020-08-12 21:33:06 +00:00
|
|
|
] );
|
2020-05-15 17:19:03 +00:00
|
|
|
$this->user = $this->getTestUser()->getUser();
|
|
|
|
$this->createTestData();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApiCreateSubscription(): void {
|
2020-08-12 21:33:06 +00:00
|
|
|
// Before max subscriptions reached
|
2020-05-15 17:19:03 +00:00
|
|
|
$params = [
|
|
|
|
'action' => 'echopushsubscriptions',
|
|
|
|
'command' => 'create',
|
|
|
|
'provider' => 'fcm',
|
|
|
|
'providertoken' => 'ABC123',
|
|
|
|
];
|
|
|
|
$result = $this->doApiRequestWithToken( $params, null, $this->user );
|
|
|
|
$this->assertEquals( 'Success', $result[0]['create']['result'] );
|
2020-08-12 21:33:06 +00:00
|
|
|
|
2021-07-23 13:23:31 +00:00
|
|
|
// Make sure it's possible to register a new token even when limit is reached
|
2020-08-12 21:33:06 +00:00
|
|
|
$params['providertoken'] = 'DEF456';
|
2021-07-23 13:23:31 +00:00
|
|
|
$result = $this->doApiRequestWithToken( $params, null, $this->user );
|
|
|
|
$this->assertEquals( 'Success', $result[0]['create']['result'] );
|
|
|
|
|
|
|
|
// Explicitly verify that the oldest token was removed
|
2022-11-13 07:53:42 +00:00
|
|
|
$subscriptionManager = Services::getInstance()->getPushSubscriptionManager();
|
2021-07-23 13:23:31 +00:00
|
|
|
$subscriptions = $subscriptionManager->getSubscriptionsForUser( Utils::getPushUserId( $this->user ) );
|
|
|
|
foreach ( $subscriptions as $subscription ) {
|
|
|
|
$this->assertNotEquals( 'XYZ789', $subscription->getToken() );
|
|
|
|
}
|
2020-05-15 17:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testApiCreateSubscriptionTokenExists(): void {
|
|
|
|
$params = [
|
|
|
|
'action' => 'echopushsubscriptions',
|
|
|
|
'command' => 'create',
|
|
|
|
'provider' => 'fcm',
|
|
|
|
'providertoken' => 'XYZ789',
|
|
|
|
];
|
|
|
|
$this->expectException( ApiUsageException::class );
|
|
|
|
$this->doApiRequestWithToken( $params, null, $this->user );
|
|
|
|
}
|
|
|
|
|
2020-08-03 18:27:47 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:19:03 +00:00
|
|
|
private function createTestData(): void {
|
2022-11-13 07:53:42 +00:00
|
|
|
$subscriptionManager = Services::getInstance()->getPushSubscriptionManager();
|
2020-08-11 20:34:21 +00:00
|
|
|
$userId = Utils::getPushUserId( $this->user );
|
|
|
|
$subscriptionManager->create( 'fcm', 'XYZ789', $userId );
|
2020-05-15 17:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|