MediaWiki  master
SessionTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Session;
4 
7 use User;
9 
15 
16  public function testClear() {
17  $session = TestUtils::getDummySession();
18  $priv = TestingAccessWrapper::newFromObject( $session );
19 
20  $backend = $this->getMockBuilder( DummySessionBackend::class )
21  ->setMethods( [ 'canSetUser', 'setUser', 'save' ] )
22  ->getMock();
23  $backend->expects( $this->once() )->method( 'canSetUser' )
24  ->will( $this->returnValue( true ) );
25  $backend->expects( $this->once() )->method( 'setUser' )
26  ->with( $this->callback( function ( $user ) {
27  return $user instanceof User && $user->isAnon();
28  } ) );
29  $backend->expects( $this->once() )->method( 'save' );
30  $priv->backend = $backend;
31  $session->clear();
32  $this->assertSame( [], $backend->data );
33  $this->assertTrue( $backend->dirty );
34 
35  $backend = $this->getMockBuilder( DummySessionBackend::class )
36  ->setMethods( [ 'canSetUser', 'setUser', 'save' ] )
37  ->getMock();
38  $backend->data = [];
39  $backend->expects( $this->once() )->method( 'canSetUser' )
40  ->will( $this->returnValue( true ) );
41  $backend->expects( $this->once() )->method( 'setUser' )
42  ->with( $this->callback( function ( $user ) {
43  return $user instanceof User && $user->isAnon();
44  } ) );
45  $backend->expects( $this->once() )->method( 'save' );
46  $priv->backend = $backend;
47  $session->clear();
48  $this->assertFalse( $backend->dirty );
49 
50  $backend = $this->getMockBuilder( DummySessionBackend::class )
51  ->setMethods( [ 'canSetUser', 'setUser', 'save' ] )
52  ->getMock();
53  $backend->expects( $this->once() )->method( 'canSetUser' )
54  ->will( $this->returnValue( false ) );
55  $backend->expects( $this->never() )->method( 'setUser' );
56  $backend->expects( $this->once() )->method( 'save' );
57  $priv->backend = $backend;
58  $session->clear();
59  $this->assertSame( [], $backend->data );
60  $this->assertTrue( $backend->dirty );
61  }
62 
63  public function testSecrets() {
64  $logger = new \TestLogger;
65  $session = TestUtils::getDummySession( null, -1, $logger );
66 
67  // Simple defaulting
68  $this->assertEquals( 'defaulted', $session->getSecret( 'test', 'defaulted' ) );
69 
70  // Bad encrypted data
71  $session->set( 'test', 'foobar' );
72  $logger->setCollect( true );
73  $this->assertEquals( 'defaulted', $session->getSecret( 'test', 'defaulted' ) );
74  $logger->setCollect( false );
75  $this->assertSame( [
76  [ LogLevel::WARNING, 'Invalid sealed-secret format' ]
77  ], $logger->getBuffer() );
78  $logger->clearBuffer();
79 
80  // Tampered data
81  $session->setSecret( 'test', 'foobar' );
82  $encrypted = $session->get( 'test' );
83  $session->set( 'test', $encrypted . 'x' );
84  $logger->setCollect( true );
85  $this->assertEquals( 'defaulted', $session->getSecret( 'test', 'defaulted' ) );
86  $logger->setCollect( false );
87  $this->assertSame( [
88  [ LogLevel::WARNING, 'Sealed secret has been tampered with, aborting.' ]
89  ], $logger->getBuffer() );
90  $logger->clearBuffer();
91 
92  // Unserializable data
93  $iv = random_bytes( 16 );
94  list( $encKey, $hmacKey ) = TestingAccessWrapper::newFromObject( $session )->getSecretKeys();
95  $ciphertext = openssl_encrypt( 'foobar', 'aes-256-ctr', $encKey, OPENSSL_RAW_DATA, $iv );
96  $sealed = base64_encode( $iv ) . '.' . base64_encode( $ciphertext );
97  $hmac = hash_hmac( 'sha256', $sealed, $hmacKey, true );
98  $encrypted = base64_encode( $hmac ) . '.' . $sealed;
99  $session->set( 'test', $encrypted );
100  \Wikimedia\suppressWarnings();
101  $this->assertEquals( 'defaulted', $session->getSecret( 'test', 'defaulted' ) );
102  \Wikimedia\restoreWarnings();
103  }
104 
109  public function testSecretsRoundTripping( $data ) {
110  $session = TestUtils::getDummySession();
111 
112  // Simple round-trip
113  $session->setSecret( 'secret', $data );
114  $this->assertNotEquals( $data, $session->get( 'secret' ) );
115  $this->assertEquals( $data, $session->getSecret( 'secret', 'defaulted' ) );
116  }
117 
118  public static function provideSecretsRoundTripping() {
119  return [
120  [ 'Foobar' ],
121  [ 42 ],
122  [ [ 'foo', 'bar' => 'baz', 'subarray' => [ 1, 2, 3 ] ] ],
123  [ (object)[ 'foo', 'bar' => 'baz', 'subarray' => [ 1, 2, 3 ] ] ],
124  [ true ],
125  [ false ],
126  [ null ],
127  ];
128  }
129 
130 }
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
Session MediaWiki\Session\Session.
Definition: SessionTest.php:14
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
testSecretsRoundTripping( $data)
provideSecretsRoundTripping
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
static getDummySession( $backend=null, $index=-1, $logger=null)
If you need a Session for testing but don&#39;t want to create a backend to construct one...
Definition: TestUtils.php:86
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
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1454