Debian Codegraph @

extensions/Echo/tests/phpunit/Api/ApiEchoCreateEventTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Api\ApiUsageException;
4 use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
5 use MediaWiki\Extension\Notifications\NotifUser;
6 use MediaWiki\Tests\Api\ApiTestCase;
7
8 /**
9 * @group API
10 * @group Database
11 * @covers \MediaWiki\Extension\Notifications\Api\ApiEchoCreateEvent
12 */
13 class ApiEchoCreateEventTest extends ApiTestCase {
14 protected function setUp(): void {
15 parent::setUp();
16 $this->overrideConfigValue( 'EchoEnableApiEvents', true );
17 }
18
19 public function testNotifySelf() {
20 $result = $this->doApiRequestWithToken( [
21 'action' => 'echocreateevent',
22 'user' => $this->getTestUser()->getUser()->getName(),
23 'header' => 'notification header',
24 'content' => 'notification content',
25 ], null, $this->getTestUser()->getUser() );
26
27 $this->assertEquals( 'success', $result[0]['echocreateevent']['result'] );
28
29 $user = NotifUser::newFromUser( $this->getTestUser()->getUser() );
30 $this->assertSame( 1, $user->getNotificationCount() );
31
32 $mapper = new NotificationMapper();
33 $notifs = $mapper->fetchByUser( $this->getTestUser()->getUser(), 5, null, [ 'api-notice' ] );
34 $this->assertCount( 1, $notifs );
35 $notif = array_values( $notifs )[0];
36 $this->assertSame( 'notification header', $notif->getEvent()->getExtraParam( 'header' ) );
37 $this->assertSame( 'notification content', $notif->getEvent()->getExtraParam( 'content' ) );
38 $this->assertTrue( $notif->getEvent()->getExtraParam( 'noemail' ) );
39 }
40
41 public function testAlertWithEmail() {
42 $result = $this->doApiRequestWithToken( [
43 'action' => 'echocreateevent',
44 'user' => $this->getTestUser()->getUser()->getName(),
45 'header' => 'notification header',
46 'content' => 'notification content',
47 'email' => true,
48 'section' => 'alert'
49 ], null, $this->getTestUser()->getUser() );
50 $this->assertEquals( 'success', $result[0]['echocreateevent']['result'] );
51
52 $mapper = new NotificationMapper();
53 $notifs = $mapper->fetchByUser( $this->getTestUser()->getUser(), 5, null, [ 'api-alert' ] );
54 $this->assertFalse( array_values( $notifs )[0]->getEvent()->getExtraParam( 'noemail' ) );
55 }
56
57 public function testNotifyOthers() {
58 $this->setGroupPermissions( 'sysop', 'echo-create', true );
59
60 $result = $this->doApiRequestWithToken( [
61 'action' => 'echocreateevent',
62 'user' => $this->getTestUser()->getUser()->getName(),
63 'header' => 'notification header',
64 'content' => 'notification content',
65 ], null, $this->getTestSysop()->getUser() );
66
67 $this->assertEquals( 'success', $result[0]['echocreateevent']['result'] );
68
69 $user = NotifUser::newFromUser( $this->getTestUser()->getUser() );
70 $this->assertSame( 1, $user->getNotificationCount() );
71 }
72
73 public function testNotifyOthersWithoutPermission() {
74 try {
75 $this->doApiRequestWithToken( [
76 'action' => 'echocreateevent',
77 'user' => $this->getTestUser()->getUser()->getName(),
78 'header' => 'notification header',
79 'content' => 'notification content',
80 ], null, $this->getTestSysop()->getUser() );
81 } catch ( ApiUsageException $ex ) {
82 $this->assertApiErrorCode( 'permissiondenied', $ex );
83 }
84 }
85 }