PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/src/Unit/RedirectRequestSubscriberTest.php

https://gitlab.com/Drulenium-bot/redirect
PHP | 280 lines | 157 code | 42 blank | 81 comment | 0 complexity | d6734eef5b173b419d48b087a1199010 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\Tests\redirect\Unit\RedirectRequestSubscriberTest.
  5. */
  6. namespace Drupal\Tests\redirect\Unit;
  7. use Drupal\Core\Language\Language;
  8. use Drupal\redirect\EventSubscriber\RedirectRequestSubscriber;
  9. use Drupal\Tests\UnitTestCase;
  10. use PHPUnit_Framework_MockObject_MockObject;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  15. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  16. /**
  17. * Tests the redirect logic.
  18. *
  19. * @group redirect
  20. *
  21. * @coversDefaultClass Drupal\redirect\EventSubscriber\RedirectRequestSubscriber
  22. */
  23. class RedirectRequestSubscriberTest extends UnitTestCase {
  24. /**
  25. * @covers ::onKernelRequestCheckRedirect
  26. */
  27. public function testRedirectLogicWithQueryRetaining() {
  28. // The request query.
  29. $request_query = array('key' => 'val');
  30. // The query defined by the redirect entity.
  31. $redirect_query = array('dummy' => 'value');
  32. // The expected final query. This query must contain values defined
  33. // by the redirect entity and values from the accessed url.
  34. $final_query = $redirect_query + $request_query;
  35. $url = $this->getMockBuilder('Drupal\Core\Url')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $url->expects($this->once())
  39. ->method('setAbsolute')
  40. ->with(TRUE)
  41. ->willReturn($url);
  42. $url->expects($this->once())
  43. ->method('getOption')
  44. ->with('query')
  45. ->willReturn($redirect_query);
  46. $url->expects($this->once())
  47. ->method('setOption')
  48. ->with('query', $final_query);
  49. $url->expects($this->once())
  50. ->method('toString')
  51. ->willReturn('/test-path');
  52. $redirect = $this->getRedirectStub($url);
  53. $event = $this->callOnKernelRequestCheckRedirect($redirect, $request_query, TRUE);
  54. $this->assertTrue($event->getResponse() instanceof RedirectResponse);
  55. $response = $event->getResponse();
  56. $this->assertEquals('/test-path', $response->getTargetUrl());
  57. $this->assertEquals(301, $response->getStatusCode());
  58. $this->assertEquals(1, $response->headers->get('X-Redirect-ID'));
  59. }
  60. /**
  61. * @covers ::onKernelRequestCheckRedirect
  62. */
  63. public function testRedirectLogicWithoutQueryRetaining() {
  64. // The request query.
  65. $request_query = array('key' => 'val');
  66. $url = $this->getMockBuilder('Drupal\Core\Url')
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $url->expects($this->once())
  70. ->method('setAbsolute')
  71. ->with(TRUE)
  72. ->willReturn($url);
  73. // No query retaining, so getOption should not be called.
  74. $url->expects($this->never())
  75. ->method('getOption');
  76. $url->expects($this->never())
  77. ->method('setOption');
  78. $url->expects($this->once())
  79. ->method('toString')
  80. ->willReturn('/test-path');
  81. $redirect = $this->getRedirectStub($url);
  82. $event = $this->callOnKernelRequestCheckRedirect($redirect, $request_query, FALSE);
  83. $this->assertTrue($event->getResponse() instanceof RedirectResponse);
  84. $response = $event->getResponse();
  85. $this->assertEquals('/test-path', $response->getTargetUrl());
  86. $this->assertEquals(301, $response->getStatusCode());
  87. $this->assertEquals(1, $response->headers->get('X-Redirect-ID'));
  88. }
  89. /**
  90. * Instantiates the subscriber and runs onKernelRequestCheckRedirect()
  91. *
  92. * @param $redirect
  93. * The redirect entity.
  94. * @param array $request_query
  95. * The query that is supposed to come via request.
  96. * @param bool $retain_query
  97. * Flag if to retain the query through the redirect.
  98. *
  99. * @return \Symfony\Component\HttpKernel\Event\GetResponseEvent
  100. * THe response event.
  101. */
  102. protected function callOnKernelRequestCheckRedirect($redirect, $request_query, $retain_query) {
  103. $event = $this->getGetResponseEventStub('non-existing', http_build_query($request_query));
  104. $request = $event->getRequest();
  105. $checker = $this->getMockBuilder('Drupal\redirect\RedirectChecker')
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $checker->expects($this->any())
  109. ->method('canRedirect')
  110. ->will($this->returnValue(TRUE));
  111. $checker->expects($this->any())
  112. ->method('isLoop')
  113. ->will($this->returnValue(FALSE));
  114. $context = $this->getMock('Symfony\Component\Routing\RequestContext');
  115. $inbound_path_processor = $this->getMockBuilder('Drupal\Core\PathProcessor\InboundPathProcessorInterface')
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $inbound_path_processor->expects($this->any())
  119. ->method('processInbound')
  120. ->with($request->getPathInfo(), $request)
  121. ->will($this->returnValue($request->getPathInfo()));
  122. $alias_manager = $this->getMockBuilder('Drupal\Core\Path\AliasManager')
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandlerInterface')
  126. ->getMock();
  127. $entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManagerInterface')
  128. ->getMock();
  129. $subscriber = new RedirectRequestSubscriber(
  130. $this->getRedirectRepositoryStub('findMatchingRedirect', $redirect),
  131. $this->getLanguageManagerStub(),
  132. $this->getConfigFactoryStub(array('redirect.settings' => array('passthrough_querystring' => $retain_query))),
  133. $alias_manager,
  134. $module_handler,
  135. $entity_manager,
  136. $checker,
  137. $context,
  138. $inbound_path_processor
  139. );
  140. // Run the main redirect method.
  141. $subscriber->onKernelRequestCheckRedirect($event);
  142. return $event;
  143. }
  144. /**
  145. * Gets the redirect repository mock object.
  146. *
  147. * @param $method
  148. * Method to mock - either load() or findMatchingRedirect().
  149. * @param $redirect
  150. * The redirect object to be returned.
  151. *
  152. * @return PHPUnit_Framework_MockObject_MockObject
  153. * The redirect repository.
  154. */
  155. protected function getRedirectRepositoryStub($method, $redirect) {
  156. $repository = $this->getMockBuilder('Drupal\redirect\RedirectRepository')
  157. ->disableOriginalConstructor()
  158. ->getMock();
  159. $repository->expects($this->any())
  160. ->method($method)
  161. ->will($this->returnValue($redirect));
  162. return $repository;
  163. }
  164. /**
  165. * Gets the redirect mock object.
  166. *
  167. * @param $url
  168. * Url to be returned from getRedirectUrl
  169. * @param int $status_code
  170. * The redirect status code.
  171. *
  172. * @return PHPUnit_Framework_MockObject_MockObject
  173. * The mocked redirect object.
  174. */
  175. protected function getRedirectStub($url, $status_code = 301) {
  176. $redirect = $this->getMockBuilder('Drupal\redirect\Entity\Redirect')
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $redirect->expects($this->once())
  180. ->method('getRedirectUrl')
  181. ->will($this->returnValue($url));
  182. $redirect->expects($this->any())
  183. ->method('getStatusCode')
  184. ->will($this->returnValue($status_code));
  185. $redirect->expects($this->any())
  186. ->method('id')
  187. ->willReturn(1);
  188. $redirect->expects($this->once())
  189. ->method('getCacheTags')
  190. ->willReturn(['redirect:1']);
  191. return $redirect;
  192. }
  193. /**
  194. * Gets post response event.
  195. *
  196. * @param array $headers
  197. * Headers to be set into the response.
  198. *
  199. * @return \Symfony\Component\HttpKernel\Event\PostResponseEvent
  200. * The post response event object.
  201. */
  202. protected function getPostResponseEvent($headers = array()) {
  203. $http_kernel = $this->getMockBuilder('\Symfony\Component\HttpKernel\HttpKernelInterface')
  204. ->getMock();
  205. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
  206. ->disableOriginalConstructor()
  207. ->getMock();
  208. $response = new Response('', 301, $headers);
  209. return new PostResponseEvent($http_kernel, $request, $response);
  210. }
  211. /**
  212. * Gets response event object.
  213. *
  214. * @param $path_info
  215. * @param $query_string
  216. *
  217. * @return GetResponseEvent
  218. */
  219. protected function getGetResponseEventStub($path_info, $query_string) {
  220. $request = Request::create($path_info . '?' . $query_string, 'GET', [], [], [], ['SCRIPT_NAME' => 'index.php']);
  221. $http_kernel = $this->getMockBuilder('\Symfony\Component\HttpKernel\HttpKernelInterface')
  222. ->getMock();
  223. return new GetResponseEvent($http_kernel, $request, 'test');
  224. }
  225. /**
  226. * Gets the language manager mock object.
  227. *
  228. * @return \Drupal\language\ConfigurableLanguageManagerInterface|PHPUnit_Framework_MockObject_MockObject
  229. */
  230. protected function getLanguageManagerStub() {
  231. $language_manager = $this->getMockBuilder('Drupal\language\ConfigurableLanguageManagerInterface')
  232. ->getMock();
  233. $language_manager->expects($this->any())
  234. ->method('getCurrentLanguage')
  235. ->will($this->returnValue(new Language(array('id' => 'en'))));
  236. return $language_manager;
  237. }
  238. }