UrlComponentTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Tools\Test\TestCase\Controller\Component;
  3. use App\Controller\UrlComponentTestController;
  4. use Cake\Core\Configure;
  5. use Cake\Core\Plugin;
  6. use Cake\Event\Event;
  7. use Cake\Network\Request;
  8. use Cake\Routing\Router;
  9. use Tools\TestSuite\TestCase;
  10. /**
  11. */
  12. class UrlComponentTest extends TestCase {
  13. /**
  14. * @var \Cake\Event\Event
  15. */
  16. public $event;
  17. /**
  18. * @var \App\Controller\UrlComponentTestController
  19. */
  20. public $Controller;
  21. /**
  22. * @return void
  23. */
  24. public function setUp() {
  25. parent::setUp();
  26. $this->event = new Event('Controller.beforeFilter');
  27. $this->Controller = new UrlComponentTestController(new Request());
  28. Configure::write('App.fullBaseUrl', 'http://localhost');
  29. }
  30. /**
  31. * @return void
  32. */
  33. public function tearDown() {
  34. parent::tearDown();
  35. unset($this->Controller);
  36. }
  37. /**
  38. * @return void
  39. */
  40. public function testDefaults() {
  41. $is = $this->Controller->Url->defaults();
  42. $expected = [
  43. 'prefix' => false,
  44. 'plugin' => false
  45. ];
  46. $this->assertSame($expected, $is);
  47. }
  48. /**
  49. * @return void
  50. */
  51. public function testBuild() {
  52. $is = $this->Controller->Url->build(['action' => 'x']);
  53. $expected = '/x';
  54. $this->assertSame($expected, $is);
  55. $is = $this->Controller->Url->build(['action' => 'x'], ['fullBase' => true]);
  56. $expected = 'http://localhost/x';
  57. $this->assertSame($expected, $is);
  58. }
  59. /**
  60. * @return void
  61. */
  62. public function testResetArray() {
  63. $result = $this->Controller->Url->resetArray(['controller' => 'foobar', 'action' => 'test']);
  64. $expected = [
  65. 'controller' => 'foobar',
  66. 'action' => 'test',
  67. 'prefix' => false,
  68. 'plugin' => false,
  69. ];
  70. $this->assertSame($expected, $result);
  71. }
  72. /**
  73. * @return void
  74. */
  75. public function testCompleteArray() {
  76. $this->Controller->Url->request->query['x'] = 'y';
  77. $result = $this->Controller->Url->completeArray(['controller' => 'foobar', 'action' => 'test']);
  78. $expected = [
  79. 'controller' => 'foobar',
  80. 'action' => 'test',
  81. '?' => ['x' => 'y']
  82. ];
  83. $this->assertSame($expected, $result);
  84. }
  85. /**
  86. * @return void
  87. */
  88. public function testBuildReset() {
  89. Router::connect('/:controller/:action/*');
  90. $result = $this->Controller->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  91. $expected = '/foobar/test';
  92. $this->assertSame($expected, $result);
  93. $this->Controller->Url->request->here = '/admin/foo/bar/baz/test';
  94. $this->Controller->Url->request->params['prefix'] = 'admin';
  95. $this->Controller->Url->request->params['plugin'] = 'Foo';
  96. Router::reload();
  97. Router::connect('/:controller/:action/*');
  98. Router::plugin('Foo', function ($routes) {
  99. $routes->fallbacks();
  100. });
  101. Router::prefix('admin', function ($routes) {
  102. $routes->plugin('Foo', function ($routes) {
  103. $routes->fallbacks();
  104. });
  105. });
  106. Plugin::routes();
  107. Router::pushRequest($this->Controller->Url->request);
  108. $result = $this->Controller->Url->build(['controller' => 'bar', 'action' => 'baz', 'x']);
  109. $expected = '/admin/foo/bar/baz/x';
  110. $this->assertSame($expected, $result);
  111. $result = $this->Controller->Url->buildReset(['controller' => 'bar', 'action' => 'baz', 'x']);
  112. $expected = '/bar/baz/x';
  113. $this->assertSame($expected, $result);
  114. }
  115. /**
  116. * @return void
  117. */
  118. public function testBuildComplete() {
  119. $this->Controller->Url->request->query['x'] = 'y';
  120. $result = $this->Controller->Url->buildComplete(['action' => 'test']);
  121. $expected = '/test?x=y';
  122. $this->assertSame($expected, $result);
  123. $result = $this->Controller->Url->buildComplete(['action' => 'test', '?' => ['a' => 'b']]);
  124. $expected = '/test?a=b&x=y';
  125. $this->assertSame($expected, $result);
  126. $expected = '/test?a=b&amp;x=y';
  127. $this->assertSame($expected, h($result));
  128. }
  129. }