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 ->onlyMethods( [ 'getCategoryIterator', 'getCategoryLinksIterator' ] ) 67 ->getMock(); 68 69 $dumpScript->expects( $this->once() ) 70 ->method( 'getCategoryIterator' ) 71 ->willReturn( $this->getCategoryIterator() ); 72 73 $dumpScript->method( 'getCategoryLinksIterator' ) 74 ->willReturnCallback( [ $this, 'getCategoryLinksIterator' ] ); 75 76 /** @var DumpCategoriesAsRdf $dumpScript */ 77 $logFileName = $this->getNewTempFile(); 78 $outFileName = $this->getNewTempFile(); 79 80 $dumpScript->loadParamsAndArgs( 81 null, 82 [ 83 'log' => $logFileName, 84 'output' => $outFileName, 85 'format' => 'nt', 86 ] 87 ); 88 89 $dumpScript->execute(); 90 $actualOut = file_get_contents( $outFileName ); 91 $actualOut = preg_replace( 92 '|<http://acme.test/wiki/Special:CategoryDump> <http://schema.org/dateModified> "[^"]+?"|', 93 '<http://acme.test/wiki/Special:CategoryDump> <http://schema.org/dateModified> "{DATE}"', 94 $actualOut 95 ); 96 97 $outFile = __DIR__ . '/../data/categoriesrdf/categoriesRdf-out.nt'; 98 $this->assertFileContains( $outFile, $actualOut ); 99 } 100 101} 102