SwitchUserTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
  11. class SwitchUserTest extends WebTestCase
  12. {
  13. /**
  14. * @dataProvider getTestParameters
  15. */
  16. public function testSwitchUser($originalUser, $targetUser, $expectedUser, $expectedStatus)
  17. {
  18. $client = $this->createAuthenticatedClient($originalUser);
  19. $client->request('GET', '/profile?_switch_user='.$targetUser);
  20. $this->assertEquals($expectedStatus, $client->getResponse()->getStatusCode());
  21. $this->assertEquals($expectedUser, $client->getProfile()->getCollector('security')->getUser());
  22. }
  23. public function testSwitchedUserCannotSwitchToOther()
  24. {
  25. $client = $this->createAuthenticatedClient('user_can_switch');
  26. $client->request('GET', '/profile?_switch_user=user_cannot_switch_1');
  27. $client->request('GET', '/profile?_switch_user=user_cannot_switch_2');
  28. $this->assertEquals(500, $client->getResponse()->getStatusCode());
  29. $this->assertEquals('user_cannot_switch_1', $client->getProfile()->getCollector('security')->getUser());
  30. }
  31. public function testSwitchedUserExit()
  32. {
  33. $client = $this->createAuthenticatedClient('user_can_switch');
  34. $client->request('GET', '/profile?_switch_user=user_cannot_switch_1');
  35. $client->request('GET', '/profile?_switch_user=_exit');
  36. $this->assertEquals(200, $client->getResponse()->getStatusCode());
  37. $this->assertEquals('user_can_switch', $client->getProfile()->getCollector('security')->getUser());
  38. }
  39. public function getTestParameters()
  40. {
  41. return array(
  42. 'unauthorized_user_cannot_switch' => array('user_cannot_switch_1', 'user_cannot_switch_1', 'user_cannot_switch_1', 403),
  43. 'authorized_user_can_switch' => array('user_can_switch', 'user_cannot_switch_1', 'user_cannot_switch_1', 200),
  44. 'authorized_user_cannot_switch_to_non_existent' => array('user_can_switch', 'user_does_not_exist', 'user_can_switch', 500),
  45. 'authorized_user_can_switch_to_himself' => array('user_can_switch', 'user_can_switch', 'user_can_switch', 200),
  46. );
  47. }
  48. protected function createAuthenticatedClient($username)
  49. {
  50. $client = $this->createClient(array('test_case' => 'StandardFormLogin', 'root_config' => 'switchuser.yml'));
  51. $client->followRedirects(true);
  52. $form = $client->request('GET', '/login')->selectButton('login')->form();
  53. $form['_username'] = $username;
  54. $form['_password'] = 'test';
  55. $client->submit($form);
  56. return $client;
  57. }
  58. }