Debian Codegraph @

tests/phpunit/integration/includes/StubObject/StubGlobalUserTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 // phpcs:disable MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
23 use MediaWiki\StubObject\StubGlobalUser;
24 use MediaWiki\User\User;
25
26 /**
27 * Tests the MediaWiki\StubObject\StubGlobalUser, including magic support for __get() and __set()
28 *
29 * @author DannyS712
30 *
31 * @covers \MediaWiki\StubObject\StubGlobalUser
32 */
33 class StubGlobalUserTest extends MediaWikiIntegrationTestCase {
34
35 /** @var int */
36 private $oldErrorLevel;
37
38 protected function setUp(): void {
39 parent::setUp();
40
41 // Make sure deprecation notices are seen
42 $this->oldErrorLevel = error_reporting( -1 );
43
44 // Using User::newFromRow() to avoid needing any integration
45 $userFields = [
46 'user_id' => 12345,
47 ];
48 $realUser = User::newFromRow( (object)$userFields );
49 StubGlobalUser::setUser( $realUser );
50 }
51
52 protected function tearDown(): void {
53 error_reporting( $this->oldErrorLevel );
54 parent::tearDown();
55 }
56
57 public function testRealUser() {
58 // Should not be emitting deprecation warnings
59 global $wgUser;
60
61 $this->assertInstanceOf( StubGlobalUser::class, $wgUser );
62
63 $real = StubGlobalUser::getRealUser( $wgUser );
64 $this->assertInstanceOf( User::class, $real );
65
66 $real2 = StubGlobalUser::getRealUser( $real );
67 $this->assertSame( $real, $real2 );
68 }
69
70 public function testRealUser_exception() {
71 $this->expectException( InvalidArgumentException::class );
72 $this->expectExceptionMessage(
73 '$globalUser must be a User (or MediaWiki\StubObject\StubGlobalUser), got int'
74 );
75 StubGlobalUser::getRealUser( 12345 );
76 }
77
78 public function testMagicCall() {
79 $this->expectDeprecationAndContinue( '/Use of \$wgUser was deprecated in MediaWiki 1\.35/' );
80 $this->expectDeprecationAndContinue( '/\$wgUser reassignment detected/' );
81
82 global $wgUser;
83 $this->assertInstanceOf(
84 StubGlobalUser::class,
85 $wgUser,
86 'Check: $wgUser should be a MediaWiki\StubObject\StubGlobalUser at the start of the test'
87 );
88 $this->assertSame(
89 12345,
90 $wgUser->getId(),
91 '__call() based on id set in ::setUp()'
92 );
93 $this->assertInstanceOf(
94 User::class,
95 $wgUser,
96 '__call() resulted in unstubbing'
97 );
98 }
99
100 public function testGetMagic() {
101 $this->expectDeprecationAndContinue( '/Use of \$wgUser was deprecated in MediaWiki 1\.35/' );
102 $this->expectDeprecationAndContinue( '/\$wgUser reassignment detected/' );
103
104 global $wgUser;
105 $this->assertInstanceOf(
106 StubGlobalUser::class,
107 $wgUser,
108 'Check: $wgUser should be a MediaWiki\StubObject\StubGlobalUser at the start of the test'
109 );
110 $this->assertSame(
111 12345,
112 $wgUser->mId,
113 '__get() based on id set in ::setUp()'
114 );
115 $this->assertInstanceOf(
116 User::class,
117 $wgUser,
118 '__get() resulted in unstubbing'
119 );
120 }
121
122 public function testSetMagic() {
123 // This test is why we need MediaWiki\StubObject\StubGlobalUser::_unstub to override MediaWiki\StubObject\StubObject::_unstub
124 // and not try to detect and throw exceptions in unstub loops - for some reason it
125 // thinks this creates a loop.
126
127 $this->expectDeprecationAndContinue( '/Use of \$wgUser was deprecated in MediaWiki 1\.35/' );
128 $this->expectDeprecationAndContinue( '/\$wgUser reassignment detected/' );
129
130 global $wgUser;
131 $this->assertInstanceOf(
132 StubGlobalUser::class,
133 $wgUser,
134 'Check: $wgUser should be a MediaWiki\StubObject\StubGlobalUser at the start of the test'
135 );
136 $wgUser->mId = 2000;
137 $this->assertInstanceOf(
138 User::class,
139 $wgUser,
140 '__set() resulted in unstubbing'
141 );
142 $this->assertSame(
143 2000,
144 $wgUser->mId,
145 '__set() call worked'
146 );
147 }
148
149 public function testDeprecationEmittedWhenReassigned() {
150 $this->expectDeprecationAndContinue( '/\$wgUser reassignment detected/' );
151 global $wgUser;
152 $wgUser = new User;
153 }
154
155 /**
156 * @doesNotPerformAssertions
157 */
158 public function testReassignmentWithRestoring() {
159 global $wgUser;
160 $oldUser = $wgUser;
161 $wgUser = new User;
162 $wgUser = $oldUser;
163 }
164
165 /**
166 * @doesNotPerformAssertions
167 */
168 public function testSetUserNoDeprecation() {
169 StubGlobalUser::setUser( new User );
170 }
171 }