| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 5.1.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Core;
- use Cake\Core\Configure;
- use Cake\Core\PluginConfig;
- use Cake\TestSuite\TestCase;
- class PluginConfigTest extends TestCase
- {
- protected string $pluginsListPath;
- protected string $pluginsConfigPath;
- protected string $originalPluginsConfigContent = '';
- /**
- * Setup
- */
- public function setUp(): void
- {
- parent::setUp();
- $this->clearPlugins();
- $this->pluginsListPath = ROOT . DS . 'cakephp-plugins.php';
- if (file_exists($this->pluginsListPath)) {
- unlink($this->pluginsListPath);
- }
- $this->pluginsConfigPath = CONFIG . DS . 'plugins.php';
- if (file_exists($this->pluginsConfigPath)) {
- $this->originalPluginsConfigContent = file_get_contents($this->pluginsConfigPath);
- }
- }
- /**
- * Reverts the changes done to the environment while testing
- */
- public function tearDown(): void
- {
- parent::tearDown();
- $this->clearPlugins();
- if (file_exists($this->pluginsListPath)) {
- unlink($this->pluginsListPath);
- }
- file_put_contents($this->pluginsConfigPath, $this->originalPluginsConfigContent);
- }
- public function testSimpleConfig(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'TestPlugin' => '/config/path',
- 'OtherPlugin' => '/config/path',
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'TestPlugin',
- 'OtherPlugin',
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- Configure::delete('plugins');
- $result = [
- 'TestPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- ],
- 'OtherPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- ],
- ];
- $this->assertSame($result, PluginConfig::getAppConfig());
- }
- public function testOnlyOnePlugin(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'TestPlugin' => '/config/path',
- 'OtherPlugin' => '/config/path',
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'TestPlugin',
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- $result = [
- 'TestPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- ],
- 'OtherPlugin' => [
- 'isLoaded' => false,
- ],
- ];
- $this->assertSame($result, PluginConfig::getAppConfig());
- }
- public function testSpecificEnvironmentsAndHooks(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'OtherPlugin' => '/config/path',
- 'AnotherPlugin' => '/config/path'
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'OtherPlugin' => ['onlyDebug' => true, 'onlyCli' => false, 'optional' => true],
- 'AnotherPlugin' => ['bootstrap' => false, 'console' => false, 'middleware' => false, 'routes' => false, 'services' => false]
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- $result = [
- 'OtherPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => true,
- 'onlyCli' => false,
- 'optional' => true,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- ],
- 'AnotherPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => false,
- 'console' => false,
- 'middleware' => false,
- 'routes' => false,
- 'services' => false,
- ],
- ];
- $this->assertSame($result, PluginConfig::getAppConfig());
- }
- public function testUnknownPlugin(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'TestPlugin' => '/config/path',
- 'OtherPlugin' => '/config/path',
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'UnknownPlugin',
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- $this->assertSame([
- 'TestPlugin' => [
- 'isLoaded' => false,
- ],
- 'OtherPlugin' => [
- 'isLoaded' => false,
- ],
- 'UnknownPlugin' => [
- 'isLoaded' => false,
- 'isUnknown' => true,
- ],
- ], PluginConfig::getAppConfig());
- }
- public function testNoPluginConfig(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'TestPlugin' => '/config/path',
- 'OtherPlugin' => '/config/path',
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- unlink($this->pluginsConfigPath);
- $this->assertSame([
- 'TestPlugin' => [
- 'isLoaded' => false,
- ],
- 'OtherPlugin' => [
- 'isLoaded' => false,
- ],
- ], PluginConfig::getAppConfig());
- }
- }
|