HelperTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * HelperTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 1.2.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Routing\Router;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Helper;
  23. use Cake\View\View;
  24. class TestHelper extends Helper
  25. {
  26. /**
  27. * Settings for this helper.
  28. *
  29. * @var array
  30. */
  31. protected $_defaultConfig = [
  32. 'key1' => 'val1',
  33. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'val2.2']
  34. ];
  35. /**
  36. * Helpers for this helper.
  37. *
  38. * @var array
  39. */
  40. public $helpers = ['Html', 'TestPlugin.OtherHelper'];
  41. }
  42. /**
  43. * HelperTest class
  44. */
  45. class HelperTest extends TestCase
  46. {
  47. /**
  48. * @var \Cake\View\View
  49. */
  50. public $View;
  51. /**
  52. * setUp method
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. parent::setUp();
  59. Router::reload();
  60. $this->View = new View();
  61. }
  62. /**
  63. * tearDown method
  64. *
  65. * @return void
  66. */
  67. public function tearDown()
  68. {
  69. parent::tearDown();
  70. Configure::delete('Asset');
  71. Plugin::unload();
  72. unset($this->View);
  73. }
  74. /**
  75. * Test settings merging
  76. *
  77. * @return void
  78. */
  79. public function testSettingsMerging()
  80. {
  81. $Helper = new TestHelper($this->View, [
  82. 'key3' => 'val3',
  83. 'key2' => ['key2.2' => 'newval']
  84. ]);
  85. $expected = [
  86. 'key1' => 'val1',
  87. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'newval'],
  88. 'key3' => 'val3'
  89. ];
  90. $this->assertEquals($expected, $Helper->getConfig());
  91. }
  92. /**
  93. * test lazy loading helpers is seamless
  94. *
  95. * @return void
  96. */
  97. public function testLazyLoadingHelpers()
  98. {
  99. Plugin::load(['TestPlugin']);
  100. $Helper = new TestHelper($this->View);
  101. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $Helper->OtherHelper);
  102. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $Helper->Html);
  103. }
  104. /**
  105. * test that a helpers Helper is not 'attached' to the collection
  106. *
  107. * @return void
  108. */
  109. public function testThatHelperHelpersAreNotAttached()
  110. {
  111. Plugin::loadAll();
  112. $events = $this->getMockBuilder('\Cake\Event\EventManager')->getMock();
  113. $this->View->setEventManager($events);
  114. $events->expects($this->never())
  115. ->method('attach');
  116. $Helper = new TestHelper($this->View);
  117. $Helper->OtherHelper;
  118. }
  119. /**
  120. * test that the lazy loader doesn't duplicate objects on each access.
  121. *
  122. * @return void
  123. */
  124. public function testLazyLoadingUsesReferences()
  125. {
  126. $Helper = new TestHelper($this->View);
  127. $resultA = $Helper->Html;
  128. $resultB = $Helper->Html;
  129. $resultA->testprop = 1;
  130. $this->assertEquals($resultA->testprop, $resultB->testprop);
  131. }
  132. /**
  133. * test getting view instance
  134. *
  135. * @return void
  136. */
  137. public function testGetView()
  138. {
  139. $Helper = new TestHelper($this->View);
  140. $this->assertSame($this->View, $Helper->getView());
  141. }
  142. /**
  143. * Tests __debugInfo
  144. *
  145. * @return void
  146. */
  147. public function testDebugInfo()
  148. {
  149. $Helper = new TestHelper($this->View);
  150. $expected = [
  151. 'helpers' => [
  152. 'Html',
  153. 'TestPlugin.OtherHelper'
  154. ],
  155. 'theme' => null,
  156. 'plugin' => null,
  157. 'fieldset' => [],
  158. 'tags' => [],
  159. 'implementedEvents' => [
  160. ],
  161. '_config' => [
  162. 'key1' => 'val1',
  163. 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'val2.2']
  164. ]
  165. ];
  166. $result = $Helper->__debugInfo();
  167. $this->assertEquals($expected, $result);
  168. }
  169. /**
  170. * Test addClass() with 'class' => array
  171. *
  172. * @return void
  173. */
  174. public function testAddClassArray()
  175. {
  176. $helper = new TestHelper($this->View);
  177. $input = ['class' => ['element1', 'element2']];
  178. $expected = ['class' => [
  179. 'element1',
  180. 'element2',
  181. 'element3'
  182. ]];
  183. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  184. }
  185. /**
  186. * Test addClass() with 'class' => string
  187. *
  188. * @return void
  189. */
  190. public function testAddClassString()
  191. {
  192. $helper = new TestHelper($this->View);
  193. $input = ['class' => 'element1 element2'];
  194. $expected = ['class' => 'element1 element2 element3'];
  195. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  196. }
  197. /**
  198. * Test addClass() with no class element
  199. *
  200. * @return void
  201. */
  202. public function testAddClassEmpty()
  203. {
  204. $helper = new TestHelper($this->View);
  205. $input = [];
  206. $expected = ['class' => 'element3'];
  207. $this->assertEquals($expected, $helper->addClass($input, 'element3'));
  208. }
  209. /**
  210. * Test addClass() with adding null class
  211. */
  212. public function testAddClassNull()
  213. {
  214. $helper = new TestHelper($this->View);
  215. $input = [];
  216. $expected = ['class' => ''];
  217. $this->assertEquals($expected, $helper->addClass($input, null));
  218. }
  219. }