Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Config/Api/DataAccessTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Config\Api;
4
5 use Wikimedia\Parsoid\Config\Api\DataAccess;
6 use Wikimedia\Parsoid\Config\PageContent;
7 use Wikimedia\Parsoid\Config\SiteConfig;
8 use Wikimedia\Parsoid\Config\StubMetadataCollector;
9 use Wikimedia\Parsoid\Mocks\MockPageConfig;
10 use Wikimedia\Parsoid\Mocks\MockSiteConfig;
11 use Wikimedia\Parsoid\Utils\Title;
12
13 /**
14 * @covers \Wikimedia\Parsoid\Config\Api\DataAccess
15 */
16 class DataAccessTest extends \PHPUnit\Framework\TestCase {
17
18 protected function getDataAccess( string $filename, ?SiteConfig $siteConfig ): DataAccess {
19 $helper = new TestApiHelper( $this, $filename );
20 return new DataAccess( $helper, $siteConfig ?? new MockSiteConfig( [] ), [] );
21 }
22
23 public function testGetRedlinkData() {
24 $siteConfig = new MockSiteConfig( [] );
25 $pageConfig = new MockPageConfig( $siteConfig, [ 'title' => 'Foobar' ], null );
26 $data = $this->getDataAccess( 'redlinkdata', $siteConfig )->getPageInfo( $pageConfig, [
27 'Foo',
28 'Bar_(disambiguation)',
29 'Special:SpecialPages',
30 'ThisPageDoesNotExist',
31 'File:Example.svg',
32 ':', // Invalid title
33 ] );
34
35 // FIXME use locked articles from testwiki so the fixture is actually fixed
36 $data['Foo']['pageId'] = 1;
37 $data['Bar_(disambiguation)']['pageId'] = 2;
38 $data['Foo']['revId'] = 10;
39 $data['Bar_(disambiguation)']['revId'] = 11;
40
41 $this->assertSame( [
42 'Foo' => [ 'pageId' => 1, 'revId' => 10, 'missing' => false,
43 'known' => true, 'redirect' => true, 'linkclasses' => [],
44 'invalid' => false ],
45 'Bar_(disambiguation)' => [ 'pageId' => 2, 'revId' => 11, 'missing' => false,
46 'known' => true, 'redirect' => false, 'linkclasses' => [ 'mw-disambig' ],
47 'invalid' => false ],
48 'Special:SpecialPages' => [ 'pageId' => null, 'revId' => null, 'missing' => false,
49 'known' => true, 'redirect' => false, 'linkclasses' => [],
50 'invalid' => false ],
51 'ThisPageDoesNotExist' => [ 'pageId' => null, 'revId' => null, 'missing' => true,
52 'known' => false, 'redirect' => false, 'linkclasses' => [],
53 'invalid' => false ],
54 'File:Example.svg' => [ 'pageId' => null, 'revId' => null, 'missing' => true,
55 'known' => true, 'redirect' => false, 'linkclasses' => [],
56 'invalid' => false ],
57 ':' => [ 'pageId' => null, 'revId' => null, 'missing' => false,
58 'known' => false, 'redirect' => false, 'linkclasses' => [],
59 'invalid' => true ],
60 ], $data );
61 }
62
63 public function testGetFileInfo() {
64 $siteConfig = new MockSiteConfig( [] );
65 $files = [
66 'Example.svg' => [ 'width' => 100 ],
67 'DoesNotExist.png' => [ 'width' => 200 ],
68 ];
69 $results = [
70 'Example.svg' => [
71 'timestamp' => '2008-11-01T11:08:14Z',
72 'size' => 10009,
73 'width' => 600,
74 'height' => 600,
75 // phpcs:ignore Generic.Files.LineLength.TooLong
76 'thumburl' => '//upload.wikimedia.org/wikipedia/commons/thumb/8/84/Example.svg/100px-Example.svg.png',
77 'thumbwidth' => 100,
78 'thumbheight' => 100,
79 'responsiveUrls' => [
80 '1.5' => '//upload.wikimedia.org/wikipedia/commons/thumb/8/84/Example.svg/150px-Example.svg.png',
81 '2' => '//upload.wikimedia.org/wikipedia/commons/thumb/8/84/Example.svg/200px-Example.svg.png',
82 ],
83 'url' => '//upload.wikimedia.org/wikipedia/commons/8/84/Example.svg',
84 "descriptionurl" => "//commons.wikimedia.org/wiki/File:Example.svg",
85 "descriptionshorturl" => "//commons.wikimedia.org/w/index.php?curid=937952",
86 'sha1' => 'eb2a8759ddf38da50f60feb11f7208f5ec11daac',
87 'mime' => 'image/svg+xml',
88 'mediatype' => 'DRAWING',
89 'badFile' => false,
90 ],
91 'DoesNotExist.png' => null,
92 ];
93 foreach ( $files as $file => $dims ) {
94 $pageConfig = new MockPageConfig( $siteConfig, [ 'title' => 'Foobar' ], null );
95 $data = $this->getDataAccess( "fileinfo.$file", $siteConfig )
96 ->getFileInfo( $pageConfig, [ [ $file, $dims ] ] );
97 $this->assertSame( [ $results[$file] ], $data );
98 }
99 }
100
101 public function testDoPst() {
102 $siteConfig = new MockSiteConfig( [] );
103 $pageConfig = new MockPageConfig( $siteConfig, [ 'title' => 'Foobar' ], null );
104 $da = $this->getDataAccess( 'dopst', $siteConfig );
105 $ret = $da->doPst( $pageConfig, 'Foobar.[[Category:Foo|Bar]]{{cn}} {{subst:unsigned|Example}} ~~~~~' );
106 $this->assertIsString( $ret );
107 $this->assertSame( 'Foobar.[[Category:Foo|Bar]]{{cn}} <!-- Template:Unsigned -->', substr( $ret, 0, 60 ) );
108
109 // Test caching. Cache miss would make TestApiHelper throw.
110 $this->assertSame(
111 $ret, $da->doPst( $pageConfig, 'Foobar.[[Category:Foo|Bar]]{{cn}} {{subst:unsigned|Example}} ~~~~~' )
112 );
113 }
114
115 public function testParseWikitext() {
116 $siteConfig = new MockSiteConfig( [] );
117 $pageConfig = new MockPageConfig( $siteConfig, [ 'title' => 'Foobar' ], null );
118 $da = $this->getDataAccess( 'parse', $siteConfig );
119 $metadata = new StubMetadataCollector( $siteConfig );
120 $ret = $da->parseWikitext(
121 $pageConfig, $metadata, 'Foobar.[[Category:Foo|Bar]]{{cn}} {{subst:unsigned|Example}} ~~~~~'
122 );
123 $this->assertEquals(
124 // phpcs:ignore Generic.Files.LineLength.TooLong
125 "<p>Foobar.<sup class=\"noprint Inline-Template Template-Fact\" style=\"white-space:nowrap;\">[<i><a href=\"/wiki/Wikipedia:Citation_needed\" title=\"Wikipedia:Citation needed\"><span title=\"This claim needs references to reliable sources.\">citation needed</span></a></i>]</sup> {{subst:unsigned|Example}} ~~~~~\n</p>", $ret );
126 $this->assertEquals( [], $metadata->getModules() );
127 $this->assertEquals( [
128 'ext.discussionTools.init.styles',
129 ], $metadata->getModuleStyles() );
130 $this->assertEquals( [], $metadata->getJsConfigVars() );
131 $this->assertEquals( [ 'Foo' ], $metadata->getCategoryNames() );
132 $this->assertEquals( 'Bar', $metadata->getCategorySortKey( 'Foo' ) );
133
134 // Test caching. Cache miss would make TestApiHelper throw.
135 $this->assertSame(
136 $ret,
137 $da->parseWikitext(
138 $pageConfig, $metadata, 'Foobar.[[Category:Foo|Bar]]{{cn}} {{subst:unsigned|Example}} ~~~~~'
139 )
140 );
141 }
142
143 public function testPreprocessWikitext() {
144 $siteConfig = new MockSiteConfig( [] );
145 $pageConfig = new MockPageConfig( $siteConfig, [ 'title' => 'Foobar' ], null );
146 $da = $this->getDataAccess( 'preprocess', $siteConfig );
147 $metadata = new StubMetadataCollector( $siteConfig );
148 $ret = $da->preprocessWikitext(
149 $pageConfig, $metadata, 'Foobar.[[Category:Foo|Bar]]{{cn}} {{subst:unsigned|Example}} ~~~~~'
150 );
151 $this->assertEquals(
152 // phpcs:ignore Generic.Files.LineLength.TooLong
153 "Foobar.[[Category:Foo|Bar]]<sup class=\"noprint Inline-Template Template-Fact\" style=\"white-space:nowrap;\">&#91;<i>[[Wikipedia:Citation needed|<span title=\"This claim needs references to reliable sources.\">citation needed</span>]]</i>&#93;</sup> {{subst:unsigned|Example}} ~~~~~",
154 $ret
155 );
156 $this->assertEquals( [], $metadata->getModules() );
157 $this->assertEquals( [], $metadata->getModuleStyles() );
158 $this->assertEquals( [], $metadata->getJsConfigVars() );
159 $this->assertEquals( [], $metadata->getCategoryNames() );
160
161 // Test caching. Cache miss would make TestApiHelper throw.
162 $this->assertSame(
163 $ret,
164 $da->preprocessWikitext(
165 $pageConfig, $metadata, 'Foobar.[[Category:Foo|Bar]]{{cn}} {{subst:unsigned|Example}} ~~~~~'
166 )
167 );
168 }
169
170 public function testFetchTemplateSource() {
171 $siteConfig = new MockSiteConfig( [] );
172 $pageConfig = new MockPageConfig( $siteConfig, [ 'title' => 'Foobar' ], null );
173 $da = $this->getDataAccess( 'pagecontent-cur', $siteConfig );
174 $c = $da->fetchTemplateSource( $pageConfig, Title::newFromText( 'Help:Sample page', $siteConfig ) );
175 $this->assertInstanceOf( PageContent::class, $c );
176 $this->assertSame( [ 'main' ], $c->getRoles() );
177 $this->assertSame( 'wikitext', $c->getModel( 'main' ) );
178 $this->assertSame( 'text/x-wiki', $c->getFormat( 'main' ) );
179 $this->assertSame(
180 // phpcs:ignore Generic.Files.LineLength.TooLong
181 "Our '''world''' is a planet where human beings have formed many societies.\n\nNobody knows whether there are intelligent beings on other worlds.\n\nThere are about one septillion (10<sup>24</sup>) worlds in the universe.<ref name=\"Kluwer Law book\">Burci, Gian Luca; Vignes, Claude-Henri (2004). [https://books.google.com/books?id=Xou_nD9jJF0C ''World Health Organization'']. Kluwer Law International. {{ISBN|9789041122735}}. Pages 15–20.</ref><ref>{{Cite journal|title = Mortality in mental disorders and global disease burden implications: a systematic review and meta-analysis|url = https://www.ncbi.nlm.nih.gov/pubmed/25671328|journal = JAMA psychiatry|date = 2015-04-01|issn = 2168-6238|pmc = 4461039|pmid = 25671328|pages = 334-341|volume = 72|issue = 4|doi = 10.1001/jamapsychiatry.2014.2502|first = Elizabeth Reisinger|last = Walker|first2 = Robin E.|last2 = McGee|first3 = Benjamin G.|last3 = Druss}} {{Open access}}</ref><ref>{{Cite journal|title = Essential surgery: key messages from Disease Control Priorities, 3rd edition|url = https://www.ncbi.nlm.nih.gov/pubmed/25662414|journal = Lancet|date = 2015-05-30|issn = 1474-547X|pmid = 25662414|pages = 2209-2219|volume = 385|issue = 9983|doi = 10.1016/S0140-6736(15)60091-5|first = Charles N.|last = Mock|first2 = Peter|last2 = Donkor|first3 = Atul|last3 = Gawande|first4 = Dean T.|last4 = Jamison|first5 = Margaret E.|last5 = Kruk|first6 = Haile T.|last6 = Debas}}</ref>\n\n== References ==\n<references />\n\n<!-- All the contet here is public domain and has been copied from https://www.mediawiki.org/w/index.php?title=Help:Sample_page&oldid=2331983. Only add content here that has been previously been placed in the public domain as this page is used to generate screenshots -->",
182 $c->getContent( 'main' )
183 );
184
185 // Test caching. Cache miss would make TestApiHelper throw.
186 $this->assertEquals(
187 $c,
188 $da->fetchTemplateSource( $pageConfig, Title::newFromText( 'Help:Sample page', $siteConfig ) )
189 );
190 }
191
192 public function testFetchTemplateData() {
193 $siteConfig = new MockSiteConfig( [] );
194 $pageConfig = new MockPageConfig( $siteConfig, [ 'title' => 'Foobar' ], null );
195 $da = $this->getDataAccess( 'templatedata', $siteConfig );
196 $ret = $da->fetchTemplateData( $pageConfig, Title::newFromText( 'Template:Citation needed', $siteConfig ) );
197 $this->assertIsArray( $ret );
198 $this->assertArrayHasKey( 'description', $ret );
199 $this->assertArrayHasKey( 'params', $ret );
200
201 // Test caching. Cache miss would make TestApiHelper throw.
202 $this->assertSame(
203 $ret,
204 $da->fetchTemplateData( $pageConfig, Title::newFromText( 'Template:Citation needed', $siteConfig ) )
205 );
206 }
207
208 public function testAddTrackingCategory() {
209 $siteConfig = new MockSiteConfig( [] );
210 $pageConfig = new MockPageConfig( $siteConfig, [ 'title' => 'Foobar' ], null );
211 $metadata = new StubMetadataCollector( $siteConfig );
212 $da = $this->getDataAccess( 'trackingcategory', $siteConfig );
213 $da->addTrackingCategory( $pageConfig, $metadata, 'broken-file-category' );
214 $this->assertEquals( [ 'Articles_with_missing_files' ], $metadata->getCategoryNames() );
215 }
216 }