| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Html2Wt; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Parsoid\Html2Wt\RemoveRedLinks; |
| 7 |
use Wikimedia\Parsoid\Mocks\MockEnv; |
| 8 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 9 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 10 |
|
| 11 |
/** |
| 12 |
* @coversDefaultClass \Wikimedia\Parsoid\Html2Wt\RemoveRedLinks |
| 13 |
*/ |
| 14 |
class RemoveRedLinksTest extends TestCase { |
| 15 |
|
| 16 |
/** |
| 17 |
* @covers ::run |
| 18 |
* @dataProvider provideRedLinks |
| 19 |
*/ |
| 20 |
public function testRun( string $html, string $expected, string $message ) { |
| 21 |
$doc = ContentUtils::createAndLoadDocument( $html, [ 'markNew' => true ] ); |
| 22 |
$body = DOMCompat::getBody( $doc ); |
| 23 |
$env = new MockEnv( [] ); |
| 24 |
$removeRedLinks = new RemoveRedLinks( $env ); |
| 25 |
$removeRedLinks->run( $body ); |
| 26 |
$actual = ContentUtils::ppToXML( $body, [ 'discardDataParsoid' => true, 'innerXML' => true ] ); |
| 27 |
$this->assertEquals( $expected, $actual, $message ); |
| 28 |
} |
| 29 |
|
| 30 |
public function provideRedLinks(): array { |
| 31 |
return [ |
| 32 |
[ |
| 33 |
'<a href="./Hello" rel="mw:WikiLink">Hello</a>', |
| 34 |
'<a href="./Hello" rel="mw:WikiLink">Hello</a>', |
| 35 |
'No redlink' |
| 36 |
], |
| 37 |
[ |
| 38 |
'<a href="./Hello?action=edit&redlink=1" rel="mw:WikiLink" class="new">Hello</a>', |
| 39 |
'<a href="./Hello" rel="mw:WikiLink" class="new">Hello</a>', |
| 40 |
'Redlink with ./ title syntax' |
| 41 |
], |
| 42 |
[ |
| 43 |
'<a href="/w/index.php?title=Hello&action=edit&redlink=1" rel="mw:WikiLink" class="new">Hello</a>', |
| 44 |
'<a href="/w/index.php?title=Hello" rel="mw:WikiLink" class="new">Hello</a>', |
| 45 |
'Redlink with index.php? title syntax' |
| 46 |
], |
| 47 |
[ |
| 48 |
'<a href="/w/index.php?action=edit&redlink=1&title=Hello" rel="mw:WikiLink" class="new">Hello</a>', |
| 49 |
'<a href="/w/index.php?title=Hello" rel="mw:WikiLink" class="new">Hello</a>', |
| 50 |
'Redlink with index.php? title syntax in another order' ], |
| 51 |
[ |
| 52 |
'<a href="./Hello?action=edit&redlink=1" rel="mw:WikiLink" class="new">Hello</a>', |
| 53 |
'<a href="./Hello" rel="mw:WikiLink" class="new">Hello</a>', |
| 54 |
'Redlink with ./ title syntax' |
| 55 |
], |
| 56 |
[ |
| 57 |
'<a href="./Hello?action=edit&redlink=1" class="new">Hello</a>', |
| 58 |
'<a href="./Hello?action=edit&redlink=1" class="new">Hello</a>', |
| 59 |
'Not a wikilink' |
| 60 |
], |
| 61 |
[ |
| 62 |
'<p><a href="./Hello?action=edit&redlink=1" rel="mw:WikiLink" class="new">Hello</a></p>' . |
| 63 |
'<p><a href="./Hello2?action=edit&redlink=1" rel="mw:WikiLink" class="new">Hello2</a></p>', |
| 64 |
|
| 65 |
'<p><a href="./Hello" rel="mw:WikiLink" class="new">Hello</a></p>' . |
| 66 |
'<p><a href="./Hello2" rel="mw:WikiLink" class="new">Hello2</a></p>', |
| 67 |
'Two redlinks' |
| 68 |
], |
| 69 |
[ |
| 70 |
'<a href="./Hello?action=edit&redlink=1#fragment" rel="mw:WikiLink" class="new">Hello</a>', |
| 71 |
'<a href="./Hello#fragment" rel="mw:WikiLink" class="new">Hello</a>', |
| 72 |
'Redlink with fragment' |
| 73 |
], |
| 74 |
[ |
| 75 |
'<a href="./Hello?action=edit&redlink=1#fragment?" rel="mw:WikiLink" class="new">Hello</a>', |
| 76 |
'<a href="./Hello#fragment?" rel="mw:WikiLink" class="new">Hello</a>', |
| 77 |
'Redlink with fragment with a question mark' |
| 78 |
], |
| 79 |
[ |
| 80 |
'<a href="./Hello#fragment?" rel="mw:WikiLink" class="new">Hello</a>', |
| 81 |
'<a href="./Hello#fragment?" rel="mw:WikiLink" class="new">Hello</a>', |
| 82 |
'Not a redlink, with fragment with a question mark' |
| 83 |
], |
| 84 |
// The code that allows for the creation of such an URL should be fixed; in the |
| 85 |
// meantime we still want to avoid breaking links that may still cached. |
| 86 |
[ |
| 87 |
'<a href="./Hello#fragment?action=edit&redlink=1" rel="mw:WikiLink" class="new">Hello</a>', |
| 88 |
'<a href="./Hello#fragment" rel="mw:WikiLink" class="new">Hello</a>', |
| 89 |
'Redlink with buggy fragment' |
| 90 |
], |
| 91 |
[ |
| 92 |
'<a href="./Hello?action=edit&redlink=1#fragment?action=edit&redlink=1" ' . |
| 93 |
'rel="mw:WikiLink" class="new">Hello</a>', |
| 94 |
'<a href="./Hello#fragment?action=edit&redlink=1" rel="mw:WikiLink" class="new">Hello</a>', |
| 95 |
'Redlink with a fragment containing action=edit&redlink=1' |
| 96 |
], |
| 97 |
[ |
| 98 |
'<a href="./Hello?action=edit&redlink=1" rel="mw:WikiLink" class="new">Hello</a>', |
| 99 |
'<a href="./Hello" rel="mw:WikiLink" class="new">Hello</a>', |
| 100 |
'Redlink with escaped &' |
| 101 |
], |
| 102 |
]; |
| 103 |
} |
| 104 |
} |