summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/cache/GenderCacheTest.php
blob: 04fb00d98a613c98015afd53cf34b94b1e6d953e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

/**
 * @group Database
 * @group Cache
 */
class GenderCacheTest extends MediaWikiLangTestCase {

	function addDBData() {
		//ensure the correct default gender
		$this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', array( 'gender' => 'unknown' ) );

		$user = User::newFromName( 'UTMale' );
		if ( $user->getID() == 0 ) {
			$user->addToDatabase();
			$user->setPassword( 'UTMalePassword' );
		}
		//ensure the right gender
		$user->setOption( 'gender', 'male' );
		$user->saveSettings();

		$user = User::newFromName( 'UTFemale' );
		if ( $user->getID() == 0 ) {
			$user->addToDatabase();
			$user->setPassword( 'UTFemalePassword' );
		}
		//ensure the right gender
		$user->setOption( 'gender', 'female' );
		$user->saveSettings();

		$user = User::newFromName( 'UTDefaultGender' );
		if ( $user->getID() == 0 ) {
			$user->addToDatabase();
			$user->setPassword( 'UTDefaultGenderPassword' );
		}
		//ensure the default gender
		$user->setOption( 'gender', null );
		$user->saveSettings();
	}

	/**
	 * test usernames
	 *
	 * @dataProvider provideUserGenders
	 * @covers GenderCache::getGenderOf
	 */
	public function testUserName( $username, $expectedGender ) {
		$genderCache = GenderCache::singleton();
		$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( $username, $expectedGender ) {
		$genderCache = GenderCache::singleton();
		$user = User::newFromName( $username );
		$gender = $genderCache->getGenderOf( $user );
		$this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
	}

	public static function provideUserGenders() {
		return array(
			array( 'UTMale', 'male' ),
			array( 'UTFemale', 'female' ),
			array( 'UTDefaultGender', 'unknown' ),
			array( 'UTNotExist', 'unknown' ),
			//some not valid user
			array( '127.0.0.1', 'unknown' ),
			array( 'user@test', 'unknown' ),
		);
	}

	/**
	 * test strip of subpages to avoid unnecessary queries
	 * against the never existing username
	 *
	 * @dataProvider provideStripSubpages
	 * @covers GenderCache::getGenderOf
	 */
	public function testStripSubpages( $pageWithSubpage, $expectedGender ) {
		$genderCache = GenderCache::singleton();
		$gender = $genderCache->getGenderOf( $pageWithSubpage );
		$this->assertEquals( $gender, $expectedGender, "GenderCache must strip of subpages" );
	}

	public static function provideStripSubpages() {
		return array(
			array( 'UTMale/subpage', 'male' ),
			array( 'UTFemale/subpage', 'female' ),
			array( 'UTDefaultGender/subpage', 'unknown' ),
			array( 'UTNotExist/subpage', 'unknown' ),
			array( '127.0.0.1/subpage', 'unknown' ),
		);
	}
}