I am working on setting up a site with fosUserBundle. I need to write some unit tests, but I cannot seems to get authentication to work correctly for use with them. I have have tried a few methods with no luck.
approach1 – chain providers in security settings to make a standard login I can rely on. When I do this the only authentication that works is the in_memory provider meaning I can login in with the admin user but not any users created by fosUserBundle
security.yml
security:
encoders:
"FOS\UserBundle\Model\UserInterface": plaintext
"Symfony\Component\Security\Core\User\User": plaintext
providers:
chain_provider:
providers: [in_memory, fos_userbundle]
in_memory:
users:
admin: { password: admin, roles: [ 'ROLE_ADMIN' ] }
fos_userbundle:
id: fos_user.user_manager
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
login_path: /
csrf_provider: form.csrf_provider
default_target_path: /media
logout: true
anonymous: true
http_basic:
provider: in_memory
Method 2- I created a user in my setup of the unit test. But with this method I can never login with the user I just created, I am not requiring user confirmation.
public function setUp() {
$kernel = static::createKernel();
$this->repo = $kernel->boot();
$this->repo = $kernel->getContainer();
$userManager = $this->repo->get('fos_user.user_manager');
$email = "testing".(string) self::newRandomNumber()."@test.com";
$un = "test".(string) self::newRandomNumber();
$fn = "testin";
$ln = "laster";
$pwd = "passworD1";
$user = $userManager->createUser();
$user->setEmail($email);
$user->setUsername($un);
$user->setFirstName($fn);
$user->setLastName($ln);
$user->setPlainPassword($pwd);
$user->setBimStaff(True);
// Persist the user to the database
$userManager->updateUser($user);
$this->user = $user;
$this->client = static::createClient();
$crawler = $this->client->request('GET', '/');
$form = $crawler->selectButton('Login')->form();
// set some values
$un = 'Lucas'.self::newRandomNumber();
$form['_username'] = $un;
$form['_password'] = $pwd;
// submit the form
$crawler = $this->client->submit($form);
print_r($this->client->getResponse());
$this->client->followRedirects();
}
I have tried both login in at the form and http basic authentication but neither seem to work.
Any one have any advice?
Thanks,
Cory
Hmm, It seems that if you chain providers in the security you cannot use just one of the providers, at least that solved the issue for me.
FOr some reason as soon as I changed both providers to
chain_providerit started cooperating.My unit test now looks like this
If copying this I would suggest using stronger passwords for your users 🙂