Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * CakePluginTest file.
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Test.Case.Core
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('CakePlugin', 'Core');
20
 
21
/**
22
 * CakePluginTest class
23
 *
24
 */
25
class CakePluginTest extends CakeTestCase {
26
 
27
/**
28
 * Sets the plugins folder for this test
29
 *
30
 * @return void
31
 */
32
	public function setUp() {
33
		parent::setUp();
34
		App::build(array(
35
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
36
		), App::RESET);
37
		App::objects('plugins', null, false);
38
	}
39
 
40
/**
41
 * Reverts the changes done to the environment while testing
42
 *
43
 * @return void
44
 */
45
	public function tearDown() {
46
		parent::tearDown();
47
		CakePlugin::unload();
48
	}
49
 
50
/**
51
 * Tests loading a single plugin
52
 *
53
 * @return void
54
 */
55
	public function testLoadSingle() {
56
		CakePlugin::unload();
57
		CakePlugin::load('TestPlugin');
58
		$expected = array('TestPlugin');
59
		$this->assertEquals($expected, CakePlugin::loaded());
60
	}
61
 
62
/**
63
 * Tests unloading plugins
64
 *
65
 * @return void
66
 */
67
	public function testUnload() {
68
		CakePlugin::load('TestPlugin');
69
		$expected = array('TestPlugin');
70
		$this->assertEquals($expected, CakePlugin::loaded());
71
 
72
		CakePlugin::unload('TestPlugin');
73
		$this->assertEquals(array(), CakePlugin::loaded());
74
 
75
		CakePlugin::load('TestPlugin');
76
		$expected = array('TestPlugin');
77
		$this->assertEquals($expected, CakePlugin::loaded());
78
 
79
		CakePlugin::unload('TestFakePlugin');
80
		$this->assertEquals($expected, CakePlugin::loaded());
81
	}
82
 
83
/**
84
 * Tests loading a plugin and its bootstrap file
85
 *
86
 * @return void
87
 */
88
	public function testLoadSingleWithBootstrap() {
89
		CakePlugin::load('TestPlugin', array('bootstrap' => true));
90
		$this->assertTrue(CakePlugin::loaded('TestPlugin'));
91
		$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
92
	}
93
 
94
/**
95
 * Tests loading a plugin with bootstrap file and routes file
96
 *
97
 * @return void
98
 */
99
	public function testLoadSingleWithBootstrapAndRoutes() {
100
		CakePlugin::load('TestPlugin', array('bootstrap' => true, 'routes' => true));
101
		$this->assertTrue(CakePlugin::loaded('TestPlugin'));
102
		$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
103
 
104
		CakePlugin::routes();
105
		$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
106
	}
107
 
108
/**
109
 * Tests loading multiple plugins at once
110
 *
111
 * @return void
112
 */
113
	public function testLoadMultiple() {
114
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
115
		$expected = array('TestPlugin', 'TestPluginTwo');
116
		$this->assertEquals($expected, CakePlugin::loaded());
117
	}
118
 
119
/**
120
 * Tests loading multiple plugins and their bootstrap files
121
 *
122
 * @return void
123
 */
124
	public function testLoadMultipleWithDefaults() {
125
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => false));
126
		$expected = array('TestPlugin', 'TestPluginTwo');
127
		$this->assertEquals($expected, CakePlugin::loaded());
128
		$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
129
		$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
130
	}
131
 
132
/**
133
 * Tests loading multiple plugins with default loading params and some overrides
134
 *
135
 * @return void
136
 */
137
	public function testLoadMultipleWithDefaultsAndOverride() {
138
		CakePlugin::load(
139
			array('TestPlugin', 'TestPluginTwo' => array('routes' => false)),
140
			array('bootstrap' => true, 'routes' => true)
141
		);
142
		$expected = array('TestPlugin', 'TestPluginTwo');
143
		$this->assertEquals($expected, CakePlugin::loaded());
144
		$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
145
		$this->assertEquals(null, Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
146
	}
147
 
148
/**
149
 * Tests that it is possible to load multiple bootstrap files at once
150
 *
151
 * @return void
152
 */
153
	public function testMultipleBootstrapFiles() {
154
		CakePlugin::load('TestPlugin', array('bootstrap' => array('bootstrap', 'custom_config')));
155
		$this->assertTrue(CakePlugin::loaded('TestPlugin'));
156
		$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
157
	}
158
 
159
/**
160
 * Tests that it is possible to load plugin bootstrap by calling a callback function
161
 *
162
 * @return void
163
 */
164
	public function testCallbackBootstrap() {
165
		CakePlugin::load('TestPlugin', array('bootstrap' => array($this, 'pluginBootstrap')));
166
		$this->assertTrue(CakePlugin::loaded('TestPlugin'));
167
		$this->assertEquals('called plugin bootstrap callback', Configure::read('CakePluginTest.test_plugin.bootstrap'));
168
	}
169
 
170
/**
171
 * Tests that loading a missing routes file throws a warning
172
 *
173
 * @return void
174
 * @expectedException PHPUNIT_FRAMEWORK_ERROR_WARNING
175
 */
176
	public function testLoadMultipleWithDefaultsMissingFile() {
177
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => true));
178
		CakePlugin::routes();
