Add more data to categories dump
[lhc/web/wiklou.git] / tests / phpunit / maintenance / categoriesRdfTest.php
1 <?php
2
3 /**
4 * @covers CategoriesRdf
5 * @covers DumpCategoriesAsRdf
6 */
7 class CategoriesRdfTest extends MediaWikiLangTestCase {
8 public function getCategoryIterator() {
9 return [
10 // batch 1
11 [
12 (object)[
13 'page_title' => 'Category One',
14 'page_id' => 1,
15 'pp_propname' => null,
16 'cat_pages' => '20',
17 'cat_subcats' => '10',
18 'cat_files' => '3'
19 ],
20 (object)[
21 'page_title' => '2 Category Two',
22 'page_id' => 2,
23 'pp_propname' => 'hiddencat',
24 'cat_pages' => 20,
25 'cat_subcats' => 0,
26 'cat_files' => 3
27 ],
28 ],
29 // batch 2
30 [
31 (object)[
32 'page_title' => 'Третья категория',
33 'page_id' => 3,
34 'pp_propname' => null,
35 'cat_pages' => '0',
36 'cat_subcats' => '0',
37 'cat_files' => '0'
38 ],
39 ]
40 ];
41 }
42
43 public function getCategoryLinksIterator( $dbr, array $ids ) {
44 $res = [];
45 foreach ( $ids as $pageid ) {
46 $res[] = (object)[ 'cl_from' => $pageid, 'cl_to' => "Parent of $pageid" ];
47 }
48 return $res;
49 }
50
51 public function testCategoriesDump() {
52 $this->setMwGlobals( [
53 'wgServer' => 'http://acme.test',
54 'wgCanonicalServer' => 'http://acme.test',
55 'wgArticlePath' => '/wiki/$1',
56 'wgRightsUrl' => '//creativecommons.org/licenses/by-sa/3.0/',
57 ] );
58
59 $dumpScript =
60 $this->getMockBuilder( DumpCategoriesAsRdf::class )
61 ->setMethods( [ 'getCategoryIterator', 'getCategoryLinksIterator' ] )
62 ->getMock();
63
64 $dumpScript->expects( $this->once() )
65 ->method( 'getCategoryIterator' )
66 ->willReturn( $this->getCategoryIterator() );
67
68 $dumpScript->expects( $this->any() )
69 ->method( 'getCategoryLinksIterator' )
70 ->willReturnCallback( [ $this, 'getCategoryLinksIterator' ] );
71
72 /** @var DumpCategoriesAsRdf $dumpScript */
73 $logFileName = tempnam( sys_get_temp_dir(), "Categories-DumpRdfTest" );
74 $outFileName = tempnam( sys_get_temp_dir(), "Categories-DumpRdfTest" );
75
76 $dumpScript->loadParamsAndArgs(
77 null,
78 [
79 'log' => $logFileName,
80 'output' => $outFileName,
81 'format' => 'nt',
82 ]
83 );
84
85 $dumpScript->execute();
86 $actualOut = file_get_contents( $outFileName );
87 $actualOut = preg_replace(
88 '|<http://acme.test/wiki/Special:CategoryDump> <http://schema.org/dateModified> "[^"]+?"|',
89 '<http://acme.test/wiki/Special:CategoryDump> <http://schema.org/dateModified> "{DATE}"',
90 $actualOut
91 );
92
93 $outFile = __DIR__ . '/../data/categoriesrdf/categoriesRdf-out.nt';
94 $this->assertFileContains( $outFile, $actualOut );
95 }
96
97 }