LabelWidgetTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Widget;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\Form\NullContext;
  19. use Cake\View\StringTemplate;
  20. use Cake\View\Widget\LabelWidget;
  21. /**
  22. * Label test case.
  23. */
  24. class LabelWidgetTest extends TestCase
  25. {
  26. /**
  27. * @var \Cake\View\Form\NullContext
  28. */
  29. protected $context;
  30. /**
  31. * @var \Cake\View\StringTemplate
  32. */
  33. protected $templates;
  34. /**
  35. * setup method.
  36. */
  37. public function setUp(): void
  38. {
  39. parent::setUp();
  40. $templates = [
  41. 'label' => '<label{{attrs}}>{{text}}</label>',
  42. ];
  43. $this->templates = new StringTemplate($templates);
  44. $this->context = new NullContext([]);
  45. }
  46. /**
  47. * test render
  48. */
  49. public function testRender(): void
  50. {
  51. $label = new LabelWidget($this->templates);
  52. $data = [
  53. 'text' => 'My text',
  54. ];
  55. $result = $label->render($data, $this->context);
  56. $expected = [
  57. 'label' => [],
  58. 'My text',
  59. '/label',
  60. ];
  61. $this->assertHtml($expected, $result);
  62. }
  63. /**
  64. * test render escape
  65. */
  66. public function testRenderEscape(): void
  67. {
  68. $label = new LabelWidget($this->templates);
  69. $data = [
  70. 'text' => 'My > text',
  71. 'for' => 'Some > value',
  72. 'escape' => false,
  73. ];
  74. $result = $label->render($data, $this->context);
  75. $expected = [
  76. 'label' => ['for' => 'Some > value'],
  77. 'My > text',
  78. '/label',
  79. ];
  80. $this->assertHtml($expected, $result);
  81. }
  82. /**
  83. * test render escape
  84. */
  85. public function testRenderAttributes(): void
  86. {
  87. $label = new LabelWidget($this->templates);
  88. $data = [
  89. 'text' => 'My > text',
  90. 'for' => 'some-id',
  91. 'id' => 'some-id',
  92. 'data-foo' => 'value',
  93. ];
  94. $result = $label->render($data, $this->context);
  95. $expected = [
  96. 'label' => ['id' => 'some-id', 'data-foo' => 'value', 'for' => 'some-id'],
  97. 'My &gt; text',
  98. '/label',
  99. ];
  100. $this->assertHtml($expected, $result);
  101. }
  102. /**
  103. * Ensure templateVars option is hooked up.
  104. */
  105. public function testRenderTemplateVars(): void
  106. {
  107. $this->templates->add([
  108. 'label' => '<label custom="{{custom}}" {{attrs}}>{{text}}</label>',
  109. ]);
  110. $label = new LabelWidget($this->templates);
  111. $data = [
  112. 'templateVars' => ['custom' => 'value'],
  113. 'text' => 'Label Text',
  114. ];
  115. $result = $label->render($data, $this->context);
  116. $expected = [
  117. 'label' => ['custom' => 'value'],
  118. 'Label Text',
  119. '/label',
  120. ];
  121. $this->assertHtml($expected, $result);
  122. }
  123. }