]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/maintenance/categoriesRdfTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / maintenance / categoriesRdfTest.php
1 <?php
2
3 class CategoriesRdfTest extends MediaWikiLangTestCase {
4         public function getCategoryIterator() {
5                 return [
6                         // batch 1
7                         [
8                                 (object)[ 'page_title' => 'Category One', 'page_id' => 1 ],
9                                 (object)[ 'page_title' => '2 Category Two', 'page_id' => 2 ],
10                         ],
11                         // batch 2
12                         [
13                                 (object)[ 'page_title' => 'Третья категория', 'page_id' => 3 ],
14                         ]
15                 ];
16         }
17
18         public function getCategoryLinksIterator( $dbr, array $ids ) {
19                 $res = [];
20                 foreach ( $ids as $pageid ) {
21                         $res[] = (object)[ 'cl_from' => $pageid, 'cl_to' => "Parent of $pageid" ];
22                 }
23                 return $res;
24         }
25
26         public function testCategoriesDump() {
27                 $this->setMwGlobals( [
28                         'wgServer' => 'http://acme.test',
29                         'wgCanonicalServer' => 'http://acme.test',
30                         'wgArticlePath' => '/wiki/$1',
31                         'wgRightsUrl' => '//creativecommons.org/licenses/by-sa/3.0/',
32                 ] );
33
34                 $dumpScript =
35                         $this->getMockBuilder( DumpCategoriesAsRdf::class )
36                                 ->setMethods( [ 'getCategoryIterator', 'getCategoryLinksIterator' ] )
37                                 ->getMock();
38
39                 $dumpScript->expects( $this->once() )
40                         ->method( 'getCategoryIterator' )
41                         ->willReturn( $this->getCategoryIterator() );
42
43                 $dumpScript->expects( $this->any() )
44                         ->method( 'getCategoryLinksIterator' )
45                         ->willReturnCallback( [ $this, 'getCategoryLinksIterator' ] );
46
47                 /** @var DumpCategoriesAsRdf  $dumpScript */
48                 $logFileName = tempnam( sys_get_temp_dir(), "Categories-DumpRdfTest" );
49                 $outFileName = tempnam( sys_get_temp_dir(), "Categories-DumpRdfTest" );
50
51                 $dumpScript->loadParamsAndArgs(
52                         null,
53                         [
54                                 'log' => $logFileName,
55                                 'output' => $outFileName,
56                                 'format' => 'nt',
57                         ]
58                 );
59
60                 $dumpScript->execute();
61                 $actualOut = file_get_contents( $outFileName );
62                 $actualOut = preg_replace(
63                         '|<http://acme.test/categoriesDump> <http://schema.org/dateModified> "[^"]+?"|',
64                         '<http://acme.test/categoriesDump> <http://schema.org/dateModified> "{DATE}"',
65                         $actualOut
66                 );
67
68                 $outFile = __DIR__ . '/../data/categoriesrdf/categoriesRdf-out.nt';
69                 $this->assertFileContains( $outFile, $actualOut );
70         }
71
72 }