UrlHelperTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\Http\ServerRequest;
  4. use Cake\Routing\RouteBuilder;
  5. use Cake\Routing\Router;
  6. use Cake\View\View;
  7. use Tools\TestSuite\TestCase;
  8. use Tools\View\Helper\UrlHelper;
  9. /**
  10. * Datetime Test Case
  11. */
  12. class UrlHelperTest extends TestCase {
  13. /**
  14. * @var \Tools\View\Helper\UrlHelper
  15. */
  16. protected $Url;
  17. /**
  18. * @return void
  19. */
  20. public function setUp(): void {
  21. parent::setUp();
  22. Router::reload();
  23. Router::connect('/:controller', ['action' => 'index']);
  24. Router::connect('/:controller/:action/*');
  25. $this->Url = new UrlHelper(new View(null));
  26. $this->Url->getView()->setRequest(new ServerRequest(['webroot' => '']));
  27. }
  28. /**
  29. * @return void
  30. */
  31. public function testBuildReset() {
  32. Router::connect('/:controller/:action/*');
  33. $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  34. $expected = '/foobar/test';
  35. $this->assertSame($expected, $result);
  36. $request = $this->Url->getView()->getRequest();
  37. $request = $request->withAttribute('here', '/admin/foobar/test')
  38. ->withParam('prefix', 'admin');
  39. $this->Url->getView()->setRequest($request);
  40. Router::reload();
  41. Router::connect('/:controller/:action/*');
  42. Router::prefix('admin', function (RouteBuilder $routes) {
  43. $routes->fallbacks();
  44. });
  45. Router::pushRequest($this->Url->getView()->getRequest());
  46. $result = $this->Url->build(['prefix' => 'admin', 'controller' => 'foobar', 'action' => 'test']);
  47. $expected = '/admin/foobar/test';
  48. $this->assertSame($expected, $result);
  49. $result = $this->Url->build(['controller' => 'foobar', 'action' => 'test']);
  50. $expected = '/admin/foobar/test';
  51. $this->assertSame($expected, $result);
  52. $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  53. $expected = '/foobar/test';
  54. $this->assertSame($expected, $result);
  55. }
  56. /**
  57. * @return void
  58. */
  59. public function testBuildResetWithPlugin() {
  60. Router::connect('/:controller/:action/*');
  61. $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  62. $expected = '/foobar/test';
  63. $this->assertSame($expected, $result);
  64. $request = $this->Url->getView()->getRequest();
  65. $request = $request->withAttribute('here', '/admin/foo/bar/baz/test')
  66. ->withParam('prefix', 'admin')
  67. ->withParam('plugin', 'Foo');
  68. $this->Url->getView()->setRequest($request);
  69. Router::reload();
  70. Router::connect('/:controller/:action/*');
  71. Router::plugin('Foo', function (RouteBuilder $routes) {
  72. $routes->fallbacks();
  73. });
  74. Router::prefix('admin', function (RouteBuilder $routes) {
  75. $routes->plugin('Foo', function (RouteBuilder $routes) {
  76. $routes->fallbacks();
  77. });
  78. });
  79. Router::pushRequest($this->Url->getView()->getRequest());
  80. $result = $this->Url->build(['controller' => 'bar', 'action' => 'baz', 'x']);
  81. $expected = '/admin/foo/bar/baz/x';
  82. $this->assertSame($expected, $result);
  83. $result = $this->Url->buildReset(['controller' => 'bar', 'action' => 'baz', 'x']);
  84. $expected = '/bar/baz/x';
  85. $this->assertSame($expected, $result);
  86. }
  87. /**
  88. * @return void
  89. */
  90. public function testBuildComplete() {
  91. $this->Url->getView()->setRequest($this->Url->getView()->getRequest()->withQueryParams(['x' => 'y']));
  92. $result = $this->Url->buildComplete(['action' => 'test']);
  93. $expected = '/test?x=y';
  94. $this->assertSame($expected, $result);
  95. $result = $this->Url->buildComplete(['action' => 'test', '?' => ['a' => 'b']]);
  96. $expected = '/test?a=b&amp;x=y';
  97. $this->assertSame($expected, $result);
  98. }
  99. /**
  100. * @return void
  101. */
  102. public function tearDown(): void {
  103. parent::tearDown();
  104. unset($this->Url);
  105. }
  106. }