HtmlHelperTest.php 2.9 KB

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