categoryChangesRdfTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Tests for CategoryChangesAsRdf recent changes exporter.
  4. * @covers CategoryChangesAsRdf
  5. */
  6. class CategoryChangesRdfTest extends MediaWikiLangTestCase {
  7. public function setUp() {
  8. parent::setUp();
  9. $this->setMwGlobals( [
  10. 'wgServer' => 'http://acme.test',
  11. 'wgCanonicalServer' => 'http://acme.test',
  12. 'wgArticlePath' => '/wiki/$1',
  13. ] );
  14. }
  15. public function provideCategoryData() {
  16. return [
  17. 'delete category' => [
  18. __DIR__ . "/../data/categoriesrdf/delete.sparql",
  19. 'getDeletedCatsIterator',
  20. 'handleDeletes',
  21. [
  22. (object)[ 'rc_title' => 'Test', 'rc_cur_id' => 1, '_processed' => 1 ],
  23. (object)[ 'rc_title' => 'Test 2', 'rc_cur_id' => 2, '_processed' => 2 ],
  24. ],
  25. ],
  26. 'move category' => [
  27. __DIR__ . "/../data/categoriesrdf/move.sparql",
  28. 'getMovedCatsIterator',
  29. 'handleMoves',
  30. [
  31. (object)[
  32. 'rc_title' => 'Test',
  33. 'rc_cur_id' => 4,
  34. 'page_title' => 'MovedTo',
  35. 'page_namespace' => NS_CATEGORY,
  36. '_processed' => 4,
  37. 'pp_propname' => null,
  38. 'cat_pages' => 10,
  39. 'cat_subcats' => 2,
  40. 'cat_files' => 1,
  41. ],
  42. (object)[
  43. 'rc_title' => 'MovedTo',
  44. 'rc_cur_id' => 4,
  45. 'page_title' => 'MovedAgain',
  46. 'page_namespace' => NS_CATEGORY,
  47. 'pp_propname' => 'hiddencat',
  48. 'cat_pages' => 10,
  49. 'cat_subcats' => 2,
  50. 'cat_files' => 1,
  51. ],
  52. (object)[
  53. 'rc_title' => 'Test 2',
  54. 'rc_cur_id' => 5,
  55. 'page_title' => 'AlsoMoved',
  56. 'page_namespace' => NS_CATEGORY,
  57. '_processed' => 5,
  58. 'pp_propname' => null,
  59. 'cat_pages' => 10,
  60. 'cat_subcats' => 2,
  61. 'cat_files' => 1,
  62. ],
  63. (object)[
  64. 'rc_title' => 'Test 3',
  65. 'rc_cur_id' => 6,
  66. 'page_title' => 'MovedOut',
  67. 'page_namespace' => NS_MAIN,
  68. 'pp_propname' => null,
  69. 'cat_pages' => 10,
  70. 'cat_subcats' => 2,
  71. 'cat_files' => 1,
  72. ],
  73. (object)[
  74. 'rc_title' => 'Test 4',
  75. 'rc_cur_id' => 7,
  76. 'page_title' => 'Already Done',
  77. 'page_namespace' => NS_CATEGORY,
  78. 'pp_propname' => null,
  79. 'cat_pages' => 10,
  80. 'cat_subcats' => 2,
  81. 'cat_files' => 1,
  82. ],
  83. ],
  84. [ 7 => true ],
  85. ],
  86. 'restore deleted category' => [
  87. __DIR__ . "/../data/categoriesrdf/restore.sparql",
  88. 'getRestoredCatsIterator',
  89. 'handleRestores',
  90. [
  91. (object)[
  92. 'rc_title' => 'Restored cat',
  93. 'rc_cur_id' => 10,
  94. '_processed' => 10,
  95. 'pp_propname' => null,
  96. 'cat_pages' => 10,
  97. 'cat_subcats' => 2,
  98. 'cat_files' => 1,
  99. ],
  100. (object)[
  101. 'rc_title' => 'Restored again',
  102. 'rc_cur_id' => 10,
  103. 'pp_propname' => null,
  104. 'cat_pages' => 10,
  105. 'cat_subcats' => 2,
  106. 'cat_files' => 1,
  107. ],
  108. (object)[
  109. 'rc_title' => 'Already seen',
  110. 'rc_cur_id' => 11,
  111. 'pp_propname' => null,
  112. 'cat_pages' => 10,
  113. 'cat_subcats' => 2,
  114. 'cat_files' => 1,
  115. ],
  116. ],
  117. [ 11 => true ],
  118. ],
  119. 'new page' => [
  120. __DIR__ . "/../data/categoriesrdf/new.sparql",
  121. 'getNewCatsIterator',
  122. 'handleAdds',
  123. [
  124. (object)[
  125. 'rc_title' => 'New category',
  126. 'rc_cur_id' => 20,
  127. '_processed' => 20,
  128. 'pp_propname' => null,
  129. 'cat_pages' => 10,
  130. 'cat_subcats' => 2,
  131. 'cat_files' => 1,
  132. ],
  133. (object)[
  134. 'rc_title' => 'Новая категория 😃',
  135. 'rc_cur_id' => 21,
  136. '_processed' => 21,
  137. 'pp_propname' => 'hiddencat',
  138. 'cat_pages' => 10,
  139. 'cat_subcats' => 2,
  140. 'cat_files' => 1,
  141. ],
  142. (object)[
  143. 'rc_title' => 'Processed already',
  144. 'rc_cur_id' => 22,
  145. ],
  146. ],
  147. [ 22 => true ],
  148. ],
  149. 'edit category' => [
  150. __DIR__ . "/../data/categoriesrdf/edit.sparql",
  151. 'getChangedCatsIterator',
  152. 'handleEdits',
  153. [
  154. (object)[
  155. 'rc_title' => 'Changed category',
  156. 'rc_cur_id' => 30,
  157. '_processed' => 30,
  158. 'pp_propname' => null,
  159. 'cat_pages' => 10,
  160. 'cat_subcats' => 2,
  161. 'cat_files' => 1,
  162. ],
  163. (object)[
  164. 'rc_title' => 'Changed again',
  165. 'rc_cur_id' => 30,
  166. 'pp_propname' => null,
  167. 'cat_pages' => 12,
  168. 'cat_subcats' => 2,
  169. 'cat_files' => 1,
  170. ],
  171. (object)[
  172. 'rc_title' => 'Processed already',
  173. 'rc_cur_id' => 31,
  174. 'pp_propname' => null,
  175. 'cat_pages' => 10,
  176. 'cat_subcats' => 2,
  177. 'cat_files' => 1,
  178. ],
  179. ],
  180. [ 31 => true ],
  181. ],
  182. // TODO: not sure how to test categorization changes, it uses the database select...
  183. ];
  184. }
  185. /**
  186. * Mock category links iterator.
  187. * @param $dbr
  188. * @param array $ids
  189. * @return array
  190. */
  191. public function getCategoryLinksIterator( $dbr, array $ids ) {
  192. $res = [];
  193. foreach ( $ids as $pageid ) {
  194. $res[] = (object)[ 'cl_from' => $pageid, 'cl_to' => "Parent of $pageid" ];
  195. }
  196. return $res;
  197. }
  198. /**
  199. * @dataProvider provideCategoryData
  200. * @param string $testFileName Name of the test, defines filename with expected results.
  201. * @param string $iterator Iterator method name to mock
  202. * @param string $handler Handler method to call
  203. * @param array $result Result to be returned from mock iterator
  204. * @param array $preProcessed List of pre-processed items
  205. */
  206. public function testSparqlUpdate( $testFileName, $iterator, $handler, $result,
  207. array $preProcessed = [] ) {
  208. $dumpScript =
  209. $this->getMockBuilder( CategoryChangesAsRdf::class )
  210. ->setMethods( [ $iterator, 'getCategoryLinksIterator' ] )
  211. ->getMock();
  212. $dumpScript->expects( $this->any() )
  213. ->method( 'getCategoryLinksIterator' )
  214. ->willReturnCallback( [ $this, 'getCategoryLinksIterator' ] );
  215. $dumpScript->expects( $this->once() )
  216. ->method( $iterator )
  217. ->willReturn( [ $result ] );
  218. $ref = new ReflectionObject( $dumpScript );
  219. $processedProperty = $ref->getProperty( 'processed' );
  220. $processedProperty->setAccessible( true );
  221. $processedProperty->setValue( $dumpScript, $preProcessed );
  222. $output = fopen( "php://memory", "w+b" );
  223. $dbr = wfGetDB( DB_REPLICA );
  224. /** @var CategoryChangesAsRdf $dumpScript */
  225. $dumpScript->initialize();
  226. $dumpScript->getRdf();
  227. $dumpScript->$handler( $dbr, $output );
  228. rewind( $output );
  229. $sparql = stream_get_contents( $output );
  230. $this->assertFileContains( $testFileName, $sparql );
  231. $processed = $processedProperty->getValue( $dumpScript );
  232. $expectedProcessed = $preProcessed;
  233. foreach ( $result as $row ) {
  234. if ( isset( $row->_processed ) ) {
  235. $this->assertArrayHasKey( $row->_processed, $processed,
  236. "ID {$row->_processed} was not processed!" );
  237. $expectedProcessed[] = $row->_processed;
  238. }
  239. }
  240. $this->assertArrayEquals( $expectedProcessed, array_keys( $processed ),
  241. 'Processed array has wrong items' );
  242. }
  243. public function testUpdateTs() {
  244. $dumpScript = new CategoryChangesAsRdf();
  245. $dumpScript->initialize();
  246. $update = $dumpScript->updateTS( 1503620949 );
  247. $outFile = __DIR__ . '/../data/categoriesrdf/updatets.txt';
  248. $this->assertFileContains( $outFile, $update );
  249. }
  250. }