File: HTMLFormTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (199 lines) | stat: -rw-r--r-- 6,922 bytes parent folder | download
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php

namespace MediaWiki\Tests\Integration\HTMLForm;

use LogicException;
use MediaWiki\Context\RequestContext;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\Language\RawMessage;
use MediaWiki\Output\OutputPage;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Status\Status;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWikiIntegrationTestCase;

/**
 * @covers \MediaWiki\HTMLForm\HTMLForm
 *
 * @license GPL-2.0-or-later
 * @author Gergő Tisza
 */
class HTMLFormTest extends MediaWikiIntegrationTestCase {

	private function newInstance() {
		$context = new RequestContext();
		$out = new OutputPage( $context );
		$out->setTitle( Title::newMainPage() );
		$context->setOutput( $out );
		$form = new HTMLForm( [], $context );
		$form->setTitle( Title::makeTitle( NS_MAIN, 'Foo' ) );
		return $form;
	}

	public function testGetHTML_empty() {
		$form = $this->newInstance();
		$form->prepareForm();
		$html = $form->getHTML( false );
		$this->assertStringStartsWith( '<form ', $html );
	}

	public function testGetHTML_noPrepare() {
		$form = $this->newInstance();
		$this->expectException( LogicException::class );
		$form->getHTML( false );
	}

	public function testAutocompleteDefaultsToNull() {
		$form = $this->newInstance();
		$this->assertStringNotContainsString( 'autocomplete', $form->wrapForm( '' ) );
	}

	public function testAutocompleteWhenSetToNull() {
		$form = $this->newInstance();
		$form->setAutocomplete( null );
		$this->assertStringNotContainsString( 'autocomplete', $form->wrapForm( '' ) );
	}

	public function testAutocompleteWhenSetToFalse() {
		$form = $this->newInstance();
		// Previously false was used instead of null to indicate the attribute should not be set
		$form->setAutocomplete( false );
		$this->assertStringNotContainsString( 'autocomplete', $form->wrapForm( '' ) );
	}

	public function testAutocompleteWhenSetToOff() {
		$form = $this->newInstance();
		$form->setAutocomplete( 'off' );
		$this->assertStringContainsString( ' autocomplete="off"', $form->wrapForm( '' ) );
	}

	public function testGetPreText() {
		$this->hideDeprecated( HTMLForm::class . '::setPreText' );
		$this->hideDeprecated( HTMLForm::class . '::getPreText' );
		$this->hideDeprecated( HTMLForm::class . '::addPreText' );

		$preText = 'TEST';
		$form = $this->newInstance();
		$form->setPreText( $preText );
		$this->assertSame( $preText, $form->getPreText() );
		$form->addPreText( $preText );
		$this->assertSame( $preText . $preText, $form->getPreText() );
	}

	public function testGetPreHtml() {
		$this->hideDeprecated( HTMLForm::class . '::setIntro' );

		$preHtml = 'TEST';
		$form = $this->newInstance();
		$form->setPreHtml( $preHtml );
		$this->assertSame( $preHtml, $form->getPreHtml() );
		$preHtml = 'TEST2';
		$form->setIntro( $preHtml );
		$this->assertSame( $preHtml, $form->getPreHtml() );
		$preHtml = 'TEST';
		$form->addPreHtml( $preHtml );
		$this->assertSame( $preHtml . '2' . $preHtml, $form->getPreHtml() );
	}

	public function testGetPostHtml() {
		$this->hideDeprecated( HTMLForm::class . '::setPostText' );
		$this->hideDeprecated( HTMLForm::class . '::addPostText' );

		$postHtml = 'TESTED';
		$form = $this->newInstance();
		$form->setPostHtml( $postHtml );
		$this->assertSame( $postHtml, $form->getPostHtml() );
		$postHtml = 'TESTED2';
		$form->setPostText( $postHtml );
		$this->assertSame( $postHtml, $form->getPostHtml() );
		$postHtml = 'TESTED';
		$form->addPostHtml( $postHtml );
		$this->assertSame( $postHtml . '2' . $postHtml, $form->getPostHtml() );
		$form->addPostText( $postHtml );
		$this->assertSame( $postHtml . '2' . $postHtml . $postHtml, $form->getPostHtml() );
	}

