Пример #1
0
 public function testAutoCreateOnLogin()
 {
     $username = self::usernameForCreation();
     $req = $this->getMock(AuthenticationRequest::class);
     $mock = $this->getMockForAbstractClass(PrimaryAuthenticationProvider::class);
     $mock->expects($this->any())->method('getUniqueId')->will($this->returnValue('primary'));
     $mock->expects($this->any())->method('beginPrimaryAuthentication')->will($this->returnValue(AuthenticationResponse::newPass($username)));
     $mock->expects($this->any())->method('accountCreationType')->will($this->returnValue(PrimaryAuthenticationProvider::TYPE_CREATE));
     $mock->expects($this->any())->method('testUserExists')->will($this->returnValue(true));
     $mock->expects($this->any())->method('testUserForCreation')->will($this->returnValue(StatusValue::newGood()));
     $mock2 = $this->getMockForAbstractClass(SecondaryAuthenticationProvider::class);
     $mock2->expects($this->any())->method('getUniqueId')->will($this->returnValue('secondary'));
     $mock2->expects($this->any())->method('beginSecondaryAuthentication')->will($this->returnValue(AuthenticationResponse::newUI([$req], $this->message('...'))));
     $mock2->expects($this->any())->method('continueSecondaryAuthentication')->will($this->returnValue(AuthenticationResponse::newAbstain()));
     $mock2->expects($this->any())->method('testUserForCreation')->will($this->returnValue(StatusValue::newGood()));
     $this->primaryauthMocks = [$mock];
     $this->secondaryauthMocks = [$mock2];
     $this->initializeManager(true);
     $this->manager->setLogger(new \Psr\Log\NullLogger());
     $session = $this->request->getSession();
     $session->clear();
     $this->assertSame(0, \User::newFromName($username)->getId(), 'sanity check');
     $callback = $this->callback(function ($user) use($username) {
         return $user->getName() === $username;
     });
     $this->hook('UserLoggedIn', $this->never());
     $this->hook('LocalUserCreated', $this->once())->with($callback, $this->equalTo(true));
     $ret = $this->manager->beginAuthentication([], 'http://localhost/');
     $this->unhook('LocalUserCreated');
     $this->unhook('UserLoggedIn');
     $this->assertSame(AuthenticationResponse::UI, $ret->status);
     $id = (int) \User::newFromName($username)->getId();
     $this->assertNotSame(0, \User::newFromName($username)->getId());
     $this->assertSame(0, $session->getUser()->getId());
     $this->hook('UserLoggedIn', $this->once())->with($callback);
     $this->hook('LocalUserCreated', $this->never());
     $ret = $this->manager->continueAuthentication([]);
     $this->unhook('LocalUserCreated');
     $this->unhook('UserLoggedIn');
     $this->assertSame(AuthenticationResponse::PASS, $ret->status);
     $this->assertSame($username, $ret->username);
     $this->assertSame($id, $session->getUser()->getId());
 }