3 namespace MediaWiki\Tests\Revision
;
5 use MediaWikiIntegrationTestCase
;
8 * Tests RevisionStore against the post-migration MCR DB schema.
10 * @group RevisionStore
14 class RevisionQueryInfoTest
extends MediaWikiIntegrationTestCase
{
16 protected function getRevisionQueryFields( $returnTextIdField = true ) {
27 if ( $returnTextIdField ) {
28 $fields[] = 'rev_text_id';
33 protected function getArchiveQueryFields( $returnTextFields = true ) {
47 if ( $returnTextFields ) {
48 $fields[] = 'ar_text_id';
53 protected function getCommentQueryFields( $prefix ) {
55 "{$prefix}_comment_text" => "comment_{$prefix}_comment.comment_text",
56 "{$prefix}_comment_data" => "comment_{$prefix}_comment.comment_data",
57 "{$prefix}_comment_cid" => "comment_{$prefix}_comment.comment_id",
61 protected function getActorQueryFields( $prefix, $tmp = false ) {
64 "{$prefix}_user" => "actor_{$prefix}_user.actor_user",
65 "{$prefix}_user_text" => "actor_{$prefix}_user.actor_name",
66 "{$prefix}_actor" => "temp_{$prefix}_user.{$prefix}actor_actor",
68 } elseif ( $prefix === 'ar' ) {
71 "{$prefix}_user" => 'archive_actor.actor_user',
72 "{$prefix}_user_text" => 'archive_actor.actor_name',
76 "{$prefix}_actor" => "{$prefix}_actor",
77 "{$prefix}_user" => "actor_{$prefix}_user.actor_user",
78 "{$prefix}_user_text" => "actor_{$prefix}_user.actor_name",
83 protected function getTextQueryFields() {
90 protected function getPageQueryFields() {
101 protected function getUserQueryFields() {
107 protected function getContentHandlerQueryFields( $prefix ) {
109 "{$prefix}_content_format",
110 "{$prefix}_content_model",
114 public function provideArchiveQueryInfo() {
115 yield
'no options' => [
120 'archive_actor' => 'actor',
121 'comment_ar_comment' => 'comment',
123 'fields' => array_merge(
124 $this->getArchiveQueryFields( false ),
125 $this->getActorQueryFields( 'ar' ),
126 $this->getCommentQueryFields( 'ar' )
130 => [ 'JOIN', 'comment_ar_comment.comment_id = ar_comment_id' ],
131 'archive_actor' => [ 'JOIN', 'actor_id=ar_actor' ],
137 public function provideQueryInfo() {
138 // TODO: more option variations
139 yield
'page and user option, actor-new' => [
147 'actor_rev_user' => 'actor',
148 'comment_rev_comment' => 'comment',
150 'fields' => array_merge(
151 $this->getRevisionQueryFields( false ),
152 $this->getPageQueryFields(),
153 $this->getUserQueryFields(),
154 $this->getActorQueryFields( 'rev' ),
155 $this->getCommentQueryFields( 'rev' )
158 'page' => [ 'JOIN', [ 'page_id = rev_page' ] ],
161 [ 'actor_rev_user.actor_user != 0', 'user_id = actor_rev_user.actor_user' ],
163 'comment_rev_comment' => [ 'JOIN', 'comment_rev_comment.comment_id = rev_comment_id' ],
164 'actor_rev_user' => [ 'JOIN', 'actor_rev_user.actor_id = rev_actor' ]
168 yield
'no options, actor-new' => [
174 'actor_rev_user' => 'actor',
175 'comment_rev_comment' => 'comment',
177 'fields' => array_merge(
178 $this->getRevisionQueryFields( false ),
179 $this->getActorQueryFields( 'rev' ),
180 $this->getCommentQueryFields( 'rev' )
183 'comment_rev_comment' => [ 'JOIN', 'comment_rev_comment.comment_id = rev_comment_id' ],
184 'actor_rev_user' => [ 'JOIN', 'actor_rev_user.actor_id = rev_actor' ],
190 public static function provideSlotsQueryInfo() {
191 yield
'no options' => [
206 'rev_id' => 'slot_revision_id',
207 'role_id' => 'slot_role_id'
211 yield
'role option' => [
227 'slot_roles' => [ 'LEFT JOIN', [ 'slot_role_id = role_id' ] ],
230 'rev_id' => 'slot_revision_id',
231 'role_id' => 'slot_role_id'
235 yield
'content option' => [
254 'content' => [ 'JOIN', [ 'slot_content_id = content_id' ] ],
257 'rev_id' => 'slot_revision_id',
258 'role_id' => 'slot_role_id',
259 'model_id' => 'content_model',
263 yield
'content and model options' => [
265 [ 'content', 'model' ],
284 'content' => [ 'JOIN', [ 'slot_content_id = content_id' ] ],
285 'content_models' => [ 'LEFT JOIN', [ 'content_model = model_id' ] ],
288 'rev_id' => 'slot_revision_id',
289 'role_id' => 'slot_role_id',
290 'model_id' => 'content_model',
297 * @dataProvider provideQueryInfo
298 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
300 public function testRevisionStoreGetQueryInfo( $migrationStageSettings, $options, $expected ) {
301 $this->overrideConfigValues( $migrationStageSettings );
303 $store = $this->getServiceContainer()->getRevisionStore();
305 $queryInfo = $store->getQueryInfo( $options );
306 $this->assertQueryInfoEquals( $expected, $queryInfo );
310 * @dataProvider provideSlotsQueryInfo
311 * @covers \MediaWiki\Revision\RevisionStore::getSlotsQueryInfo
313 public function testRevisionStoreGetSlotsQueryInfo(
314 $migrationStageSettings,
318 $this->overrideConfigValues( $migrationStageSettings );
320 $store = $this->getServiceContainer()->getRevisionStore();
322 $queryInfo = $store->getSlotsQueryInfo( $options );
323 $this->assertQueryInfoEquals( $expected, $queryInfo );
327 * @dataProvider provideArchiveQueryInfo
328 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
330 public function testRevisionStoreGetArchiveQueryInfo( $migrationStageSettings, $expected ) {
331 $this->overrideConfigValues( $migrationStageSettings );
333 $store = $this->getServiceContainer()->getRevisionStore();
335 $queryInfo = $store->getArchiveQueryInfo();
336 $this->assertQueryInfoEquals( $expected, $queryInfo );
339 private function assertQueryInfoEquals( $expected, $queryInfo ) {
340 $this->assertArrayEqualsIgnoringIntKeyOrder(
342 $queryInfo['tables'],
345 $this->assertArrayEqualsIgnoringIntKeyOrder(
347 $queryInfo['fields'],
350 $this->assertArrayEqualsIgnoringIntKeyOrder(
355 if ( isset( $expected['keys'] ) ) {
356 $this->assertArrayEqualsIgnoringIntKeyOrder(
365 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
368 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
369 * split into a string key array and an int key array before assertions occur.
371 * @param array $expected
372 * @param array $actual
373 * @param string|null $message
375 private function assertArrayEqualsIgnoringIntKeyOrder(
380 $this->objectAssociativeSort( $expected );
381 $this->objectAssociativeSort( $actual );
383 // Separate the int key values from the string key values so that assertion failures are
384 // easier to understand.
385 $expectedIntKeyValues = [];
386 $actualIntKeyValues = [];
388 // Remove all int keys and re add them at the end after sorting by value
389 // This will result in all int keys being in the same order with same ints at the end of
391 foreach ( $expected as $key => $value ) {
392 if ( is_int( $key ) ) {
393 unset( $expected[$key] );
394 $expectedIntKeyValues[] = $value;
397 foreach ( $actual as $key => $value ) {
398 if ( is_int( $key ) ) {
399 unset( $actual[$key] );
400 $actualIntKeyValues[] = $value;
404 $this->objectAssociativeSort( $expected );
405 $this->objectAssociativeSort( $actual );
407 $this->objectAssociativeSort( $expectedIntKeyValues );
408 $this->objectAssociativeSort( $actualIntKeyValues );
410 $this->assertEquals( $expected, $actual, $message );
411 $this->assertEquals( $expectedIntKeyValues, $actualIntKeyValues, $message );