Debian Codegraph @

tests/phpunit/includes/CommentStore/CommentStoreTest.php

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