93 'provider' => $this->provider,
94 'id' => self::SESSIONID,
100 $logger = new \Psr\Log\NullLogger();
103 $this->fail(
'Expected exception not thrown' );
104 }
catch ( \InvalidArgumentException $ex ) {
106 "Refusing to create session for unverified user {$info->getUserInfo()}",
112 'id' => self::SESSIONID,
119 $this->fail(
'Expected exception not thrown' );
120 }
catch ( \InvalidArgumentException $ex ) {
121 $this->assertSame(
'Cannot create session without a provider', $ex->getMessage() );
125 'provider' => $this->provider,
126 'id' => self::SESSIONID,
131 $id =
new SessionId(
'!' . $info->getId() );
134 $this->fail(
'Expected exception not thrown' );
135 }
catch ( \InvalidArgumentException $ex ) {
137 'SessionId and SessionInfo don\'t match',
143 'provider' => $this->provider,
144 'id' => self::SESSIONID,
150 $backend =
new SessionBackend( $id, $info, $this->store, $logger, 10 );
151 $this->assertSame( self::SESSIONID, $backend->getId() );
152 $this->assertSame( $id, $backend->getSessionId() );
153 $this->assertSame( $this->provider, $backend->getProvider() );
154 $this->assertInstanceOf( User::class, $backend->getUser() );
155 $this->assertSame(
'UTSysop', $backend->getUser()->getName() );
156 $this->assertSame( $info->wasPersisted(), $backend->isPersistent() );
157 $this->assertSame( $info->wasRemembered(), $backend->shouldRememberUser() );
158 $this->assertSame( $info->forceHTTPS(), $backend->shouldForceHTTPS() );
160 $expire = time() + 100;
161 $this->store->setSessionMeta( self::SESSIONID, [
'expires' => $expire ] );
164 'provider' => $this->provider,
165 'id' => self::SESSIONID,
167 'forceHTTPS' =>
true,
168 'metadata' => [
'foo' ],
172 $backend =
new SessionBackend( $id, $info, $this->store, $logger, 10 );
173 $this->assertSame( self::SESSIONID, $backend->getId() );
174 $this->assertSame( $id, $backend->getSessionId() );
175 $this->assertSame( $this->provider, $backend->getProvider() );
176 $this->assertInstanceOf( User::class, $backend->getUser() );
177 $this->assertTrue( $backend->getUser()->isAnon() );
178 $this->assertSame( $info->wasPersisted(), $backend->isPersistent() );
179 $this->assertSame( $info->wasRemembered(), $backend->shouldRememberUser() );
180 $this->assertSame( $info->forceHTTPS(), $backend->shouldForceHTTPS() );
181 $this->assertSame( $expire, TestingAccessWrapper::newFromObject( $backend )->expires );
182 $this->assertSame( [
'foo' ], $backend->getProviderMetadata() );
270 $builder = $this->getMockBuilder( \DummySessionProvider::class )
271 ->setMethods( [
'persistsSessionId',
'sessionIdWasReset' ] );
273 $this->provider = $builder->getMock();
274 $this->provider->expects( $this->any() )->method(
'persistsSessionId' )
275 ->will( $this->returnValue(
false ) );
276 $this->provider->expects( $this->never() )->method(
'sessionIdWasReset' );
278 $manager = TestingAccessWrapper::newFromObject( $this->manager );
279 $sessionId = $backend->getSessionId();
281 $this->assertSame( self::SESSIONID, $backend->getId() );
282 $this->assertSame( $backend->getId(), $sessionId->getId() );
283 $this->assertSame( $id, session_id() );
284 $this->assertSame( $backend,
$manager->allSessionBackends[self::SESSIONID] );
286 $this->provider = $builder->getMock();
287 $this->provider->expects( $this->any() )->method(
'persistsSessionId' )
288 ->will( $this->returnValue(
true ) );
290 $this->provider->expects( $this->once() )->method(
'sessionIdWasReset' )
291 ->with( $this->identicalTo( $backend ), $this->identicalTo( self::SESSIONID ) );
292 $manager = TestingAccessWrapper::newFromObject( $this->manager );
293 $sessionId = $backend->getSessionId();
295 $this->assertNotEquals( self::SESSIONID, $backend->getId() );
296 $this->assertSame( $backend->getId(), $sessionId->getId() );
297 $this->assertInternalType(
'array', $this->store->getSession( $backend->getId() ) );
298 $this->assertFalse( $this->store->getSession( self::SESSIONID ) );
299 $this->assertSame( $id, session_id() );
300 $this->assertArrayNotHasKey( self::SESSIONID,
$manager->allSessionBackends );
301 $this->assertArrayHasKey( $backend->getId(),
$manager->allSessionBackends );
302 $this->assertSame( $backend,
$manager->allSessionBackends[$backend->getId()] );
502 $user = static::getTestSysop()->getUser();
504 $testData = [
'foo' =>
'foo!',
'bar', [
'baz',
null ] ];
506 $neverHook = $this->getMockBuilder( __CLASS__ )
507 ->setMethods( [
'onSessionMetadata' ] )->getMock();
508 $neverHook->expects( $this->never() )->method(
'onSessionMetadata' );
510 $builder = $this->getMockBuilder( \DummySessionProvider::class )
511 ->setMethods( [
'persistSession',
'unpersistSession' ] );
513 $neverProvider = $builder->getMock();
514 $neverProvider->expects( $this->never() )->method(
'persistSession' );
515 $neverProvider->expects( $this->never() )->method(
'unpersistSession' );
518 $this->provider = $neverProvider;
520 $this->store->setSessionData( self::SESSIONID, $testData );
522 $this->store->deleteSession( self::SESSIONID );
523 $this->assertFalse( $backend->isPersistent(),
'sanity check' );
524 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
false;
525 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
false;
527 $this->assertFalse( $this->store->getSession( self::SESSIONID ),
'making sure it didn\'t save' );
530 $this->provider = $builder->getMock();
531 $this->provider->expects( $this->never() )->method(
'persistSession' );
532 $this->provider->expects( $this->atLeastOnce() )->method(
'unpersistSession' );
534 $this->store->setSessionData( self::SESSIONID, $testData );
536 $this->store->deleteSession( self::SESSIONID );
537 TestingAccessWrapper::newFromObject( $backend )->persist =
false;
538 TestingAccessWrapper::newFromObject( $backend )->forcePersist =
true;
539 $this->assertFalse( $backend->isPersistent(),
'sanity check' );
540 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
false;
541 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
false;
543 $this->assertFalse( $this->store->getSession( self::SESSIONID ),
'making sure it didn\'t save' );
546 $this->provider = $neverProvider;
548 $this->store->setSessionData( self::SESSIONID, $testData );
550 TestingAccessWrapper::newFromObject( $backend )->requests[100]
552 $this->store->deleteSession( self::SESSIONID );
553 TestingAccessWrapper::newFromObject( $backend )->persist =
false;
554 TestingAccessWrapper::newFromObject( $backend )->forcePersist =
true;
555 $this->assertFalse( $backend->isPersistent(),
'sanity check' );
556 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
false;
557 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
false;
559 $this->assertFalse( $this->store->getSession( self::SESSIONID ),
'making sure it didn\'t save' );
562 $this->provider = $neverProvider;
563 $this->onSessionMetadataCalled =
false;
565 $this->store->setSessionData( self::SESSIONID, $testData );
567 $this->store->deleteSession( self::SESSIONID );
568 $this->assertFalse( $backend->isPersistent(),
'sanity check' );
569 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
false;
570 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
true;
572 $this->assertTrue( $this->onSessionMetadataCalled );
573 $blob = $this->store->getSession( self::SESSIONID );
574 $this->assertInternalType(
'array',
$blob );
575 $this->assertArrayHasKey(
'metadata',
$blob );
576 $metadata =
$blob[
'metadata'];
577 $this->assertInternalType(
'array', $metadata );
578 $this->assertArrayHasKey(
'???', $metadata );
579 $this->assertSame(
'!!!', $metadata[
'???'] );
580 $this->assertFalse( $this->store->getSessionFromBackend( self::SESSIONID ),
581 'making sure it didn\'t save to backend' );
584 $this->provider = $neverProvider;
586 $this->store->setSessionData( self::SESSIONID, $testData );
588 $this->store->deleteSession( self::SESSIONID );
589 TestingAccessWrapper::newFromObject( $backend )->persist =
true;
590 $this->assertTrue( $backend->isPersistent(),
'sanity check' );
591 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
false;
592 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
false;
594 $this->assertFalse( $this->store->getSession( self::SESSIONID ),
'making sure it didn\'t save' );
597 $this->provider = $builder->getMock();
598 $this->provider->expects( $this->atLeastOnce() )->method(
'persistSession' );
599 $this->provider->expects( $this->never() )->method(
'unpersistSession' );
601 $this->store->setSessionData( self::SESSIONID, $testData );
603 $this->store->deleteSession( self::SESSIONID );
604 TestingAccessWrapper::newFromObject( $backend )->persist =
true;
605 TestingAccessWrapper::newFromObject( $backend )->forcePersist =
true;
606 $this->assertTrue( $backend->isPersistent(),
'sanity check' );
607 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
false;
608 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
false;
610 $this->assertFalse( $this->store->getSession( self::SESSIONID ),
'making sure it didn\'t save' );
613 $this->provider = $neverProvider;
614 $this->onSessionMetadataCalled =
false;
616 $this->store->setSessionData( self::SESSIONID, $testData );
618 $this->store->deleteSession( self::SESSIONID );
619 TestingAccessWrapper::newFromObject( $backend )->persist =
true;
620 $this->assertTrue( $backend->isPersistent(),
'sanity check' );
621 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
false;
622 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
true;
624 $this->assertTrue( $this->onSessionMetadataCalled );
625 $blob = $this->store->getSession( self::SESSIONID );
626 $this->assertInternalType(
'array',
$blob );
627 $this->assertArrayHasKey(
'metadata',
$blob );
628 $metadata =
$blob[
'metadata'];
629 $this->assertInternalType(
'array', $metadata );
630 $this->assertArrayHasKey(
'???', $metadata );
631 $this->assertSame(
'!!!', $metadata[
'???'] );
632 $this->assertNotSame(
false, $this->store->getSessionFromBackend( self::SESSIONID ),
633 'making sure it did save to backend' );
636 $this->provider = $builder->getMock();
637 $this->provider->expects( $this->atLeastOnce() )->method(
'persistSession' );
638 $this->provider->expects( $this->never() )->method(
'unpersistSession' );
639 $this->onSessionMetadataCalled =
false;
641 $this->store->setSessionData( self::SESSIONID, $testData );
643 $this->store->deleteSession( self::SESSIONID );
644 TestingAccessWrapper::newFromObject( $backend )->persist =
true;
645 TestingAccessWrapper::newFromObject( $backend )->forcePersist =
true;
646 $this->assertTrue( $backend->isPersistent(),
'sanity check' );
647 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
false;
648 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
true;
650 $this->assertTrue( $this->onSessionMetadataCalled );
651 $blob = $this->store->getSession( self::SESSIONID );
652 $this->assertInternalType(
'array',
$blob );
653 $this->assertArrayHasKey(
'metadata',
$blob );
654 $metadata =
$blob[
'metadata'];
655 $this->assertInternalType(
'array', $metadata );
656 $this->assertArrayHasKey(
'???', $metadata );
657 $this->assertSame(
'!!!', $metadata[
'???'] );
658 $this->assertNotSame(
false, $this->store->getSessionFromBackend( self::SESSIONID ),
659 'making sure it did save to backend' );
662 $this->provider = $builder->getMock();
663 $this->provider->expects( $this->atLeastOnce() )->method(
'persistSession' );
664 $this->provider->expects( $this->never() )->method(
'unpersistSession' );
665 $this->onSessionMetadataCalled =
false;
667 $this->store->setSessionData( self::SESSIONID, $testData );
669 $this->store->deleteSession( self::SESSIONID );
670 TestingAccessWrapper::newFromObject( $backend )->persist =
true;
671 $this->assertTrue( $backend->isPersistent(),
'sanity check' );
672 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
true;
673 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
false;
675 $this->assertTrue( $this->onSessionMetadataCalled );
676 $blob = $this->store->getSession( self::SESSIONID );
677 $this->assertInternalType(
'array',
$blob );
678 $this->assertArrayHasKey(
'metadata',
$blob );
679 $metadata =
$blob[
'metadata'];
680 $this->assertInternalType(
'array', $metadata );
681 $this->assertArrayHasKey(
'???', $metadata );
682 $this->assertSame(
'!!!', $metadata[
'???'] );
683 $this->assertNotSame(
false, $this->store->getSessionFromBackend( self::SESSIONID ),
684 'making sure it did save to backend' );
688 $this->provider = $neverProvider;
689 $this->onSessionMetadataCalled =
false;
691 $this->store->setSessionData( self::SESSIONID, $testData );
693 $this->store->deleteSession( self::SESSIONID );
694 TestingAccessWrapper::newFromObject( $backend )->persist =
true;
695 $this->assertTrue( $backend->isPersistent(),
'sanity check' );
696 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
false;
697 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
false;
698 TestingAccessWrapper::newFromObject( $backend )->dataHash =
'Doesn\'t match';
700 $this->assertTrue( $this->onSessionMetadataCalled );
701 $blob = $this->store->getSession( self::SESSIONID );
702 $this->assertInternalType(
'array',
$blob );
703 $this->assertArrayHasKey(
'metadata',
$blob );
704 $metadata =
$blob[
'metadata'];
705 $this->assertInternalType(
'array', $metadata );
706 $this->assertArrayHasKey(
'???', $metadata );
707 $this->assertSame(
'!!!', $metadata[
'???'] );
708 $this->assertNotSame(
false, $this->store->getSessionFromBackend( self::SESSIONID ),
709 'making sure it did save to backend' );
712 $this->provider =
null;
713 $mockHook = $this->getMockBuilder( __CLASS__ )
714 ->setMethods( [
'onSessionMetadata' ] )->getMock();
715 $mockHook->expects( $this->any() )->method(
'onSessionMetadata' )
716 ->will( $this->returnCallback(
718 $metadata[
'userId']++;
722 $this->store->setSessionData( self::SESSIONID, $testData );
727 $this->fail(
'Expected exception not thrown' );
728 }
catch ( \UnexpectedValueException $ex ) {
730 'SessionMetadata hook changed metadata key "userId"',
736 TestingAccessWrapper::newFromObject( $this->manager )->preventUsers = [
737 $user->getName() =>
true,
739 $this->provider = $neverProvider;
741 $this->store->setSessionData( self::SESSIONID, $testData );
743 $this->store->deleteSession( self::SESSIONID );
744 TestingAccessWrapper::newFromObject( $backend )->persist =
true;
745 $this->assertTrue( $backend->
isPersistent(),
'sanity check' );
746 TestingAccessWrapper::newFromObject( $backend )->metaDirty =
true;
747 TestingAccessWrapper::newFromObject( $backend )->dataDirty =
true;
749 $this->assertFalse( $this->store->getSession( self::SESSIONID ),
'making sure it didn\'t save' );
753 $user = static::getTestSysop()->getUser();
755 $testData = [
'foo' =>
'foo!',
'bar', [
'baz',
null ] ];
758 $this->provider = $this->getMockBuilder( \DummySessionProvider::class )
759 ->setMethods( [
'persistSession' ] )->getMock();
760 $this->provider->expects( $this->never() )->method(
'persistSession' );
761 $this->onSessionMetadataCalled =
false;
763 $this->store->setSessionData( self::SESSIONID, $testData );
765 $this->store->deleteSession( self::SESSIONID );
766 $wrap = TestingAccessWrapper::newFromObject( $backend );
767 $this->assertFalse( $backend->isPersistent(),
'sanity check' );
768 $wrap->metaDirty =
false;
769 $wrap->dataDirty =
false;
770 $wrap->forcePersist =
false;
773 $this->assertTrue( $this->onSessionMetadataCalled );
774 $blob = $this->store->getSession( self::SESSIONID );
775 $this->assertInternalType(
'array',
$blob );
776 $this->assertArrayHasKey(
'metadata',
$blob );
777 $metadata =
$blob[
'metadata'];
778 $this->assertInternalType(
'array', $metadata );
779 $this->assertArrayHasKey(
'???', $metadata );
780 $this->assertSame(
'!!!', $metadata[
'???'] );
781 $this->assertNotEquals( 0, $wrap->expires );
784 $this->provider = $this->getMockBuilder( \DummySessionProvider::class )
785 ->setMethods( [
'persistSession' ] )->getMock();
786 $this->provider->expects( $this->atLeastOnce() )->method(
'persistSession' );
787 $this->onSessionMetadataCalled =
false;
789 $this->store->setSessionData( self::SESSIONID, $testData );
791 $this->store->deleteSession( self::SESSIONID );
792 $wrap = TestingAccessWrapper::newFromObject( $backend );
793 $wrap->persist =
true;
794 $this->assertTrue( $backend->isPersistent(),
'sanity check' );
795 $wrap->metaDirty =
false;
796 $wrap->dataDirty =
false;
797 $wrap->forcePersist =
false;
800 $this->assertTrue( $this->onSessionMetadataCalled );
801 $blob = $this->store->getSession( self::SESSIONID );
802 $this->assertInternalType(
'array',
$blob );
803 $this->assertArrayHasKey(
'metadata',
$blob );
804 $metadata =
$blob[
'metadata'];
805 $this->assertInternalType(
'array', $metadata );
806 $this->assertArrayHasKey(
'???', $metadata );
807 $this->assertSame(
'!!!', $metadata[
'???'] );
808 $this->assertNotEquals( 0, $wrap->expires );
811 $this->provider = $this->getMockBuilder( \DummySessionProvider::class )
812 ->setMethods( [
'persistSession' ] )->getMock();
813 $this->provider->expects( $this->never() )->method(
'persistSession' );
814 $this->onSessionMetadataCalled =
false;
816 $this->store->setSessionData( self::SESSIONID, $testData );
818 $this->store->deleteSession( self::SESSIONID );
819 $wrap = TestingAccessWrapper::newFromObject( $backend );
820 $this->assertFalse( $backend->isPersistent(),
'sanity check' );
821 $wrap->metaDirty =
false;
822 $wrap->dataDirty =
false;
823 $wrap->forcePersist =
false;
824 $expires = time() + $wrap->lifetime + 100;
825 $wrap->expires = $expires;
827 $this->assertFalse( $this->onSessionMetadataCalled );
828 $this->assertFalse( $this->store->getSession( self::SESSIONID ),
'making sure it didn\'t save' );
829 $this->assertEquals( $expires, $wrap->expires );