Debian Codegraph @

tests/phpunit/integration/includes/HTMLForm/HTMLFormTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Integration\HTMLForm;
4
5 use LogicException;
6 use MediaWiki\Context\RequestContext;
7 use MediaWiki\HTMLForm\HTMLForm;
8 use MediaWiki\Language\RawMessage;
9 use MediaWiki\Output\OutputPage;
10 use MediaWiki\Request\FauxRequest;
11 use MediaWiki\Status\Status;
12 use MediaWiki\Title\Title;
13 use MediaWiki\User\User;
14 use MediaWikiIntegrationTestCase;
15
16 /**
17 * @covers \MediaWiki\HTMLForm\HTMLForm
18 *
19 * @license GPL-2.0-or-later
20 * @author Gergő Tisza
21 */
22 class HTMLFormTest extends MediaWikiIntegrationTestCase {
23
24 private function newInstance() {
25 $context = new RequestContext();
26 $out = new OutputPage( $context );
27 $out->setTitle( Title::newMainPage() );
28 $context->setOutput( $out );
29 $form = new HTMLForm( [], $context );
30 $form->setTitle( Title::makeTitle( NS_MAIN, 'Foo' ) );
31 return $form;
32 }
33
34 public function testGetHTML_empty() {
35 $form = $this->newInstance();
36 $form->prepareForm();
37 $html = $form->getHTML( false );
38 $this->assertStringStartsWith( '<form ', $html );
39 }
40
41 public function testGetHTML_noPrepare() {
42 $form = $this->newInstance();
43 $this->expectException( LogicException::class );
44 $form->getHTML( false );
45 }
46
47 public function testAutocompleteDefaultsToNull() {
48 $form = $this->newInstance();
49 $this->assertStringNotContainsString( 'autocomplete', $form->wrapForm( '' ) );
50 }
51
52 public function testAutocompleteWhenSetToNull() {
53 $form = $this->newInstance();
54 $form->setAutocomplete( null );
55 $this->assertStringNotContainsString( 'autocomplete', $form->wrapForm( '' ) );
56 }
57
58 public function testAutocompleteWhenSetToFalse() {
59 $form = $this->newInstance();
60 // Previously false was used instead of null to indicate the attribute should not be set
61 $form->setAutocomplete( false );
62 $this->assertStringNotContainsString( 'autocomplete', $form->wrapForm( '' ) );
63 }
64
65 public function testAutocompleteWhenSetToOff() {
66 $form = $this->newInstance();
67 $form->setAutocomplete( 'off' );
68 $this->assertStringContainsString( ' autocomplete="off"', $form->wrapForm( '' ) );
69 }
70
71 public function testGetPreText() {
72 $this->hideDeprecated( HTMLForm::class . '::setPreText' );
73 $this->hideDeprecated( HTMLForm::class . '::getPreText' );
74 $this->hideDeprecated( HTMLForm::class . '::addPreText' );
75
76 $preText = 'TEST';
77 $form = $this->newInstance();
78 $form->setPreText( $preText );
79 $this->assertSame( $preText, $form->getPreText() );
80 $form->addPreText( $preText );
81 $this->assertSame( $preText . $preText, $form->getPreText() );
82 }
83
84 public function testGetPreHtml() {
85 $this->hideDeprecated( HTMLForm::class . '::setIntro' );
86
87 $preHtml = 'TEST';
88 $form = $this->newInstance();
89 $form->setPreHtml( $preHtml );
90 $this->assertSame( $preHtml, $form->getPreHtml() );
91 $preHtml = 'TEST2';
92 $form->setIntro( $preHtml );
93 $this->assertSame( $preHtml, $form->getPreHtml() );
94 $preHtml = 'TEST';
95 $form->addPreHtml( $preHtml );
96 $this->assertSame( $preHtml . '2' . $preHtml, $form->getPreHtml() );
97 }
98
99 public function testGetPostHtml() {
100 $this->hideDeprecated( HTMLForm::class . '::setPostText' );
101 $this->hideDeprecated( HTMLForm::class . '::addPostText' );
102
103 $postHtml = 'TESTED';
104 $form = $this->newInstance();
105 $form->setPostHtml( $postHtml );
106 $this->assertSame( $postHtml, $form->getPostHtml() );
107 $postHtml = 'TESTED2';
108 $form->setPostText( $postHtml );
109 $this->assertSame( $postHtml, $form->getPostHtml() );
110 $postHtml = 'TESTED';
111 $form->addPostHtml( $postHtml );
112 $this->assertSame( $postHtml . '2' . $postHtml, $form->getPostHtml() );
113 $form->addPostText( $postHtml );
114 $this->assertSame( $postHtml . '2' . $postHtml . $postHtml, $form->getPostHtml() );
115 }
116
117 public function testCollapsible() {
118 $form = $this->newInstance();
119 $form->prepareForm()->getHTML( '' );
120 $this->assertContains( 'mediawiki.htmlform', $form->getOutput()->getModules() );
121 $this->assertNotContains( 'jquery.makeCollapsible', $form->getOutput()->getModules() );
122
123 $form = $this->newInstance();
124 $form->setCollapsibleOptions( null );
125 $form->prepareForm()->getHTML( '' );
126 $this->assertContains( 'jquery.makeCollapsible', $form->getOutput()->getModules() );
127
128 $form = $this->newInstance();
129 $form->setCollapsibleOptions( false );
130 $form->prepareForm()->getHTML( '' );
131 $this->assertContains( 'jquery.makeCollapsible', $form->getOutput()->getModules() );
132
133 $form = $this->newInstance();
134 $form->setCollapsibleOptions( true );
135 $form->prepareForm()->getHTML( '' );
136 $this->assertContains( 'jquery.makeCollapsible', $form->getOutput()->getModules() );
137 }
138
139 public function testGetErrorsOrWarningsWithRawParams() {
140 $form = $this->newInstance();
141 $msg = new RawMessage( 'message with $1' );
142 $msg->rawParams( '<a href="raw">params</a>' );
143 $status = Status::newFatal( $msg );
144
145 $result = $form->getErrorsOrWarnings( $status, 'error' );
146
147 $this->assertStringContainsString( 'message with <a href="raw">params</a>', $result );
148 }
149
150 /**
151 * @dataProvider provideCsrf
152 * @param string|null $formTokenSalt Salt to pass to HTMLForm::setTokenSalt()
153 * @param array $requestData HTTP request data
154 * @param array|null $tokens User's CSRF tokens in a salt => value format, or null for anon
155 * @param bool $shouldBeAuthorized
156 */
157 public function testCsrf(
158 ?string $formTokenSalt,
159 array $requestData,
160 ?array $tokens,
161 bool $shouldBeAuthorized
162 ) {
163 $user = $this->createNoOpMock( User::class, [ 'isRegistered', 'matchEditToken' ] );
164 $user->method( 'isRegistered' )->willReturn( $tokens !== null );
165 $user->method( 'matchEditToken' )->willReturnCallback(
166 static function ( $token, $salt ) use ( $tokens ) {
167 return $tokens && isset( $tokens[$salt] ) && $tokens[$salt] === $token;
168 } );
169 $context = $this->createConfiguredMock( RequestContext::class, [
170 'getRequest' => new FauxRequest( $requestData, true ),
171 'getUser' => $user,
172 ] );
173 $form = new HTMLForm( [], $context );
174 if ( $formTokenSalt !== null ) {
175 $form->setTokenSalt( $formTokenSalt );
176 }
177 $form->setSubmitCallback( static function () {
178 return true;
179 } );
180
181 $this->assertSame( $shouldBeAuthorized, $form->tryAuthorizedSubmit() );
182 }
183
184 public static function provideCsrf() {
185 return [
186 // form token salt, request data, tokens, should be authorized?
187 'Anon user, CSRF token ignored' => [ null, [], null, true ],
188 'No CSRF token sent' => [ null, [], [ '' => '123' ], false ],
189 'Wrong CSRF token sent' => [ null, [ 'wpEditToken' => 'xyz' ], [ '' => '123' ], false ],
190 // this isn't possible but helps catch errors in the test itself
191 'User has no CSRF token' => [ null, [ 'wpEditToken' => 'xyz' ], [], false ],
192 'Correct CSRF token' => [ null, [ 'wpEditToken' => '123' ], [ '' => '123' ], true ],
193 'Wrong CSRF token type' => [ 'delete', [ 'wpEditToken' => '123' ], [ '' => '123' ], false ],
194 'Correct non-default CSRF token' => [ 'delete', [ 'wpEditToken' => 'xyz' ],
195 [ '' => 123, 'delete' => 'xyz' ], true ],
196 ];
197 }
198
199 }