Merge "Drop cache interwiki"
[mediawiki.git] / tests / phpunit / includes / specials / SpecialCreateAccountTest.php
blob5f44ebd4be74a1e466275960a4c5b3fe571495b9
1 <?php
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;
14 /**
15 * @covers \MediaWiki\Specials\SpecialCreateAccount
16 * @group Database
18 class SpecialCreateAccountTest extends SpecialPageTestBase {
19 /**
20 * @inheritDoc
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 $services->getUserIdentityUtils()
30 $page->setContext( $context );
31 $context->setTitle( $page->getPageTitle() );
32 return $page;
35 public function testCheckPermissions() {
36 $readOnlyMode = $this->getServiceContainer()->getReadOnlyMode();
37 $readOnlyMode->setReason( 'Test' );
39 $this->expectException( ErrorPageError::class );
40 $specialPage = $this->newSpecialPage();
41 $specialPage->checkPermissions();
44 /**
45 * Regression test for T360717 -- missing hidden fields from Special:CreateAccount
47 public function testHiddenField() {
48 $config = $this->getServiceContainer()->getMainConfig()->get( MainConfigNames::AuthManagerConfig );
49 $config['preauth']['MockAuthProviderWithHiddenField'] = [
50 'class' => MockAuthProviderWithHiddenField::class
52 $this->overrideConfigValue( MainConfigNames::AuthManagerConfig, $config );
53 $specialPage = $this->newSpecialPage();
54 $specialPage->execute( null );
55 $html = $specialPage->getOutput()->getHTML();
56 $this->assertStringContainsString(
57 '<input id="mw-input-captchaId" name="captchaId" type="hidden" value="T360717">',
58 $html
62 public function testShouldShowTemporaryPasswordAndCreationReasonFieldsForRegisteredUser(): void {
63 $user = $this->getTestUser()->getUser();
65 $context = new DerivativeContext( RequestContext::getMain() );
66 $context->setUser( $user );
68 $specialPage = $this->newSpecialPage( $context );
69 $specialPage->execute( null );
70 $doc = self::getOutputHtml( $specialPage );
72 $this->assertNotNull( $doc->getElementById( 'wpReason' ) );
73 $this->assertNotNull( $doc->getElementById( 'wpCreateaccountMail' ) );
76 public function testShouldNotShowTemporaryPasswordAndCreationReasonFieldsForTempUser(): void {
77 $req = new FauxRequest();
78 $tempUser = $this->getServiceContainer()
79 ->getTempUserCreator()
80 ->create( null, $req )
81 ->getUser();
83 $context = new DerivativeContext( RequestContext::getMain() );
84 $context->setUser( $tempUser );
86 $specialPage = $this->newSpecialPage( $context );
87 $specialPage->execute( null );
88 $doc = self::getOutputHtml( $specialPage );
90 $this->assertNull(
91 $doc->getElementById( 'wpReason' ),
92 'Temporary users should not have to provide a reason for their account creation (T328718)'
94 $this->assertNull(
95 $doc->getElementById( 'wpCreateaccountMail' ),
96 'Temporary users should not have the option to have a temporary password sent on signup (T328718)'
101 * Convenience function to get the parsed DOM of the HTML generated by the given special page.
102 * @param SpecialPage $page
103 * @return DOMDocument
105 private static function getOutputHtml( SpecialPage $page ): DOMDocument {
106 $html = HtmlFormatter::wrapHTML( $page->getOutput()->getHTML() );
107 return ( new HtmlFormatter( $html ) )->getDoc();
111 class MockAuthRequestWithHiddenField extends AuthenticationRequest {
112 public function getFieldInfo() {
113 return [
114 'captchaId' => [
115 'type' => 'hidden',
116 'value' => 'T360717',
117 'label' => '',
118 'help' => '',
124 class MockAuthProviderWithHiddenField extends AbstractPreAuthenticationProvider {
125 public function getAuthenticationRequests( $action, array $options ) {
126 return [ new MockAuthRequestWithHiddenField ];