Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Wt2Html/DOM/Handlers/AddLinkAttributesTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Wt2Html\DOM\Handlers;
4
5 use PHPUnit\Framework\TestCase;
6 use Wikimedia\Parsoid\Mocks\MockEnv;
7 use Wikimedia\Parsoid\Mocks\MockPageConfig;
8 use Wikimedia\Parsoid\Mocks\MockSiteConfig;
9 use Wikimedia\Parsoid\Utils\ContentUtils;
10 use Wikimedia\Parsoid\Utils\DOMCompat;
11 use Wikimedia\Parsoid\Utils\DOMDataUtils;
12 use Wikimedia\Parsoid\Wt2Html\DOM\Handlers\AddLinkAttributes;
13 use Wikimedia\Parsoid\Wt2Html\DOMPPTraverser;
14
15 class AddLinkAttributesTest extends TestCase {
16
17 private function verifyNoFollow( string $html, int $pageNs, string $expected ): void {
18 $siteConfig = new MockSiteConfig( [] );
19 $pageConfig = new MockPageConfig( $siteConfig, [ 'pagens' => $pageNs ], null );
20 $mockEnv = new MockEnv( [ 'pageConfig' => $pageConfig, "siteConfig" => $siteConfig ] );
21 $doc = ContentUtils::createAndLoadDocument( $html );
22 $body = DOMCompat::getBody( $doc );
23 $traverser = new DOMPPTraverser( null, false, true );
24 $traverser->addHandler( 'a', static fn ( $node, $state ) => AddLinkAttributes::handler( $node, $state ) );
25 $traverser->run( $mockEnv, $body, [ 'env' => $mockEnv ] );
26
27 $innerHtml = DOMCompat::getInnerHTML( $body );
28 $pattern = '/ ' . DOMDataUtils::DATA_OBJECT_ATTR_NAME . '="\d+"/';
29 $actual = preg_replace( $pattern, '', $innerHtml );
30 $this->assertEquals( $expected, $actual );
31 }
32
33 private function verifyTarget( string $html, string $target, string $expected ) {
34 $siteConfig = new MockSiteConfig( [ 'externallinktarget' => $target ] );
35 $pageConfig = new MockPageConfig( $siteConfig, [], null );
36 $mockEnv = new MockEnv( [ 'pageConfig' => $pageConfig, "siteConfig" => $siteConfig ] );
37 $doc = ContentUtils::createAndLoadDocument( $html );
38 $body = DOMCompat::getBody( $doc );
39 $traverser = new DOMPPTraverser( null, false, true );
40 $traverser->addHandler( 'a', static fn ( $node, $state ) => AddLinkAttributes::handler( $node, $state ) );
41 $traverser->run( $mockEnv, $body, [ 'env' => $mockEnv ] );
42
43 $innerHtml = DOMCompat::getInnerHTML( $body );
44 $pattern = '/ ' . DOMDataUtils::DATA_OBJECT_ATTR_NAME . '="\d+"/';
45 $actual = preg_replace( $pattern, '', $innerHtml );
46 $this->assertEquals( $expected, $actual );
47 }
48
49 /**
50 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Handlers\AddLinkAttributes::handler
51 * @dataProvider provideNoFollow
52 * @param string $html
53 * @param int $pageNs
54 * @param string $expected
55 */
56 public function testNoFollow( string $html, int $pageNs, string $expected ): void {
57 $this->verifyNoFollow( $html, $pageNs, $expected );
58 }
59
60 /**
61 * @covers \Wikimedia\Parsoid\Wt2Html\DOM\Handlers\AddLinkAttributes::handler
62 * @dataProvider provideTarget
63 * @param string $html
64 * @param string $target
65 * @param string|false $expected
66 */
67 public function testTarget( string $html, $target, string $expected ): void {
68 $this->verifyTarget( $html, $target, $expected );
69 }
70
71 public function provideTarget() {
72 return [
73 [
74 '<a href="http://www.example.com/plop" rel="mw:ExtLink">example.com</a>',
75 "_blank",
76 // The mocked SiteConfig excludes example.com from the "nofollow" rule, hence why
77 // it's not added here
78 '<a href="http://www.example.com/plop" rel="mw:ExtLink noreferrer noopener"' .
79 ' class="external text" target="_blank">example.com</a>'
80 ],
81 [
82 '<a href="http://www.somethingelse.org/plop" rel="mw:ExtLink">somethingelse.org</a>',
83 "_self",
84 '<a href="http://www.somethingelse.org/plop" rel="mw:ExtLink nofollow"' .
85 ' class="external text" target="_self">somethingelse.org</a>'
86 ],
87 [
88 '<a href="http://www.somethingelse.org/plop" rel="mw:ExtLink">somethingelse.org</a>',
89 false,
90 '<a href="http://www.somethingelse.org/plop" rel="mw:ExtLink nofollow"' .
91 ' class="external text">somethingelse.org</a>'
92 ],
93 ];
94 }
95
96 public function provideNoFollow() {
97 // The mocked SiteConfig sets nofollow exceptions for the domain example.com and for the
98 // namespace 1
99 return [
100 [
101 '<a href="http://www.example.com/plop" rel="mw:ExtLink">example.com</a>', 0,
102 '<a href="http://www.example.com/plop" rel="mw:ExtLink"' .
103 ' class="external text">example.com</a>'
104 ],
105 [
106 '<a href="http://www.example.org/plop" rel="mw:ExtLink">example.org</a>', 1,
107 '<a href="http://www.example.org/plop" rel="mw:ExtLink"' .
108 ' class="external text">example.org</a>'
109 ],
110 [
111 '<a href="http://www.example.org/plop" rel="mw:ExtLink">example.org</a>', 0,
112 '<a href="http://www.example.org/plop" rel="mw:ExtLink nofollow"' .
113 ' class="external text">example.org</a>'
114 ],
115 [
116 '<a href="./Foo" rel="mw:WikiLink">Foo</a>', 0,
117 '<a href="./Foo" rel="mw:WikiLink">Foo</a>'
118 ]
119 ];
120 }
121 }