HelperRegistryTest.php 12 KB

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