PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php

https://gitlab.com/Marwamimo/Crowdrise_Web
PHP | 266 lines | 196 code | 46 blank | 24 comment | 0 complexity | 4cfd79e1e1b3044ff966f16ca1f78e9e MD5 | raw file
  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\Component\Security\Http\Tests;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  13. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  14. use Symfony\Component\Security\Core\SecurityContextInterface;
  15. use Symfony\Component\Security\Http\HttpUtils;
  16. class HttpUtilsTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testCreateRedirectResponseWithPath()
  19. {
  20. $utils = new HttpUtils($this->getUrlGenerator());
  21. $response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
  22. $this->assertTrue($response->isRedirect('http://localhost/foobar'));
  23. $this->assertEquals(302, $response->getStatusCode());
  24. }
  25. public function testCreateRedirectResponseWithAbsoluteUrl()
  26. {
  27. $utils = new HttpUtils($this->getUrlGenerator());
  28. $response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/');
  29. $this->assertTrue($response->isRedirect('http://symfony.com/'));
  30. }
  31. public function testCreateRedirectResponseWithRouteName()
  32. {
  33. $utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
  34. $urlGenerator
  35. ->expects($this->any())
  36. ->method('generate')
  37. ->with('foobar', array(), true)
  38. ->will($this->returnValue('http://localhost/foo/bar'))
  39. ;
  40. $urlGenerator
  41. ->expects($this->any())
  42. ->method('getContext')
  43. ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
  44. ;
  45. $response = $utils->createRedirectResponse($this->getRequest(), 'foobar');
  46. $this->assertTrue($response->isRedirect('http://localhost/foo/bar'));
  47. }
  48. public function testCreateRequestWithPath()
  49. {
  50. $request = $this->getRequest();
  51. $request->server->set('Foo', 'bar');
  52. $utils = new HttpUtils($this->getUrlGenerator());
  53. $subRequest = $utils->createRequest($request, '/foobar');
  54. $this->assertEquals('GET', $subRequest->getMethod());
  55. $this->assertEquals('/foobar', $subRequest->getPathInfo());
  56. $this->assertEquals('bar', $subRequest->server->get('Foo'));
  57. }
  58. public function testCreateRequestWithRouteName()
  59. {
  60. $utils = new HttpUtils($urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
  61. $urlGenerator
  62. ->expects($this->once())
  63. ->method('generate')
  64. ->will($this->returnValue('/foo/bar'))
  65. ;
  66. $urlGenerator
  67. ->expects($this->any())
  68. ->method('getContext')
  69. ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
  70. ;
  71. $subRequest = $utils->createRequest($this->getRequest(), 'foobar');
  72. $this->assertEquals('/foo/bar', $subRequest->getPathInfo());
  73. }
  74. public function testCreateRequestWithAbsoluteUrl()
  75. {
  76. $utils = new HttpUtils($this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'));
  77. $subRequest = $utils->createRequest($this->getRequest(), 'http://symfony.com/');
  78. $this->assertEquals('/', $subRequest->getPathInfo());
  79. }
  80. public function testCreateRequestPassesSessionToTheNewRequest()
  81. {
  82. $request = $this->getRequest();
  83. $request->setSession($session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
  84. $utils = new HttpUtils($this->getUrlGenerator());
  85. $subRequest = $utils->createRequest($request, '/foobar');
  86. $this->assertSame($session, $subRequest->getSession());
  87. }
  88. /**
  89. * @dataProvider provideSecurityContextAttributes
  90. */
  91. public function testCreateRequestPassesSecurityContextAttributesToTheNewRequest($attribute)
  92. {
  93. $request = $this->getRequest();
  94. $request->attributes->set($attribute, 'foo');
  95. $utils = new HttpUtils($this->getUrlGenerator());
  96. $subRequest = $utils->createRequest($request, '/foobar');
  97. $this->assertSame('foo', $subRequest->attributes->get($attribute));
  98. }
  99. public function provideSecurityContextAttributes()
  100. {
  101. return array(
  102. array(SecurityContextInterface::AUTHENTICATION_ERROR),
  103. array(SecurityContextInterface::ACCESS_DENIED_ERROR),
  104. array(SecurityContextInterface::LAST_USERNAME),
  105. );
  106. }
  107. public function testCheckRequestPath()
  108. {
  109. $utils = new HttpUtils($this->getUrlGenerator());
  110. $this->assertTrue($utils->checkRequestPath($this->getRequest(), '/'));
  111. $this->assertFalse($utils->checkRequestPath($this->getRequest(), '/foo'));
  112. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo%20bar'), '/foo bar'));
  113. // Plus must not decoded to space
  114. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo+bar'), '/foo+bar'));
  115. // Checking unicode
  116. $this->assertTrue($utils->checkRequestPath($this->getRequest(urlencode('/вход')), '/вход'));
  117. }
  118. public function testCheckRequestPathWithUrlMatcherAndResourceNotFound()
  119. {
  120. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
  121. $urlMatcher
  122. ->expects($this->any())
  123. ->method('match')
  124. ->with('/')
  125. ->will($this->throwException(new ResourceNotFoundException()))
  126. ;
  127. $utils = new HttpUtils(null, $urlMatcher);
  128. $this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
  129. }
  130. public function testCheckRequestPathWithUrlMatcherAndMethodNotAllowed()
  131. {
  132. $request = $this->getRequest();
  133. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
  134. $urlMatcher
  135. ->expects($this->any())
  136. ->method('matchRequest')
  137. ->with($request)
  138. ->will($this->throwException(new MethodNotAllowedException(array())))
  139. ;
  140. $utils = new HttpUtils(null, $urlMatcher);
  141. $this->assertFalse($utils->checkRequestPath($request, 'foobar'));
  142. }
  143. public function testCheckRequestPathWithUrlMatcherAndResourceFoundByUrl()
  144. {
  145. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
  146. $urlMatcher
  147. ->expects($this->any())
  148. ->method('match')
  149. ->with('/foo/bar')
  150. ->will($this->returnValue(array('_route' => 'foobar')))
  151. ;
  152. $utils = new HttpUtils(null, $urlMatcher);
  153. $this->assertTrue($utils->checkRequestPath($this->getRequest('/foo/bar'), 'foobar'));
  154. }
  155. public function testCheckRequestPathWithUrlMatcherAndResourceFoundByRequest()
  156. {
  157. $request = $this->getRequest();
  158. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
  159. $urlMatcher
  160. ->expects($this->any())
  161. ->method('matchRequest')
  162. ->with($request)
  163. ->will($this->returnValue(array('_route' => 'foobar')))
  164. ;
  165. $utils = new HttpUtils(null, $urlMatcher);
  166. $this->assertTrue($utils->checkRequestPath($request, 'foobar'));
  167. }
  168. /**
  169. * @expectedException \RuntimeException
  170. */
  171. public function testCheckRequestPathWithUrlMatcherLoadingException()
  172. {
  173. $urlMatcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
  174. $urlMatcher
  175. ->expects($this->any())
  176. ->method('match')
  177. ->will($this->throwException(new \RuntimeException()))
  178. ;
  179. $utils = new HttpUtils(null, $urlMatcher);
  180. $utils->checkRequestPath($this->getRequest(), 'foobar');
  181. }
  182. /**
  183. * @expectedException \InvalidArgumentException
  184. * @expectedExceptionMessage Matcher must either implement UrlMatcherInterface or RequestMatcherInterface
  185. */
  186. public function testUrlMatcher()
  187. {
  188. new HttpUtils($this->getUrlGenerator(), new \stdClass());
  189. }
  190. public function testGenerateUriRemovesQueryString()
  191. {
  192. $utils = new HttpUtils($this->getUrlGenerator('/foo/bar'));
  193. $this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
  194. $utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value'));
  195. $this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
  196. }
  197. /**
  198. * @expectedException \LogicException
  199. * @expectedExceptionMessage You must provide a UrlGeneratorInterface instance to be able to use routes.
  200. */
  201. public function testUrlGeneratorIsRequiredToGenerateUrl()
  202. {
  203. $utils = new HttpUtils();
  204. $utils->generateUri(new Request(), 'route_name');
  205. }
  206. private function getUrlGenerator($generatedUrl = '/foo/bar')
  207. {
  208. $urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
  209. $urlGenerator
  210. ->expects($this->any())
  211. ->method('generate')
  212. ->will($this->returnValue($generatedUrl))
  213. ;
  214. return $urlGenerator;
  215. }
  216. private function getRequest($path = '/')
  217. {
  218. return Request::create($path, 'get');
  219. }
  220. }