1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
<?php
use MediaWiki\CommentStore\CommentStore;
use MediaWiki\CommentStore\CommentStoreComment;
use MediaWiki\Language\Language;
use MediaWiki\Language\RawMessage;
use MediaWiki\Message\Message;
use Wikimedia\Rdbms\IMaintainableDatabase;
/**
* @group Database
* @covers \MediaWiki\CommentStore\CommentStore
* @covers \MediaWiki\CommentStore\CommentStoreComment
*/
class CommentStoreTest extends MediaWikiLangTestCase {
protected function getSchemaOverrides( IMaintainableDatabase $db ) {
return [
'scripts' => [
__DIR__ . '/CommentStoreTest.sql',
],
'drop' => [],
'create' => [ 'commentstore1', 'commentstore2', 'commentstore2_temp' ],
'alter' => [],
];
}
/**
* Create a store for a particular stage
* @return CommentStore
*/
private function makeStore() {
$lang = $this->createMock( Language::class );
$lang->method( 'truncateForDatabase' )->willReturnCallback( static function ( $str, $len ) {
return strlen( $str ) > $len ? substr( $str, 0, $len - 3 ) . '...' : $str;
} );
$lang->method( 'truncateForVisual' )->willReturnCallback( static function ( $str, $len ) {
return mb_strlen( $str ) > $len ? mb_substr( $str, 0, $len - 3 ) . '...' : $str;
} );
return new CommentStore( $lang );
}
/**
* @dataProvider provideGetJoin
* @param string $key
* @param array $expect
*/
public function testGetJoin( $key, $expect ) {
$store = $this->makeStore();
$result = $store->getJoin( $key );
$this->assertEquals( $expect, $result );
}
public static function provideGetJoin() {
return [
'Simple table' => [
'ipb_reason', [
'tables' => [ 'comment_ipb_reason' => 'comment' ],
'fields' => [
'ipb_reason_text' => 'comment_ipb_reason.comment_text',
'ipb_reason_data' => 'comment_ipb_reason.comment_data',
'ipb_reason_cid' => 'comment_ipb_reason.comment_id',
],
'joins' => [
'comment_ipb_reason' => [ 'JOIN', 'comment_ipb_reason.comment_id = ipb_reason_id' ],
],
],
],
'Revision' => [
'rev_comment', [
'tables' => [
'comment_rev_comment' => 'comment',
],
'fields' => [
'rev_comment_text' => 'comment_rev_comment.comment_text',
'rev_comment_data' => 'comment_rev_comment.comment_data',
'rev_comment_cid' => 'comment_rev_comment.comment_id',
],
'joins' => [
'comment_rev_comment' => [ 'JOIN', 'comment_rev_comment.comment_id = rev_comment_id' ],
],
],
],
'Image' => [
'img_description', [
'tables' => [
'comment_img_description' => 'comment',
],
'fields' => [
'img_description_text' => 'comment_img_description.comment_text',
'img_description_data' => 'comment_img_description.comment_data',
'img_description_cid' => 'comment_img_description.comment_id',
],
'joins' => [
'comment_img_description' => [ 'JOIN',
'comment_img_description.comment_id = img_description_id',
],
],
],
],
];
}
private function assertComment( $expect, $actual, $from ) {
$this->assertSame( $expect['text'], $actual->text, "text $from" );
$this->assertInstanceOf( get_class( $expect['message'] ), $actual->message,
"message class $from" );
$this->assertSame( $expect['message']->getKeysToTry(), $actual->message->getKeysToTry(),
"message keys $from" );
$this->assertEquals( $expect['message']->text(), $actual->message->text(),
"message rendering $from" );
$this->assertEquals( $expect['text'], $actual->message->text(),
"message rendering and text $from" );
$this->assertEquals( $expect['data'], $actual->data, "data $from" );
}
/**
* @dataProvider provideInsertRoundTrip
* @param string $table
* @param string $key
* @param string $pk
* @param string|Message $comment
* @param array|null $data
* @param array $expect
*/
public function testInsertRoundTrip( $table, $key, $pk, $comment, $data, $expect ) {
static $id = 1;
$wstore = $this->makeStore();
$fields = $wstore->insert( $this->getDb(), $key, $comment, $data );
$this->assertArrayNotHasKey( $key, $fields, "old field" );
$this->assertArrayHasKey( "{$key}_id", $fields, "new field" );
$this->getDb()->newInsertQueryBuilder()
->insertInto( $table )
->row( [ $pk => ++$id ] + $fields )
->caller( __METHOD__ )
->execute();
$rstore = $this->makeStore();
$fieldRow = $this->getDb()->newSelectQueryBuilder()
->select( [ "{$key}_id" => "{$key}_id" ] )
->from( $table )
->where( [ $pk => $id ] )
->caller( __METHOD__ )->fetchRow();
$queryInfo = $rstore->getJoin( $key );
$joinRow = $this->getDb()->newSelectQueryBuilder()
->queryInfo( $queryInfo )
->from( $table )
->where( [ $pk => $id ] )
->caller( __METHOD__ )
->fetchRow();
$this->assertComment(
$expect,
$rstore->getCommentLegacy( $this->getDb(), $key, $fieldRow ),
"from getFields()"
);
$this->assertComment(
$expect,
$rstore->getComment( $key, $joinRow ),
"from getJoin()"
);
}
public static function provideInsertRoundTrip() {
$msgComment = new Message( 'parentheses', [ 'message comment' ] );
$textCommentMsg = new RawMessage( '$1', [ Message::plaintextParam( '{{text}} comment' ) ] );
$nestedMsgComment = new Message( [ 'parentheses', 'rawmessage' ], [ new Message( 'mainpage' ) ] );
$comStoreComment = new CommentStoreComment(
null, 'comment store comment', null, [ 'foo' => 'bar' ]
);
return [
'Simple table, text comment' => [
'commentstore1', 'cs1_comment', 'cs1_id', '{{text}} comment', null, [
'text' => '{{text}} comment',
'message' => $textCommentMsg,
'data' => null,
]
],
'Simple table, text comment with data' => [
'commentstore1', 'cs1_comment', 'cs1_id', '{{text}} comment', [ 'message' => 42 ], [
'text' => '{{text}} comment',
'message' => $textCommentMsg,
'data' => [ 'message' => 42 ],
]
],
'Simple table, message comment' => [
'commentstore1', 'cs1_comment', 'cs1_id', $msgComment, null, [
'text' => '(message comment)',
'message' => $msgComment,
'data' => null,
]
],
'Simple table, message comment with data' => [
'commentstore1', 'cs1_comment', 'cs1_id', $msgComment, [ 'message' => 42 ], [
'text' => '(message comment)',
'message' => $msgComment,
'data' => [ 'message' => 42 ],
]
],
'Simple table, nested message comment' => [
'commentstore1', 'cs1_comment', 'cs1_id', $nestedMsgComment, null, [
'text' => '(Main Page)',
'message' => $nestedMsgComment,
'data' => null,
]
],
'Simple table, CommentStoreComment' => [
'commentstore1', 'cs1_comment', 'cs1_id', clone $comStoreComment, [ 'baz' => 'baz' ], [
'text' => 'comment store comment',
'message' => $comStoreComment->message,
'data' => [ 'foo' => 'bar' ],
]
],
];
}
public function testGetCommentErrors() {
$store = $this->makeStore();
try {
$store->getComment( 'dummy', [ 'dummy' => 'comment' ] );
$this->fail( 'Expected exception not thrown' );
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( '$row does not contain fields needed for comment dummy', $ex->getMessage() );
}
// Ignore: Using deprecated fallback handling for comment dummy
$res = @$store->getComment( 'dummy', [ 'dummy' => 'comment' ], true );
$this->assertSame( 'comment', $res->text );
try {
$store->getComment( 'dummy', [ 'dummy_id' => 1 ] );
$this->fail( 'Expected exception not thrown' );
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'$row does not contain fields needed for comment dummy and getComment(), '
. 'but does have fields for getCommentLegacy()',
$ex->getMessage()
);
}
$store = $this->makeStore();
try {
$store->getComment( 'rev_comment', [ 'rev_comment' => 'comment' ] );
$this->fail( 'Expected exception not thrown' );
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'$row does not contain fields needed for comment rev_comment', $ex->getMessage()
);
}
$res = @$store->getComment( 'rev_comment', [ 'rev_comment' => 'comment' ], true );
$this->assertSame( 'comment', $res->text );
try {
$store->getComment( 'rev_comment', [ 'rev_comment_id' => 1 ] );
$this->fail( 'Expected exception not thrown' );
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'$row does not contain fields needed for comment rev_comment and getComment(), '
. 'but does have fields for getCommentLegacy()',
$ex->getMessage()
);
}
}
public function testInsertTruncation() {
$comment = str_repeat( '💣', 16400 );
$truncated = str_repeat( '💣', CommentStore::COMMENT_CHARACTER_LIMIT - 3 ) . '...';
$store = $this->makeStore();
$fields = $store->insert( $this->getDb(), 'ipb_reason', $comment );
$stored = $this->getDb()->newSelectQueryBuilder()
->select( 'comment_text' )
->from( 'comment' )
->where( [ 'comment_id' => $fields['ipb_reason_id'] ] )
->caller( __METHOD__ )->fetchField();
$this->assertSame( $truncated, $stored );
}
public function testInsertTooMuchData() {
$store = $this->makeStore();
$this->expectException( OverflowException::class );
$this->expectExceptionMessage( "Comment data is too long (65611 bytes, maximum is 65535)" );
$store->insert( $this->getDb(), 'ipb_reason', 'foo', [
'long' => str_repeat( '💣', 16400 )
] );
}
}
|