-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathViewParserFunctionTest.php
More file actions
210 lines (155 loc) · 7.44 KB
/
Copy pathViewParserFunctionTest.php
File metadata and controls
210 lines (155 loc) · 7.44 KB
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
200
201
202
203
204
205
206
207
208
209
210
<?php
declare( strict_types = 1 );
namespace ProfessionalWiki\NeoWiki\Tests\EntryPoints;
use MediaWiki\Language\RawMessage;
use MediaWiki\Parser\Parser;
use MediaWiki\Title\Title;
use PHPUnit\Framework\TestCase;
use ProfessionalWiki\NeoWiki\Domain\Page\PageSubjects;
use ProfessionalWiki\NeoWiki\Domain\Schema\SchemaName;
use ProfessionalWiki\NeoWiki\Domain\Subject\StatementList;
use ProfessionalWiki\NeoWiki\Domain\Subject\Subject;
use ProfessionalWiki\NeoWiki\Domain\Subject\SubjectId;
use ProfessionalWiki\NeoWiki\Domain\Subject\SubjectLabel;
use ProfessionalWiki\NeoWiki\Domain\Subject\SubjectMap;
use ProfessionalWiki\NeoWiki\EntryPoints\ViewParserFunction;
use ProfessionalWiki\NeoWiki\Tests\TestDoubles\InMemorySubjectContentRepository;
/**
* @covers \ProfessionalWiki\NeoWiki\EntryPoints\ViewParserFunction
*/
class ViewParserFunctionTest extends TestCase {
private const string MAIN_SUBJECT_ID = 's11111111111111';
private const string EXPLICIT_SUBJECT_ID = 's22222222222222';
private const string OTHER_SUBJECT_ID = 's33333333333333';
public function testEmitsPlaceholderWithExplicitPositionalSubject(): void {
$result = $this->callView( self::EXPLICIT_SUBJECT_ID );
$this->assertRendersSubject( $result, self::EXPLICIT_SUBJECT_ID );
}
public function testEmitsPlaceholderWithLayoutAsNamedArg(): void {
$result = $this->callView( 'layout=Finances' );
$this->assertRendersSubject( $result, self::MAIN_SUBJECT_ID, 'Finances' );
}
public function testEmitsPlaceholderWithSubjectAsNamedArg(): void {
$result = $this->callView( 'subject=' . self::EXPLICIT_SUBJECT_ID );
$this->assertRendersSubject( $result, self::EXPLICIT_SUBJECT_ID );
}
public function testEmitsPlaceholderWithSubjectAndLayoutNamedArgs(): void {
$result = $this->callView( 'subject=' . self::EXPLICIT_SUBJECT_ID, 'layout=Finances' );
$this->assertRendersSubject( $result, self::EXPLICIT_SUBJECT_ID, 'Finances' );
}
public function testEmitsPlaceholderWithMixedPositionalAndNamed(): void {
$result = $this->callView( self::EXPLICIT_SUBJECT_ID, 'layout=Finances' );
$this->assertRendersSubject( $result, self::EXPLICIT_SUBJECT_ID, 'Finances' );
}
public function testEmitsPlaceholderWhenNamedArgComesBeforePositional(): void {
$result = $this->callView( 'layout=Finances', self::EXPLICIT_SUBJECT_ID );
$this->assertRendersSubject( $result, self::EXPLICIT_SUBJECT_ID, 'Finances' );
}
public function testTreatsEmptyNamedSubjectAsFallbackToMainSubject(): void {
$result = $this->callView( 'subject=', 'layout=Finances' );
$this->assertRendersSubject( $result, self::MAIN_SUBJECT_ID, 'Finances' );
}
public function testTreatsEmptyNamedLayoutAsUnset(): void {
$result = $this->callView( self::EXPLICIT_SUBJECT_ID, 'layout=' );
$this->assertRendersSubject( $result, self::EXPLICIT_SUBJECT_ID );
}
public function testFallsBackToMainSubjectWhenNoArgs(): void {
$result = $this->callView();
$this->assertRendersSubject( $result, self::MAIN_SUBJECT_ID );
}
public function testReturnsEmptyStringWhenNoSubjectAvailable(): void {
$parserFunction = new ViewParserFunction( new InMemorySubjectContentRepository() );
$result = $parserFunction->handle( $this->createMockParser() );
$this->assertSame( '', $result );
}
public function testReturnsEmptyStringWhenPageHasNoMainSubject(): void {
$parserFunction = new ViewParserFunction(
new InMemorySubjectContentRepository( new PageSubjects( null, new SubjectMap() ) )
);
$result = $parserFunction->handle( $this->createMockParser() );
$this->assertSame( '', $result );
}
public function testRendersErrorOnExtraPositional(): void {
$result = $this->callView( self::EXPLICIT_SUBJECT_ID, self::OTHER_SUBJECT_ID );
$this->assertRendersError( $result, 'neowiki-view-error-extra-positional', self::OTHER_SUBJECT_ID );
}
public function testOldPositionalLayoutFormProducesExtraPositionalError(): void {
$result = $this->callView( self::EXPLICIT_SUBJECT_ID, 'Finances' );
$this->assertRendersError( $result, 'neowiki-view-error-extra-positional', 'Finances' );
}
public function testRendersErrorOnConflictingSubject(): void {
$result = $this->callView( self::EXPLICIT_SUBJECT_ID, 'subject=' . self::OTHER_SUBJECT_ID );
$this->assertRendersError( $result, 'neowiki-view-error-conflicting-subject' );
}
public function testRendersErrorOnUnknownNamedArg(): void {
$result = $this->callView( 'layuot=Finances' );
$this->assertRendersError( $result, 'neowiki-view-error-unknown-arg', 'layuot' );
}
public function testRendersErrorOnArgWithEmptyName(): void {
$result = $this->callView( '=Finances' );
$this->assertRendersError( $result, 'neowiki-view-error-unknown-arg', '=Finances' );
}
public function testErrorIsArmouredAgainstWikitextTransformation(): void {
$result = $this->callView( self::EXPLICIT_SUBJECT_ID, 'https://example.com' );
$this->assertIsArray( $result, 'Error detail echoes the offending argument, which can carry a URL, so it needs the same armour.' );
$this->assertTrue( $result['isHTML'] );
$this->assertTrue( $result['noparse'] );
}
private function callView( string ...$args ): string|array {
return ( new ViewParserFunction( $this->repositoryWithMainSubject() ) )
->handle( $this->createMockParser(), ...$args );
}
private function repositoryWithMainSubject(): InMemorySubjectContentRepository {
$mainSubject = new Subject(
id: new SubjectId( self::MAIN_SUBJECT_ID ),
label: new SubjectLabel( 'Main' ),
schemaName: new SchemaName( 'TestSchema' ),
statements: new StatementList(),
);
return new InMemorySubjectContentRepository( new PageSubjects( $mainSubject, new SubjectMap() ) );
}
private function createMockParser(): Parser {
$title = $this->createStub( Title::class );
$parser = $this->createStub( Parser::class );
$parser->method( 'getTitle' )->willReturn( $title );
$parser->method( 'msg' )->willReturnCallback(
static fn ( string $key, ...$params ) => new RawMessage( $key . ': $1', $params )
);
return $parser;
}
/**
* @param string|array{0: string, noparse: true, isHTML: true} $result
*/
private function assertRendersSubject( string|array $result, string $subjectId, ?string $layoutName = null ): void {
$this->assertIsArray( $result, 'Expected a placeholder array; got an error string or empty string.' );
$this->assertTrue( $result['isHTML'] );
$this->assertTrue( $result['noparse'] );
$html = $this->html( $result );
$this->assertStringContainsString( 'data-mw-neowiki-subject-id="' . $subjectId . '"', $html );
if ( $layoutName === null ) {
$this->assertStringNotContainsString( 'data-mw-neowiki-layout-name', $html );
} else {
$this->assertStringContainsString( 'data-mw-neowiki-layout-name="' . $layoutName . '"', $html );
}
}
/**
* @param string|array{0: string, noparse: true, isHTML: true} $result
*/
private function assertRendersError( string|array $result, string $messageKey, ?string $insertion = null ): void {
$this->assertIsArray( $result, 'Expected an armoured error array; got an error string or empty string.' );
$html = $this->html( $result );
$this->assertStringContainsString( 'class="error"', $html );
$this->assertStringContainsString( $messageKey, $html );
if ( $insertion !== null ) {
$this->assertStringContainsString( $insertion, $html );
}
}
/**
* The HTML the parser function hands back, from either its result or its error shape.
*
* @param array{0: string, noparse: true, isHTML: true} $result
*/
private function html( array $result ): string {
return $result[0];
}
}