	public function testCollapsible() {
		$form = $this->newInstance();
		$form->prepareForm()->getHTML( '' );
		$this->assertContains( 'mediawiki.htmlform', $form->getOutput()->getModules() );
		$this->assertNotContains( 'jquery.makeCollapsible', $form->getOutput()->getModules() );

		$form = $this->newInstance();
		$form->setCollapsibleOptions( null );
		$form->prepareForm()->getHTML( '' );
		$this->assertContains( 'jquery.makeCollapsible', $form->getOutput()->getModules() );

		$form = $this->newInstance();
		$form->setCollapsibleOptions( false );
		$form->prepareForm()->getHTML( '' );
		$this->assertContains( 'jquery.makeCollapsible', $form->getOutput()->getModules() );

		$form = $this->newInstance();
		$form->setCollapsibleOptions( true );
		$form->prepareForm()->getHTML( '' );
		$this->assertContains( 'jquery.makeCollapsible', $form->getOutput()->getModules() );
	}

	public function testGetErrorsOrWarningsWithRawParams() {
		$form = $this->newInstance();
		$msg = new RawMessage( 'message with $1' );
		$msg->rawParams( '<a href="raw">params</a>' );
		$status = Status::newFatal( $msg );

		$result = $form->getErrorsOrWarnings( $status, 'error' );

		$this->assertStringContainsString( 'message with <a href="raw">params</a>', $result );
	}

	/**
	 * @dataProvider provideCsrf
	 * @param string|null $formTokenSalt Salt to pass to HTMLForm::setTokenSalt()
	 * @param array $requestData HTTP request data
	 * @param array|null $tokens User's CSRF tokens in a salt => value format, or null for anon
	 * @param bool $shouldBeAuthorized
	 */
	public function testCsrf(
		?string $formTokenSalt,
		array $requestData,
		?array $tokens,
		bool $shouldBeAuthorized
	) {
		$user = $this->createNoOpMock( User::class, [ 'isRegistered', 'matchEditToken' ] );
		$user->method( 'isRegistered' )->willReturn( $tokens !== null );
		$user->method( 'matchEditToken' )->willReturnCallback(
			static function ( $token, $salt ) use ( $tokens ) {
				return $tokens && isset( $tokens[$salt] ) && $tokens[$salt] === $token;
			} );
		$context = $this->createConfiguredMock( RequestContext::class, [
			'getRequest' => new FauxRequest( $requestData, true ),
			'getUser' => $user,
		] );
		$form = new HTMLForm( [], $context );
		if ( $formTokenSalt !== null ) {
			$form->setTokenSalt( $formTokenSalt );
		}
		$form->setSubmitCallback( static function () {
			return true;
		} );

		$this->assertSame( $shouldBeAuthorized, $form->tryAuthorizedSubmit() );
	}

	public static function provideCsrf() {
		return [
			// form token salt, request data, tokens, should be authorized?
			'Anon user, CSRF token ignored' => [ null, [], null, true ],
			'No CSRF token sent' => [ null, [], [ '' => '123' ], false ],
			'Wrong CSRF token sent' => [ null, [ 'wpEditToken' => 'xyz' ], [ '' => '123' ], false ],
			// this isn't possible but helps catch errors in the test itself
			'User has no CSRF token' => [ null, [ 'wpEditToken' => 'xyz' ], [], false ],
			'Correct CSRF token' => [ null, [ 'wpEditToken' => '123' ], [ '' => '123' ], true ],
			'Wrong CSRF token type' => [ 'delete', [ 'wpEditToken' => '123' ], [ '' => '123' ], false ],
			'Correct non-default CSRF token' => [ 'delete', [ 'wpEditToken' => 'xyz' ],
				[ '' => 123, 'delete' => 'xyz' ], true ],
		];
	}

}