From 69a4e83f4a0f1690fcc4b219d71df17c5682fee4 Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Fri, 15 May 2020 12:45:42 +1000 Subject: [PATCH 1/3] Create WebTestCase::loginClient --- doc/logged.md | 14 +++++++------- src/Test/WebTestCase.php | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/doc/logged.md b/doc/logged.md index 43c81a50..9d64827e 100644 --- a/doc/logged.md +++ b/doc/logged.md @@ -8,7 +8,7 @@ You have three alternatives to create an already logged in client: 1. Use the `liip_functional_test.authentication` key in the `config_test.yml` file; 2. Pass an array with login parameters directly when you call the method; -3. Use the method `WebTestCase::loginAs()`; +3. Use the method `WebTestCase::loginClient()`; ### Logging in a user from the `config_test.yml` file @@ -37,22 +37,22 @@ $credentials = array( $client = $this->makeClient($credentials); ``` -### Logging in a user using `WebTestCase::loginAs()` +### Logging in a user using `WebTestCase::loginClient()` -To use the method `WebTestCase::loginAs()` you have to [return the repository containing all references set in the +To use the method `WebTestCase::loginClient()` you have to [return the repository containing all references set in the fixtures](#referencing-fixtures-in-tests) using the method `getReferenceRepository()` and pass the reference of the `User` -object to the method `WebTestCase::loginAs()`. +object to the method `WebTestCase::loginClient()`. ```php +$client = $this->makeClient(); $fixtures = $this->loadFixtures(array( 'AppBundle\DataFixtures\ORM\LoadUserData' ))->getReferenceRepository(); -$this->loginAs($fixtures->getReference('account-alpha'), 'main'); -$client = $this->makeClient(); +$this->loginClient($client, $fixtures->getReference('account-alpha'), 'main'); ``` -Remember that `WebTestCase::loginAs()` accepts objects that implement the interface `Symfony\Component\Security\Core\User\UserInterface`. +Remember that `WebTestCase::loginClient()` accepts objects that implement the interface `Symfony\Component\Security\Core\User\UserInterface`. **If you get the error message *"Missing session.storage.options#name"***, you have to simply add to your [`config_test.yml`](https://github.com/liip/LiipFunctionalTestBundle/blob/master/Tests/App/config.yml#L16) diff --git a/src/Test/WebTestCase.php b/src/Test/WebTestCase.php index e7b60814..745bcb4f 100644 --- a/src/Test/WebTestCase.php +++ b/src/Test/WebTestCase.php @@ -399,11 +399,37 @@ public function fetchCrawler(string $path, string $method = 'GET', bool $authent */ public function loginAs(UserInterface $user, string $firewallName): self { + @trigger_error(sprintf('"%s()" is deprecated, use loginClient() after creating a client.', __METHOD__), E_USER_DEPRECATED); + $this->firewallLogins[$firewallName] = $user; return $this; } + public function loginClient(KernelBrowser $client, UserInterface $user, string $firewallName): void + { + // has to be set otherwise "hasPreviousSession" in Request returns false. + $options = $client->getContainer()->getParameter('session.storage.options'); + + if (!$options || !isset($options['name'])) { + throw new \InvalidArgumentException('Missing session.storage.options#name'); + } + + $session = $client->getContainer()->get('session'); + $session->setId(uniqid()); + + $client->getCookieJar()->set(new Cookie($options['name'], $session->getId())); + + $token = $this->createUserToken($user, $firewallName); + + $tokenStorage = $client->getContainer()->get('security.token_storage'); + + $tokenStorage->setToken($token); + $session->set('_security_'.$firewallName, serialize($token)); + + $session->save(); + } + /** * Asserts that the HTTP response code of the last request performed by * $client matches the expected code. If not, raises an error with more From 34958608e2dcc7ada9cdcd170fdc6695940025e9 Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Fri, 15 May 2020 13:19:56 +1000 Subject: [PATCH 2/3] Test loginClient --- tests/Test/WebTestCaseConfigTest.php | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/Test/WebTestCaseConfigTest.php b/tests/Test/WebTestCaseConfigTest.php index 08cc6c0d..c3e17235 100644 --- a/tests/Test/WebTestCaseConfigTest.php +++ b/tests/Test/WebTestCaseConfigTest.php @@ -139,6 +139,38 @@ public function testIndexAuthenticationLoginAs(): void ); } + /** + * Log in as the user defined in the Data Fixture. + */ + public function testIndexAuthenticationLoginClient(): void + { + $this->client = static::makeClient(); + + $this->schemaUpdate(); + $user = $this->loadTestFixtures(); + + $this->loginClient($this->client, $user, 'secured_area'); + + $path = '/'; + + $crawler = $this->client->request('GET', $path); + + $this->assertStatusCode(200, $this->client); + + $this->assertSame(1, + $crawler->filter('html > body')->count()); + + $this->assertSame( + 'Logged in as foo bar.', + $crawler->filter('p#user')->text() + ); + + $this->assertSame( + 'LiipFunctionalTestBundle', + $crawler->filter('h1')->text() + ); + } + /** * Log in as the user defined in the Data Fixtures and except an * AllowedQueriesExceededException exception. From 6db746aa167fb12d3b51e6908b309d0337c994f6 Mon Sep 17 00:00:00 2001 From: Nathan Dench Date: Fri, 15 May 2020 13:43:34 +1000 Subject: [PATCH 3/3] fix styles --- tests/Test/WebTestCaseConfigTest.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/Test/WebTestCaseConfigTest.php b/tests/Test/WebTestCaseConfigTest.php index c3e17235..f48471a6 100644 --- a/tests/Test/WebTestCaseConfigTest.php +++ b/tests/Test/WebTestCaseConfigTest.php @@ -59,8 +59,10 @@ public function testIndexClientWithCredentials(): void $this->assertStatusCode(200, $this->client); - $this->assertSame(1, - $crawler->filter('html > body')->count()); + $this->assertSame( + 1, + $crawler->filter('html > body')->count() + ); $this->assertSame( 'Logged in as foobar.', @@ -88,8 +90,10 @@ public function testIndexAuthenticatedClient(): void $this->assertStatusCode(200, $this->client); - $this->assertSame(1, - $crawler->filter('html > body')->count()); + $this->assertSame( + 1, + $crawler->filter('html > body')->count() + ); $this->assertSame( 'Logged in as foobar.', @@ -125,8 +129,10 @@ public function testIndexAuthenticationLoginAs(): void $this->assertStatusCode(200, $this->client); - $this->assertSame(1, - $crawler->filter('html > body')->count()); + $this->assertSame( + 1, + $crawler->filter('html > body')->count() + ); $this->assertSame( 'Logged in as foo bar.', @@ -157,8 +163,10 @@ public function testIndexAuthenticationLoginClient(): void $this->assertStatusCode(200, $this->client); - $this->assertSame(1, - $crawler->filter('html > body')->count()); + $this->assertSame( + 1, + $crawler->filter('html > body')->count() + ); $this->assertSame( 'Logged in as foo bar.',