3 use Wikimedia\Rdbms\IDatabase
;
6 * Tests for CategoryChangesAsRdf recent changes exporter.
7 * @covers CategoryChangesAsRdf
9 class CategoryChangesAsRdfTest
extends MediaWikiLangTestCase
{
11 public function setUp() {
13 $this->setMwGlobals( [
14 'wgServer' => 'http://acme.test',
15 'wgCanonicalServer' => 'http://acme.test',
16 'wgArticlePath' => '/wiki/$1',
20 public function provideCategoryData() {
22 'delete category' => [
23 __DIR__
. "/../data/categoriesrdf/delete.sparql",
24 'getDeletedCatsIterator',
27 (object)[ 'rc_title' => 'Test', 'rc_cur_id' => 1, '_processed' => 1 ],
28 (object)[ 'rc_title' => 'Test 2', 'rc_cur_id' => 2, '_processed' => 2 ],
32 __DIR__
. "/../data/categoriesrdf/move.sparql",
33 'getMovedCatsIterator',
39 'page_title' => 'MovedTo',
40 'page_namespace' => NS_CATEGORY
,
42 'pp_propname' => null,
48 'rc_title' => 'MovedTo',
50 'page_title' => 'MovedAgain',
51 'page_namespace' => NS_CATEGORY
,
52 'pp_propname' => 'hiddencat',
58 'rc_title' => 'Test 2',
60 'page_title' => 'AlsoMoved',
61 'page_namespace' => NS_CATEGORY
,
63 'pp_propname' => null,
69 'rc_title' => 'Test 3',
71 'page_title' => 'MovedOut',
72 'page_namespace' => NS_MAIN
,
73 'pp_propname' => null,
79 'rc_title' => 'Test 4',
81 'page_title' => 'Already Done',
82 'page_namespace' => NS_CATEGORY
,
83 'pp_propname' => null,
91 'restore deleted category' => [
92 __DIR__
. "/../data/categoriesrdf/restore.sparql",
93 'getRestoredCatsIterator',
97 'rc_title' => 'Restored cat',
100 'pp_propname' => null,
106 'rc_title' => 'Restored again',
108 'pp_propname' => null,
114 'rc_title' => 'Already seen',
116 'pp_propname' => null,
125 __DIR__
. "/../data/categoriesrdf/new.sparql",
126 'getNewCatsIterator',
130 'rc_title' => 'New category',
133 'pp_propname' => null,
139 'rc_title' => 'Новая категория 😃',
142 'pp_propname' => 'hiddencat',
148 'rc_title' => 'Processed already',
155 __DIR__
. "/../data/categoriesrdf/edit.sparql",
156 'getChangedCatsIterator',
160 'rc_title' => 'Changed category',
163 'pp_propname' => null,
169 'rc_title' => 'Changed again',
171 'pp_propname' => null,
177 'rc_title' => 'Processed already',
179 'pp_propname' => null,
187 // TODO: not sure how to test categorization changes, it uses the database select...
192 * Mock category links iterator.
193 * @param IDatabase $dbr
197 public function getCategoryLinksIterator( $dbr, array $ids ) {
199 foreach ( $ids as $pageid ) {
200 $res[] = (object)[ 'cl_from' => $pageid, 'cl_to' => "Parent of $pageid" ];
206 * @dataProvider provideCategoryData
207 * @param string $testFileName Name of the test, defines filename with expected results.
208 * @param string $iterator Iterator method name to mock
209 * @param string $handler Handler method to call
210 * @param array $result Result to be returned from mock iterator
211 * @param array $preProcessed List of pre-processed items
213 public function testSparqlUpdate( $testFileName, $iterator, $handler, $result,
214 array $preProcessed = [] ) {
216 $this->getMockBuilder( CategoryChangesAsRdf
::class )
217 ->setMethods( [ $iterator, 'getCategoryLinksIterator' ] )
220 $dumpScript->expects( $this->any() )
221 ->method( 'getCategoryLinksIterator' )
222 ->willReturnCallback( [ $this, 'getCategoryLinksIterator' ] );
224 $dumpScript->expects( $this->once() )
225 ->method( $iterator )
226 ->willReturn( [ $result ] );
228 $ref = new ReflectionObject( $dumpScript );
229 $processedProperty = $ref->getProperty( 'processed' );
230 $processedProperty->setAccessible( true );
231 $processedProperty->setValue( $dumpScript, $preProcessed );
233 $output = fopen( "php://memory", "w+b" );
234 $dbr = wfGetDB( DB_REPLICA
);
235 /** @var CategoryChangesAsRdf $dumpScript */
236 $dumpScript->initialize();
237 $dumpScript->getRdf();
238 $dumpScript->$handler( $dbr, $output );
241 $sparql = stream_get_contents( $output );
242 $this->assertFileContains( $testFileName, $sparql );
244 $processed = $processedProperty->getValue( $dumpScript );
245 $expectedProcessed = array_keys( $preProcessed );
246 foreach ( $result as $row ) {
247 if ( isset( $row->_processed
) ) {
248 $this->assertArrayHasKey( $row->_processed
, $processed,
249 "ID {$row->_processed} was not processed!" );
250 $expectedProcessed[] = $row->_processed
;
253 $this->assertSame( $expectedProcessed, array_keys( $processed ),
254 'Processed array has wrong items' );
257 public function testUpdateTs() {
258 $dumpScript = new CategoryChangesAsRdf();
259 $dumpScript->initialize();
260 $update = $dumpScript->updateTS( 1503620949 );
261 $outFile = __DIR__
. '/../data/categoriesrdf/updatets.txt';
262 $this->assertFileContains( $outFile, $update );