diff options
Diffstat (limited to 'www/wiki/tests/phpunit/includes/cache')
3 files changed, 368 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/includes/cache/GenderCacheTest.php b/www/wiki/tests/phpunit/includes/cache/GenderCacheTest.php new file mode 100644 index 00000000..e5bb2379 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/cache/GenderCacheTest.php @@ -0,0 +1,87 @@ +<?php +use MediaWiki\MediaWikiServices; + +/** + * @group Database + * @group Cache + */ +class GenderCacheTest extends MediaWikiLangTestCase { + + /** @var string[] User key => username */ + private static $nameMap; + + function addDBDataOnce() { + // ensure the correct default gender + $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'gender' => 'unknown' ] ); + + $male = $this->getMutableTestUser()->getUser(); + $male->setOption( 'gender', 'male' ); + $male->saveSettings(); + + $female = $this->getMutableTestUser()->getUser(); + $female->setOption( 'gender', 'female' ); + $female->saveSettings(); + + $default = $this->getMutableTestUser()->getUser(); + $default->setOption( 'gender', null ); + $default->saveSettings(); + + self::$nameMap = [ + 'UTMale' => $male->getName(), + 'UTFemale' => $female->getName(), + 'UTDefaultGender' => $default->getName() + ]; + } + + /** + * test usernames + * + * @dataProvider provideUserGenders + * @covers GenderCache::getGenderOf + */ + public function testUserName( $userKey, $expectedGender ) { + $genderCache = MediaWikiServices::getInstance()->getGenderCache(); + $username = isset( self::$nameMap[$userKey] ) ? self::$nameMap[$userKey] : $userKey; + $gender = $genderCache->getGenderOf( $username ); + $this->assertEquals( $gender, $expectedGender, "GenderCache normal" ); + } + + /** + * genderCache should work with user objects, too + * + * @dataProvider provideUserGenders + * @covers GenderCache::getGenderOf + */ + public function testUserObjects( $userKey, $expectedGender ) { + $username = isset( self::$nameMap[$userKey] ) ? self::$nameMap[$userKey] : $userKey; + $genderCache = MediaWikiServices::getInstance()->getGenderCache(); + $gender = $genderCache->getGenderOf( $username ); + $this->assertEquals( $gender, $expectedGender, "GenderCache normal" ); + } + + public static function provideUserGenders() { + return [ + [ 'UTMale', 'male' ], + [ 'UTFemale', 'female' ], + [ 'UTDefaultGender', 'unknown' ], + [ 'UTNotExist', 'unknown' ], + // some not valid user + [ '127.0.0.1', 'unknown' ], + [ 'user@test', 'unknown' ], + ]; + } + + /** + * test strip of subpages to avoid unnecessary queries + * against the never existing username + * + * @dataProvider provideUserGenders + * @covers GenderCache::getGenderOf + */ + public function testStripSubpages( $userKey, $expectedGender ) { + $username = isset( self::$nameMap[$userKey] ) ? self::$nameMap[$userKey] : $userKey; + $genderCache = MediaWikiServices::getInstance()->getGenderCache(); + $gender = $genderCache->getGenderOf( "$username/subpage" ); + $this->assertEquals( $gender, $expectedGender, "GenderCache must strip of subpages" ); + } +} diff --git a/www/wiki/tests/phpunit/includes/cache/LocalisationCacheTest.php b/www/wiki/tests/phpunit/includes/cache/LocalisationCacheTest.php new file mode 100644 index 00000000..42957b60 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/cache/LocalisationCacheTest.php @@ -0,0 +1,107 @@ +<?php +/** + * @group Database + * @group Cache + * @covers LocalisationCache + * @author Niklas Laxström + */ +class LocalisationCacheTest extends MediaWikiTestCase { + protected function setUp() { + parent::setUp(); + $this->setMwGlobals( [ + 'wgExtensionMessagesFiles' => [], + 'wgHooks' => [], + ] ); + } + + /** + * @return LocalisationCache + */ + protected function getMockLocalisationCache() { + global $IP; + $lc = $this->getMockBuilder( \LocalisationCache::class ) + ->setConstructorArgs( [ [ 'store' => 'detect' ] ] ) + ->setMethods( [ 'getMessagesDirs' ] ) + ->getMock(); + $lc->expects( $this->any() )->method( 'getMessagesDirs' ) + ->will( $this->returnValue( + [ "$IP/tests/phpunit/data/localisationcache" ] + ) ); + + return $lc; + } + + public function testPuralRulesFallback() { + $cache = $this->getMockLocalisationCache(); + + $this->assertEquals( + $cache->getItem( 'ar', 'pluralRules' ), + $cache->getItem( 'arz', 'pluralRules' ), + 'arz plural rules (undefined) fallback to ar (defined)' + ); + + $this->assertEquals( + $cache->getItem( 'ar', 'compiledPluralRules' ), + $cache->getItem( 'arz', 'compiledPluralRules' ), + 'arz compiled plural rules (undefined) fallback to ar (defined)' + ); + + $this->assertNotEquals( + $cache->getItem( 'ksh', 'pluralRules' ), + $cache->getItem( 'de', 'pluralRules' ), + 'ksh plural rules (defined) dont fallback to de (defined)' + ); + + $this->assertNotEquals( + $cache->getItem( 'ksh', 'compiledPluralRules' ), + $cache->getItem( 'de', 'compiledPluralRules' ), + 'ksh compiled plural rules (defined) dont fallback to de (defined)' + ); + } + + public function testRecacheFallbacks() { + $lc = $this->getMockLocalisationCache(); + $lc->recache( 'ba' ); + $this->assertEquals( + [ + 'present-ba' => 'ba', + 'present-ru' => 'ru', + 'present-en' => 'en', + ], + $lc->getItem( 'ba', 'messages' ), + 'Fallbacks are only used to fill missing data' + ); + } + + public function testRecacheFallbacksWithHooks() { + // Use hook to provide updates for messages. This is what the + // LocalisationUpdate extension does. See T70781. + $this->mergeMwGlobalArrayValue( 'wgHooks', [ + 'LocalisationCacheRecacheFallback' => [ + function ( + LocalisationCache $lc, + $code, + array &$cache + ) { + if ( $code === 'ru' ) { + $cache['messages']['present-ba'] = 'ru-override'; + $cache['messages']['present-ru'] = 'ru-override'; + $cache['messages']['present-en'] = 'ru-override'; + } + } + ] + ] ); + + $lc = $this->getMockLocalisationCache(); + $lc->recache( 'ba' ); + $this->assertEquals( + [ + 'present-ba' => 'ba', + 'present-ru' => 'ru-override', + 'present-en' => 'ru-override', + ], + $lc->getItem( 'ba', 'messages' ), + 'Updates provided by hooks follow the normal fallback order.' + ); + } +} diff --git a/www/wiki/tests/phpunit/includes/cache/MessageCacheTest.php b/www/wiki/tests/phpunit/includes/cache/MessageCacheTest.php new file mode 100644 index 00000000..b03eeba2 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/cache/MessageCacheTest.php @@ -0,0 +1,174 @@ +<?php + +/** + * @group Database + * @group Cache + * @covers MessageCache + */ +class MessageCacheTest extends MediaWikiLangTestCase { + + protected function setUp() { + parent::setUp(); + $this->configureLanguages(); + MessageCache::singleton()->enable(); + } + + /** + * Helper function -- setup site language for testing + */ + protected function configureLanguages() { + // for the test, we need the content language to be anything but English, + // let's choose e.g. German (de) + $this->setUserLang( 'de' ); + $this->setContentLang( 'de' ); + } + + function addDBDataOnce() { + $this->configureLanguages(); + + // Set up messages and fallbacks ab -> ru -> de + $this->makePage( 'FallbackLanguageTest-Full', 'ab' ); + $this->makePage( 'FallbackLanguageTest-Full', 'ru' ); + $this->makePage( 'FallbackLanguageTest-Full', 'de' ); + + // Fallbacks where ab does not exist + $this->makePage( 'FallbackLanguageTest-Partial', 'ru' ); + $this->makePage( 'FallbackLanguageTest-Partial', 'de' ); + + // Fallback to the content language + $this->makePage( 'FallbackLanguageTest-ContLang', 'de' ); + + // Add customizations for an existing message. + $this->makePage( 'sunday', 'ru' ); + + // Full key tests -- always want russian + $this->makePage( 'MessageCacheTest-FullKeyTest', 'ab' ); + $this->makePage( 'MessageCacheTest-FullKeyTest', 'ru' ); + + // In content language -- get base if no derivative + $this->makePage( 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ); + } + + /** + * Helper function for addDBData -- adds a simple page to the database + * + * @param string $title Title of page to be created + * @param string $lang Language and content of the created page + * @param string|null $content Content of the created page, or null for a generic string + */ + protected function makePage( $title, $lang, $content = null ) { + global $wgContLang; + + if ( $content === null ) { + $content = $lang; + } + if ( $lang !== $wgContLang->getCode() ) { + $title = "$title/$lang"; + } + + $title = Title::newFromText( $title, NS_MEDIAWIKI ); + $wikiPage = new WikiPage( $title ); + $contentHandler = ContentHandler::makeContent( $content, $title ); + $wikiPage->doEditContent( $contentHandler, "$lang translation test case" ); + } + + /** + * Test message fallbacks, bug #1495 + * + * @dataProvider provideMessagesForFallback + */ + public function testMessageFallbacks( $message, $lang, $expectedContent ) { + $result = MessageCache::singleton()->get( $message, true, $lang ); + $this->assertEquals( $expectedContent, $result, "Message fallback failed." ); + } + + function provideMessagesForFallback() { + return [ + [ 'FallbackLanguageTest-Full', 'ab', 'ab' ], + [ 'FallbackLanguageTest-Partial', 'ab', 'ru' ], + [ 'FallbackLanguageTest-ContLang', 'ab', 'de' ], + [ 'FallbackLanguageTest-None', 'ab', false ], + + // Existing message with customizations on the fallbacks + [ 'sunday', 'ab', 'амҽыш' ], + + // T48579 + [ 'FallbackLanguageTest-NoDervContLang', 'de', 'de/none' ], + // UI language different from content language should only use de/none as last option + [ 'FallbackLanguageTest-NoDervContLang', 'fit', 'de/none' ], + ]; + } + + public function testReplaceMsg() { + global $wgContLang; + + $messageCache = MessageCache::singleton(); + $message = 'go'; + $uckey = $wgContLang->ucfirst( $message ); + $oldText = $messageCache->get( $message ); // "Ausführen" + + $dbw = wfGetDB( DB_MASTER ); + $dbw->startAtomic( __METHOD__ ); // simulate request and block deferred updates + $messageCache->replace( $uckey, 'Allez!' ); + $this->assertEquals( 'Allez!', + $messageCache->getMsgFromNamespace( $uckey, 'de' ), + 'Updates are reflected in-process immediately' ); + $this->assertEquals( 'Allez!', + $messageCache->get( $message ), + 'Updates are reflected in-process immediately' ); + $this->makePage( 'Go', 'de', 'Race!' ); + $dbw->endAtomic( __METHOD__ ); + + $this->assertEquals( 0, + DeferredUpdates::pendingUpdatesCount(), + 'Post-commit deferred update triggers a run of all updates' ); + + $this->assertEquals( 'Race!', $messageCache->get( $message ), 'Correct final contents' ); + + $this->makePage( 'Go', 'de', $oldText ); + $messageCache->replace( $uckey, $oldText ); // deferred update runs immediately + $this->assertEquals( $oldText, $messageCache->get( $message ), 'Content restored' ); + } + + /** + * There's a fallback case where the message key is given as fully qualified -- this + * should ignore the passed $lang and use the language from the key + * + * @dataProvider provideMessagesForFullKeys + */ + public function testFullKeyBehaviour( $message, $lang, $expectedContent ) { + $result = MessageCache::singleton()->get( $message, true, $lang, true ); + $this->assertEquals( $expectedContent, $result, "Full key message fallback failed." ); + } + + function provideMessagesForFullKeys() { + return [ + [ 'MessageCacheTest-FullKeyTest/ru', 'ru', 'ru' ], + [ 'MessageCacheTest-FullKeyTest/ru', 'ab', 'ru' ], + [ 'MessageCacheTest-FullKeyTest/ru/foo', 'ru', false ], + ]; + } + + /** + * @dataProvider provideNormalizeKey + */ + public function testNormalizeKey( $key, $expected ) { + $actual = MessageCache::normalizeKey( $key ); + $this->assertEquals( $expected, $actual ); + } + + public function provideNormalizeKey() { + return [ + [ 'Foo', 'foo' ], + [ 'foo', 'foo' ], + [ 'fOo', 'fOo' ], + [ 'FOO', 'fOO' ], + [ 'Foo bar', 'foo_bar' ], + [ 'Ćab', 'ćab' ], + [ 'Ćab_e 3', 'ćab_e_3' ], + [ 'ĆAB', 'ćAB' ], + [ 'ćab', 'ćab' ], + [ 'ćaB', 'ćaB' ], + ]; + } +} |