| @@ 50-82 (lines=33) @@ | ||
| 47 | * | |
| 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->assertSame(200, $client->getResponse()->getStatusCode(), 'Status-Code 200 expected.'); | |
| 57 | ||
| 58 | // if redirected to a login-page, login as admin-user | |
| 59 |         if (5 == $crawler->filter('input')->count() && 1 == $crawler->filter('#username')->count() && 1 == $crawler->filter('#password')->count()) { | |
| 60 | // set the password of the admin | |
| 61 |             $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email'); | |
| 62 | $user = $userProvider->loadUserByUsername($username); | |
| 63 | $user->setPlainPassword($password); | |
| 64 |             $user->addRole('ROLE_ADMIN'); | |
| 65 | ||
| 66 |             $userManager = $this->getContainer()->get('fos_user.user_manager'); | |
| 67 | $userManager->updateUser($user); | |
| 68 | ||
| 69 |             $crawler = $crawler->filter("input[type='submit']"); | |
| 70 | $form = $crawler->form(); | |
| 71 |             $form->get('_username')->setValue($username); | |
| 72 |             $form->get('_password')->setValue($password); | |
| 73 | $crawler = $client->submit($form); | |
| 74 | } | |
| 75 | ||
| 76 | $this->assertSame(200, $client->getResponse()->getStatusCode(), 'Login failed.'); | |
| 77 | $client->followRedirects(false); | |
| 78 | ||
| 79 | $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri()); | |
| 80 | ||
| 81 | return $crawler; | |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * @var ContainerInterface | |
| @@ 222-254 (lines=33) @@ | ||
| 219 | * | |
| 220 | * @return Crawler $crawler of the page of the url or the page after the login | |
| 221 | */ | |
| 222 | private function loginUserIfRequired(Client $client, $url, $username = 'dominik', $password = 'lkjlkjlkjlkj') | |
| 223 |     { | |
| 224 | // try to get the url | |
| 225 | $client->followRedirects(); | |
| 226 |         $crawler = $client->request('GET', $url); | |
| 227 | ||
| 228 | $this->assertSame(200, $client->getResponse()->getStatusCode(), 'Status-Code 200 expected.'); | |
| 229 | ||
| 230 | // if redirected to a login-page, login as admin-user | |
| 231 |         if (5 == $crawler->filter('input')->count() && 1 == $crawler->filter('#username')->count() && 1 == $crawler->filter('#password')->count()) { | |
| 232 | // set the password of the admin | |
| 233 |             $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email'); | |
| 234 | $user = $userProvider->loadUserByUsername($username); | |
| 235 | $user->setPlainPassword($password); | |
| 236 |             $user->addRole('ROLE_ADMIN'); | |
| 237 | ||
| 238 |             $userManager = $this->getContainer()->get('fos_user.user_manager'); | |
| 239 | $userManager->updateUser($user); | |
| 240 | ||
| 241 |             $crawler = $crawler->filter("input[type='submit']"); | |
| 242 | $form = $crawler->form(); | |
| 243 |             $form->get('_username')->setValue($username); | |
| 244 |             $form->get('_password')->setValue($password); | |
| 245 | $crawler = $client->submit($form); | |
| 246 | } | |
| 247 | ||
| 248 | $this->assertSame(200, $client->getResponse()->getStatusCode(), 'Login failed.'); | |
| 249 | $client->followRedirects(false); | |
| 250 | ||
| 251 | $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri()); | |
| 252 | ||
| 253 | return $crawler; | |
| 254 | } | |
| 255 | ||
| 256 | /** | |
| 257 | * @var ContainerInterface | |