HtmlHelperTest.php 2.8 KB

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