HelperRegistryTest.php 12 KB

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