| @@ 50-83 (lines=34) @@ | ||
| 47 | * @param Client $client |
|
| 48 | * @return Crawler $crawler of the page of the url or the page after the login |
|
| 49 | */ |
|
| 50 | private function loginUserIfRequired(Client $client, $url, $username = "dominik", $password = "lkjlkjlkjlkj") |
|
| 51 | { |
|
| 52 | // try to get the url |
|
| 53 | $client->followRedirects(); |
|
| 54 | $crawler = $client->request("GET", $url); |
|
| 55 | ||
| 56 | $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Status-Code 200 expected."); |
|
| 57 | ||
| 58 | // if redirected to a login-page, login as admin-user |
|
| 59 | if ($crawler->filter("input")->count() == 5 && $crawler->filter("#username")->count() == 1 && $crawler->filter("#password")->count() == 1 ) { |
|
| 60 | ||
| 61 | // set the password of the admin |
|
| 62 | $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email'); |
|
| 63 | $user = $userProvider->loadUserByUsername($username); |
|
| 64 | $user->setPlainPassword($password); |
|
| 65 | $user->addRole("ROLE_ADMIN"); |
|
| 66 | ||
| 67 | $userManager = $this->getContainer()->get('fos_user.user_manager'); |
|
| 68 | $userManager->updateUser($user); |
|
| 69 | ||
| 70 | $crawler = $crawler->filter("input[type='submit']"); |
|
| 71 | $form = $crawler->form(); |
|
| 72 | $form->get('_username')->setValue($username); |
|
| 73 | $form->get('_password')->setValue($password); |
|
| 74 | $crawler = $client->submit($form); |
|
| 75 | } |
|
| 76 | ||
| 77 | $this->assertEquals(200, $client->getResponse()->getStatusCode(),"Login failed."); |
|
| 78 | $client->followRedirects(false); |
|
| 79 | ||
| 80 | $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri()); |
|
| 81 | ||
| 82 | return $crawler; |
|
| 83 | } |
|
| 84 | ||
| 85 | /** |
|
| 86 | * @var ContainerInterface |
|
| @@ 185-218 (lines=34) @@ | ||
| 182 | * @param Client $client |
|
| 183 | * @return Crawler $crawler of the page of the url or the page after the login |
|
| 184 | */ |
|
| 185 | private function loginUserIfRequired(Client $client, $url, $username = "dominik", $password = "lkjlkjlkjlkj") |
|
| 186 | { |
|
| 187 | // try to get the url |
|
| 188 | $client->followRedirects(); |
|
| 189 | $crawler = $client->request("GET", $url); |
|
| 190 | ||
| 191 | $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Status-Code 200 expected."); |
|
| 192 | ||
| 193 | // if redirected to a login-page, login as admin-user |
|
| 194 | if ($crawler->filter("input")->count() == 5 && $crawler->filter("#username")->count() == 1 && $crawler->filter("#password")->count() == 1 ) { |
|
| 195 | ||
| 196 | // set the password of the admin |
|
| 197 | $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email'); |
|
| 198 | $user = $userProvider->loadUserByUsername($username); |
|
| 199 | $user->setPlainPassword($password); |
|
| 200 | $user->addRole("ROLE_ADMIN"); |
|
| 201 | ||
| 202 | $userManager = $this->getContainer()->get('fos_user.user_manager'); |
|
| 203 | $userManager->updateUser($user); |
|
| 204 | ||
| 205 | $crawler = $crawler->filter("input[type='submit']"); |
|
| 206 | $form = $crawler->form(); |
|
| 207 | $form->get('_username')->setValue($username); |
|
| 208 | $form->get('_password')->setValue($password); |
|
| 209 | $crawler = $client->submit($form); |
|
| 210 | } |
|
| 211 | ||
| 212 | $this->assertEquals(200, $client->getResponse()->getStatusCode(),"Login failed."); |
|
| 213 | $client->followRedirects(false); |
|
| 214 | ||
| 215 | $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri()); |
|
| 216 | ||
| 217 | return $crawler; |
|
| 218 | } |
|
| 219 | ||
| 220 | /** |
|
| 221 | * @var ContainerInterface |
|