93 'provider' => $this->provider,
94 'id' => self::SESSIONID,
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() );
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() );
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() );
453 $priv = TestingAccessWrapper::newFromObject( $backend );
454 $priv->persist =
true;
457 $this->onSessionMetadataCalled =
false;
458 $priv->metaDirty =
true;
460 $this->
assertTrue( $this->onSessionMetadataCalled,
'sanity check' );
462 $this->onSessionMetadataCalled =
false;
463 $priv->metaDirty =
true;
465 $this->
assertTrue( $this->onSessionMetadataCalled,
'sanity check' );
467 $delay = $backend->delaySave();
470 $this->onSessionMetadataCalled =
false;
471 $priv->metaDirty =
true;
473 $this->
assertFalse( $this->onSessionMetadataCalled );
477 $this->
assertTrue( $this->onSessionMetadataCalled );
480 $this->onSessionMetadataCalled =
false;
481 $priv->metaDirty =
true;
482 \Wikimedia\ScopedCallback::consume( $delay );
483 $this->
assertTrue( $this->onSessionMetadataCalled );
486 $delay1 = $backend->delaySave();
487 $delay2 = $backend->delaySave();
488 $delay3 = $backend->delaySave();
489 $this->onSessionMetadataCalled =
false;
490 $priv->metaDirty =
true;
492 $this->
assertFalse( $this->onSessionMetadataCalled );
493 \Wikimedia\ScopedCallback::consume( $delay3 );
494 $this->
assertFalse( $this->onSessionMetadataCalled );
495 \Wikimedia\ScopedCallback::consume( $delay1 );
496 $this->
assertFalse( $this->onSessionMetadataCalled );
497 \Wikimedia\ScopedCallback::consume( $delay2 );
498 $this->
assertTrue( $this->onSessionMetadataCalled );
502 $user = static::getTestSysop()->getUser();
504 $testData = [
'foo' =>
'foo!',
'bar', [
'baz',
null ] ];
507 ->setMethods( [
'onSessionMetadata' ] )->getMock();
508 $neverHook->expects( $this->never() )->method(
'onSessionMetadata' );
511 ->setMethods( [
'persistSession',
'unpersistSession' ] );
513 $neverProvider = $builder->getMock();
514 $neverProvider->expects( $this->never() )->method(
'persistSession' );
515 $neverProvider->expects( $this->never() )->method(
'unpersistSession' );
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' );
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' );
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 );
576 $metadata =
$blob[
'metadata'];
580 $this->
assertFalse( $this->store->getSessionFromBackend( self::SESSIONID ),
581 'making sure it didn\'t save to backend' );
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' );
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 );
628 $metadata =
$blob[
'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 );
654 $metadata =
$blob[
'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 );
679 $metadata =
$blob[
'metadata'];
683 $this->
assertNotSame(
false, $this->store->getSessionFromBackend( self::SESSIONID ),
684 'making sure it did save to backend' );
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 );
704 $metadata =
$blob[
'metadata'];
708 $this->
assertNotSame(
false, $this->store->getSessionFromBackend( self::SESSIONID ),
709 'making sure it did save to backend' );
712 $this->provider =
null;
714 ->setMethods( [
'onSessionMetadata' ] )->getMock();
715 $mockHook->expects( $this->
any() )->method(
'onSessionMetadata' )
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,
741 $this->store->setSessionData( self::SESSIONID, $testData );
743 $this->store->deleteSession( self::SESSIONID );
744 TestingAccessWrapper::newFromObject( $backend )->persist =
true;
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 );
777 $metadata =
$blob[
'metadata'];
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 );
804 $metadata =
$blob[
'metadata'];
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' );