HtmlHelperTest.php 2.9 KB

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