1<?php
2
3/**
4 * @covers HTMLForm
5 *
6 * @license GPL-2.0-or-later
7 * @author Gergő Tisza
8 */
9class HTMLFormTest extends MediaWikiIntegrationTestCase {
10
11	private function newInstance() {
12		$context = new RequestContext();
13		$out = new OutputPage( $context );
14		$out->setTitle( Title::newMainPage() );
15		$context->setOutput( $out );
16		$form = new HTMLForm( [], $context );
17		$form->setTitle( Title::newFromText( 'Foo' ) );
18		return $form;
19	}
20
21	public function testGetHTML_empty() {
22		$form = $this->newInstance();
23		$form->prepareForm();
24		$html = $form->getHTML( false );
25		$this->assertStringStartsWith( '<form ', $html );
26	}
27
28	public function testGetHTML_noPrepare() {
29		$form = $this->newInstance();
30		$this->expectException( LogicException::class );
31		$form->getHTML( false );
32	}
33
34	public function testAutocompleteDefaultsToNull() {
35		$form = $this->newInstance();
36		$this->assertStringNotContainsString( 'autocomplete', $form->wrapForm( '' ) );
37	}
38
39	public function testAutocompleteWhenSetToNull() {
40		$form = $this->newInstance();
41		$form->setAutocomplete( null );
42		$this->assertStringNotContainsString( 'autocomplete', $form->wrapForm( '' ) );
43	}
44
45	public function testAutocompleteWhenSetToFalse() {
46		$form = $this->newInstance();
47		// Previously false was used instead of null to indicate the attribute should not be set
48		$form->setAutocomplete( false );
49		$this->assertStringNotContainsString( 'autocomplete', $form->wrapForm( '' ) );
50	}
51
52	public function testAutocompleteWhenSetToOff() {
53		$form = $this->newInstance();
54		$form->setAutocomplete( 'off' );
55		$this->assertStringContainsString( ' autocomplete="off"', $form->wrapForm( '' ) );
56	}
57
58	public function testGetPreText() {
59		$preText = 'TEST';
60		$form = $this->newInstance();
61		$form->setPreText( $preText );
62		$this->assertSame( $preText, $form->getPreText() );
63	}
64
65	public function testGetErrorsOrWarningsWithRawParams() {
66		$form = $this->newInstance();
67		$msg = new RawMessage( 'message with $1' );
68		$msg->rawParams( '<a href="raw">params</a>' );
69		$status = Status::newFatal( $msg );
70
71		$result = $form->getErrorsOrWarnings( $status, 'error' );
72
73		$this->assertStringContainsString( 'message with <a href="raw">params</a>', $result );
74	}
75
76}
77