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