PluginConfigTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if (file_exists($this->pluginsConfigPath)) {
  51. file_put_contents($this->pluginsConfigPath, $this->originalPluginsConfigContent);
  52. }
  53. }
  54. public function testSimpleConfig(): void
  55. {
  56. $file = <<<PHP
  57. <?php
  58. return [
  59. 'plugins' => [
  60. 'TestPlugin' => '/config/path',
  61. 'OtherPlugin' => '/config/path',
  62. ]
  63. ];
  64. PHP;
  65. file_put_contents($this->pluginsListPath, $file);
  66. $config = <<<PHP
  67. <?php
  68. return [
  69. 'TestPlugin',
  70. 'OtherPlugin',
  71. ];
  72. PHP;
  73. file_put_contents($this->pluginsConfigPath, $config);
  74. Configure::delete('plugins');
  75. $config = new PluginConfig();
  76. $result = [
  77. 'TestPlugin' => [
  78. 'isLoaded' => true,
  79. 'onlyDebug' => false,
  80. 'onlyCli' => false,
  81. 'optional' => false,
  82. 'bootstrap' => true,
  83. 'console' => true,
  84. 'middleware' => true,
  85. 'routes' => true,
  86. 'services' => true,
  87. ],
  88. 'OtherPlugin' => [
  89. 'isLoaded' => true,
  90. 'onlyDebug' => false,
  91. 'onlyCli' => false,
  92. 'optional' => false,
  93. 'bootstrap' => true,
  94. 'console' => true,
  95. 'middleware' => true,
  96. 'routes' => true,
  97. 'services' => true,
  98. ],
  99. ];
  100. $this->assertSame($result, $config->getConfig());
  101. }
  102. public function testOnlyOnePlugin(): void
  103. {
  104. $file = <<<PHP
  105. <?php
  106. return [
  107. 'plugins' => [
  108. 'TestPlugin' => '/config/path',
  109. 'OtherPlugin' => '/config/path',
  110. ]
  111. ];
  112. PHP;
  113. file_put_contents($this->pluginsListPath, $file);
  114. $config = <<<PHP
  115. <?php
  116. return [
  117. 'TestPlugin',
  118. ];
  119. PHP;
  120. file_put_contents($this->pluginsConfigPath, $config);
  121. $config = new PluginConfig();
  122. $result = [
  123. 'TestPlugin' => [
  124. 'isLoaded' => true,
  125. 'onlyDebug' => false,
  126. 'onlyCli' => false,
  127. 'optional' => false,
  128. 'bootstrap' => true,
  129. 'console' => true,
  130. 'middleware' => true,
  131. 'routes' => true,
  132. 'services' => true,
  133. ],
  134. 'OtherPlugin' => [
  135. 'isLoaded' => false,
  136. ],
  137. ];
  138. $this->assertSame($result, $config->getConfig());
  139. }
  140. public function testSpecificEnvironmentsAndHooks(): void
  141. {
  142. $file = <<<PHP
  143. <?php
  144. return [
  145. 'plugins' => [
  146. 'OtherPlugin' => '/config/path',
  147. 'AnotherPlugin' => '/config/path'
  148. ]
  149. ];
  150. PHP;
  151. file_put_contents($this->pluginsListPath, $file);
  152. $config = <<<PHP
  153. <?php
  154. return [
  155. 'OtherPlugin' => ['onlyDebug' => true, 'onlyCli' => false, 'optional' => true],
  156. 'AnotherPlugin' => ['bootstrap' => false, 'console' => false, 'middleware' => false, 'routes' => false, 'services' => false]
  157. ];
  158. PHP;
  159. file_put_contents($this->pluginsConfigPath, $config);
  160. $config = new PluginConfig();
  161. $result = [
  162. 'OtherPlugin' => [
  163. 'isLoaded' => true,
  164. 'onlyDebug' => true,
  165. 'onlyCli' => false,
  166. 'optional' => true,
  167. 'bootstrap' => true,
  168. 'console' => true,
  169. 'middleware' => true,
  170. 'routes' => true,
  171. 'services' => true,
  172. ],
  173. 'AnotherPlugin' => [
  174. 'isLoaded' => true,
  175. 'onlyDebug' => false,
  176. 'onlyCli' => false,
  177. 'optional' => false,
  178. 'bootstrap' => false,
  179. 'console' => false,
  180. 'middleware' => false,
  181. 'routes' => false,
  182. 'services' => false,
  183. ],
  184. ];
  185. $this->assertSame($result, $config->getConfig());
  186. }
  187. public function testUnknownPlugin(): void
  188. {
  189. $file = <<<PHP
  190. <?php
  191. return [
  192. 'plugins' => [
  193. 'TestPlugin' => '/config/path',
  194. 'OtherPlugin' => '/config/path',
  195. ]
  196. ];
  197. PHP;
  198. file_put_contents($this->pluginsListPath, $file);
  199. $config = <<<PHP
  200. <?php
  201. return [
  202. 'UnknownPlugin',
  203. ];
  204. PHP;
  205. file_put_contents($this->pluginsConfigPath, $config);
  206. $config = new PluginConfig();
  207. $this->assertSame([
  208. 'TestPlugin' => [
  209. 'isLoaded' => false,
  210. ],
  211. 'OtherPlugin' => [
  212. 'isLoaded' => false,
  213. ],
  214. 'UnknownPlugin' => [
  215. 'isLoaded' => false,
  216. 'isUnknown' => true,
  217. ],
  218. ], $config->getConfig());
  219. }
  220. }