PluginConfigTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * 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 5.1.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Core;
  16. use Cake\Core\Configure;
  17. use Cake\Core\PluginConfig;
  18. use Cake\TestSuite\TestCase;
  19. class PluginConfigTest extends TestCase
  20. {
  21. protected string $pluginsListPath;
  22. protected string $pluginsConfigPath;
  23. protected string $originalPluginsConfigContent = '';
  24. /**
  25. * Setup
  26. */
  27. public function setUp(): void
  28. {
  29. parent::setUp();
  30. $this->clearPlugins();
  31. $this->pluginsListPath = ROOT . DS . 'cakephp-plugins.php';
  32. if (file_exists($this->pluginsListPath)) {
  33. unlink($this->pluginsListPath);
  34. }
  35. $this->pluginsConfigPath = CONFIG . DS . 'plugins.php';
  36. if (file_exists($this->pluginsConfigPath)) {
  37. $this->originalPluginsConfigContent = file_get_contents($this->pluginsConfigPath);
  38. }
  39. }
  40. /**
  41. * Reverts the changes done to the environment while testing
  42. */
  43. public function tearDown(): void
  44. {
  45. parent::tearDown();
  46. $this->clearPlugins();
  47. if (file_exists($this->pluginsListPath)) {
  48. unlink($this->pluginsListPath);
  49. }
  50. file_put_contents($this->pluginsConfigPath, $this->originalPluginsConfigContent);
  51. }
  52. public function testSimpleConfig(): void
  53. {
  54. $file = <<<PHP
  55. <?php
  56. return [
  57. 'plugins' => [
  58. 'TestPlugin' => '/config/path',
  59. 'OtherPlugin' => '/config/path',
  60. ]
  61. ];
  62. PHP;
  63. file_put_contents($this->pluginsListPath, $file);
  64. $config = <<<PHP
  65. <?php
  66. return [
  67. 'TestPlugin',
  68. 'OtherPlugin',
  69. ];
  70. PHP;
  71. file_put_contents($this->pluginsConfigPath, $config);
  72. Configure::delete('plugins');
  73. $result = [
  74. 'TestPlugin' => [
  75. 'isLoaded' => true,
  76. 'onlyDebug' => false,
  77. 'onlyCli' => false,
  78. 'optional' => false,
  79. 'bootstrap' => true,
  80. 'console' => true,
  81. 'middleware' => true,
  82. 'routes' => true,
  83. 'services' => true,
  84. ],
  85. 'OtherPlugin' => [
  86. 'isLoaded' => true,
  87. 'onlyDebug' => false,
  88. 'onlyCli' => false,
  89. 'optional' => false,
  90. 'bootstrap' => true,
  91. 'console' => true,
  92. 'middleware' => true,
  93. 'routes' => true,
  94. 'services' => true,
  95. ],
  96. ];
  97. $this->assertSame($result, PluginConfig::getAppConfig());
  98. }
  99. public function testOnlyOnePlugin(): void
  100. {
  101. $file = <<<PHP
  102. <?php
  103. return [
  104. 'plugins' => [
  105. 'TestPlugin' => '/config/path',
  106. 'OtherPlugin' => '/config/path',
  107. ]
  108. ];
  109. PHP;
  110. file_put_contents($this->pluginsListPath, $file);
  111. $config = <<<PHP
  112. <?php
  113. return [
  114. 'TestPlugin',
  115. ];
  116. PHP;
  117. file_put_contents($this->pluginsConfigPath, $config);
  118. $result = [
  119. 'TestPlugin' => [
  120. 'isLoaded' => true,
  121. 'onlyDebug' => false,
  122. 'onlyCli' => false,
  123. 'optional' => false,
  124. 'bootstrap' => true,
  125. 'console' => true,
  126. 'middleware' => true,
  127. 'routes' => true,
  128. 'services' => true,
  129. ],
  130. 'OtherPlugin' => [
  131. 'isLoaded' => false,
  132. ],
  133. ];
  134. $this->assertSame($result, PluginConfig::getAppConfig());
  135. }
  136. public function testSpecificEnvironmentsAndHooks(): void
  137. {
  138. $file = <<<PHP
  139. <?php
  140. return [
  141. 'plugins' => [
  142. 'OtherPlugin' => '/config/path',
  143. 'AnotherPlugin' => '/config/path'
  144. ]
  145. ];
  146. PHP;
  147. file_put_contents($this->pluginsListPath, $file);
  148. $config = <<<PHP
  149. <?php
  150. return [
  151. 'OtherPlugin' => ['onlyDebug' => true, 'onlyCli' => false, 'optional' => true],
  152. 'AnotherPlugin' => ['bootstrap' => false, 'console' => false, 'middleware' => false, 'routes' => false, 'services' => false]
  153. ];
  154. PHP;
  155. file_put_contents($this->pluginsConfigPath, $config);
  156. $result = [
  157. 'OtherPlugin' => [
  158. 'isLoaded' => true,
  159. 'onlyDebug' => true,
  160. 'onlyCli' => false,
  161. 'optional' => true,
  162. 'bootstrap' => true,
  163. 'console' => true,
  164. 'middleware' => true,
  165. 'routes' => true,
  166. 'services' => true,
  167. ],
  168. 'AnotherPlugin' => [
  169. 'isLoaded' => true,
  170. 'onlyDebug' => false,
  171. 'onlyCli' => false,
  172. 'optional' => false,
  173. 'bootstrap' => false,
  174. 'console' => false,
  175. 'middleware' => false,
  176. 'routes' => false,
  177. 'services' => false,
  178. ],
  179. ];
  180. $this->assertSame($result, PluginConfig::getAppConfig());
  181. }
  182. public function testUnknownPlugin(): void
  183. {
  184. $file = <<<PHP
  185. <?php
  186. return [
  187. 'plugins' => [
  188. 'TestPlugin' => '/config/path',
  189. 'OtherPlugin' => '/config/path',
  190. ]
  191. ];
  192. PHP;
  193. file_put_contents($this->pluginsListPath, $file);
  194. $config = <<<PHP
  195. <?php
  196. return [
  197. 'UnknownPlugin',
  198. ];
  199. PHP;
  200. file_put_contents($this->pluginsConfigPath, $config);
  201. $this->assertSame([
  202. 'TestPlugin' => [
  203. 'isLoaded' => false,
  204. ],
  205. 'OtherPlugin' => [
  206. 'isLoaded' => false,
  207. ],
  208. 'UnknownPlugin' => [
  209. 'isLoaded' => false,
  210. 'isUnknown' => true,
  211. ],
  212. ], PluginConfig::getAppConfig());
  213. }
  214. public function testNoPluginConfig(): void
  215. {
  216. $file = <<<PHP
  217. <?php
  218. return [
  219. 'plugins' => [
  220. 'TestPlugin' => '/config/path',
  221. 'OtherPlugin' => '/config/path',
  222. ]
  223. ];
  224. PHP;
  225. file_put_contents($this->pluginsListPath, $file);
  226. unlink($this->pluginsConfigPath);
  227. $this->assertSame([
  228. 'TestPlugin' => [
  229. 'isLoaded' => false,
  230. ],
  231. 'OtherPlugin' => [
  232. 'isLoaded' => false,
  233. ],
  234. ], PluginConfig::getAppConfig());
  235. }
  236. }