MediaWiki  1.28.0
SessionProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Session;
4 
6 
13 
14  public function testBasics() {
15  $manager = new SessionManager();
16  $logger = new \TestLogger();
17  $config = new \HashConfig();
18 
19  $provider = $this->getMockForAbstractClass( SessionProvider::class );
20  $priv = \TestingAccessWrapper::newFromObject( $provider );
21 
22  $provider->setConfig( $config );
23  $this->assertSame( $config, $priv->config );
24  $provider->setLogger( $logger );
25  $this->assertSame( $logger, $priv->logger );
26  $provider->setManager( $manager );
27  $this->assertSame( $manager, $priv->manager );
28  $this->assertSame( $manager, $provider->getManager() );
29 
30  $provider->invalidateSessionsForUser( new \User );
31 
32  $this->assertSame( [], $provider->getVaryHeaders() );
33  $this->assertSame( [], $provider->getVaryCookies() );
34  $this->assertSame( null, $provider->suggestLoginUsername( new \FauxRequest ) );
35 
36  $this->assertSame( get_class( $provider ), (string)$provider );
37 
38  $this->assertNull( $provider->getRememberUserDuration() );
39 
40  $this->assertNull( $provider->whyNoSession() );
41 
43  'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
44  'provider' => $provider,
45  ] );
46  $metadata = [ 'foo' ];
47  $this->assertTrue( $provider->refreshSessionInfo( $info, new \FauxRequest, $metadata ) );
48  $this->assertSame( [ 'foo' ], $metadata );
49  }
50 
57  public function testNewSessionInfo( $persistId, $persistUser, $ok ) {
58  $manager = new SessionManager();
59 
60  $provider = $this->getMockBuilder( SessionProvider::class )
61  ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
62  ->getMockForAbstractClass();
63  $provider->expects( $this->any() )->method( 'persistsSessionId' )
64  ->will( $this->returnValue( $persistId ) );
65  $provider->expects( $this->any() )->method( 'canChangeUser' )
66  ->will( $this->returnValue( $persistUser ) );
67  $provider->setManager( $manager );
68 
69  if ( $ok ) {
70  $info = $provider->newSessionInfo();
71  $this->assertNotNull( $info );
72  $this->assertFalse( $info->wasPersisted() );
73  $this->assertTrue( $info->isIdSafe() );
74 
75  $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
76  $info = $provider->newSessionInfo( $id );
77  $this->assertNotNull( $info );
78  $this->assertSame( $id, $info->getId() );
79  $this->assertFalse( $info->wasPersisted() );
80  $this->assertTrue( $info->isIdSafe() );
81  } else {
82  $this->assertNull( $provider->newSessionInfo() );
83  }
84  }
85 
86  public function testMergeMetadata() {
87  $provider = $this->getMockBuilder( SessionProvider::class )
88  ->getMockForAbstractClass();
89 
90  try {
91  $provider->mergeMetadata(
92  [ 'foo' => 1, 'baz' => 3 ],
93  [ 'bar' => 2, 'baz' => '3' ]
94  );
95  $this->fail( 'Expected exception not thrown' );
96  } catch ( MetadataMergeException $ex ) {
97  $this->assertSame( 'Key "baz" changed', $ex->getMessage() );
98  $this->assertSame(
99  [ 'old_value' => 3, 'new_value' => '3' ], $ex->getContext() );
100  }
101 
102  $res = $provider->mergeMetadata(
103  [ 'foo' => 1, 'baz' => 3 ],
104  [ 'bar' => 2, 'baz' => 3 ]
105  );
106  $this->assertSame( [ 'bar' => 2, 'baz' => 3 ], $res );
107  }
108 
109  public static function provideNewSessionInfo() {
110  return [
111  [ false, false, false ],
112  [ true, false, false ],
113  [ false, true, false ],
114  [ true, true, true ],
115  ];
116  }
117 
118  public function testImmutableSessions() {
119  $provider = $this->getMockBuilder( SessionProvider::class )
120  ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
121  ->getMockForAbstractClass();
122  $provider->expects( $this->any() )->method( 'canChangeUser' )
123  ->will( $this->returnValue( true ) );
124  $provider->preventSessionsForUser( 'Foo' );
125 
126  $provider = $this->getMockBuilder( SessionProvider::class )
127  ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
128  ->getMockForAbstractClass();
129  $provider->expects( $this->any() )->method( 'canChangeUser' )
130  ->will( $this->returnValue( false ) );
131  try {
132  $provider->preventSessionsForUser( 'Foo' );
133  $this->fail( 'Expected exception not thrown' );
134  } catch ( \BadMethodCallException $ex ) {
135  $this->assertSame(
136  'MediaWiki\\Session\\SessionProvider::preventSessionsForUser must be implmented ' .
137  'when canChangeUser() is false',
138  $ex->getMessage()
139  );
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 }
const MIN_PRIORITY
Minimum allowed priority.
Definition: SessionInfo.php:36
Config $config
Definition: MediaWiki.php:38
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Session Database MediaWiki\Session\SessionProvider.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testNewSessionInfo($persistId, $persistUser, $ok)
provideNewSessionInfo
Subclass of UnexpectedValueException that can be annotated with additional data for debug logging...
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:1936
$res
Definition: database.txt:21
static getDummySessionBackend()
If you need a SessionBackend for testing but don't want to create a real one, use this...
Definition: TestUtils.php:64
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
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
This serves as the entry point to the MediaWiki session handling system.
static newFromObject($object)
Return the same object, without access restrictions.
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:181
Value object returned by SessionProvider.
Definition: SessionInfo.php:34