MediaWiki  1.33.0
SessionProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Session;
4 
6 use Wikimedia\TestingAccessWrapper;
7 
14 
15  public function testBasics() {
16  $manager = new SessionManager();
17  $logger = new \TestLogger();
18  $config = new \HashConfig();
19 
20  $provider = $this->getMockForAbstractClass( SessionProvider::class );
21  $priv = TestingAccessWrapper::newFromObject( $provider );
22 
23  $provider->setConfig( $config );
24  $this->assertSame( $config, $priv->config );
25  $provider->setLogger( $logger );
26  $this->assertSame( $logger, $priv->logger );
27  $provider->setManager( $manager );
28  $this->assertSame( $manager, $priv->manager );
29  $this->assertSame( $manager, $provider->getManager() );
30 
31  $provider->invalidateSessionsForUser( new \User );
32 
33  $this->assertSame( [], $provider->getVaryHeaders() );
34  $this->assertSame( [], $provider->getVaryCookies() );
35  $this->assertSame( null, $provider->suggestLoginUsername( new \FauxRequest ) );
36 
37  $this->assertSame( get_class( $provider ), (string)$provider );
38 
39  $this->assertNull( $provider->getRememberUserDuration() );
40 
41  $this->assertNull( $provider->whyNoSession() );
42 
44  'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
45  'provider' => $provider,
46  ] );
47  $metadata = [ 'foo' ];
48  $this->assertTrue( $provider->refreshSessionInfo( $info, new \FauxRequest, $metadata ) );
49  $this->assertSame( [ 'foo' ], $metadata );
50  }
51 
58  public function testNewSessionInfo( $persistId, $persistUser, $ok ) {
59  $manager = new SessionManager();
60 
61  $provider = $this->getMockBuilder( SessionProvider::class )
62  ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
63  ->getMockForAbstractClass();
64  $provider->expects( $this->any() )->method( 'persistsSessionId' )
65  ->will( $this->returnValue( $persistId ) );
66  $provider->expects( $this->any() )->method( 'canChangeUser' )
67  ->will( $this->returnValue( $persistUser ) );
68  $provider->setManager( $manager );
69 
70  if ( $ok ) {
71  $info = $provider->newSessionInfo();
72  $this->assertNotNull( $info );
73  $this->assertFalse( $info->wasPersisted() );
74  $this->assertTrue( $info->isIdSafe() );
75 
76  $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
77  $info = $provider->newSessionInfo( $id );
78  $this->assertNotNull( $info );
79  $this->assertSame( $id, $info->getId() );
80  $this->assertFalse( $info->wasPersisted() );
81  $this->assertTrue( $info->isIdSafe() );
82  } else {
83  $this->assertNull( $provider->newSessionInfo() );
84  }
85  }
86 
87  public function testMergeMetadata() {
88  $provider = $this->getMockBuilder( SessionProvider::class )
89  ->getMockForAbstractClass();
90 
91  try {
92  $provider->mergeMetadata(
93  [ 'foo' => 1, 'baz' => 3 ],
94  [ 'bar' => 2, 'baz' => '3' ]
95  );
96  $this->fail( 'Expected exception not thrown' );
97  } catch ( MetadataMergeException $ex ) {
98  $this->assertSame( 'Key "baz" changed', $ex->getMessage() );
99  $this->assertSame(
100  [ 'old_value' => 3, 'new_value' => '3' ], $ex->getContext() );
101  }
102 
103  $res = $provider->mergeMetadata(
104  [ 'foo' => 1, 'baz' => 3 ],
105  [ 'bar' => 2, 'baz' => 3 ]
106  );
107  $this->assertSame( [ 'bar' => 2, 'baz' => 3 ], $res );
108  }
109 
110  public static function provideNewSessionInfo() {
111  return [
112  [ false, false, false ],
113  [ true, false, false ],
114  [ false, true, false ],
115  [ true, true, true ],
116  ];
117  }
118 
119  public function testImmutableSessions() {
120  $provider = $this->getMockBuilder( SessionProvider::class )
121  ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
122  ->getMockForAbstractClass();
123  $provider->expects( $this->any() )->method( 'canChangeUser' )
124  ->will( $this->returnValue( true ) );
125  $provider->preventSessionsForUser( 'Foo' );
126 
127  $provider = $this->getMockBuilder( SessionProvider::class )
128  ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
129  ->getMockForAbstractClass();
130  $provider->expects( $this->any() )->method( 'canChangeUser' )
131  ->will( $this->returnValue( false ) );
132  try {
133  $provider->preventSessionsForUser( 'Foo' );
134  $this->fail( 'Expected exception not thrown' );
135  } catch ( \BadMethodCallException $ex ) {
136  $this->assertSame(
137  'MediaWiki\\Session\\SessionProvider::preventSessionsForUser must be implemented ' .
138  'when canChangeUser() is false',
139  $ex->getMessage()
140  );
141  }
142  }
143 
144  public function testHashToSessionId() {
145  $config = new \HashConfig( [
146  'SecretKey' => 'Shhh!',
147  ] );
148 
149  $provider = $this->getMockForAbstractClass( SessionProvider::class,
150  [], 'MockSessionProvider' );
151  $provider->setConfig( $config );
152  $priv = TestingAccessWrapper::newFromObject( $provider );
153 
154  $this->assertSame( 'eoq8cb1mg7j30ui5qolafps4hg29k5bb', $priv->hashToSessionId( 'foobar' ) );
155  $this->assertSame( '4do8j7tfld1g8tte9jqp3csfgmulaun9',
156  $priv->hashToSessionId( 'foobar', 'secret' ) );
157 
158  try {
159  $priv->hashToSessionId( [] );
160  $this->fail( 'Expected exception not thrown' );
161  } catch ( \InvalidArgumentException $ex ) {
162  $this->assertSame(
163  '$data must be a string, array was passed',
164  $ex->getMessage()
165  );
166  }
167  try {
168  $priv->hashToSessionId( '', false );
169  $this->fail( 'Expected exception not thrown' );
170  } catch ( \InvalidArgumentException $ex ) {
171  $this->assertSame(
172  '$key must be a string or null, boolean was passed',
173  $ex->getMessage()
174  );
175  }
176  }
177 
178  public function testDescribe() {
179  $provider = $this->getMockForAbstractClass( SessionProvider::class,
180  [], 'MockSessionProvider' );
181 
182  $this->assertSame(
183  'MockSessionProvider sessions',
184  $provider->describe( \Language::factory( 'en' ) )
185  );
186  }
187 
188  public function testGetAllowedUserRights() {
189  $provider = $this->getMockForAbstractClass( SessionProvider::class );
191 
192  try {
193  $provider->getAllowedUserRights( $backend );
194  $this->fail( 'Expected exception not thrown' );
195  } catch ( \InvalidArgumentException $ex ) {
196  $this->assertSame(
197  'Backend\'s provider isn\'t $this',
198  $ex->getMessage()
199  );
200  }
201 
202  TestingAccessWrapper::newFromObject( $backend )->provider = $provider;
203  $this->assertNull( $provider->getAllowedUserRights( $backend ) );
204  }
205 
206 }
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
MediaWiki\Session\SessionProviderTest\testDescribe
testDescribe()
Definition: SessionProviderTest.php:178
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaWiki\Session\SessionProviderTest\testNewSessionInfo
testNewSessionInfo( $persistId, $persistUser, $ok)
provideNewSessionInfo
Definition: SessionProviderTest.php:58
MediaWiki\Session\MetadataMergeException
Subclass of UnexpectedValueException that can be annotated with additional data for debug logging.
Definition: MetadataMergeException.php:35
MediaWiki\Session\SessionProviderTest\provideNewSessionInfo
static provideNewSessionInfo()
Definition: SessionProviderTest.php:110
$res
$res
Definition: database.txt:21
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Session\MetadataMergeException\getContext
getContext()
Get context data.
Definition: MetadataMergeException.php:59
MediaWiki\Session\SessionProviderTest\testBasics
testBasics()
Definition: SessionProviderTest.php:15
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Session\SessionProviderTest\testGetAllowedUserRights
testGetAllowedUserRights()
Definition: SessionProviderTest.php:188
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\TestUtils\getDummySessionBackend
static getDummySessionBackend()
If you need a SessionBackend for testing but don't want to create a real one, use this.
Definition: TestUtils.php:65
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
MediaWiki\Session\SessionProviderTest
Session Database MediaWiki\Session\SessionProvider.
Definition: SessionProviderTest.php:13
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:50
MediaWiki\Session\SessionInfo
Value object returned by SessionProvider.
Definition: SessionInfo.php:34
MediaWiki\Session\SessionProviderTest\testHashToSessionId
testHashToSessionId()
Definition: SessionProviderTest.php:144
MediaWiki\Session\SessionProviderTest\testMergeMetadata
testMergeMetadata()
Definition: SessionProviderTest.php:87
MediaWiki\Session\SessionProviderTest\testImmutableSessions
testImmutableSessions()
Definition: SessionProviderTest.php:119
MediaWiki\$config
Config $config
Definition: MediaWiki.php:43
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1985
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:215
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
MediaWiki\Session\SessionInfo\MIN_PRIORITY
const MIN_PRIORITY
Minimum allowed priority.
Definition: SessionInfo.php:36