3 namespace MediaWiki\Tests\Revision
;
5 use MediaWiki\MediaWikiServices
;
6 use MediaWiki\Revision\SlotRecord
;
11 * Tests RevisionStore against the post-migration MCR DB schema.
13 * @group RevisionStore
17 class RevisionQueryInfoTest
extends MediaWikiTestCase
{
19 protected function getRevisionQueryFields( $returnTextIdField = true ) {
30 if ( $returnTextIdField ) {
31 $fields[] = 'rev_text_id';
36 protected function getArchiveQueryFields( $returnTextFields = true ) {
50 if ( $returnTextFields ) {
51 $fields[] = 'ar_text_id';
56 protected function getNewCommentQueryFields( $prefix ) {
58 "{$prefix}_comment_text" => "comment_{$prefix}_comment.comment_text",
59 "{$prefix}_comment_data" => "comment_{$prefix}_comment.comment_data",
60 "{$prefix}_comment_cid" => "comment_{$prefix}_comment.comment_id",
64 protected function getNewActorQueryFields( $prefix, $tmp = false ) {
66 "{$prefix}_user" => "actor_{$prefix}_user.actor_user",
67 "{$prefix}_user_text" => "actor_{$prefix}_user.actor_name",
68 "{$prefix}_actor" => $tmp ?
"temp_{$prefix}_user.{$prefix}actor_actor" : "{$prefix}_actor",
72 protected function getTextQueryFields() {
79 protected function getPageQueryFields() {
90 protected function getUserQueryFields() {
96 protected function getContentHandlerQueryFields( $prefix ) {
98 "{$prefix}_content_format",
99 "{$prefix}_content_model",
103 public function provideArchiveQueryInfo() {
104 yield
'MCR, comment, actor' => [
106 'wgMultiContentRevisionSchemaMigrationStage' => SCHEMA_COMPAT_NEW
,
111 'actor_ar_user' => 'actor',
112 'comment_ar_comment' => 'comment',
114 'fields' => array_merge(
115 $this->getArchiveQueryFields( false ),
116 $this->getNewActorQueryFields( 'ar' ),
117 $this->getNewCommentQueryFields( 'ar' )
121 => [ 'JOIN', 'comment_ar_comment.comment_id = ar_comment_id' ],
122 'actor_ar_user' => [ 'JOIN', 'actor_ar_user.actor_id = ar_actor' ],
126 yield
'read-new MCR, comment, actor' => [
128 'wgContentHandlerUseDB' => true,
129 'wgMultiContentRevisionSchemaMigrationStage'
130 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
,
135 'actor_ar_user' => 'actor',
136 'comment_ar_comment' => 'comment',
138 'fields' => array_merge(
139 $this->getArchiveQueryFields( false ),
140 $this->getNewActorQueryFields( 'ar' ),
141 $this->getNewCommentQueryFields( 'ar' )
145 => [ 'JOIN', 'comment_ar_comment.comment_id = ar_comment_id' ],
146 'actor_ar_user' => [ 'JOIN', 'actor_ar_user.actor_id = ar_actor' ],
150 yield
'MCR write-both/read-old' => [
152 'wgContentHandlerUseDB' => true,
153 'wgMultiContentRevisionSchemaMigrationStage'
154 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
,
159 'actor_ar_user' => 'actor',
160 'comment_ar_comment' => 'comment',
162 'fields' => array_merge(
163 $this->getArchiveQueryFields( true ),
164 $this->getContentHandlerQueryFields( 'ar' ),
165 $this->getNewActorQueryFields( 'ar' ),
166 $this->getNewCommentQueryFields( 'ar' )
170 => [ 'JOIN', 'comment_ar_comment.comment_id = ar_comment_id' ],
171 'actor_ar_user' => [ 'JOIN', 'actor_ar_user.actor_id = ar_actor' ],
177 public function provideQueryInfo() {
178 // TODO: more option variations
179 yield
'MCR, page, user, comment, actor' => [
181 'wgContentHandlerUseDB' => true,
182 'wgMultiContentRevisionSchemaMigrationStage' => SCHEMA_COMPAT_NEW
,
190 'temp_rev_user' => 'revision_actor_temp',
191 'temp_rev_comment' => 'revision_comment_temp',
192 'actor_rev_user' => 'actor',
193 'comment_rev_comment' => 'comment',
195 'fields' => array_merge(
196 $this->getRevisionQueryFields( false ),
197 $this->getPageQueryFields(),
198 $this->getUserQueryFields(),
199 $this->getNewActorQueryFields( 'rev', 'temp_rev_user.revactor_actor' ),
200 $this->getNewCommentQueryFields( 'rev' )
203 'page' => [ 'JOIN', [ 'page_id = rev_page' ] ],
206 [ 'actor_rev_user.actor_user != 0', 'user_id = actor_rev_user.actor_user' ],
208 'comment_rev_comment' => [
210 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
212 'actor_rev_user' => [
214 'actor_rev_user.actor_id = temp_rev_user.revactor_actor',
216 'temp_rev_user' => [ 'JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
217 'temp_rev_comment' => [ 'JOIN', 'temp_rev_comment.revcomment_rev = rev_id' ],
218 'temp_rev_user' => [ 'JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
219 'actor_rev_user' => [ 'JOIN', 'actor_rev_user.actor_id = temp_rev_user.revactor_actor' ],
223 yield
'MCR read-new, page, user, comment, actor' => [
225 'wgContentHandlerUseDB' => true,
226 'wgMultiContentRevisionSchemaMigrationStage'
227 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
,
235 'temp_rev_user' => 'revision_actor_temp',
236 'temp_rev_comment' => 'revision_comment_temp',
237 'actor_rev_user' => 'actor',
238 'comment_rev_comment' => 'comment',
240 'fields' => array_merge(
241 $this->getRevisionQueryFields( false ),
242 $this->getPageQueryFields(),
243 $this->getUserQueryFields(),
244 $this->getNewActorQueryFields( 'rev', 'temp_rev_user.revactor_actor' ),
245 $this->getNewCommentQueryFields( 'rev' )
248 'page' => [ 'JOIN', [ 'page_id = rev_page' ] ],
252 'actor_rev_user.actor_user != 0',
253 'user_id = actor_rev_user.actor_user',
256 'temp_rev_comment' => [ 'JOIN', 'temp_rev_comment.revcomment_rev = rev_id' ],
257 'comment_rev_comment'
258 => [ 'JOIN', 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id' ],
259 'temp_rev_user' => [ 'JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
260 'actor_rev_user' => [ 'JOIN', 'actor_rev_user.actor_id = temp_rev_user.revactor_actor' ],
264 yield
'MCR read-new' => [
266 'wgContentHandlerUseDB' => true,
267 'wgMultiContentRevisionSchemaMigrationStage'
268 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
,
276 'temp_rev_user' => 'revision_actor_temp',
277 'temp_rev_comment' => 'revision_comment_temp',
278 'actor_rev_user' => 'actor',
279 'comment_rev_comment' => 'comment',
281 'fields' => array_merge(
282 $this->getRevisionQueryFields( false ),
283 $this->getPageQueryFields(),
284 $this->getUserQueryFields(),
285 $this->getNewActorQueryFields( 'rev', 'temp_rev_user.revactor_actor' ),
286 $this->getNewCommentQueryFields( 'rev' )
289 'page' => [ 'JOIN', [ 'page_id = rev_page' ] ],
293 'actor_rev_user.actor_user != 0',
294 'user_id = actor_rev_user.actor_user'
297 'temp_rev_comment' => [ 'JOIN', 'temp_rev_comment.revcomment_rev = rev_id' ],
298 'comment_rev_comment'
299 => [ 'JOIN', 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id' ],
300 'temp_rev_user' => [ 'JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
301 'actor_rev_user' => [ 'JOIN', 'actor_rev_user.actor_id = temp_rev_user.revactor_actor' ],
305 yield
'MCR write-both/read-old' => [
307 'wgContentHandlerUseDB' => true,
308 'wgMultiContentRevisionSchemaMigrationStage'
309 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
,
315 'temp_rev_comment' => 'revision_comment_temp',
316 'comment_rev_comment' => 'comment',
317 'temp_rev_user' => 'revision_actor_temp',
318 'actor_rev_user' => 'actor',
320 'fields' => array_merge(
321 $this->getRevisionQueryFields( true ),
322 $this->getContentHandlerQueryFields( 'rev' ),
323 $this->getNewActorQueryFields( 'rev', 'temp_rev_user.revactor_actor' ),
324 $this->getNewCommentQueryFields( 'rev' )
327 'temp_rev_comment' => [ 'JOIN', 'temp_rev_comment.revcomment_rev = rev_id' ],
328 'comment_rev_comment'
329 => [ 'JOIN', 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id' ],
330 'temp_rev_user' => [ 'JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
331 'actor_rev_user' => [ 'JOIN', 'actor_rev_user.actor_id = temp_rev_user.revactor_actor' ],
335 yield
'MCR write-both/read-old, page, user' => [
337 'wgContentHandlerUseDB' => true,
338 'wgMultiContentRevisionSchemaMigrationStage'
339 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
,
347 'temp_rev_comment' => 'revision_comment_temp',
348 'comment_rev_comment' => 'comment',
349 'temp_rev_user' => 'revision_actor_temp',
350 'actor_rev_user' => 'actor',
352 'fields' => array_merge(
353 $this->getRevisionQueryFields( true ),
354 $this->getContentHandlerQueryFields( 'rev' ),
355 $this->getUserQueryFields(),
356 $this->getPageQueryFields(),
357 $this->getNewActorQueryFields( 'rev', 'temp_rev_user.revactor_actor' ),
358 $this->getNewCommentQueryFields( 'rev' )
361 'page' => [ 'JOIN', [ 'page_id = rev_page' ] ],
365 'actor_rev_user.actor_user != 0',
366 'user_id = actor_rev_user.actor_user',
369 'temp_rev_comment' => [ 'JOIN', 'temp_rev_comment.revcomment_rev = rev_id' ],
370 'comment_rev_comment'
371 => [ 'JOIN', 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id' ],
372 'temp_rev_user' => [ 'JOIN', 'temp_rev_user.revactor_rev = rev_id' ],
373 'actor_rev_user' => [ 'JOIN', 'actor_rev_user.actor_id = temp_rev_user.revactor_actor' ],
379 public function provideSlotsQueryInfo() {
380 yield
'MCR, no options' => [
382 'wgMultiContentRevisionSchemaMigrationStage' => SCHEMA_COMPAT_NEW
,
397 'rev_id' => 'slot_revision_id',
398 'role_id' => 'slot_role_id'
402 yield
'MCR, role option' => [
404 'wgMultiContentRevisionSchemaMigrationStage' => SCHEMA_COMPAT_NEW
,
420 'slot_roles' => [ 'LEFT JOIN', [ 'slot_role_id = role_id' ] ],
423 'rev_id' => 'slot_revision_id',
424 'role_id' => 'slot_role_id'
428 yield
'MCR read-new, content option' => [
430 'wgMultiContentRevisionSchemaMigrationStage'
431 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
,
450 'content' => [ 'JOIN', [ 'slot_content_id = content_id' ] ],
453 'rev_id' => 'slot_revision_id',
454 'role_id' => 'slot_role_id',
455 'model_id' => 'content_model',
459 yield
'MCR read-new, content and model options' => [
461 'wgMultiContentRevisionSchemaMigrationStage'
462 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
,
464 [ 'content', 'model' ],
483 'content' => [ 'JOIN', [ 'slot_content_id = content_id' ] ],
484 'content_models' => [ 'LEFT JOIN', [ 'content_model = model_id' ] ],
487 'rev_id' => 'slot_revision_id',
488 'role_id' => 'slot_role_id',
489 'model_id' => 'content_model',
494 $db = wfGetDB( DB_REPLICA
);
496 yield
'MCR write-both/read-old' => [
498 'wgMultiContentRevisionSchemaMigrationStage'
499 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
,
506 'fields' => array_merge(
508 'slot_revision_id' => 'rev_id',
509 'slot_content_id' => 'NULL',
510 'slot_origin' => 'rev_id',
511 'role_name' => $db->addQuotes( SlotRecord
::MAIN
),
520 yield
'MCR write-both/read-old, content' => [
522 'wgMultiContentRevisionSchemaMigrationStage'
523 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
,
530 'fields' => array_merge(
532 'slot_revision_id' => 'rev_id',
533 'slot_content_id' => 'NULL',
534 'slot_origin' => 'rev_id',
535 'role_name' => $db->addQuotes( SlotRecord
::MAIN
),
536 'content_size' => 'rev_len',
537 'content_sha1' => 'rev_sha1',
538 'content_address' => $db->buildConcat( [
539 $db->addQuotes( 'tt:' ), 'rev_text_id' ] ),
540 'rev_text_id' => 'rev_text_id',
541 'model_name' => 'rev_content_model',
550 yield
'MCR write-both/read-old, content, model, role' => [
552 'wgMultiContentRevisionSchemaMigrationStage'
553 => SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_OLD
,
555 [ 'content', 'model', 'role' ],
560 'fields' => array_merge(
562 'slot_revision_id' => 'rev_id',
563 'slot_content_id' => 'NULL',
564 'slot_origin' => 'rev_id',
565 'role_name' => $db->addQuotes( SlotRecord
::MAIN
),
566 'content_size' => 'rev_len',
567 'content_sha1' => 'rev_sha1',
568 'content_address' => $db->buildConcat( [
569 $db->addQuotes( 'tt:' ), 'rev_text_id' ] ),
570 'rev_text_id' => 'rev_text_id',
571 'model_name' => 'rev_content_model',
583 * @covers Revision::getArchiveQueryInfo
584 * @dataProvider provideArchiveQueryInfo
586 public function testRevisionGetArchiveQueryInfo( $migrationStageSettings, $expected ) {
587 $this->setMwGlobals( $migrationStageSettings );
589 $queryInfo = Revision
::getArchiveQueryInfo();
590 $this->assertQueryInfoEquals( $expected, $queryInfo );
594 * @covers Revision::getQueryInfo
595 * @dataProvider provideQueryInfo
597 public function testRevisionGetQueryInfo( $migrationStageSettings, $options, $expected ) {
598 $this->setMwGlobals( $migrationStageSettings );
600 $queryInfo = Revision
::getQueryInfo( $options );
601 $this->assertQueryInfoEquals( $expected, $queryInfo );
605 * @dataProvider provideQueryInfo
606 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
608 public function testRevisionStoreGetQueryInfo( $migrationStageSettings, $options, $expected ) {
609 $this->setMwGlobals( $migrationStageSettings );
611 $store = MediaWikiServices
::getInstance()->getRevisionStore();
613 $queryInfo = $store->getQueryInfo( $options );
614 $this->assertQueryInfoEquals( $expected, $queryInfo );
618 * @dataProvider provideSlotsQueryInfo
619 * @covers \MediaWiki\Revision\RevisionStore::getSlotsQueryInfo
621 public function testRevisionStoreGetSlotsQueryInfo(
622 $migrationStageSettings,
626 $this->setMwGlobals( $migrationStageSettings );
628 $store = MediaWikiServices
::getInstance()->getRevisionStore();
630 $queryInfo = $store->getSlotsQueryInfo( $options );
631 $this->assertQueryInfoEquals( $expected, $queryInfo );
635 * @dataProvider provideArchiveQueryInfo
636 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
638 public function testRevisionStoreGetArchiveQueryInfo( $migrationStageSettings, $expected ) {
639 $this->setMwGlobals( $migrationStageSettings );
641 $store = MediaWikiServices
::getInstance()->getRevisionStore();
643 $queryInfo = $store->getArchiveQueryInfo();
644 $this->assertQueryInfoEquals( $expected, $queryInfo );
647 private function assertQueryInfoEquals( $expected, $queryInfo ) {
648 $this->assertArrayEqualsIgnoringIntKeyOrder(
650 $queryInfo['tables'],
653 $this->assertArrayEqualsIgnoringIntKeyOrder(
655 $queryInfo['fields'],
658 $this->assertArrayEqualsIgnoringIntKeyOrder(
663 if ( isset( $expected['keys'] ) ) {
664 $this->assertArrayEqualsIgnoringIntKeyOrder(
673 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
676 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
677 * split into a string key array and an int key array before assertions occur.
679 * @param array $expected
680 * @param array $actual
682 private function assertArrayEqualsIgnoringIntKeyOrder(
687 $this->objectAssociativeSort( $expected );
688 $this->objectAssociativeSort( $actual );
690 // Separate the int key values from the string key values so that assertion failures are
691 // easier to understand.
692 $expectedIntKeyValues = [];
693 $actualIntKeyValues = [];
695 // Remove all int keys and re add them at the end after sorting by value
696 // This will result in all int keys being in the same order with same ints at the end of
698 foreach ( $expected as $key => $value ) {
699 if ( is_int( $key ) ) {
700 unset( $expected[$key] );
701 $expectedIntKeyValues[] = $value;
704 foreach ( $actual as $key => $value ) {
705 if ( is_int( $key ) ) {
706 unset( $actual[$key] );
707 $actualIntKeyValues[] = $value;
711 $this->objectAssociativeSort( $expected );
712 $this->objectAssociativeSort( $actual );
714 $this->objectAssociativeSort( $expectedIntKeyValues );
715 $this->objectAssociativeSort( $actualIntKeyValues );
717 $this->assertEquals( $expected, $actual, $message );
718 $this->assertEquals( $expectedIntKeyValues, $actualIntKeyValues, $message );