Fix CommentStoreComment RawMessage construction
authorLucas Werkmeister <lucas.werkmeister@wikimedia.de>
Wed, 13 Feb 2019 10:44:06 +0000 (11:44 +0100)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 13 Feb 2019 16:17:10 +0000 (11:17 -0500)
If a CommentStoreComment is constructed without a Message argument, then
the RawMessage it uses instead should specify the comment text as a
plain-text parameter, not as a regular parameter: we don’t want any
syntax in the text to be interpreted at the Message level.

Change-Id: If14debde2bceae695c8955604ee96bd5005d8b66

includes/CommentStoreComment.php
tests/phpunit/includes/CommentStoreCommentTest.php [new file with mode: 0644]
tests/phpunit/includes/CommentStoreTest.php

index af866cd..9f1681d 100644 (file)
@@ -50,7 +50,7 @@ class CommentStoreComment {
        public function __construct( $id, $text, Message $message = null, array $data = null ) {
                $this->id = $id;
                $this->text = $text;
-               $this->message = $message ?: new RawMessage( '$1', [ $text ] );
+               $this->message = $message ?: new RawMessage( '$1', [ Message::plaintextParam( $text ) ] );
                $this->data = $data;
        }
 
diff --git a/tests/phpunit/includes/CommentStoreCommentTest.php b/tests/phpunit/includes/CommentStoreCommentTest.php
new file mode 100644 (file)
index 0000000..2dfe03a
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @covers CommentStoreComment
+ *
+ * @license GPL-2.0-or-later
+ */
+class CommentStoreCommentTest extends TestCase {
+
+       public function testConstructorWithMessage() {
+               $message = new Message( 'test' );
+               $comment = new CommentStoreComment( null, 'test', $message );
+
+               $this->assertSame( $message, $comment->message );
+       }
+
+       public function testConstructorWithoutMessage() {
+               $text = '{{template|param}}';
+               $comment = new CommentStoreComment( null, $text );
+
+               $this->assertSame( $text, $comment->message->text() );
+       }
+
+}
index 78c5bf3..7361047 100644 (file)
@@ -383,6 +383,8 @@ class CommentStoreTest extends MediaWikiLangTestCase {
                        "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" );
        }
 
@@ -400,7 +402,7 @@ class CommentStoreTest extends MediaWikiLangTestCase {
 
                $expectOld = [
                        'text' => $expect['text'],
-                       'message' => new RawMessage( '$1', [ $expect['text'] ] ),
+                       'message' => new RawMessage( '$1', [ Message::plaintextParam( $expect['text'] ) ] ),
                        'data' => null,
                ];
 
@@ -490,7 +492,7 @@ class CommentStoreTest extends MediaWikiLangTestCase {
 
                $expectOld = [
                        'text' => $expect['text'],
-                       'message' => new RawMessage( '$1', [ $expect['text'] ] ),
+                       'message' => new RawMessage( '$1', [ Message::plaintextParam( $expect['text'] ) ] ),
                        'data' => null,
                ];
 
@@ -568,7 +570,7 @@ class CommentStoreTest extends MediaWikiLangTestCase {
                $db = wfGetDB( DB_REPLICA ); // for timestamps
 
                $msgComment = new Message( 'parentheses', [ 'message comment' ] );
-               $textCommentMsg = new RawMessage( '$1', [ 'text 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' ]
@@ -576,15 +578,15 @@ class CommentStoreTest extends MediaWikiLangTestCase {
 
                return [
                        'Simple table, text comment' => [
-                               'commentstore1', 'cs1_comment', 'cs1_id', 'text comment', null, [
-                                       'text' => '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',
+                               'commentstore1', 'cs1_comment', 'cs1_id', '{{text}} comment', [ 'message' => 42 ], [
+                                       'text' => '{{text}} comment',
                                        'message' => $textCommentMsg,
                                        'data' => [ 'message' => 42 ],
                                ]
@@ -619,15 +621,15 @@ class CommentStoreTest extends MediaWikiLangTestCase {
                        ],
 
                        'Revision, text comment' => [
-                               'commentstore2', 'cs2_comment', 'cs2_id', 'text comment', null, [
-                                       'text' => 'text comment',
+                               'commentstore2', 'cs2_comment', 'cs2_id', '{{text}} comment', null, [
+                                       'text' => '{{text}} comment',
                                        'message' => $textCommentMsg,
                                        'data' => null,
                                ]
                        ],
                        'Revision, text comment with data' => [
-                               'commentstore2', 'cs2_comment', 'cs2_id', 'text comment', [ 'message' => 42 ], [
-                                       'text' => 'text comment',
+                               'commentstore2', 'cs2_comment', 'cs2_id', '{{text}} comment', [ 'message' => 42 ], [
+                                       'text' => '{{text}} comment',
                                        'message' => $textCommentMsg,
                                        'data' => [ 'message' => 42 ],
                                ]