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