| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\HookContainer\HookContainer; |
| 4 |
use MediaWiki\MainConfigNames; |
| 5 |
use MediaWiki\Request\FauxRequest; |
| 6 |
use MediaWiki\Specials\SpecialMute; |
| 7 |
use MediaWiki\User\Options\UserOptionsManager; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class SpecialMuteTest extends SpecialPageTestBase { |
| 15 |
|
| 16 |
|
| 17 |
private $userOptionsManager; |
| 18 |
|
| 19 |
protected function setUp(): void { |
| 20 |
parent::setUp(); |
| 21 |
|
| 22 |
$this->userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); |
| 23 |
$this->overrideConfigValue( MainConfigNames::EnableUserEmailMuteList, true ); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
protected function newSpecialPage() { |
| 30 |
return new SpecialMute( |
| 31 |
$this->getServiceContainer()->getCentralIdLookupFactory()->getLookup( 'local' ), |
| 32 |
$this->userOptionsManager, |
| 33 |
$this->getServiceContainer()->getUserIdentityLookup(), |
| 34 |
$this->getServiceContainer()->getUserIdentityUtils() |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function testInvalidTarget() { |
| 42 |
$user = $this->getTestUser()->getUser(); |
| 43 |
$this->expectException( ErrorPageError::class ); |
| 44 |
$this->expectExceptionMessage( "username requested could not be found" ); |
| 45 |
$this->executeSpecialPage( |
| 46 |
'InvalidUser', null, 'qqx', $user |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function testEmailBlacklistNotEnabled() { |
| 54 |
$this->setTemporaryHook( |
| 55 |
'SpecialMuteModifyFormFields', |
| 56 |
HookContainer::NOOP |
| 57 |
); |
| 58 |
|
| 59 |
$this->overrideConfigValue( MainConfigNames::EnableUserEmailMuteList, false ); |
| 60 |
|
| 61 |
$user = $this->getTestUser()->getUser(); |
| 62 |
$this->expectException( ErrorPageError::class ); |
| 63 |
$this->expectExceptionMessage( "Mute features are unavailable" ); |
| 64 |
$this->executeSpecialPage( |
| 65 |
$user->getName(), null, 'qqx', $user |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function testUserNotLoggedIn() { |
| 73 |
$this->expectException( UserNotLoggedIn::class ); |
| 74 |
$this->executeSpecialPage( 'TestUser' ); |
| 75 |
} |
| 76 |
|
| 77 |
public function testUserEmailNotConfirmed() { |
| 78 |
$targetUser = $this->getTestUser()->getUser(); |
| 79 |
|
| 80 |
$loggedInUser = $this->getMutableTestUser()->getUser(); |
| 81 |
$this->userOptionsManager->setOption( $loggedInUser, 'email-blacklist', "999" ); |
| 82 |
$loggedInUser->invalidateEmail(); |
| 83 |
$loggedInUser->saveSettings(); |
| 84 |
|
| 85 |
$this->expectExceptionMessage( wfMessage( 'specialmute-error-no-email-set' ) ); |
| 86 |
|
| 87 |
$fauxRequest = new FauxRequest( [ 'wpemail-blacklist' => true ], true ); |
| 88 |
$this->executeSpecialPage( $targetUser->getName(), $fauxRequest, 'qqx', $loggedInUser ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function testMuteAddsUserToEmailBlacklist() { |
| 95 |
$targetUser = $this->getTestUser()->getUser(); |
| 96 |
|
| 97 |
$loggedInUser = $this->getMutableTestUser()->getUser(); |
| 98 |
$this->userOptionsManager->setOption( $loggedInUser, 'email-blacklist', "999" ); |
| 99 |
$loggedInUser->confirmEmail(); |
| 100 |
$loggedInUser->saveSettings(); |
| 101 |
|
| 102 |
$fauxRequest = new FauxRequest( [ 'wpemail-blacklist' => true ], true ); |
| 103 |
[ $html, ] = $this->executeSpecialPage( |
| 104 |
$targetUser->getName(), $fauxRequest, 'qqx', $loggedInUser |
| 105 |
); |
| 106 |
|
| 107 |
$this->assertStringContainsString( 'specialmute-success', $html ); |
| 108 |
$this->assertEquals( |
| 109 |
"999\n" . $targetUser->getId(), |
| 110 |
$this->userOptionsManager->getOption( $loggedInUser, 'email-blacklist' ) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function testUnmuteRemovesUserFromEmailBlacklist() { |
| 118 |
$targetUser = $this->getTestUser()->getUser(); |
| 119 |
|
| 120 |
$loggedInUser = $this->getMutableTestUser()->getUser(); |
| 121 |
$this->userOptionsManager->setOption( $loggedInUser, 'email-blacklist', "999\n" . $targetUser->getId() ); |
| 122 |
$loggedInUser->confirmEmail(); |
| 123 |
$loggedInUser->saveSettings(); |
| 124 |
|
| 125 |
$fauxRequest = new FauxRequest( [ 'wpemail-blacklist' => false ], true ); |
| 126 |
[ $html, ] = $this->executeSpecialPage( |
| 127 |
$targetUser->getName(), $fauxRequest, 'qqx', $loggedInUser |
| 128 |
); |
| 129 |
|
| 130 |
$this->assertStringContainsString( 'specialmute-success', $html ); |
| 131 |
$this->assertSame( "999", $this->userOptionsManager->getOption( $loggedInUser, 'email-blacklist' ) ); |
| 132 |
} |
| 133 |
} |