| 1 |
<?php |
| 2 |
|
| 3 |
use HtmlFormatter\HtmlFormatter; |
| 4 |
use MediaWiki\Auth\AbstractPreAuthenticationProvider; |
| 5 |
use MediaWiki\Auth\AuthenticationRequest; |
| 6 |
use MediaWiki\Context\DerivativeContext; |
| 7 |
use MediaWiki\Context\IContextSource; |
| 8 |
use MediaWiki\Context\RequestContext; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Request\FauxRequest; |
| 11 |
use MediaWiki\SpecialPage\SpecialPage; |
| 12 |
use MediaWiki\Specials\SpecialCreateAccount; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class SpecialCreateAccountTest extends SpecialPageTestBase { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
protected function newSpecialPage( ?IContextSource $context = null ) { |
| 23 |
$services = $this->getServiceContainer(); |
| 24 |
$context ??= RequestContext::getMain(); |
| 25 |
$page = new SpecialCreateAccount( |
| 26 |
$services->getAuthManager(), |
| 27 |
$services->getFormatterFactory() |
| 28 |
); |
| 29 |
$page->setContext( $context ); |
| 30 |
$context->setTitle( $page->getPageTitle() ); |
| 31 |
return $page; |
| 32 |
} |
| 33 |
|
| 34 |
public function testCheckPermissions() { |
| 35 |
$readOnlyMode = $this->getServiceContainer()->getReadOnlyMode(); |
| 36 |
$readOnlyMode->setReason( 'Test' ); |
| 37 |
|
| 38 |
$this->expectException( ErrorPageError::class ); |
| 39 |
$specialPage = $this->newSpecialPage(); |
| 40 |
$specialPage->checkPermissions(); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function testHiddenField() { |
| 47 |
$config = $this->getServiceContainer()->getMainConfig()->get( MainConfigNames::AuthManagerConfig ); |
| 48 |
$config['preauth']['MockAuthProviderWithHiddenField'] = [ |
| 49 |
'class' => MockAuthProviderWithHiddenField::class |
| 50 |
]; |
| 51 |
$this->overrideConfigValue( MainConfigNames::AuthManagerConfig, $config ); |
| 52 |
$specialPage = $this->newSpecialPage(); |
| 53 |
$specialPage->execute( null ); |
| 54 |
$html = $specialPage->getOutput()->getHTML(); |
| 55 |
$this->assertStringContainsString( |
| 56 |
'<input id="mw-input-captchaId" name="captchaId" type="hidden" value="T360717">', |
| 57 |
$html |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public function testShouldShowTemporaryPasswordAndCreationReasonFieldsForRegisteredUser(): void { |
| 62 |
$user = $this->getTestUser()->getUser(); |
| 63 |
|
| 64 |
$context = new DerivativeContext( RequestContext::getMain() ); |
| 65 |
$context->setUser( $user ); |
| 66 |
|
| 67 |
$specialPage = $this->newSpecialPage( $context ); |
| 68 |
$specialPage->execute( null ); |
| 69 |
$doc = self::getOutputHtml( $specialPage ); |
| 70 |
|
| 71 |
$this->assertNotNull( $doc->getElementById( 'wpReason' ) ); |
| 72 |
$this->assertNotNull( $doc->getElementById( 'wpCreateaccountMail' ) ); |
| 73 |
} |
| 74 |
|
| 75 |
public function testShouldNotShowTemporaryPasswordAndCreationReasonFieldsForTempUser(): void { |
| 76 |
$req = new FauxRequest(); |
| 77 |
$tempUser = $this->getServiceContainer() |
| 78 |
->getTempUserCreator() |
| 79 |
->create( null, $req ) |
| 80 |
->getUser(); |
| 81 |
|
| 82 |
$context = new DerivativeContext( RequestContext::getMain() ); |
| 83 |
$context->setUser( $tempUser ); |
| 84 |
|
| 85 |
$specialPage = $this->newSpecialPage( $context ); |
| 86 |
$specialPage->execute( null ); |
| 87 |
$doc = self::getOutputHtml( $specialPage ); |
| 88 |
|
| 89 |
$this->assertNull( |
| 90 |
$doc->getElementById( 'wpReason' ), |
| 91 |
'Temporary users should not have to provide a reason for their account creation (T328718)' |
| 92 |
); |
| 93 |
$this->assertNull( |
| 94 |
$doc->getElementById( 'wpCreateaccountMail' ), |
| 95 |
'Temporary users should not have the option to have a temporary password sent on signup (T328718)' |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
private static function getOutputHtml( SpecialPage $page ): DOMDocument { |
| 105 |
$html = HtmlFormatter::wrapHTML( $page->getOutput()->getHTML() ); |
| 106 |
return ( new HtmlFormatter( $html ) )->getDoc(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
class MockAuthRequestWithHiddenField extends AuthenticationRequest { |
| 111 |
public function getFieldInfo() { |
| 112 |
return [ |
| 113 |
'captchaId' => [ |
| 114 |
'type' => 'hidden', |
| 115 |
'value' => 'T360717', |
| 116 |
'label' => '', |
| 117 |
'help' => '', |
| 118 |
], |
| 119 |
]; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
class MockAuthProviderWithHiddenField extends AbstractPreAuthenticationProvider { |
| 124 |
public function getAuthenticationRequests( $action, array $options ) { |
| 125 |
return [ new MockAuthRequestWithHiddenField ]; |
| 126 |
} |
| 127 |
} |