GenderCacheTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. use MediaWiki\MediaWikiServices;
  3. /**
  4. * @group Database
  5. * @group Cache
  6. */
  7. class GenderCacheTest extends MediaWikiLangTestCase {
  8. /** @var string[] User key => username */
  9. private static $nameMap;
  10. function addDBDataOnce() {
  11. // ensure the correct default gender
  12. $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'gender' => 'unknown' ] );
  13. $male = $this->getMutableTestUser()->getUser();
  14. $male->setOption( 'gender', 'male' );
  15. $male->saveSettings();
  16. $female = $this->getMutableTestUser()->getUser();
  17. $female->setOption( 'gender', 'female' );
  18. $female->saveSettings();
  19. $default = $this->getMutableTestUser()->getUser();
  20. $default->setOption( 'gender', null );
  21. $default->saveSettings();
  22. self::$nameMap = [
  23. 'UTMale' => $male->getName(),
  24. 'UTFemale' => $female->getName(),
  25. 'UTDefaultGender' => $default->getName()
  26. ];
  27. }
  28. /**
  29. * test usernames
  30. *
  31. * @dataProvider provideUserGenders
  32. * @covers GenderCache::getGenderOf
  33. */
  34. public function testUserName( $userKey, $expectedGender ) {
  35. $genderCache = MediaWikiServices::getInstance()->getGenderCache();
  36. $username = self::$nameMap[$userKey] ?? $userKey;
  37. $gender = $genderCache->getGenderOf( $username );
  38. $this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
  39. }
  40. /**
  41. * genderCache should work with user objects, too
  42. *
  43. * @dataProvider provideUserGenders
  44. * @covers GenderCache::getGenderOf
  45. */
  46. public function testUserObjects( $userKey, $expectedGender ) {
  47. $username = self::$nameMap[$userKey] ?? $userKey;
  48. $genderCache = MediaWikiServices::getInstance()->getGenderCache();
  49. $gender = $genderCache->getGenderOf( $username );
  50. $this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
  51. }
  52. public static function provideUserGenders() {
  53. return [
  54. [ 'UTMale', 'male' ],
  55. [ 'UTFemale', 'female' ],
  56. [ 'UTDefaultGender', 'unknown' ],
  57. [ 'UTNotExist', 'unknown' ],
  58. // some not valid user
  59. [ '127.0.0.1', 'unknown' ],
  60. [ 'user@test', 'unknown' ],
  61. ];
  62. }
  63. /**
  64. * test strip of subpages to avoid unnecessary queries
  65. * against the never existing username
  66. *
  67. * @dataProvider provideUserGenders
  68. * @covers GenderCache::getGenderOf
  69. */
  70. public function testStripSubpages( $userKey, $expectedGender ) {
  71. $username = self::$nameMap[$userKey] ?? $userKey;
  72. $genderCache = MediaWikiServices::getInstance()->getGenderCache();
  73. $gender = $genderCache->getGenderOf( "$username/subpage" );
  74. $this->assertEquals( $gender, $expectedGender, "GenderCache must strip of subpages" );
  75. }
  76. }