UrlHelperTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\Core\Plugin;
  4. use Cake\Network\Request;
  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. * @return void
  15. */
  16. public function setUp() {
  17. parent::setUp();
  18. $this->Url = new UrlHelper(new View(null));
  19. $this->Url->request = new Request();
  20. $this->Url->request->webroot = '';
  21. }
  22. /**
  23. * Tests
  24. *
  25. * @return void
  26. */
  27. public function testReset() {
  28. Router::connect('/:controller/:action/*');
  29. $result = $this->Url->reset(['controller' => 'foobar', 'action' => 'test']);
  30. $expected = '/foobar/test';
  31. $this->assertSame($expected, $result);
  32. $this->Url->request->here = '/admin/foobar/test';
  33. $this->Url->request->params['prefix'] = 'admin';
  34. Router::reload();
  35. Router::connect('/:controller/:action/*');
  36. Router::prefix('admin', function ($routes) {
  37. $routes->fallbacks();
  38. });
  39. Router::pushRequest($this->Url->request);
  40. $result = $this->Url->build(['prefix' => 'admin', 'controller' => 'foobar', 'action' => 'test']);
  41. $expected = '/admin/foobar/test';
  42. $this->assertSame($expected, $result);
  43. $result = $this->Url->build(['controller' => 'foobar', 'action' => 'test']);
  44. $expected = '/admin/foobar/test';
  45. $this->assertSame($expected, $result);
  46. $result = $this->Url->reset(['controller' => 'foobar', 'action' => 'test']);
  47. $expected = '/foobar/test';
  48. $this->assertSame($expected, $result);
  49. }
  50. /**
  51. * Tests
  52. *
  53. * @return void
  54. */
  55. public function testResetWithPlugin() {
  56. Router::connect('/:controller/:action/*');
  57. $result = $this->Url->reset(['controller' => 'foobar', 'action' => 'test']);
  58. $expected = '/foobar/test';
  59. $this->assertSame($expected, $result);
  60. $this->Url->request->here = '/admin/foo/bar/baz/test';
  61. $this->Url->request->params['prefix'] = 'admin';
  62. $this->Url->request->params['plugin'] = 'Foo';
  63. Router::reload();
  64. Router::connect('/:controller/:action/*');
  65. Router::plugin('Foo', function ($routes) {
  66. $routes->fallbacks();
  67. });
  68. Router::prefix('admin', function ($routes) {
  69. $routes->plugin('Foo', function ($routes) {
  70. $routes->fallbacks();
  71. });
  72. });
  73. Plugin::routes();
  74. Router::pushRequest($this->Url->request);
  75. $result = $this->Url->build(['controller' => 'bar', 'action' => 'baz', 'x']);
  76. $expected = '/admin/foo/bar/baz/x';
  77. $this->assertSame($expected, $result);
  78. $result = $this->Url->reset(['controller' => 'bar', 'action' => 'baz', 'x']);
  79. $expected = '/bar/baz/x';
  80. $this->assertSame($expected, $result);
  81. }
  82. /**
  83. * Tests
  84. *
  85. * @return void
  86. */
  87. public function testComplete() {
  88. $this->Url->request->query['x'] = 'y';
  89. $result = $this->Url->complete(['action' => 'test']);
  90. $expected = '/test?x=y';
  91. $this->assertSame($expected, $result);
  92. $result = $this->Url->complete(['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. }