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