aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/user/UserIdentityValueTest.php
blob: 26cd8b8c5a203dcd03f3582c0ee08649949879ce (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

use MediaWiki\User\UserIdentityValue;
use Wikimedia\Assert\PreconditionException;

/**
 * @coversDefaultClass \MediaWiki\User\UserIdentityValue
 */
class UserIdentityValueTest extends MediaWikiUnitTestCase {

	/**
	 * @covers ::getActorId
	 */
	public function testGetActorIdLocalUIVForeignParam() {
		$foreignWikiId = 'Foreign Wiki';
		$user = new UserIdentityValue( 0, 'TestUserName', UserIdentityValue::LOCAL );

		$this->expectDeprecationAndContinue( '/Use of MediaWiki\\\\User\\\\UserIdentityValue::getActorId was deprecated in MediaWiki 1\.36/' );
		$this->assertSame( 0, $user->getActorId( $foreignWikiId ) );
	}

	/**
	 * @covers ::getActorId
	 */
	public function testGetActorIdDeprecated() {
		$user = new UserIdentityValue( 0, 'TestUserName' );

		$this->expectDeprecationAndContinue( '/Use of MediaWiki\\\\User\\\\UserIdentityValue::getActorId was deprecated in MediaWiki 1\.36/' );
		$this->assertSame( 0, $user->getActorId() );
	}

	/**
	 * @covers ::getActorId
	 */
	public function testGetActorIdLocalUIVNoParam() {
		$this->filterDeprecated( '/UserIdentityValue::getActorId was deprecated/' );
		$user = new UserIdentityValue( 0, 'TestUserName', UserIdentityValue::LOCAL );

		$this->assertSame( 0, $user->getActorId() );
	}

	/**
	 * @covers ::getActorId
	 */
	public function testGetActorIdLocalUIVLocalParam() {
		$this->filterDeprecated( '/UserIdentityValue::getActorId was deprecated/' );
		$user = new UserIdentityValue( 0, 'TestUserName', UserIdentityValue::LOCAL );

		$this->assertSame( 0, $user->getActorId( UserIdentityValue::LOCAL ) );
	}

	/**
	 * @covers ::getActorId
	 */
	public function testGetActorIdForeignUIVForeignParam() {
		$this->filterDeprecated( '/UserIdentityValue::getActorId was deprecated/' );
		$foreignWikiId = 'Foreign Wiki';
		$user = new UserIdentityValue( 0, 'TestUserName', $foreignWikiId );

		$this->assertSame( 0, $user->getActorId( $foreignWikiId ) );
	}

	/**
	 * @return Generator
	 */
	public static function provideWikiIds() {
		yield [ UserIdentityValue::LOCAL ];
		yield [ 'Foreign Wiki' ];
	}

	/**
	 * @covers ::getWikiId
	 * @dataProvider provideWikiIds
	 * @param string|false $wikiId
	 */
	public function testGetWiki( $wikiId ) {
		$user = new UserIdentityValue( 0, 'TestUserName', $wikiId );
		$this->assertSame( $wikiId, $user->getWikiId() );
	}

	/**
	 * @covers ::assertWiki
	 */
	public function testAssertWikiLocalUIV() {
		$foreignWikiId = 'Foreign Wiki';
		$user = new UserIdentityValue( 0, 'TestUserName', UserIdentityValue::LOCAL );

		$user->assertWiki( UserIdentityValue::LOCAL );
		$this->assertTrue( true, 'User is for same wiki' );

		$this->expectException( PreconditionException::class );
		$user->assertWiki( $foreignWikiId );
	}

	/**
	 * @covers ::assertWiki
	 */
	public function testAssertWikiForeignUIV() {
		$foreignWikiId = 'Foreign Wiki';
		$user = new UserIdentityValue( 0, 'TestUserName', $foreignWikiId );

		$user->assertWiki( $foreignWikiId );
		$this->assertTrue( true, 'User is for same wiki' );

		$this->expectException( PreconditionException::class );
		$user->assertWiki( UserIdentityValue::LOCAL );
	}

	/**
	 * @covers \MediaWiki\User\UserIdentityValue::newAnonymous
	 */
	public function testNewAnonymous() {
		$user = UserIdentityValue::newAnonymous( 'TEST', 'acmewiki' );
		$this->assertFalse( $user->isRegistered() );
		$this->assertSame( 'TEST', $user->getName() );
		$this->assertSame( 0, $user->getId( 'acmewiki' ) );
		$this->assertSame( 'acmewiki', $user->getWikiId() );
	}

	/**
	 * @covers \MediaWiki\User\UserIdentityValue::newRegistered
	 */
	public function testNewRegistered() {
		$user = UserIdentityValue::newRegistered( 1, 'TEST', 'acmewiki' );
		$this->assertTrue( $user->isRegistered() );
		$this->assertSame( 'TEST', $user->getName() );
		$this->assertSame( 1, $user->getId( 'acmewiki' ) );
		$this->assertSame( 'acmewiki', $user->getWikiId() );
	}

	/**
	 * @covers \MediaWiki\User\UserIdentityValue::newRegistered
	 */
	public function testNewRegistered_invalid() {
		$this->expectException( InvalidArgumentException::class );
		UserIdentityValue::newRegistered( 0, 'TEST', 'acmewiki' );
	}

	/**
	 * @covers \MediaWiki\User\UserIdentityValue::newExternal
	 */
	public function testNewExternal() {
		$user = UserIdentityValue::newExternal( 'imported', 'TEST', 'acmewiki' );
		$this->assertFalse( $user->isRegistered() );
		$this->assertSame( 'imported>TEST', $user->getName() );
		$this->assertSame( 0, $user->getId( 'acmewiki' ) );
		$this->assertSame( 'acmewiki', $user->getWikiId() );
	}
}