categoriesRdfTest.php 2.5 KB

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