| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test\API; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiUsageException; |
| 6 |
use MediaWiki\Extension\Notifications\Push\Utils; |
| 7 |
use MediaWiki\Extension\Notifications\Services; |
| 8 |
use MediaWiki\Tests\Api\ApiTestCase; |
| 9 |
use MediaWiki\User\User; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ApiEchoPushSubscriptionsDeleteTest extends ApiTestCase { |
| 18 |
|
| 19 |
|
| 20 |
private $user; |
| 21 |
|
| 22 |
|
| 23 |
private $subscriptionManager; |
| 24 |
|
| 25 |
|
| 26 |
private $otherUser; |
| 27 |
|
| 28 |
protected function setUp(): void { |
| 29 |
parent::setUp(); |
| 30 |
$this->overrideConfigValues( [ |
| 31 |
'EchoEnablePush' => true, |
| 32 |
'EchoPushMaxSubscriptionsPerUser' => 3, |
| 33 |
] ); |
| 34 |
|
| 35 |
|
| 36 |
$this->user = $this->getMutableTestUser()->getUser(); |
| 37 |
$this->otherUser = $this->getMutableTestUser()->getUser(); |
| 38 |
$this->subscriptionManager = $this->getTestUser( 'push-subscription-manager' )->getUser(); |
| 39 |
|
| 40 |
$this->createTestData(); |
| 41 |
} |
| 42 |
|
| 43 |
public function testApiDeleteSubscription(): void { |
| 44 |
$params = [ |
| 45 |
'action' => 'echopushsubscriptions', |
| 46 |
'command' => 'delete', |
| 47 |
'providertoken' => 'ABC', |
| 48 |
]; |
| 49 |
$result = $this->doApiRequestWithToken( $params, null, $this->user ); |
| 50 |
$this->assertEquals( 'Success', $result[0]['delete']['result'] ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testApiDeleteSubscriptionNotFound(): void { |
| 54 |
$params = [ |
| 55 |
'action' => 'echopushsubscriptions', |
| 56 |
'command' => 'delete', |
| 57 |
'providertoken' => 'XYZ', |
| 58 |
]; |
| 59 |
$this->expectException( ApiUsageException::class ); |
| 60 |
$this->doApiRequestWithToken( $params, null, $this->user ); |
| 61 |
} |
| 62 |
|
| 63 |
public function testApiDeleteSubscriptionWithOwnCentralId(): void { |
| 64 |
$params = [ |
| 65 |
'action' => 'echopushsubscriptions', |
| 66 |
'command' => 'delete', |
| 67 |
'providertoken' => 'ABC', |
| 68 |
'centraluserid' => Utils::getPushUserId( $this->user ), |
| 69 |
]; |
| 70 |
$result = $this->doApiRequestWithToken( $params, null, $this->user ); |
| 71 |
$this->assertEquals( 'Success', $result[0]['delete']['result'] ); |
| 72 |
} |
| 73 |
|
| 74 |
public function testApiDeleteSubscriptionWithOtherNonSubscriptionManagerUser(): void { |
| 75 |
$params = [ |
| 76 |
'action' => 'echopushsubscriptions', |
| 77 |
'command' => 'delete', |
| 78 |
'providertoken' => 'ABC', |
| 79 |
'centraluserid' => Utils::getPushUserId( $this->user ), |
| 80 |
]; |
| 81 |
$this->expectException( ApiUsageException::class ); |
| 82 |
$this->doApiRequestWithToken( $params, null, $this->otherUser ); |
| 83 |
} |
| 84 |
|
| 85 |
public function testApiDeleteSubscriptionWithPushSubscriptionManager(): void { |
| 86 |
$params = [ |
| 87 |
'action' => 'echopushsubscriptions', |
| 88 |
'command' => 'delete', |
| 89 |
'providertoken' => 'ABC', |
| 90 |
]; |
| 91 |
$result = $this->doApiRequestWithToken( $params, null, $this->subscriptionManager ); |
| 92 |
$this->assertEquals( 'Success', $result[0]['delete']['result'] ); |
| 93 |
} |
| 94 |
|
| 95 |
public function testApiDeleteSubscriptionProviderTokenEmpty(): void { |
| 96 |
$params = [ |
| 97 |
'action' => 'echopushsubscriptions', |
| 98 |
'command' => 'delete', |
| 99 |
'providertoken' => '' |
| 100 |
]; |
| 101 |
$this->expectException( ApiUsageException::class ); |
| 102 |
$result = $this->doApiRequestWithToken( $params, null, $this->user ); |
| 103 |
} |
| 104 |
|
| 105 |
private function createTestData(): void { |
| 106 |
$subscriptionManager = Services::getInstance()->getPushSubscriptionManager(); |
| 107 |
$userId = Utils::getPushUserId( $this->user ); |
| 108 |
$subscriptionManager->create( 'fcm', 'ABC', $userId ); |
| 109 |
} |
| 110 |
|
| 111 |
} |