HelperRegistryTest.php 12 KB

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