HtmlHelperTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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('admin', true)
  53. ->withParam('prefix', 'admin');
  54. $this->Html->getView()->setRequest($request);
  55. Router::reload();
  56. Router::connect('/:controller/:action/*');
  57. Router::prefix('admin', function (RouteBuilder $routes) {
  58. $routes->connect('/:controller/:action/*');
  59. });
  60. $result = $this->Html->link('Foo', ['prefix' => 'admin', 'controller' => 'foobar', 'action' => 'test']);
  61. $expected = '<a href="/admin/foobar/test">Foo</a>';
  62. $this->assertEquals($expected, $result);
  63. $result = $this->Html->link('Foo', ['controller' => 'foobar', 'action' => 'test']);
  64. $expected = '<a href="/admin/foobar/test">Foo</a>';
  65. //debug($result);
  66. //$this->assertEquals($expected, $result);
  67. $result = $this->Html->linkReset('Foo', ['controller' => 'foobar', 'action' => 'test']);
  68. $expected = '<a href="/foobar/test">Foo</a>';
  69. $this->assertEquals($expected, $result);
  70. }
  71. /**
  72. * Tests
  73. *
  74. * @return void
  75. */
  76. public function testLinkComplete() {
  77. $this->Html->getView()->setRequest($this->Html->getView()->getRequest()->withQueryParams(['x' => 'y']));
  78. $result = $this->Html->linkComplete('Foo', ['action' => 'test']);
  79. $expected = '<a href="/test?x=y">Foo</a>';
  80. $this->assertEquals($expected, $result);
  81. $result = $this->Html->linkComplete('Foo', ['action' => 'test', '?' => ['a' => 'b']]);
  82. $expected = '<a href="/test?a=b&amp;x=y">Foo</a>';
  83. $this->assertEquals($expected, $result);
  84. }
  85. /**
  86. * TearDown method
  87. *
  88. * @return void
  89. */
  90. public function tearDown(): void {
  91. parent::tearDown();
  92. unset($this->Html);
  93. }
  94. }