179
	}
180
 
181
/**
182
 * Test ignoring missing bootstrap/routes file
183
 *
184
 * @return void
185
 */
186
	public function testIgnoreMissingFiles() {
187
		CakePlugin::loadAll(array(array(
188
			'bootstrap' => true,
189
			'routes' => true,
190
			'ignoreMissing' => true
191
		)));
192
		CakePlugin::routes();
193
	}
194
 
195
/**
196
 * Tests that CakePlugin::load() throws an exception on unknown plugin
197
 *
198
 * @return void
199
 * @expectedException MissingPluginException
200
 */
201
	public function testLoadNotFound() {
202
		CakePlugin::load('MissingPlugin');
203
	}
204
 
205
/**
206
 * Tests that CakePlugin::path() returns the correct path for the loaded plugins
207
 *
208
 * @return void
209
 */
210
	public function testPath() {
211
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
212
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
213
		$this->assertEquals($expected, CakePlugin::path('TestPlugin'));
214
 
215
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS;
216
		$this->assertEquals($expected, CakePlugin::path('TestPluginTwo'));
217
	}
218
 
219
/**
220
 * Tests that CakePlugin::path() throws an exception on unknown plugin
221
 *
222
 * @return void
223
 * @expectedException MissingPluginException
224
 */
225
	public function testPathNotFound() {
226
		CakePlugin::path('TestPlugin');
227
	}
228
 
229
/**
230
 * Tests that CakePlugin::loadAll() will load all plugins in the configured folder
231
 *
232
 * @return void
233
 */
234
	public function testLoadAll() {
235
		CakePlugin::loadAll();
236
		$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
237
		$this->assertEquals($expected, CakePlugin::loaded());
238
	}
239
 
240
/**
241
 * Tests that CakePlugin::loadAll() will load all plugins in the configured folder with bootstrap loading
242
 *
243
 * @return void
244
 */
245
	public function testLoadAllWithDefaults() {
246
		$defaults = array('bootstrap' => true);
247
		CakePlugin::loadAll(array($defaults));
248
		$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
249
		$this->assertEquals($expected, CakePlugin::loaded());
250
		$this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
251
		$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
252
		$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
253
	}
254
 
255
/**
256
 * Tests that CakePlugin::loadAll() will load all plugins in the configured folder with defaults
257
 * and merges in global defaults.
258
 *
259
 * @return void
260
 */
261
	public function testLoadAllWithDefaultsAndOverride() {
262
		CakePlugin::loadAll(array(array('bootstrap' => true), 'TestPlugin' => array('routes' => true)));
263
		CakePlugin::routes();
264
 
265
		$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
266
		$this->assertEquals($expected, CakePlugin::loaded());
267
		$this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
268
		$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
269
		$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
270
		$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
271
	}
272
 
273
/**
274
 * Tests that CakePlugin::loadAll() will load all plugins in the configured folder with defaults
275
 * and overrides for a plugin
276
 *
277
 * @return void
278
 */
279
	public function testLoadAllWithDefaultsAndOverrideComplex() {
280
		CakePlugin::loadAll(array(array('bootstrap' => true), 'TestPlugin' => array('routes' => true, 'bootstrap' => false)));
281
		CakePlugin::routes();
282
 
283
		$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
284
		$this->assertEquals($expected, CakePlugin::loaded());
285
		$this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
286
		$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
287
		$this->assertEquals(null, Configure::read('CakePluginTest.test_plugin.bootstrap'));
288
		$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
289
	}
290
 
291
/**
292
 * Auxiliary function to test plugin bootstrap callbacks
293
 *
294
 * @return void
295
 */
296
	public function pluginBootstrap() {
297
		Configure::write('CakePluginTest.test_plugin.bootstrap', 'called plugin bootstrap callback');
298
	}
299
}