| @@ 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 |
|
| @@ 164-197 (lines=34) @@ | ||
| 161 | * @param Client $client |
|
| 162 | * @return Crawler $crawler of the page of the url or the page after the login |
|
| 163 | */ |
|
| 164 | private function loginUserIfRequired(Client $client, $url, $username = "dominik", $password = "lkjlkjlkjlkj") |
|
| 165 | { |
|
| 166 | // try to get the url |
|
| 167 | $client->followRedirects(); |
|
| 168 | $crawler = $client->request("GET", $url); |
|
| 169 | ||
| 170 | $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Status-Code 200 expected."); |
|
| 171 | ||
| 172 | // if redirected to a login-page, login as admin-user |
|
| 173 | if ($crawler->filter("input")->count() == 5 && $crawler->filter("#username")->count() == 1 && $crawler->filter("#password")->count() == 1 ) { |
|
| 174 | ||
| 175 | // set the password of the admin |
|
| 176 | $userProvider = $this->getContainer()->get('fos_user.user_provider.username_email'); |
|
| 177 | $user = $userProvider->loadUserByUsername($username); |
|
| 178 | $user->setPlainPassword($password); |
|
| 179 | $user->addRole("ROLE_ADMIN"); |
|
| 180 | ||
| 181 | $userManager = $this->getContainer()->get('fos_user.user_manager'); |
|
| 182 | $userManager->updateUser($user); |
|
| 183 | ||
| 184 | $crawler = $crawler->filter("input[type='submit']"); |
|
| 185 | $form = $crawler->form(); |
|
| 186 | $form->get('_username')->setValue($username); |
|
| 187 | $form->get('_password')->setValue($password); |
|
| 188 | $crawler = $client->submit($form); |
|
| 189 | } |
|
| 190 | ||
| 191 | $this->assertEquals(200, $client->getResponse()->getStatusCode(),"Login failed."); |
|
| 192 | $client->followRedirects(false); |
|
| 193 | ||
| 194 | $this->assertStringEndsWith($url, $client->getRequest()->getUri(), "Login failed or not redirected to requested url: $url vs. ".$client->getRequest()->getUri()); |
|
| 195 | ||
| 196 | return $crawler; |
|
| 197 | } |
|
| 198 | ||
| 199 | /** |
|
| 200 | * @var ContainerInterface |
|