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
211
212
213
214
|
<?php
namespace MediaWiki\Tests\Rest\Helper;
use BagOStuff;
use DeferredUpdates;
use EmptyBagOStuff;
use Exception;
use ExtensionRegistry;
use HashBagOStuff;
use MediaWiki\Rest\Handler\ParsoidHTMLHelper;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWikiIntegrationTestCase;
use MWTimestamp;
use NullStatsdDataFactory;
use ParserCache;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\NullLogger;
use Wikimedia\Message\MessageValue;
use Wikimedia\Parsoid\Core\ClientError;
use Wikimedia\Parsoid\Core\PageBundle;
use Wikimedia\Parsoid\Core\ResourceLimitExceededException;
use Wikimedia\Parsoid\Parsoid;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \MediaWiki\Rest\Handler\ParsoidHTMLHelper
* @group Database
*/
class ParsoidHTMLHelperTest extends MediaWikiIntegrationTestCase {
private const WIKITEXT = 'Hello \'\'\'World\'\'\'';
private const HTML = '<p>Hello <b>World</b></p>';
protected function setUp(): void {
parent::setUp();
if ( !ExtensionRegistry::getInstance()->isLoaded( 'Parsoid' ) ) {
$this->markTestSkipped( 'Parsoid is not configured' );
}
// Clean up these tables after each test
$this->tablesUsed = [
'page',
'revision',
'comment',
'text',
'content'
];
}
/**
* @param BagOStuff|null $cache
* @param Parsoid|MockObject|null $parsoid
* @return ParsoidHTMLHelper
* @throws Exception
*/
private function newHelper( BagOStuff $cache = null, Parsoid $parsoid = null ): ParsoidHTMLHelper {
$parserCache = new ParserCache(
'Test',
$cache ?: new EmptyBagOStuff(),
0,
$this->getServiceContainer()->getHookContainer(),
$this->getServiceContainer()->getJsonCodec(),
new NullStatsdDataFactory(),
new NullLogger()
);
$helper = new ParsoidHTMLHelper(
$parserCache,
$this->getServiceContainer()->getWikiPageFactory()
);
if ( $parsoid !== null ) {
$wrapper = TestingAccessWrapper::newFromObject( $helper );
$wrapper->parsoid = $parsoid;
}
return $helper;
}
public function testGetHtml() {
$page = $this->getExistingTestPage( 'HtmlHelperTestPage/with/slashes' );
$this->assertTrue(
$this->editPage( $page, self::WIKITEXT )->isGood(),
'Sanity: edited a page'
);
$helper = $this->newHelper();
$helper->init( $page->getTitle() );
$htmlresult = $helper->getHtml()->getRawText();
$this->assertStringContainsString( '<!DOCTYPE html>', $htmlresult );
$this->assertStringContainsString( '<html', $htmlresult );
$this->assertStringContainsString( self::HTML, $htmlresult );
}
public function testHtmlIsCached() {
$page = $this->getExistingTestPage( 'HtmlHelperTestPage/with/slashes' );
$cache = new HashBagOStuff();
$parsoid = $this->createNoOpMock( Parsoid::class, [ 'wikitext2html' ] );
$parsoid->expects( $this->once() )
->method( 'wikitext2html' )
->willReturn( new PageBundle( 'mocked HTML', null, null, '1.0' ) );
$helper = $this->newHelper( $cache, $parsoid );
$helper->init( $page->getTitle() );
$htmlresult = $helper->getHtml()->getRawText();
$this->assertStringContainsString( 'mocked HTML', $htmlresult );
// check that we can run the test again and ensure that the parse is only run once
$helper = $this->newHelper( $cache, $parsoid );
$helper->init( $page->getTitle() );
$htmlresult = $helper->getHtml()->getRawText();
$this->assertStringContainsString( 'mocked HTML', $htmlresult );
}
public function testEtagLastModified() {
$time = time();
MWTimestamp::setFakeTime( $time );
$page = $this->getExistingTestPage( 'HtmlHelperTestPage/with/slashes' );
$cache = new HashBagOStuff();
// First, test it works if nothing was cached yet.
// Make some time pass since page was created:
MWTimestamp::setFakeTime( $time + 10 );
$helper = $this->newHelper( $cache );
$helper->init( $page->getTitle() );
$etag = $helper->getETag(); // remember etag using LastModified
$helper->getHtml(); // put HTML into the cache
// Now, test that headers work when getting from cache too.
$helper = $this->newHelper( $cache );
$helper->init( $page->getTitle() );
$this->assertNotSame( $etag, $helper->getETag() );
$etag = $helper->getETag();
$this->assertSame(
MWTimestamp::convert( TS_RFC2822, $time + 10 ),
MWTimestamp::convert( TS_RFC2822, $helper->getLastModified() )
);
// Now, expire the cache
$time += 1000;
MWTimestamp::setFakeTime( $time );
$this->assertTrue(
$page->getTitle()->invalidateCache( MWTimestamp::convert( TS_MW, $time ) ),
'Sanity: can invalidate cache'
);
DeferredUpdates::doUpdates();
$helper = $this->newHelper( $cache );
$helper->init( $page->getTitle() );
$this->assertNotSame( $etag, $helper->getETag() );
$this->assertSame(
MWTimestamp::convert( TS_RFC2822, $time ),
MWTimestamp::convert( TS_RFC2822, $helper->getLastModified() )
);
}
public function provideHandlesParsoidError() {
yield 'ClientError' => [
new ClientError( 'TEST_TEST' ),
new LocalizedHttpException(
new MessageValue( 'rest-html-backend-error' ),
400,
[
'reason' => 'TEST_TEST'
]
)
];
yield 'ResourceLimitExceededException' => [
new ResourceLimitExceededException( 'TEST_TEST' ),
new LocalizedHttpException(
new MessageValue( 'rest-resource-limit-exceeded' ),
413,
[
'reason' => 'TEST_TEST'
]
)
];
}
/**
* @dataProvider provideHandlesParsoidError
* @param Exception $parsoidException
* @param Exception $expectedException
*/
public function testHandlesParsoidError(
Exception $parsoidException,
Exception $expectedException
) {
$page = $this->getExistingTestPage( 'HtmlHelperTestPage/with/slashes' );
$parsoid = $this->createNoOpMock( Parsoid::class, [ 'wikitext2html' ] );
$parsoid->expects( $this->once() )
->method( 'wikitext2html' )
->willThrowException( $parsoidException );
$helper = $this->newHelper( null, $parsoid );
$helper->init( $page->getTitle() );
$this->expectExceptionObject( $expectedException );
$helper->getHtml();
}
}
|