PluginConfigTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. 'events' => true,
  85. ],
  86. 'OtherPlugin' => [
  87. 'isLoaded' => true,
  88. 'onlyDebug' => false,
  89. 'onlyCli' => false,
  90. 'optional' => false,
  91. 'bootstrap' => true,
  92. 'console' => true,
  93. 'middleware' => true,
  94. 'routes' => true,
  95. 'services' => true,
  96. 'events' => true,
  97. ],
  98. ];
  99. $this->assertSame($result, PluginConfig::getAppConfig());
  100. }
  101. public function testOnlyOnePlugin(): void
  102. {
  103. $file = <<<PHP
  104. <?php
  105. return [
  106. 'plugins' => [
  107. 'TestPlugin' => '/config/path',
  108. 'OtherPlugin' => '/config/path',
  109. ]
  110. ];
  111. PHP;
  112. file_put_contents($this->pluginsListPath, $file);
  113. $config = <<<PHP
  114. <?php
  115. return [
  116. 'TestPlugin',
  117. ];
  118. PHP;
  119. file_put_contents($this->pluginsConfigPath, $config);
  120. $result = [
  121. 'TestPlugin' => [
  122. 'isLoaded' => true,
  123. 'onlyDebug' => false,
  124. 'onlyCli' => false,
  125. 'optional' => false,
  126. 'bootstrap' => true,
  127. 'console' => true,
  128. 'middleware' => true,
  129. 'routes' => true,
  130. 'services' => true,
  131. 'events' => true,
  132. ],
  133. 'OtherPlugin' => [
  134. 'isLoaded' => false,
  135. ],
  136. ];
  137. $this->assertSame($result, PluginConfig::getAppConfig());
  138. }
  139. public function testSpecificEnvironmentsAndHooks(): void
  140. {
  141. $file = <<<PHP
  142. <?php
  143. return [
  144. 'plugins' => [
  145. 'OtherPlugin' => '/config/path',
  146. 'AnotherPlugin' => '/config/path'
  147. ]
  148. ];
  149. PHP;
  150. file_put_contents($this->pluginsListPath, $file);
  151. $config = <<<PHP
  152. <?php
  153. return [
  154. 'OtherPlugin' => ['onlyDebug' => true, 'onlyCli' => false, 'optional' => true],
  155. 'AnotherPlugin' => ['bootstrap' => false, 'console' => false, 'middleware' => false, 'routes' => false, 'services' => false, 'events' => false],
  156. ];
  157. PHP;
  158. file_put_contents($this->pluginsConfigPath, $config);
  159. $result = [
  160. 'OtherPlugin' => [
  161. 'isLoaded' => true,
  162. 'onlyDebug' => true,
  163. 'onlyCli' => false,
  164. 'optional' => true,
  165. 'bootstrap' => true,
  166. 'console' => true,
  167. 'middleware' => true,
  168. 'routes' => true,
  169. 'services' => true,
  170. 'events' => true,
  171. ],
  172. 'AnotherPlugin' => [
  173. 'isLoaded' => true,
  174. 'onlyDebug' => false,
  175. 'onlyCli' => false,
  176. 'optional' => false,
  177. 'bootstrap' => false,
  178. 'console' => false,
  179. 'middleware' => false,
  180. 'routes' => false,
  181. 'services' => false,
  182. 'events' => false,
  183. ],
  184. ];
  185. $this->assertSame($result, PluginConfig::getAppConfig());
  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. $this->assertSame([
  207. 'TestPlugin' => [
  208. 'isLoaded' => false,
  209. ],
  210. 'OtherPlugin' => [
  211. 'isLoaded' => false,
  212. ],
  213. 'UnknownPlugin' => [
  214. 'isLoaded' => false,
  215. 'isUnknown' => true,
  216. ],
  217. ], PluginConfig::getAppConfig());
  218. }
  219. public function testNoPluginConfig(): void
  220. {
  221. $file = <<<PHP
  222. <?php
  223. return [
  224. 'plugins' => [
  225. 'TestPlugin' => '/config/path',
  226. 'OtherPlugin' => '/config/path',
  227. ]
  228. ];
  229. PHP;
  230. file_put_contents($this->pluginsListPath, $file);
  231. unlink($this->pluginsConfigPath);
  232. $this->assertSame([
  233. 'TestPlugin' => [
  234. 'isLoaded' => false,
  235. ],
  236. 'OtherPlugin' => [
  237. 'isLoaded' => false,
  238. ],
  239. ], PluginConfig::getAppConfig());
  240. }
  241. public function testGetVersions(): void
  242. {
  243. $test = PluginConfig::getVersions(ROOT . DS . 'tests' . DS . 'composer.lock');
  244. $expected = [
  245. 'packages' => [
  246. 'cakephp/chronos' => '3.0.4',
  247. 'psr/simple-cache' => '3.0.0',
  248. ],
  249. 'devPackages' => [
  250. 'cakephp/cakephp-codesniffer' => '5.1.1',
  251. 'squizlabs/php_codesniffer' => '3.8.1',
  252. 'theseer/tokenizer' => '1.2.2',
  253. ],
  254. ];
  255. $this->assertEquals($expected, $test);
  256. }
  257. public function testSimpleConfiWithVersions(): void
  258. {
  259. $file = <<<PHP
  260. <?php
  261. return [
  262. 'plugins' => [
  263. 'Chronos' => ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'chronos',
  264. 'CodeSniffer' => ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'cakephp-codesniffer'
  265. ]
  266. ];
  267. PHP;
  268. file_put_contents($this->pluginsListPath, $file);
  269. $config = <<<PHP
  270. <?php
  271. return [
  272. 'Chronos',
  273. 'CodeSniffer'
  274. ];
  275. PHP;
  276. file_put_contents($this->pluginsConfigPath, $config);
  277. Configure::delete('plugins');
  278. $pathToRootVendor = ROOT . DS . 'vendor' . DS;
  279. $result = [
  280. 'Chronos' => [
  281. 'isLoaded' => true,
  282. 'onlyDebug' => false,
  283. 'onlyCli' => false,
  284. 'optional' => false,
  285. 'bootstrap' => true,
  286. 'console' => true,
  287. 'middleware' => true,
  288. 'routes' => true,
  289. 'services' => true,
  290. 'events' => true,
  291. 'packagePath' => $pathToRootVendor . 'cakephp' . DS . 'chronos',
  292. 'package' => 'cakephp/chronos',
  293. 'version' => '3.0.4',
  294. 'isDevPackage' => false,
  295. ],
  296. 'CodeSniffer' => [
  297. 'isLoaded' => true,
  298. 'onlyDebug' => false,
  299. 'onlyCli' => false,
  300. 'optional' => false,
  301. 'bootstrap' => true,
  302. 'console' => true,
  303. 'middleware' => true,
  304. 'routes' => true,
  305. 'services' => true,
  306. 'events' => true,
  307. 'packagePath' => $pathToRootVendor . 'cakephp' . DS . 'cakephp-codesniffer',
  308. 'package' => 'cakephp/cakephp-codesniffer',
  309. 'version' => '5.1.1',
  310. 'isDevPackage' => true,
  311. ],
  312. ];
  313. $this->assertSame($result, PluginConfig::getAppConfig(ROOT . DS . 'tests' . DS . 'composer.lock'));
  314. }
  315. public function testInvalidComposerLock(): void
  316. {
  317. $path = ROOT . DS . 'tests' . DS . 'unknown_composer.lock';
  318. $this->assertSame([], PluginConfig::getAppConfig($path));
  319. file_put_contents($path, 'invalid-json');
  320. $this->assertSame([], PluginConfig::getAppConfig($path));
  321. unlink($path);
  322. }
  323. public function testInvalidComposerJson(): void
  324. {
  325. $pathToTestPlugin = ROOT . DS . 'tests' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
  326. $file = <<<PHP
  327. <?php
  328. return [
  329. 'plugins' => [
  330. 'TestPlugin' => ROOT . DS . 'tests' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
  331. ]
  332. ];
  333. PHP;
  334. file_put_contents($this->pluginsListPath, $file);
  335. $config = <<<PHP
  336. <?php
  337. return [
  338. 'TestPlugin',
  339. ];
  340. PHP;
  341. file_put_contents($this->pluginsConfigPath, $config);
  342. file_put_contents($pathToTestPlugin . 'composer.json', 'invalid-json');
  343. $this->assertSame([
  344. 'TestPlugin' => [
  345. 'isLoaded' => true,
  346. 'onlyDebug' => false,
  347. 'onlyCli' => false,
  348. 'optional' => false,
  349. 'bootstrap' => true,
  350. 'console' => true,
  351. 'middleware' => true,
  352. 'routes' => true,
  353. 'services' => true,
  354. 'events' => true,
  355. ],
  356. ], PluginConfig::getAppConfig());
  357. unlink($pathToTestPlugin . 'composer.json');
  358. }
  359. }