1<?php
2
3namespace MediaWiki\ParamValidator\TypeDef;
4
5use CommentStoreComment;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Revision\SlotRecord;
8use Title;
9use TitleValue;
10use User;
11use Wikimedia\ParamValidator\ParamValidator;
12use Wikimedia\ParamValidator\SimpleCallbacks;
13use Wikimedia\ParamValidator\TypeDef\TypeDefTestCase;
14use WikitextContent;
15
16/**
17 * @covers \MediaWiki\ParamValidator\TypeDef\TitleDef
18 */
19class TitleDefTest extends TypeDefTestCase {
20
21	protected function getInstance( SimpleCallbacks $callbacks, array $options ) {
22		return new TitleDef(
23			$callbacks,
24			MediaWikiServices::getInstance()->getTitleFactory()
25		);
26	}
27
28	/**
29	 * @inheritDoc
30	 * @dataProvider provideValidate
31	 */
32	public function testValidate(
33		$value, $expect, array $settings = [], array $options = [], array $expectConds = []
34	) {
35		if ( $this->dataName() === 'must exist (success)' ) {
36			$updater = MediaWikiServices::getInstance()->getWikiPageFactory()
37				->newFromTitle( Title::newFromText( 'exists' ) )
38				->newPageUpdater( new User );
39			$updater->setContent( SlotRecord::MAIN, new WikitextContent( 'exists' ) );
40			$updater->saveRevision( CommentStoreComment::newUnsavedComment( 'test' ) );
41			$this->assertTrue( $updater->getStatus()->isOK(), 'sanity' );
42		}
43		parent::testValidate( $value, $expect, $settings, $options, $expectConds );
44	}
45
46	public function provideValidate() {
47		return [
48			'plain' => [
49				'value' => 'Foo',
50				'expect' => 'Foo',
51				'settings' => [],
52			],
53			'normalization' => [
54				'value' => 'foo_bar',
55				'expect' => 'Foo bar',
56				'settings' => [],
57			],
58			'bad title' => [
59				'value' => '<script>',
60				'expect' => $this->getValidationException( 'badtitle', '<script>' ),
61				'settings' => [],
62			],
63			'as object' => [
64				'value' => 'Foo',
65				'expect' => new TitleValue( NS_MAIN, 'Foo' ),
66				'settings' => [ TitleDef::PARAM_RETURN_OBJECT => true ],
67			],
68			'as object, with namespace' => [
69				'value' => 'User:Foo',
70				'expect' => new TitleValue( NS_USER, 'Foo' ),
71				'settings' => [ TitleDef::PARAM_RETURN_OBJECT => true ],
72			],
73			'object normalization' => [
74				'value' => 'foo_bar',
75				'expect' => new TitleValue( NS_MAIN, 'Foo bar' ),
76				'settings' => [ TitleDef::PARAM_RETURN_OBJECT => true ],
77			],
78			'must exist (success)' => [
79				'value' => 'Exists',
80				'expect' => 'Exists',
81				'settings' => [ TitleDef::PARAM_MUST_EXIST => true ],
82			],
83			'must exist (failure)' => [
84				'value' => 'does not exist',
85				'expect' => $this->getValidationException( 'missingtitle', 'does not exist',
86					[ TitleDef::PARAM_MUST_EXIST => true ] ),
87				'settings' => [ TitleDef::PARAM_MUST_EXIST => true ],
88			],
89		];
90	}
91
92	public function provideStringifyValue() {
93		return [
94			// Underscore-to-space conversion not happening here but later in validate().
95			'String' => [ 'User:John_Doe', 'User:John_Doe' ],
96			'TitleValue' => [ new TitleValue( NS_USER, 'John_Doe' ), 'User:John Doe' ],
97			'Title' => [ Title::newFromText( 'User:John_Doe' ), 'User:John Doe' ],
98		];
99	}
100
101	public function provideCheckSettings() {
102		// checkSettings() is itself used in tests. Testing it is a waste of time,
103		// just provide the minimum required.
104		return [
105			'Basic test' => [ [], self::STDRET, array_merge_recursive( self::STDRET, [
106				'allowedKeys' => [ TitleDef::PARAM_MUST_EXIST, TitleDef::PARAM_RETURN_OBJECT ],
107			] ) ],
108		];
109	}
110
111	public function provideGetInfo() {
112		return [
113			'no mustExist' => [
114				'settings' => [],
115				'expectParamInfo' => [ 'mustExist' => false ],
116				'expectHelpInfo' => [
117					ParamValidator::PARAM_TYPE =>
118						'<message key="paramvalidator-help-type-title"></message>',
119					TitleDef::PARAM_MUST_EXIST =>
120						'<message key="paramvalidator-help-type-title-no-must-exist"></message>'
121				],
122			],
123			'mustExist' => [
124				'settings' => [ TitleDef::PARAM_MUST_EXIST => true ],
125				'expectParamInfo' => [ 'mustExist' => true ],
126				'expectHelpInfo' => [
127					ParamValidator::PARAM_TYPE =>
128						'<message key="paramvalidator-help-type-title"></message>',
129					TitleDef::PARAM_MUST_EXIST =>
130						'<message key="paramvalidator-help-type-title-must-exist"></message>'
131				],
132			],
133		];
134	}
135
136}
137