HelperRegistryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Core\Plugin;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\Helper;
  19. use Cake\View\HelperRegistry;
  20. use Cake\View\View;
  21. /**
  22. * Extended HtmlHelper
  23. */
  24. class HtmlAliasHelper extends Helper
  25. {
  26. public function afterRender($viewFile)
  27. {
  28. }
  29. }
  30. /**
  31. * HelperRegistryTest
  32. */
  33. class HelperRegistryTest extends TestCase
  34. {
  35. /**
  36. * @var \Cake\View\HelperRegistry
  37. */
  38. public $Helpers;
  39. /**
  40. * @var \Cake\Event\EventManager
  41. */
  42. public $Events;
  43. /**
  44. * setUp
  45. *
  46. * @return void
  47. */
  48. public function setUp()
  49. {
  50. parent::setUp();
  51. $this->View = new View();
  52. $this->Events = $this->View->getEventManager();
  53. $this->Helpers = new HelperRegistry($this->View);
  54. }
  55. /**
  56. * tearDown
  57. *
  58. * @return void
  59. */
  60. public function tearDown()
  61. {
  62. $this->clearPlugins();
  63. unset($this->Helpers, $this->View);
  64. parent::tearDown();
  65. }
  66. /**
  67. * test loading helpers.
  68. *
  69. * @return void
  70. */
  71. public function testLoad()
  72. {
  73. $result = $this->Helpers->load('Html');
  74. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
  75. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $this->Helpers->Html);
  76. $result = $this->Helpers->loaded();
  77. $this->assertEquals(['Html'], $result, 'loaded() results are wrong.');
  78. }
  79. /**
  80. * test lazy loading of helpers
  81. *
  82. * @return void
  83. */
  84. public function testLazyLoad()
  85. {
  86. $result = $this->Helpers->Html;
  87. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
  88. $result = $this->Helpers->Form;
  89. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $result);
  90. $this->View->setPlugin('TestPlugin');
  91. $this->loadPlugins(['TestPlugin']);
  92. $result = $this->Helpers->OtherHelper;
  93. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result);
  94. }
  95. /**
  96. * test lazy loading of helpers
  97. *
  98. * @return void
  99. */
  100. public function testLazyLoadException()
  101. {
  102. $this->expectException(\Cake\View\Exception\MissingHelperException::class);
  103. $this->Helpers->NotAHelper;
  104. }
  105. /**
  106. * Test that loading helpers subscribes to events.
  107. *
  108. * @return void
  109. */
  110. public function testLoadSubscribeEvents()
  111. {
  112. $this->Helpers->load('Html', ['className' => __NAMESPACE__ . '\HtmlAliasHelper']);
  113. $result = $this->Events->listeners('View.afterRender');
  114. $this->assertCount(1, $result);
  115. }
  116. /**
  117. * Tests loading as an alias
  118. *
  119. * @return void
  120. */
  121. public function testLoadWithAlias()
  122. {
  123. $result = $this->Helpers->load('Html', ['className' => __NAMESPACE__ . '\HtmlAliasHelper']);
  124. $this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $result);
  125. $this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $this->Helpers->Html);
  126. $result = $this->Helpers->loaded();
  127. $this->assertEquals(['Html'], $result, 'loaded() results are wrong.');
  128. $result = $this->Helpers->load('Html');
  129. $this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $result);
  130. }
  131. /**
  132. * Test loading helpers with aliases and plugins.
  133. *
  134. * @return void
  135. */
  136. public function testLoadWithAliasAndPlugin()
  137. {
  138. $this->loadPlugins(['TestPlugin']);
  139. $result = $this->Helpers->load('SomeOther', ['className' => 'TestPlugin.OtherHelper']);
  140. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result);
  141. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $this->Helpers->SomeOther);
  142. $result = $this->Helpers->loaded();
  143. $this->assertEquals(['SomeOther'], $result, 'loaded() results are wrong.');
  144. }
  145. /**
  146. * test that the enabled setting disables the helper.
  147. *
  148. * @return void
  149. */
  150. public function testLoadWithEnabledFalse()
  151. {
  152. $result = $this->Helpers->load('Html', ['enabled' => false]);
  153. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
  154. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $this->Helpers->Html);
  155. $this->assertEmpty($this->Events->listeners('View.beforeRender'));
  156. }
  157. /**
  158. * test missinghelper exception
  159. *
  160. * @return void
  161. */
  162. public function testLoadMissingHelper()
  163. {
  164. $this->expectException(\Cake\View\Exception\MissingHelperException::class);
  165. $this->Helpers->load('ThisHelperShouldAlwaysBeMissing');
  166. }
  167. /**
  168. * test loading a plugin helper.
  169. *
  170. * @return void
  171. */
  172. public function testLoadPluginHelper()
  173. {
  174. $this->loadPlugins(['TestPlugin']);
  175. $result = $this->Helpers->load('TestPlugin.OtherHelper');
  176. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result, 'Helper class is wrong.');
  177. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $this->Helpers->OtherHelper, 'Class is wrong');
  178. }
  179. /**
  180. * test loading helpers with dotted aliases
  181. *
  182. * @return void
  183. */
  184. public function testLoadPluginHelperDottedAlias()
  185. {
  186. $this->loadPlugins(['TestPlugin']);
  187. $result = $this->Helpers->load('thing.helper', [
  188. 'className' => 'TestPlugin.OtherHelper',
  189. ]);
  190. $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result, 'Helper class is wrong.');
  191. $this->assertInstanceOf(
  192. 'TestPlugin\View\Helper\OtherHelperHelper',
  193. $this->Helpers->get('thing.helper'),
  194. 'Class is wrong'
  195. );
  196. $this->assertTrue($this->Helpers->has('thing.helper'));
  197. $this->assertFalse($this->Helpers->has('thing'));
  198. $this->assertFalse($this->Helpers->has('helper'));
  199. $this->Helpers->unload('thing.helper');
  200. $this->assertFalse($this->Helpers->has('thing.helper'), 'Should be gone now.');
  201. }
  202. /**
  203. * Test reset.
  204. *
  205. * @return void
  206. */
  207. public function testReset()
  208. {
  209. static::setAppNamespace();
  210. $instance = $this->Helpers->load('EventListenerTest');
  211. $this->assertSame(
  212. $instance,
  213. $this->Helpers->EventListenerTest,
  214. 'Instance in registry should be the same as previously loaded'
  215. );
  216. $this->assertCount(1, $this->Events->listeners('View.beforeRender'));
  217. $this->Helpers->reset();
  218. $this->assertCount(0, $this->Events->listeners('View.beforeRender'));
  219. $this->assertNotSame($instance, $this->Helpers->load('EventListenerTest'));
  220. }
  221. /**
  222. * Test unloading.
  223. *
  224. * @return void
  225. */
  226. public function testUnload()
  227. {
  228. static::setAppNamespace();
  229. $instance = $this->Helpers->load('EventListenerTest');
  230. $this->assertSame(
  231. $instance,
  232. $this->Helpers->EventListenerTest,
  233. 'Instance in registry should be the same as previously loaded'
  234. );
  235. $this->assertCount(1, $this->Events->listeners('View.beforeRender'));
  236. $this->assertSame($this->Helpers, $this->Helpers->unload('EventListenerTest'));
  237. $this->assertCount(0, $this->Events->listeners('View.beforeRender'));
  238. }
  239. /**
  240. * Test that unloading a none existing helper triggers an error.
  241. *
  242. * @return void
  243. */
  244. public function testUnloadUnknown()
  245. {
  246. $this->expectException(\Cake\View\Exception\MissingHelperException::class);
  247. $this->expectExceptionMessage('Helper class FooHelper could not be found.');
  248. $this->Helpers->unload('Foo');
  249. }
  250. /**
  251. * Loading a helper with no config should "just work"
  252. *
  253. * The addToAssertionCount call is to record that no exception was thrown
  254. *
  255. * @return void
  256. */
  257. public function testLoadMultipleTimesNoConfig()
  258. {
  259. $this->Helpers->load('Html');
  260. $this->Helpers->load('Html');
  261. $this->addToAssertionCount(1);
  262. }
  263. /**
  264. * Loading a helper with bespoke config, where the subsequent load specifies no
  265. * config should "just work"
  266. *
  267. * The addToAssertionCount call is to record that no exception was thrown
  268. *
  269. * @return void
  270. */
  271. public function testLoadMultipleTimesAlreadyConfigured()
  272. {
  273. $this->Helpers->load('Html', ['same' => 'stuff']);
  274. $this->Helpers->load('Html');
  275. $this->addToAssertionCount(1);
  276. }
  277. /**
  278. * Loading a helper overriding defaults to default value
  279. * should "just work"
  280. *
  281. * @return void
  282. */
  283. public function testLoadMultipleTimesDefaultConfigValuesWorks()
  284. {
  285. $this->Helpers->load('Number', ['engine' => 'Cake\I18n\Number']);
  286. $this->Helpers->load('Number');
  287. $this->addToAssertionCount(1);
  288. }
  289. /**
  290. * Loading a helper with different config, should throw an exception
  291. *
  292. * @return void
  293. */
  294. public function testLoadMultipleTimesDifferentConfigured()
  295. {
  296. $this->expectException(\RuntimeException::class);
  297. $this->expectExceptionMessage('The "Html" alias has already been loaded');
  298. $this->Helpers->load('Html');
  299. $this->Helpers->load('Html', ['same' => 'stuff']);
  300. }
  301. /**
  302. * Loading a helper with different config, should throw an exception
  303. *
  304. * @return void
  305. */
  306. public function testLoadMultipleTimesDifferentConfigValues()
  307. {
  308. $this->expectException(\RuntimeException::class);
  309. $this->expectExceptionMessage('The "Html" alias has already been loaded');
  310. $this->Helpers->load('Html', ['key' => 'value']);
  311. $this->Helpers->load('Html', ['key' => 'new value']);
  312. }
  313. /**
  314. * Test ObjectRegistry normalizeArray
  315. *
  316. * @return void
  317. */
  318. public function testArrayIsNormalized()
  319. {
  320. $config = [
  321. 'SomeHelper' => [
  322. 'value' => 1,
  323. 'value2' => 2,
  324. ],
  325. 'Plugin.SomeOtherHelper' => [
  326. 'value' => 1,
  327. 'value2' => 2,
  328. ],
  329. ];
  330. $result = $this->Helpers->normalizeArray($config);
  331. $expected = [
  332. 'SomeHelper' => [
  333. 'class' => 'SomeHelper',
  334. 'config' => [
  335. 'value' => 1,
  336. 'value2' => 2,
  337. ],
  338. ],
  339. 'SomeOtherHelper' => [
  340. 'class' => 'Plugin.SomeOtherHelper',
  341. 'config' => [
  342. 'value' => 1,
  343. 'value2' => 2,
  344. ],
  345. ],
  346. ];
  347. $this->assertEquals($expected, $result);
  348. }
  349. /**
  350. * Test that calling normalizeArray multiple times does
  351. * not nest the configuration.
  352. *
  353. * @return void
  354. */
  355. public function testArrayIsNormalizedAfterMultipleCalls()
  356. {
  357. $config = [
  358. 'SomeHelper' => [
  359. 'value' => 1,
  360. 'value2' => 2,
  361. ],
  362. 'Plugin.SomeOtherHelper' => [
  363. 'value' => 1,
  364. 'value2' => 2,
  365. ],
  366. ];
  367. $result1 = $this->Helpers->normalizeArray($config);
  368. $result2 = $this->Helpers->normalizeArray($result1);
  369. $expected = [
  370. 'SomeHelper' => [
  371. 'class' => 'SomeHelper',
  372. 'config' => [
  373. 'value' => 1,
  374. 'value2' => 2,
  375. ],
  376. ],
  377. 'SomeOtherHelper' => [
  378. 'class' => 'Plugin.SomeOtherHelper',
  379. 'config' => [
  380. 'value' => 1,
  381. 'value2' => 2,
  382. ],
  383. ],
  384. ];
  385. $this->assertEquals($expected, $result2);
  386. }
  387. }