cakephp2-php8/lib/Cake/Test/Case/Core/CakePluginTest.php

299 lines
9.5 KiB
PHP
Raw Normal View History

<?php
/**
* CakePluginTest file.
*
2017-06-10 21:33:55 +00:00
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
2017-06-10 21:33:55 +00:00
* @link https://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Core
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakePlugin', 'Core');
/**
* CakePluginTest class
*/
class CakePluginTest extends CakeTestCase {
/**
* Sets the plugins folder for this test
*
* @return void
*/
public function setUp() {
parent::setUp();
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
App::objects('plugins', null, false);
}
/**
* Reverts the changes done to the environment while testing
*
* @return void
*/
public function tearDown() {
parent::tearDown();
2011-05-09 03:55:32 +00:00
CakePlugin::unload();
}
/**
* Tests loading a single plugin
*
* @return void
*/
public function testLoadSingle() {
CakePlugin::unload();
CakePlugin::load('TestPlugin');
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests unloading plugins
*
* @return void
*/
public function testUnload() {
CakePlugin::load('TestPlugin');
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
2011-05-15 16:53:53 +00:00
CakePlugin::unload('TestPlugin');
$this->assertEquals(array(), CakePlugin::loaded());
CakePlugin::load('TestPlugin');
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
2011-05-15 16:53:53 +00:00
CakePlugin::unload('TestFakePlugin');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests loading a plugin and its bootstrap file
*
* @return void
*/
public function testLoadSingleWithBootstrap() {
CakePlugin::load('TestPlugin', array('bootstrap' => true));
$this->assertTrue(CakePlugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
}
/**
* Tests loading a plugin with bootstrap file and routes file
*
* @return void
*/
public function testLoadSingleWithBootstrapAndRoutes() {
CakePlugin::load('TestPlugin', array('bootstrap' => true, 'routes' => true));
$this->assertTrue(CakePlugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
CakePlugin::routes();
$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
}
/**
* Tests loading multiple plugins at once
*
* @return void
*/
public function testLoadMultiple() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
$expected = array('TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests loading multiple plugins and their bootstrap files
*
* @return void
*/
public function testLoadMultipleWithDefaults() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => false));
$expected = array('TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests loading multiple plugins with default loading params and some overrides
*
* @return void
*/
public function testLoadMultipleWithDefaultsAndOverride() {
CakePlugin::load(
array('TestPlugin', 'TestPluginTwo' => array('routes' => false)),
array('bootstrap' => true, 'routes' => true)
);
$expected = array('TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals(null, Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests that it is possible to load multiple bootstrap files at once
*
* @return void
*/
public function testMultipleBootstrapFiles() {
CakePlugin::load('TestPlugin', array('bootstrap' => array('bootstrap', 'custom_config')));
$this->assertTrue(CakePlugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
}
2011-04-29 16:53:11 +00:00
/**
* Tests that it is possible to load plugin bootstrap by calling a callback function
*
* @return void
*/
public function testCallbackBootstrap() {
CakePlugin::load('TestPlugin', array('bootstrap' => array($this, 'pluginBootstrap')));
$this->assertTrue(CakePlugin::loaded('TestPlugin'));
$this->assertEquals('called plugin bootstrap callback', Configure::read('CakePluginTest.test_plugin.bootstrap'));
}
/**
* Tests that loading a missing routes file throws a warning
*
* @return void
* @expectedException PHPUNIT_FRAMEWORK_ERROR_WARNING
*/
public function testLoadMultipleWithDefaultsMissingFile() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => true));
CakePlugin::routes();
}
/**
* Test ignoring missing bootstrap/routes file
*
* @return void
*/
public function testIgnoreMissingFiles() {
CakePlugin::loadAll(array(array(
'bootstrap' => true,
'routes' => true,
'ignoreMissing' => true
)));
CakePlugin::routes();
}
2011-04-29 16:53:11 +00:00
/**
* Tests that CakePlugin::load() throws an exception on unknown plugin
*
* @return void
* @expectedException MissingPluginException
*/
public function testLoadNotFound() {
CakePlugin::load('MissingPlugin');
}
/**
* Tests that CakePlugin::path() returns the correct path for the loaded plugins
*
* @return void
*/
public function testPath() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
2011-05-15 16:53:53 +00:00
$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
2014-01-09 23:33:27 +00:00
$this->assertEquals($expected, CakePlugin::path('TestPlugin'));
2011-04-29 16:53:11 +00:00
2011-05-15 16:53:53 +00:00
$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS;
2014-01-09 23:33:27 +00:00
$this->assertEquals($expected, CakePlugin::path('TestPluginTwo'));
2011-04-29 16:53:11 +00:00
}
/**
* Tests that CakePlugin::path() throws an exception on unknown plugin
*
* @return void
* @expectedException MissingPluginException
*/
public function testPathNotFound() {
CakePlugin::path('TestPlugin');
}
2011-05-06 05:23:30 +00:00
/**
2012-02-23 23:29:53 +00:00
* Tests that CakePlugin::loadAll() will load all plugins in the configured folder
2011-05-06 05:23:30 +00:00
*
* @return void
*/
public function testLoadAll() {
CakePlugin::loadAll();
$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
2012-02-23 23:29:53 +00:00
* Tests that CakePlugin::loadAll() will load all plugins in the configured folder with bootstrap loading
2011-05-06 05:23:30 +00:00
*
* @return void
*/
public function testLoadAllWithDefaults() {
$defaults = array('bootstrap' => true);
CakePlugin::loadAll(array($defaults));
2011-05-06 05:23:30 +00:00
$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests that CakePlugin::loadAll() will load all plugins in the configured folder with defaults
* and merges in global defaults.
2011-05-06 05:23:30 +00:00
*
* @return void
*/
public function testLoadAllWithDefaultsAndOverride() {
CakePlugin::loadAll(array(array('bootstrap' => true), 'TestPlugin' => array('routes' => true)));
CakePlugin::routes();
$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests that CakePlugin::loadAll() will load all plugins in the configured folder with defaults
* and overrides for a plugin
*
* @return void
*/
public function testLoadAllWithDefaultsAndOverrideComplex() {
CakePlugin::loadAll(array(array('bootstrap' => true), 'TestPlugin' => array('routes' => true, 'bootstrap' => false)));
CakePlugin::routes();
2011-05-06 05:23:30 +00:00
$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
$this->assertEquals(null, Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Auxiliary function to test plugin bootstrap callbacks
*
* @return void
*/
public function pluginBootstrap() {
Configure::write('CakePluginTest.test_plugin.bootstrap', 'called plugin bootstrap callback');
}
2011-05-15 16:53:53 +00:00
}