4 * Tests for CategoryChangesAsRdf recent changes exporter.
5 * @covers CategoryChangesAsRdf
7 class CategoryChangesAsRdfTest
extends MediaWikiLangTestCase
{
9 public function setUp() {
11 $this->setMwGlobals( [
12 'wgServer' => 'http://acme.test',
13 'wgCanonicalServer' => 'http://acme.test',
14 'wgArticlePath' => '/wiki/$1',
18 public function provideCategoryData() {
20 'delete category' => [
21 __DIR__
. "/../data/categoriesrdf/delete.sparql",
22 'getDeletedCatsIterator',
25 (object)[ 'rc_title' => 'Test', 'rc_cur_id' => 1, '_processed' => 1 ],
26 (object)[ 'rc_title' => 'Test 2', 'rc_cur_id' => 2, '_processed' => 2 ],
30 __DIR__
. "/../data/categoriesrdf/move.sparql",
31 'getMovedCatsIterator',
37 'page_title' => 'MovedTo',
38 'page_namespace' => NS_CATEGORY
,
40 'pp_propname' => null,
46 'rc_title' => 'MovedTo',
48 'page_title' => 'MovedAgain',
49 'page_namespace' => NS_CATEGORY
,
50 'pp_propname' => 'hiddencat',
56 'rc_title' => 'Test 2',
58 'page_title' => 'AlsoMoved',
59 'page_namespace' => NS_CATEGORY
,
61 'pp_propname' => null,
67 'rc_title' => 'Test 3',
69 'page_title' => 'MovedOut',
70 'page_namespace' => NS_MAIN
,
71 'pp_propname' => null,
77 'rc_title' => 'Test 4',
79 'page_title' => 'Already Done',
80 'page_namespace' => NS_CATEGORY
,
81 'pp_propname' => null,
89 'restore deleted category' => [
90 __DIR__
. "/../data/categoriesrdf/restore.sparql",
91 'getRestoredCatsIterator',
95 'rc_title' => 'Restored cat',
98 'pp_propname' => null,
104 'rc_title' => 'Restored again',
106 'pp_propname' => null,
112 'rc_title' => 'Already seen',
114 'pp_propname' => null,
123 __DIR__
. "/../data/categoriesrdf/new.sparql",
124 'getNewCatsIterator',
128 'rc_title' => 'New category',
131 'pp_propname' => null,
137 'rc_title' => 'Новая категория 😃',
140 'pp_propname' => 'hiddencat',
146 'rc_title' => 'Processed already',
153 __DIR__
. "/../data/categoriesrdf/edit.sparql",
154 'getChangedCatsIterator',
158 'rc_title' => 'Changed category',
161 'pp_propname' => null,
167 'rc_title' => 'Changed again',
169 'pp_propname' => null,
175 'rc_title' => 'Processed already',
177 'pp_propname' => null,
185 // TODO: not sure how to test categorization changes, it uses the database select...
190 * Mock category links iterator.
195 public function getCategoryLinksIterator( $dbr, array $ids ) {
197 foreach ( $ids as $pageid ) {
198 $res[] = (object)[ 'cl_from' => $pageid, 'cl_to' => "Parent of $pageid" ];
204 * @dataProvider provideCategoryData
205 * @param string $testFileName Name of the test, defines filename with expected results.
206 * @param string $iterator Iterator method name to mock
207 * @param string $handler Handler method to call
208 * @param array $result Result to be returned from mock iterator
209 * @param array $preProcessed List of pre-processed items
211 public function testSparqlUpdate( $testFileName, $iterator, $handler, $result,
212 array $preProcessed = [] ) {
214 $this->getMockBuilder( CategoryChangesAsRdf
::class )
215 ->setMethods( [ $iterator, 'getCategoryLinksIterator' ] )
218 $dumpScript->expects( $this->any() )
219 ->method( 'getCategoryLinksIterator' )
220 ->willReturnCallback( [ $this, 'getCategoryLinksIterator' ] );
222 $dumpScript->expects( $this->once() )
223 ->method( $iterator )
224 ->willReturn( [ $result ] );
226 $ref = new ReflectionObject( $dumpScript );
227 $processedProperty = $ref->getProperty( 'processed' );
228 $processedProperty->setAccessible( true );
229 $processedProperty->setValue( $dumpScript, $preProcessed );
231 $output = fopen( "php://memory", "w+b" );
232 $dbr = wfGetDB( DB_REPLICA
);
233 /** @var CategoryChangesAsRdf $dumpScript */
234 $dumpScript->initialize();
235 $dumpScript->getRdf();
236 $dumpScript->$handler( $dbr, $output );
239 $sparql = stream_get_contents( $output );
240 $this->assertFileContains( $testFileName, $sparql );
242 $processed = $processedProperty->getValue( $dumpScript );
243 $expectedProcessed = $preProcessed;
244 foreach ( $result as $row ) {
245 if ( isset( $row->_processed
) ) {
246 $this->assertArrayHasKey( $row->_processed
, $processed,
247 "ID {$row->_processed} was not processed!" );
248 $expectedProcessed[] = $row->_processed
;
251 $this->assertArrayEquals( $expectedProcessed, array_keys( $processed ),
252 'Processed array has wrong items' );
255 public function testUpdateTs() {
256 $dumpScript = new CategoryChangesAsRdf();
257 $dumpScript->initialize();
258 $update = $dumpScript->updateTS( 1503620949 );
259 $outFile = __DIR__
. '/../data/categoriesrdf/updatets.txt';
260 $this->assertFileContains( $outFile, $update );