UrlHelperTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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() {
  21. parent::setUp();
  22. $this->Url = new UrlHelper(new View(null));
  23. $this->Url->getView()->setRequest(new ServerRequest(['webroot' => '']));
  24. }
  25. /**
  26. * @return void
  27. */
  28. public function testBuildReset() {
  29. Router::connect('/:controller/:action/*');
  30. $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  31. $expected = '/foobar/test';
  32. $this->assertSame($expected, $result);
  33. $request = $this->Url->getView()->getRequest();
  34. $request = $request->withAttribute('here', '/admin/foobar/test')
  35. ->withParam('prefix', 'admin');
  36. $this->Url->getView()->setRequest($request);
  37. Router::reload();
  38. Router::connect('/:controller/:action/*');
  39. Router::prefix('admin', function (RouteBuilder $routes) {
  40. $routes->fallbacks();
  41. });
  42. Router::pushRequest($this->Url->getView()->getRequest());
  43. $result = $this->Url->build(['prefix' => 'admin', 'controller' => 'foobar', 'action' => 'test']);
  44. $expected = '/admin/foobar/test';
  45. $this->assertSame($expected, $result);
  46. $result = $this->Url->build(['controller' => 'foobar', 'action' => 'test']);
  47. $expected = '/admin/foobar/test';
  48. $this->assertSame($expected, $result);
  49. $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  50. $expected = '/foobar/test';
  51. $this->assertSame($expected, $result);
  52. }
  53. /**
  54. * @return void
  55. */
  56. public function testBuildResetWithPlugin() {
  57. Router::connect('/:controller/:action/*');
  58. $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  59. $expected = '/foobar/test';
  60. $this->assertSame($expected, $result);
  61. $request = $this->Url->getView()->getRequest();
  62. $request = $request->withAttribute('here', '/admin/foo/bar/baz/test')
  63. ->withParam('prefix', 'admin')
  64. ->withParam('plugin', 'Foo');
  65. $this->Url->getView()->setRequest($request);
  66. Router::reload();
  67. Router::connect('/:controller/:action/*');
  68. Router::plugin('Foo', function (RouteBuilder $routes) {
  69. $routes->fallbacks();
  70. });
  71. Router::prefix('admin', function (RouteBuilder $routes) {
  72. $routes->plugin('Foo', function (RouteBuilder $routes) {
  73. $routes->fallbacks();
  74. });
  75. });
  76. Router::pushRequest($this->Url->getView()->getRequest());
  77. $result = $this->Url->build(['controller' => 'bar', 'action' => 'baz', 'x']);
  78. $expected = '/admin/foo/bar/baz/x';
  79. $this->assertSame($expected, $result);
  80. $result = $this->Url->buildReset(['controller' => 'bar', 'action' => 'baz', 'x']);
  81. $expected = '/bar/baz/x';
  82. $this->assertSame($expected, $result);
  83. }
  84. /**
  85. * @return void
  86. */
  87. public function testBuildComplete() {
  88. $this->Url->getView()->setRequest($this->Url->getView()->getRequest()->withQueryParams(['x' => 'y']));
  89. $result = $this->Url->buildComplete(['action' => 'test']);
  90. $expected = '/test?x=y';
  91. $this->assertSame($expected, $result);
  92. $result = $this->Url->buildComplete(['action' => 'test', '?' => ['a' => 'b']]);
  93. $expected = '/test?a=b&amp;x=y';
  94. $this->assertSame($expected, $result);
  95. }
  96. /**
  97. * @return void
  98. */
  99. public function tearDown() {
  100. parent::tearDown();
  101. unset($this->Url);
  102. }
  103. }