PluginConfigTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. public function testGetVersions(): void
  237. {
  238. $test = PluginConfig::getVersions(ROOT . DS . 'tests' . DS . 'composer.lock');
  239. $expected = [
  240. 'packages' => [
  241. 'cakephp/chronos' => '3.0.4',
  242. 'psr/simple-cache' => '3.0.0',
  243. ],
  244. 'devPackages' => [
  245. 'cakephp/cakephp-codesniffer' => '5.1.1',
  246. 'squizlabs/php_codesniffer' => '3.8.1',
  247. 'theseer/tokenizer' => '1.2.2',
  248. ],
  249. ];
  250. $this->assertEquals($expected, $test);
  251. }
  252. public function testSimpleConfiWithVersions(): void
  253. {
  254. $file = <<<PHP
  255. <?php
  256. return [
  257. 'plugins' => [
  258. 'Chronos' => ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'chronos',
  259. 'CodeSniffer' => ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'cakephp-codesniffer'
  260. ]
  261. ];
  262. PHP;
  263. file_put_contents($this->pluginsListPath, $file);
  264. $config = <<<PHP
  265. <?php
  266. return [
  267. 'Chronos',
  268. 'CodeSniffer'
  269. ];
  270. PHP;
  271. file_put_contents($this->pluginsConfigPath, $config);
  272. Configure::delete('plugins');
  273. $pathToRootVendor = ROOT . DS . 'vendor' . DS;
  274. $result = [
  275. 'Chronos' => [
  276. 'isLoaded' => true,
  277. 'onlyDebug' => false,
  278. 'onlyCli' => false,
  279. 'optional' => false,
  280. 'bootstrap' => true,
  281. 'console' => true,
  282. 'middleware' => true,
  283. 'routes' => true,
  284. 'services' => true,
  285. 'packagePath' => $pathToRootVendor . 'cakephp' . DS . 'chronos',
  286. 'package' => 'cakephp/chronos',
  287. 'version' => '3.0.4',
  288. 'isDevPackage' => false,
  289. ],
  290. 'CodeSniffer' => [
  291. 'isLoaded' => true,
  292. 'onlyDebug' => false,
  293. 'onlyCli' => false,
  294. 'optional' => false,
  295. 'bootstrap' => true,
  296. 'console' => true,
  297. 'middleware' => true,
  298. 'routes' => true,
  299. 'services' => true,
  300. 'packagePath' => $pathToRootVendor . 'cakephp' . DS . 'cakephp-codesniffer',
  301. 'package' => 'cakephp/cakephp-codesniffer',
  302. 'version' => '5.1.1',
  303. 'isDevPackage' => true,
  304. ],
  305. ];
  306. $this->assertSame($result, PluginConfig::getAppConfig(ROOT . DS . 'tests' . DS . 'composer.lock'));
  307. }
  308. public function testInvalidComposerLock(): void
  309. {
  310. $path = ROOT . DS . 'tests' . DS . 'unknown_composer.lock';
  311. $this->assertSame([], PluginConfig::getAppConfig($path));
  312. file_put_contents($path, 'invalid-json');
  313. $this->assertSame([], PluginConfig::getAppConfig($path));
  314. unlink($path);
  315. }
  316. public function testInvalidComposerJson(): void
  317. {
  318. $pathToTestPlugin = ROOT . DS . 'tests' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
  319. $file = <<<PHP
  320. <?php
  321. return [
  322. 'plugins' => [
  323. 'TestPlugin' => ROOT . DS . 'tests' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
  324. ]
  325. ];
  326. PHP;
  327. file_put_contents($this->pluginsListPath, $file);
  328. $config = <<<PHP
  329. <?php
  330. return [
  331. 'TestPlugin',
  332. ];
  333. PHP;
  334. file_put_contents($this->pluginsConfigPath, $config);
  335. file_put_contents($pathToTestPlugin . 'composer.json', 'invalid-json');
  336. $this->assertSame([
  337. 'TestPlugin' => [
  338. 'isLoaded' => true,
  339. 'onlyDebug' => false,
  340. 'onlyCli' => false,
  341. 'optional' => false,
  342. 'bootstrap' => true,
  343. 'console' => true,
  344. 'middleware' => true,
  345. 'routes' => true,
  346. 'services' => true,
  347. ],
  348. ], PluginConfig::getAppConfig());
  349. unlink($pathToTestPlugin . 'composer.json');
  350. }
  351. }