Merge "Use an example with working digit transformation"
[lhc/web/wiklou.git] / tests / phpunit / includes / session / SessionTest.php
index 858996d..a4727c4 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace MediaWiki\Session;
 
+use Psr\Log\LogLevel;
 use MediaWikiTestCase;
 use User;
 
@@ -13,10 +14,10 @@ class SessionTest extends MediaWikiTestCase {
 
        public function testConstructor() {
                $backend = TestUtils::getDummySessionBackend();
-               \TestingAccessWrapper::newFromObject( $backend )->requests = array( -1 => 'dummy' );
+               \TestingAccessWrapper::newFromObject( $backend )->requests = [ -1 => 'dummy' ];
                \TestingAccessWrapper::newFromObject( $backend )->id = new SessionId( 'abc' );
 
-               $session = new Session( $backend, 42 );
+               $session = new Session( $backend, 42, new \TestLogger );
                $priv = \TestingAccessWrapper::newFromObject( $session );
                $this->assertSame( $backend, $priv->backend );
                $this->assertSame( 42, $priv->index );
@@ -37,19 +38,19 @@ class SessionTest extends MediaWikiTestCase {
         */
        public function testMethods( $m, $args, $index, $ret ) {
                $mock = $this->getMock( 'MediaWiki\\Session\\DummySessionBackend',
-                       array( $m, 'deregisterSession' ) );
+                       [ $m, 'deregisterSession' ] );
                $mock->expects( $this->once() )->method( 'deregisterSession' )
                        ->with( $this->identicalTo( 42 ) );
 
                $tmp = $mock->expects( $this->once() )->method( $m );
-               $expectArgs = array();
+               $expectArgs = [];
                if ( $index ) {
                        $expectArgs[] = $this->identicalTo( 42 );
                }
                foreach ( $args as $arg ) {
                        $expectArgs[] = $this->identicalTo( $arg );
                }
-               $tmp = call_user_func_array( array( $tmp, 'with' ), $expectArgs );
+               $tmp = call_user_func_array( [ $tmp, 'with' ], $expectArgs );
 
                $retval = new \stdClass;
                $tmp->will( $this->returnValue( $retval ) );
@@ -57,9 +58,9 @@ class SessionTest extends MediaWikiTestCase {
                $session = TestUtils::getDummySession( $mock, 42 );
 
                if ( $ret ) {
-                       $this->assertSame( $retval, call_user_func_array( array( $session, $m ), $args ) );
+                       $this->assertSame( $retval, call_user_func_array( [ $session, $m ], $args ) );
                } else {
-                       $this->assertNull( call_user_func_array( array( $session, $m ), $args ) );
+                       $this->assertNull( call_user_func_array( [ $session, $m ], $args ) );
                }
 
                // Trigger Session destructor
@@ -67,30 +68,31 @@ class SessionTest extends MediaWikiTestCase {
        }
 
        public static function provideMethods() {
-               return array(
-                       array( 'getId', array(), false, true ),
-                       array( 'getSessionId', array(), false, true ),
-                       array( 'resetId', array(), false, true ),
-                       array( 'getProvider', array(), false, true ),
-                       array( 'isPersistent', array(), false, true ),
-                       array( 'persist', array(), false, false ),
-                       array( 'shouldRememberUser', array(), false, true ),
-                       array( 'setRememberUser', array( true ), false, false ),
-                       array( 'getRequest', array(), true, true ),
-                       array( 'getUser', array(), false, true ),
-                       array( 'getAllowedUserRights', array(), false, true ),
-                       array( 'canSetUser', array(), false, true ),
-                       array( 'setUser', array( new \stdClass ), false, false ),
-                       array( 'suggestLoginUsername', array(), true, true ),
-                       array( 'shouldForceHTTPS', array(), false, true ),
-                       array( 'setForceHTTPS', array( true ), false, false ),
-                       array( 'getLoggedOutTimestamp', array(), false, true ),
-                       array( 'setLoggedOutTimestamp', array( 123 ), false, false ),
-                       array( 'getProviderMetadata', array(), false, true ),
-                       array( 'save', array(), false, false ),
-                       array( 'delaySave', array(), false, true ),
-                       array( 'renew', array(), false, false ),
-               );
+               return [
+                       [ 'getId', [], false, true ],
+                       [ 'getSessionId', [], false, true ],
+                       [ 'resetId', [], false, true ],
+                       [ 'getProvider', [], false, true ],
+                       [ 'isPersistent', [], false, true ],
+                       [ 'persist', [], false, false ],
+                       [ 'unpersist', [], false, false ],
+                       [ 'shouldRememberUser', [], false, true ],
+                       [ 'setRememberUser', [ true ], false, false ],
+                       [ 'getRequest', [], true, true ],
+                       [ 'getUser', [], false, true ],
+                       [ 'getAllowedUserRights', [], false, true ],
+                       [ 'canSetUser', [], false, true ],
+                       [ 'setUser', [ new \stdClass ], false, false ],
+                       [ 'suggestLoginUsername', [], true, true ],
+                       [ 'shouldForceHTTPS', [], false, true ],
+                       [ 'setForceHTTPS', [ true ], false, false ],
+                       [ 'getLoggedOutTimestamp', [], false, true ],
+                       [ 'setLoggedOutTimestamp', [ 123 ], false, false ],
+                       [ 'getProviderMetadata', [], false, true ],
+                       [ 'save', [], false, false ],
+                       [ 'delaySave', [], false, true ],
+                       [ 'renew', [], false, false ],
+               ];
        }
 
        public function testDataAccess() {
@@ -136,12 +138,12 @@ class SessionTest extends MediaWikiTestCase {
                $session->remove( 101 );
                $this->assertFalse( $backend->dirty );
 
-               $backend->data = array( 'a', 'b', '?' => 'c' );
+               $backend->data = [ 'a', 'b', '?' => 'c' ];
                $this->assertSame( 3, $session->count() );
                $this->assertSame( 3, count( $session ) );
                $this->assertFalse( $backend->dirty );
 
-               $data = array();
+               $data = [];
                foreach ( $session as $key => $value ) {
                        $data[$key] = $value;
                }
@@ -152,12 +154,77 @@ class SessionTest extends MediaWikiTestCase {
                $this->assertFalse( $backend->dirty );
        }
 
+       public function testArrayAccess() {
+               $logger = new \TestLogger;
+               $session = TestUtils::getDummySession( null, -1, $logger );
+               $backend = \TestingAccessWrapper::newFromObject( $session )->backend;
+
+               $this->assertEquals( 1, $session['foo'] );
+               $this->assertEquals( 'zero', $session[0] );
+               $this->assertFalse( $backend->dirty );
+
+               $logger->setCollect( true );
+               $this->assertEquals( null, $session['null'] );
+               $logger->setCollect( false );
+               $this->assertFalse( $backend->dirty );
+               $this->assertSame( [
+                       [ LogLevel::DEBUG, 'Undefined index (auto-adds to session with a null value): null' ]
+               ], $logger->getBuffer() );
+               $logger->clearBuffer();
+
+               $session['foo'] = 55;
+               $this->assertEquals( 55, $backend->data['foo'] );
+               $this->assertTrue( $backend->dirty );
+               $backend->dirty = false;
+
+               $session[1] = 'one';
+               $this->assertEquals( 'one', $backend->data[1] );
+               $this->assertTrue( $backend->dirty );
+               $backend->dirty = false;
+
+               $session[1] = 'one';
+               $this->assertFalse( $backend->dirty );
+
+               $session['bar'] = [ 'baz' => [] ];
+               $session['bar']['baz']['quux'] = 2;
+               $this->assertEquals( [ 'baz' => [ 'quux' => 2 ] ], $backend->data['bar'] );
+
+               $logger->setCollect( true );
+               $session['bar2']['baz']['quux'] = 3;
+               $logger->setCollect( false );
+               $this->assertEquals( [ 'baz' => [ 'quux' => 3 ] ], $backend->data['bar2'] );
+               $this->assertSame( [
+                       [ LogLevel::DEBUG, 'Undefined index (auto-adds to session with a null value): bar2' ]
+               ], $logger->getBuffer() );
+               $logger->clearBuffer();
+
+               $backend->dirty = false;
+               $this->assertTrue( isset( $session['foo'] ) );
+               $this->assertTrue( isset( $session[1] ) );
+               $this->assertFalse( isset( $session['null'] ) );
+               $this->assertFalse( isset( $session['missing'] ) );
+               $this->assertFalse( isset( $session[100] ) );
+               $this->assertFalse( $backend->dirty );
+
+               unset( $session['foo'] );
+               $this->assertArrayNotHasKey( 'foo', $backend->data );
+               $this->assertTrue( $backend->dirty );
+               $backend->dirty = false;
+               unset( $session[1] );
+               $this->assertArrayNotHasKey( 1, $backend->data );
+               $this->assertTrue( $backend->dirty );
+               $backend->dirty = false;
+
+               unset( $session[101] );
+               $this->assertFalse( $backend->dirty );
+       }
+
        public function testClear() {
                $session = TestUtils::getDummySession();
                $priv = \TestingAccessWrapper::newFromObject( $session );
 
                $backend = $this->getMock(
-                       'MediaWiki\\Session\\DummySessionBackend', array( 'canSetUser', 'setUser', 'save' )
+                       'MediaWiki\\Session\\DummySessionBackend', [ 'canSetUser', 'setUser', 'save' ]
                );
                $backend->expects( $this->once() )->method( 'canSetUser' )
                        ->will( $this->returnValue( true ) );
@@ -168,13 +235,13 @@ class SessionTest extends MediaWikiTestCase {
                $backend->expects( $this->once() )->method( 'save' );
                $priv->backend = $backend;
                $session->clear();
-               $this->assertSame( array(), $backend->data );
+               $this->assertSame( [], $backend->data );
                $this->assertTrue( $backend->dirty );
 
                $backend = $this->getMock(
-                       'MediaWiki\\Session\\DummySessionBackend', array( 'canSetUser', 'setUser', 'save' )
+                       'MediaWiki\\Session\\DummySessionBackend', [ 'canSetUser', 'setUser', 'save' ]
                );
-               $backend->data = array();
+               $backend->data = [];
                $backend->expects( $this->once() )->method( 'canSetUser' )
                        ->will( $this->returnValue( true ) );
                $backend->expects( $this->once() )->method( 'setUser' )
@@ -187,7 +254,7 @@ class SessionTest extends MediaWikiTestCase {
                $this->assertFalse( $backend->dirty );
 
                $backend = $this->getMock(
-                       'MediaWiki\\Session\\DummySessionBackend', array( 'canSetUser', 'setUser', 'save' )
+                       'MediaWiki\\Session\\DummySessionBackend', [ 'canSetUser', 'setUser', 'save' ]
                );
                $backend->expects( $this->once() )->method( 'canSetUser' )
                        ->will( $this->returnValue( false ) );
@@ -195,7 +262,7 @@ class SessionTest extends MediaWikiTestCase {
                $backend->expects( $this->once() )->method( 'save' );
                $priv->backend = $backend;
                $session->clear();
-               $this->assertSame( array(), $backend->data );
+               $this->assertSame( [], $backend->data );
                $this->assertTrue( $backend->dirty );
        }
 
@@ -231,7 +298,7 @@ class SessionTest extends MediaWikiTestCase {
 
                $backend->data['wsTokenSecrets']['secret'] = 'sekret';
                $token = \TestingAccessWrapper::newFromObject(
-                       $session->getToken( array( 'bar', 'baz' ), 'secret' )
+                       $session->getToken( [ 'bar', 'baz' ], 'secret' )
                );
                $this->assertSame( 'sekret', $token->secret );
                $this->assertSame( 'bar|baz', $token->salt );