| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
namespace Cite\Tests\Integration; |
| 6 |
|
| 7 |
use MediaWiki\Registration\ExtensionRegistry; |
| 8 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 9 |
use Wikimedia\Parsoid\Core\SelserData; |
| 10 |
use Wikimedia\Parsoid\DOM\Element; |
| 11 |
use Wikimedia\Parsoid\Mocks\MockDataAccess; |
| 12 |
use Wikimedia\Parsoid\Mocks\MockPageConfig; |
| 13 |
use Wikimedia\Parsoid\Mocks\MockPageContent; |
| 14 |
use Wikimedia\Parsoid\Mocks\MockSiteConfig; |
| 15 |
use Wikimedia\Parsoid\Parsoid; |
| 16 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 17 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class CiteParsoidTest extends \MediaWikiIntegrationTestCase { |
| 24 |
|
| 25 |
protected function setUp(): void { |
| 26 |
|
| 27 |
parent::setUp(); |
| 28 |
$this->overrideConfigValue( 'CiteResponsiveReferences', true ); |
| 29 |
$this->overrideConfigValue( 'CiteResponsiveReferencesThreshold', 10 ); |
| 30 |
} |
| 31 |
|
| 32 |
private function getSiteConfig( $options ) { |
| 33 |
$objectFactory = $this->getServiceContainer()->getObjectFactory(); |
| 34 |
$siteConfig = new class( $options, $objectFactory ) extends MockSiteConfig { |
| 35 |
private ObjectFactory $objectFactory; |
| 36 |
|
| 37 |
public function __construct( |
| 38 |
array $opts, |
| 39 |
ObjectFactory $objectFactory |
| 40 |
) { |
| 41 |
parent::__construct( $opts ); |
| 42 |
$this->objectFactory = $objectFactory; |
| 43 |
} |
| 44 |
|
| 45 |
public function getObjectFactory(): ObjectFactory { |
| 46 |
return $this->objectFactory; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
$extensionParsoidModules = |
| 51 |
ExtensionRegistry::getInstance()->getAttribute( 'ParsoidModules' ); |
| 52 |
foreach ( $extensionParsoidModules as $configOrSpec ) { |
| 53 |
$siteConfig->registerExtensionModule( $configOrSpec ); |
| 54 |
} |
| 55 |
return $siteConfig; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private function parseWT( string $wt, array $pageOpts = [], $wrapSections = false ): Element { |
| 65 |
$siteConfig = $this->getSiteConfig( [] ); |
| 66 |
$dataAccess = new MockDataAccess( $siteConfig, [] ); |
| 67 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 68 |
|
| 69 |
$content = new MockPageContent( [ 'main' => $wt ] ); |
| 70 |
$pageConfig = new MockPageConfig( $siteConfig, $pageOpts, $content ); |
| 71 |
$html = $parsoid->wikitext2html( $pageConfig, [ "wrapSections" => $wrapSections ] ); |
| 72 |
|
| 73 |
$doc = DOMUtils::parseHTML( $html ); |
| 74 |
|
| 75 |
$docBody = DOMCompat::getBody( $doc ); |
| 76 |
|
| 77 |
return( $docBody ); |
| 78 |
} |
| 79 |
|
| 80 |
private function wtToLint( string $wt, array $options = [] ): array { |
| 81 |
$opts = [ |
| 82 |
'prefix' => $options['prefix'] ?? 'enwiki', |
| 83 |
'pageName' => $options['pageName'] ?? 'main', |
| 84 |
'wrapSections' => false |
| 85 |
]; |
| 86 |
|
| 87 |
$siteOptions = [ 'linting' => true ] + $options; |
| 88 |
$siteConfig = $this->getSiteConfig( $siteOptions ); |
| 89 |
$dataAccess = new MockDataAccess( $siteConfig, [] ); |
| 90 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 91 |
|
| 92 |
$content = new MockPageContent( [ $opts['pageName'] => $wt ] ); |
| 93 |
$pageConfig = new MockPageConfig( $siteConfig, [], $content ); |
| 94 |
|
| 95 |
return $parsoid->wikitext2lint( $pageConfig, [] ); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function testWikilinkUseDotSlashPrefix(): void { |
| 106 |
$description = "Regression Specs: should use ./ prefixed urls for cite links"; |
| 107 |
$wt = "a [[Foo]] <ref>b</ref>"; |
| 108 |
$docBody = $this->parseWT( $wt, [ 'title' => 'Main_Page' ] ); |
| 109 |
|
| 110 |
$attrib = DOMCompat::getAttribute( |
| 111 |
DOMCompat::querySelectorAll( $docBody, ".mw-ref a" )[0], |
| 112 |
'href' |
| 113 |
); |
| 114 |
$this->assertEquals( './Main_Page#cite_note-1', $attrib, $description ); |
| 115 |
|
| 116 |
$attrib = DOMCompat::getAttribute( |
| 117 |
DOMCompat::querySelectorAll( $docBody, "#cite_note-1 a" )[0], |
| 118 |
'href' |
| 119 |
); |
| 120 |
$this->assertEquals( './Main_Page#cite_ref-1', $attrib, $description ); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function testSelserFollowsWrap(): void { |
| 131 |
$wt = 'Hi ho <ref follow="123">hi ho</ref>'; |
| 132 |
$html = <<<EOT |
| 133 |
<p data-parsoid='{"dsr":[0,35,0,0]}'>Hi ho <sup about="#mwt2" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Extension/ref mw:Error" data-parsoid='{"dsr":[6,35,18,6]}' data-mw='{"name":"ref","attrs":{"follow":"123"},"body":{"id":"mw-reference-text-cite_note-1"},"errors":[{"key":"cite_error_references_missing_key","params":["123"]}]}'><a href="./Main_Page#cite_note-1" style="counter-reset: mw-Ref 1;" data-parsoid="{}"><span class="mw-reflink-text" data-parsoid="{}">[1]</span></a></sup></p> |
| 134 |
|
| 135 |
<div class="mw-references-wrap" typeof="mw:Extension/references" about="#mwt3" data-parsoid='{"dsr":[36,36,0,0]}' data-mw='{"name":"references","attrs":{},"autoGenerated":true}'><ol class="mw-references references" data-parsoid="{}"><li about="#cite_note-1" id="cite_note-1" data-parsoid="{}"><a href="./Main_Page#cite_ref-1" rel="mw:referencedBy" data-parsoid="{}"><span class="mw-linkback-text" data-parsoid="{}">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text" data-parsoid="{}">hi ho</span></li></ol></div> |
| 136 |
EOT; |
| 137 |
$editedHtml = <<<EOT |
| 138 |
<p data-parsoid='{"dsr":[0,35,0,0]}'>Ha ha <sup about="#mwt2" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Extension/ref mw:Error" data-parsoid='{"dsr":[6,35,18,6]}' data-mw='{"name":"ref","attrs":{"follow":"123"},"body":{"id":"mw-reference-text-cite_note-1"},"errors":[{"key":"cite_error_references_missing_key","params":["123"]}]}'><a href="./Main_Page#cite_note-1" style="counter-reset: mw-Ref 1;" data-parsoid="{}"><span class="mw-reflink-text" data-parsoid="{}">[1]</span></a></sup></p> |
| 139 |
|
| 140 |
<div class="mw-references-wrap" typeof="mw:Extension/references" about="#mwt3" data-parsoid='{"dsr":[36,36,0,0]}' data-mw='{"name":"references","attrs":{},"autoGenerated":true}'><ol class="mw-references references" data-parsoid="{}"><li about="#cite_note-1" id="cite_note-1" data-parsoid="{}"><a href="./Main_Page#cite_ref-1" rel="mw:referencedBy" data-parsoid="{}"><span class="mw-linkback-text" data-parsoid="{}">↑ </span></a> <span id="mw-reference-text-cite_note-1" class="mw-reference-text" data-parsoid="{}">hi ho</span></li></ol></div> |
| 141 |
EOT; |
| 142 |
|
| 143 |
$siteConfig = $this->getSiteConfig( [] ); |
| 144 |
$dataAccess = new MockDataAccess( $siteConfig, [] ); |
| 145 |
$parsoid = new Parsoid( $siteConfig, $dataAccess ); |
| 146 |
$content = new MockPageContent( [ 'main' => $wt ] ); |
| 147 |
$pageConfig = new MockPageConfig( $siteConfig, [], $content ); |
| 148 |
|
| 149 |
|
| 150 |
$editedWt = $parsoid->html2wikitext( $pageConfig, $editedHtml, [], null ); |
| 151 |
$this->assertEquals( "Ha ha <ref follow=\"123\"></ref>\n\n<references />", $editedWt ); |
| 152 |
|
| 153 |
|
| 154 |
$selserData = new SelserData( $wt, $html ); |
| 155 |
$editedWt = $parsoid->html2wikitext( $pageConfig, $editedHtml, [], $selserData ); |
| 156 |
$this->assertEquals( "Ha ha <ref follow=\"123\">hi ho</ref>\n\n", $editedWt ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function testLintIssueInRefTags(): void { |
| 163 |
$desc = "should attribute linter issues to the ref tag"; |
| 164 |
$result = $this->wtToLint( "a <ref><b>x</ref> <references/>" ); |
| 165 |
$this->assertCount( 1, $result, $desc ); |
| 166 |
$this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc ); |
| 167 |
$this->assertEquals( [ 7, 11, 3, 0 ], $result[0]['dsr'], $desc ); |
| 168 |
$this->assertTrue( isset( $result[0]['params'] ), $desc ); |
| 169 |
$this->assertEquals( 'b', $result[0]['params']['name'], $desc ); |
| 170 |
|
| 171 |
$desc = "should attribute linter issues to the ref tag even if references is templated"; |
| 172 |
$result = $this->wtToLint( "a <ref><b>x</ref> {{1x|<references/>}}" ); |
| 173 |
$this->assertCount( 1, $result, $desc ); |
| 174 |
$this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc ); |
| 175 |
$this->assertEquals( [ 7, 11, 3, 0 ], $result[0]['dsr'], $desc ); |
| 176 |
$this->assertTrue( isset( $result[0]['params'] ), $desc ); |
| 177 |
$this->assertEquals( 'b', $result[0]['params']['name'], $desc ); |
| 178 |
|
| 179 |
$desc = "should attribute linter issues to the ref tag even when " . |
| 180 |
"ref and references are both templated"; |
| 181 |
$wt = "a <ref><b>x</ref> b <ref>{{1x|<b>x}}</ref> " . |
| 182 |
"{{1x|c <ref><b>y</ref>}} {{1x|<references/>}}"; |
| 183 |
$result = $this->wtToLint( $wt ); |
| 184 |
$this->assertCount( 3, $result, $desc ); |
| 185 |
$this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc ); |
| 186 |
$this->assertEquals( [ 7, 11, 3, 0 ], $result[0]['dsr'], $desc ); |
| 187 |
$this->assertTrue( isset( $result[0]['params'] ), $desc ); |
| 188 |
$this->assertEquals( 'b', $result[0]['params']['name'], $desc ); |
| 189 |
|
| 190 |
$this->assertEquals( 'missing-end-tag', $result[1]['type'], $desc ); |
| 191 |
$this->assertEquals( [ 25, 36, null, null ], $result[1]['dsr'], $desc ); |
| 192 |
$this->assertTrue( isset( $result[1]['params'] ), $desc ); |
| 193 |
$this->assertEquals( 'b', $result[1]['params']['name'], $desc ); |
| 194 |
$this->assertTrue( isset( $result[1]['templateInfo'] ), $desc ); |
| 195 |
$this->assertEquals( 'Template:1x', $result[1]['templateInfo']['name'], $desc ); |
| 196 |
|
| 197 |
$this->assertEquals( 'missing-end-tag', $result[2]['type'], $desc ); |
| 198 |
$this->assertEquals( [ 43, 67, null, null ], $result[2]['dsr'], $desc ); |
| 199 |
$this->assertTrue( isset( $result[2]['params'] ), $desc ); |
| 200 |
$this->assertEquals( 'b', $result[2]['params']['name'], $desc ); |
| 201 |
$this->assertTrue( isset( $result[2]['templateInfo'] ), $desc ); |
| 202 |
$this->assertEquals( 'Template:1x', $result[2]['templateInfo']['name'], $desc ); |
| 203 |
|
| 204 |
$desc = "should attribute linter issues properly when ref " . |
| 205 |
"tags are in non-templated references tag"; |
| 206 |
$wt = "a <ref><s>x</ref> b <ref name='x' /> <references> " . |
| 207 |
"<ref name='x'>{{1x|<b>boo}}</ref> </references>"; |
| 208 |
$result = $this->wtToLint( $wt ); |
| 209 |
$this->assertCount( 2, $result, $desc ); |
| 210 |
|
| 211 |
$this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc ); |
| 212 |
$this->assertEquals( [ 7, 11, 3, 0 ], $result[0]['dsr'], $desc ); |
| 213 |
$this->assertTrue( isset( $result[0]['params'] ), $desc ); |
| 214 |
$this->assertEquals( 's', $result[0]['params']['name'], $desc ); |
| 215 |
|
| 216 |
$this->assertEquals( 'missing-end-tag', $result[1]['type'], $desc ); |
| 217 |
$this->assertEquals( [ 64, 77, null, null ], $result[1]['dsr'], $desc ); |
| 218 |
$this->assertTrue( isset( $result[1]['params'] ), $desc ); |
| 219 |
$this->assertEquals( 'b', $result[1]['params']['name'], $desc ); |
| 220 |
$this->assertTrue( isset( $result[1]['templateInfo'] ), $desc ); |
| 221 |
$this->assertEquals( 'Template:1x', $result[1]['templateInfo']['name'], $desc ); |
| 222 |
|
| 223 |
$desc = "should lint inside ref with redefinition"; |
| 224 |
$wt = "<ref name=\"test\">123</ref>\n" . |
| 225 |
"<ref name=\"test\"><s>345</ref>\n" . |
| 226 |
"</references>"; |
| 227 |
$result = $this->wtToLint( $wt ); |
| 228 |
$this->assertCount( 1, $result, $desc ); |
| 229 |
$this->assertEquals( 'missing-end-tag', $result[0]['type'], $desc ); |
| 230 |
$this->assertEquals( [ 44, 50, 3, 0 ], $result[0]['dsr'], $desc ); |
| 231 |
$this->assertTrue( isset( $result[0]['params'] ), $desc ); |
| 232 |
$this->assertEquals( 's', $result[0]['params']['name'], $desc ); |
| 233 |
|
| 234 |
$desc = "should not get into a cycle trying to lint ref in ref"; |
| 235 |
$result = $this->wtToLint( |
| 236 |
"{{#tag:ref|<ref name='y' />|name='x'}}{{#tag:ref|<ref name='x' />|name='y'}}<ref name='x' />" |
| 237 |
); |
| 238 |
$this->wtToLint( "<ref name='x' />{{#tag:ref|<ref name='x' />|name=x}}" ); |
| 239 |
} |
| 240 |
} |