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