HelperRegistryTest.php 12 KB

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