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