HtmlHelperTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\Core\Plugin;
  4. use Cake\Http\ServerRequest;
  5. use Cake\Routing\RouteBuilder;
  6. use Cake\Routing\Router;
  7. use Cake\Routing\Route\DashedRoute;
  8. use Cake\View\View;
  9. use Tools\TestSuite\TestCase;
  10. use Tools\View\Helper\HtmlHelper;
  11. /**
  12. * Datetime Test Case
  13. */
  14. class HtmlHelperTest extends TestCase {
  15. /**
  16. * @var \Tools\View\Helper\HtmlHelper
  17. */
  18. protected $Html;
  19. /**
  20. * @return void
  21. */
  22. public function setUp(): void {
  23. parent::setUp();
  24. Router::reload();
  25. Router::connect('/:controller', ['action' => 'index']);
  26. Router::connect('/:controller/:action/*');
  27. $this->Html = new HtmlHelper(new View(null));
  28. $this->Html->getView()->setRequest(new ServerRequest(['webroot' => '']));
  29. }
  30. /**
  31. * HtmlHelperTest::testImageFromBlob()
  32. *
  33. * @return void
  34. */
  35. public function testImageFromBlob() {
  36. $folder = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS;
  37. $content = file_get_contents($folder . 'hotel.png');
  38. $is = $this->Html->imageFromBlob($content);
  39. $this->assertTrue(!empty($is));
  40. }
  41. /**
  42. * Tests
  43. *
  44. * @return void
  45. */
  46. public function testLinkReset() {
  47. $result = $this->Html->linkReset('Foo', ['controller' => 'FooBar', 'action' => 'test']);
  48. $expected = '<a href="/foo-bar/test">Foo</a>';
  49. $this->assertEquals($expected, $result);
  50. $request = $this->Html->getView()->getRequest();
  51. $request = $request->withAttribute('here', '/admin/foo-bar/test')
  52. ->withParam('prefix', 'Admin');
  53. $this->Html->getView()->setRequest($request);
  54. Router::prefix('Admin', function (RouteBuilder $routes) {
  55. $routes->fallbacks(DashedRoute::class);
  56. });
  57. Router::setRequest($request);
  58. $result = $this->Html->link('Foo', ['prefix' => 'Admin', 'controller' => 'FooBar', 'action' => 'test']);
  59. $expected = '<a href="/admin/foo-bar/test">Foo</a>';
  60. $this->assertEquals($expected, $result);
  61. $result = $this->Html->link('Foo', ['controller' => 'FooBar', 'action' => 'test']);
  62. $expected = '<a href="/admin/foo-bar/test">Foo</a>';
  63. $this->assertEquals($expected, $result);
  64. $result = $this->Html->linkReset('Foo', ['controller' => 'FooBar', 'action' => 'test']);
  65. $expected = '<a href="/foo-bar/test">Foo</a>';
  66. $this->assertEquals($expected, $result);
  67. }
  68. /**
  69. * Tests
  70. *
  71. * @return void
  72. */
  73. public function testLinkComplete() {
  74. $this->Html->getView()->setRequest($this->Html->getView()->getRequest()->withQueryParams(['x' => 'y']));
  75. $result = $this->Html->linkComplete('Foo', ['controller' => 'FooBar', 'action' => 'test']);
  76. $expected = '<a href="/foo-bar/test?x=y">Foo</a>';
  77. $this->assertEquals($expected, $result);
  78. $result = $this->Html->linkComplete('Foo', ['controller' => 'FooBar', 'action' => 'test', '?' => ['a' => 'b']]);
  79. $expected = '<a href="/foo-bar/test?a=b&amp;x=y">Foo</a>';
  80. $this->assertEquals($expected, $result);
  81. }
  82. /**
  83. * TearDown method
  84. *
  85. * @return void
  86. */
  87. public function tearDown(): void {
  88. parent::tearDown();
  89. unset($this->Html);
  90. }
  91. }