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