Make Gender normalize usernames
authorBrian Wolff <bawolff+wn@gmail.com>
Sun, 17 Dec 2017 22:12:06 +0000 (22:12 +0000)
committerBrian Wolff <bawolff+wn@gmail.com>
Mon, 22 Jan 2018 23:12:01 +0000 (23:12 +0000)
This ensures that if GENDER is fed wfEscapeWikitext()'d version
of a username, it will normalize it.

See discussion on T182800.

Note, we do not need to worry about the case of a user named
"Project:*foo" as such namespace prefixes are illegal in
usernames.

Change-Id: Ic5a8fc76c28dca43ce8e334ef1874c2673433f00

includes/parser/CoreParserFunctions.php
tests/phpunit/includes/parser/CoreParserFunctionsTest.php [new file with mode: 0644]

index 07944d4..ad56afc 100644 (file)
@@ -337,8 +337,8 @@ class CoreParserFunctions {
                // default
                $gender = User::getDefaultOption( 'gender' );
 
-               // allow prefix.
-               $title = Title::newFromText( $username );
+               // allow prefix and normalize (e.g. "&#42;foo" -> "*foo" ).
+               $title = Title::newFromText( $username, NS_USER );
 
                if ( $title && $title->inNamespace( NS_USER ) ) {
                        $username = $title->getText();
diff --git a/tests/phpunit/includes/parser/CoreParserFunctionsTest.php b/tests/phpunit/includes/parser/CoreParserFunctionsTest.php
new file mode 100644 (file)
index 0000000..c630447
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/**
+ * @group Database
+ * @covers CoreParserFunctions
+ */
+class CoreParserFunctionsTest extends MediaWikiTestCase {
+
+       public function testGender() {
+               $user = User::createNew( '*Female' );
+               $user->setOption( 'gender', 'female' );
+               $user->saveSettings();
+
+               $msg = ( new RawMessage( '{{GENDER:*Female|m|f|o}}' ) )->parse();
+               $this->assertEquals( $msg, 'f', 'Works unescaped' );
+               $escapedName = wfEscapeWikiText( '*Female' );
+               $msg2 = ( new RawMessage( '{{GENDER:' . $escapedName . '|m|f|o}}' ) )
+                       ->parse();
+               $this->assertEquals( $msg, 'f', 'Works escaped' );
+       }
+
+}