Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * AppTest 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
/**
20
 * AppTest class
21
 *
22
 * @package       Cake.Test.Case.Core
23
 */
24
class AppTest extends CakeTestCase {
25
 
26
/**
27
 * tearDown method
28
 *
29
 * @return void
30
 */
31
	public function tearDown() {
32
		parent::tearDown();
33
		CakePlugin::unload();
34
	}
35
 
36
/**
37
 * testBuild method
38
 *
39
 * @return void
40
 */
41
	public function testBuild() {
42
		$old = App::path('Model');
43
		$expected = array(
44
			APP . 'Model' . DS
45
		);
46
		$this->assertEquals($expected, $old);
47
 
48
		App::build(array('Model' => array('/path/to/models/')));
49
		$new = App::path('Model');
50
		$expected = array(
51
			'/path/to/models/',
52
			APP . 'Model' . DS
53
		);
54
		$this->assertEquals($expected, $new);
55
 
56
		App::build();
57
		App::build(array('Model' => array('/path/to/models/')), App::PREPEND);
58
		$new = App::path('Model');
59
		$expected = array(
60
			'/path/to/models/',
61
			APP . 'Model' . DS
62
		);
63
		$this->assertEquals($expected, $new);
64
 
65
		App::build();
66
		App::build(array('Model' => array('/path/to/models/')), App::APPEND);
67
		$new = App::path('Model');
68
		$expected = array(
69
			APP . 'Model' . DS,
70
			'/path/to/models/'
71
		);
72
		$this->assertEquals($expected, $new);
73
 
74
		App::build();
75
		App::build(array(
76
			'Model' => array('/path/to/models/'),
77
			'Controller' => array('/path/to/controllers/'),
78
		), App::APPEND);
79
		$new = App::path('Model');
80
		$expected = array(
81
			APP . 'Model' . DS,
82
			'/path/to/models/'
83
		);
84
		$this->assertEquals($expected, $new);
85
		$new = App::path('Controller');
86
		$expected = array(
87
			APP . 'Controller' . DS,
88
			'/path/to/controllers/'
89
		);
90
		$this->assertEquals($expected, $new);
91
 
92
		App::build(); //reset defaults
93
		$defaults = App::path('Model');
94
		$this->assertEquals($old, $defaults);
95
	}
96
 
97
/**
98
 * tests that it is possible to set up paths using the CakePHP 1.3 notation for them (models, behaviors, controllers...)
99
 *
100
 * @return void
101
 */
102
	public function testCompatibleBuild() {
103
		$old = App::path('models');
104
		$expected = array(
105
			APP . 'Model' . DS
106
		);
107
		$this->assertEquals($expected, $old);
108
 
109
		App::build(array('models' => array('/path/to/models/')));
110
 
111
		$new = App::path('models');
112
 
113
		$expected = array(
114
			'/path/to/models/',
115
			APP . 'Model' . DS
116
		);
117
		$this->assertEquals($expected, $new);
118
		$this->assertEquals($expected, App::path('Model'));
119
 
120
		App::build(array('datasources' => array('/path/to/datasources/')));
121
		$expected = array(
122
			'/path/to/datasources/',
123
			APP . 'Model' . DS . 'Datasource' . DS
124
		);
125
		$result = App::path('datasources');
126
		$this->assertEquals($expected, $result);
127
		$this->assertEquals($expected, App::path('Model/Datasource'));
128
 
129
		App::build(array('behaviors' => array('/path/to/behaviors/')));
130
		$expected = array(
131
			'/path/to/behaviors/',
132
			APP . 'Model' . DS . 'Behavior' . DS
133
		);
134
		$result = App::path('behaviors');
135
		$this->assertEquals($expected, $result);
136
		$this->assertEquals($expected, App::path('Model/Behavior'));
137
 
138
		App::build(array('controllers' => array('/path/to/controllers/')));
139
		$expected = array(
140
			'/path/to/controllers/',
141
			APP . 'Controller' . DS
142
		);
143
		$result = App::path('controllers');
144
		$this->assertEquals($expected, $result);
145
		$this->assertEquals($expected, App::path('Controller'));
146
 
147
		App::build(array('components' => array('/path/to/components/')));
148
		$expected = array(
149
			'/path/to/components/',
150
			APP . 'Controller' . DS . 'Component' . DS
151
		);
152
		$result = App::path('components');
153
		$this->assertEquals($expected, $result);
154
		$this->assertEquals($expected, App::path('Controller/Component'));
155
 
156
		App::build(array('views' => array('/path/to/views/')));
157
		$expected = array(
158
			'/path/to/views/',
159
			APP . 'View' . DS
160
		);
161
		$result = App::path('views');
162
		$this->assertEquals($expected, $result);
163
		$this->assertEquals($expected, App::path('View'));
164
 
165
		App::build(array('helpers' => array('/path/to/helpers/')));
166
		$expected = array(
167
			'/path/to/helpers/',
168
			APP . 'View' . DS . 'Helper' . DS
169
		);
170
		$result = App::path('helpers');
171
		$this->assertEquals($expected, $result);
172
		$this->assertEquals($expected, App::path('View/Helper'));
173
 
174
		App::build(array('shells' => array('/path/to/shells/')));
175
		$expected = array(
176
			'/path/to/shells/',
177
			APP . 'Console' . DS . 'Command' . DS
178
		);
179
		$result = App::path('shells');
180
		$this->assertEquals($expected, $result);
181
		$this->assertEquals($expected, App::path('Console/Command'));
182
 
183
		App::build(); //reset defaults
184
		$defaults = App::path('Model');
185
		$this->assertEquals($old, $defaults);
186
	}
187
 
188
/**
189
 * test package build() with App::REGISTER.
190
 *
191
 * @return void
192
 */
193
	public function testBuildPackage() {
194
		$pluginPaths = array(
195
			'/foo/bar',
196
			APP . 'Plugin' . DS,
197
			dirname(dirname(CAKE)) . DS . 'plugins' . DS
198
		);
199
		App::build(array(
200
			'Plugin' => array(
201
				'/foo/bar'
202
			)
203
		));
204
		$result = App::path('Plugin');
205
		$this->assertEquals($pluginPaths, $result);
206
 
207
		$paths = App::path('Service');
208
		$this->assertEquals(array(), $paths);
209
 
210
		App::build(array(
211
			'Service' => array(
212
				'%s' . 'Service' . DS,
213
			),
214
		), App::REGISTER);
215
 
216
		$expected = array(
217
			APP . 'Service' . DS,
218
		);
219
		$result = App::path('Service');
220
		$this->assertEquals($expected, $result);
221
 
222
		//Ensure new paths registered for other packages are not affected
223
		$result = App::path('Plugin');
224
		$this->assertEquals($pluginPaths, $result);
225
 
226
		App::build();
227
		$paths = App::path('Service');
228
		$this->assertEquals(array(), $paths);
229
	}
230
 
231
/**
232
 * test path() with a plugin.
233
 *
234
 * @return void
235
 */
236
	public function testPathWithPlugins() {
237
		$basepath = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
238
		App::build(array(
239
			'Plugin' => array($basepath),
240
		));
241
		CakePlugin::load('TestPlugin');
242
 
243
		$result = App::path('Vendor', 'TestPlugin');
244
		$this->assertEquals($basepath . 'TestPlugin' . DS . 'Vendor' . DS, $result[0]);
245
	}
246
 
247
/**
248
 * testBuildWithReset method
249
 *
250
 * @return void
251
 */
252
	public function testBuildWithReset() {
253
		$old = App::path('Model');
254
		$expected = array(
255
			APP . 'Model' . DS
256
		);
257
		$this->assertEquals($expected, $old);
258
 
259
		App::build(array('Model' => array('/path/to/models/')), App::RESET);
260
 
261
		$new = App::path('Model');
262
 
263
		$expected = array(
264
			'/path/to/models/'
265
		);
266
		$this->assertEquals($expected, $new);
267
 
268
		App::build(); //reset defaults
269
		$defaults = App::path('Model');
270
		$this->assertEquals($old, $defaults);
271
	}
272
 
273
/**
274
 * testCore method
275
 *
276
 * @return void
277
 */
278
	public function testCore() {
279
		$model = App::core('Model');
280
		$this->assertEquals(array(CAKE . 'Model' . DS), $model);
281
 
282
		$view = App::core('View');
283
		$this->assertEquals(array(CAKE . 'View' . DS), $view);
284
 
285
		$controller = App::core('Controller');
286
		$this->assertEquals(array(CAKE . 'Controller' . DS), $controller);
287
 
288
		$component = App::core('Controller/Component');
289
		$this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS), str_replace('/', DS, $component));
290
 
291
		$auth = App::core('Controller/Component/Auth');
292
		$this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS), str_replace('/', DS, $auth));
293
 
294
		$datasource = App::core('Model/Datasource');
295
		$this->assertEquals(array(CAKE . 'Model' . DS . 'Datasource' . DS), str_replace('/', DS, $datasource));
296
	}
297
 
298
/**
299
 * testListObjects method
300
 *
301
 * @return void
302
 */
303
	public function testListObjects() {
304
		$result = App::objects('class', CAKE . 'Routing', false);
305
		$this->assertTrue(in_array('Dispatcher', $result));
306
		$this->assertTrue(in_array('Router', $result));
307
 
308
		App::build(array(
309
			'Model/Behavior' => App::core('Model/Behavior'),
310
			'Controller' => App::core('Controller'),
311
			'Controller/Component' => App::core('Controller/Component'),
312
			'View' => App::core('View'),
313
			'Model' => App::core('Model'),
314
			'View/Helper' => App::core('View/Helper'),
315
		), App::RESET);
316
		$result = App::objects('behavior', null, false);
317
		$this->assertTrue(in_array('TreeBehavior', $result));
318
		$result = App::objects('Model/Behavior', null, false);
319
		$this->assertTrue(in_array('TreeBehavior', $result));
320
 
321
		$result = App::objects('component', null, false);
322
		$this->assertTrue(in_array('AuthComponent', $result));
323
		$result = App::objects('Controller/Component', null, false);
324
		$this->assertTrue(in_array('AuthComponent', $result));
325
 
326
		$result = App::objects('view', null, false);
327
		$this->assertTrue(in_array('MediaView', $result));
328
		$result = App::objects('View', null, false);
329
		$this->assertTrue(in_array('MediaView', $result));
330
 
331
		$result = App::objects('helper', null, false);
332
		$this->assertTrue(in_array('HtmlHelper', $result));
333
		$result = App::objects('View/Helper', null, false);
334
		$this->assertTrue(in_array('HtmlHelper', $result));
335
 
336
		$result = App::objects('model', null, false);
337
		$this->assertTrue(in_array('AcoAction', $result));
338
		$result = App::objects('Model', null, false);
339
		$this->assertTrue(in_array('AcoAction', $result));
340
 
341
		$result = App::objects('file');
342
		$this->assertFalse($result);
343
 
344
		$result = App::objects('file', 'non_existing_configure');
345
		$expected = array();
346
		$this->assertEquals($expected, $result);
347
 
348
		$result = App::objects('NonExistingType');
349
		$this->assertSame(array(), $result);
350
 
351
		App::build(array(
352
			'plugins' => array(
353
				CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS
354
			)
355
		));
356
		$result = App::objects('plugin', null, false);
357
		$this->assertTrue(in_array('Cache', $result));
358
		$this->assertTrue(in_array('Log', $result));
359
 
360
		App::build();
361
	}
362
 
363
/**
364
 * Make sure that .svn and friends are excluded from App::objects('plugin')
365
 */
366
	public function testListObjectsIgnoreDotDirectories() {
367
		$path = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
368
 
369
		$this->skipIf(!is_writable($path), $path . ' is not writable.');
370
 
371
		App::build(array(
372
			'plugins' => array($path)
373
		), App::RESET);
374
		mkdir($path . '.svn');
375
		$result = App::objects('plugin', null, false);
376
		rmdir($path . '.svn');
377
 
378
		$this->assertNotContains('.svn', $result);
379
	}
380
 
381
/**
382
 * Tests listing objects within a plugin
383
 *
384
 * @return void
385
 */
386
	public function testListObjectsInPlugin() {
387
		App::build(array(
388
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
389
			'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
390
		), App::RESET);
391
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
392
 
393
		$result = App::objects('TestPlugin.model');
394
		$this->assertTrue(in_array('TestPluginPost', $result));
395
		$result = App::objects('TestPlugin.Model');
396
		$this->assertTrue(in_array('TestPluginPost', $result));
397
 
398
		$result = App::objects('TestPlugin.behavior');
399
		$this->assertTrue(in_array('TestPluginPersisterOneBehavior', $result));
400
		$result = App::objects('TestPlugin.Model/Behavior');
401
		$this->assertTrue(in_array('TestPluginPersisterOneBehavior', $result));
402
 
403
		$result = App::objects('TestPlugin.helper');
404
		$expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
405
		$this->assertEquals($expected, $result);
406
		$result = App::objects('TestPlugin.View/Helper');
407
		$expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
408
		$this->assertEquals($expected, $result);
409
 
410
		$result = App::objects('TestPlugin.component');
411
		$this->assertTrue(in_array('OtherComponent', $result));
412
		$result = App::objects('TestPlugin.Controller/Component');
413
		$this->assertTrue(in_array('OtherComponent', $result));
414
 
415
		$result = App::objects('TestPluginTwo.behavior');
416
		$this->assertSame(array(), $result);
417
		$result = App::objects('TestPluginTwo.Model/Behavior');
418
		$this->assertSame(array(), $result);
419
 
420
		$result = App::objects('model', null, false);
421
		$this->assertTrue(in_array('Comment', $result));
422
		$this->assertTrue(in_array('Post', $result));
423
 
424
		$result = App::objects('Model', null, false);
425
		$this->assertTrue(in_array('Comment', $result));
426
		$this->assertTrue(in_array('Post', $result));
427
 
428
		App::build();
429
	}
430
 
431
/**
432
 * test that pluginPath can find paths for plugins.
433
 *
434
 * @return void
435
 */
436
	public function testPluginPath() {
437
		App::build(array(
438
			'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
439
		));
440
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
441
 
442
		$path = App::pluginPath('TestPlugin');
443
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
444
		$this->assertEquals($expected, $path);
445
 
446
		$path = App::pluginPath('TestPluginTwo');
447
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS;
448
		$this->assertEquals($expected, $path);
449
		App::build();
450
	}
451
 
452
/**
453
 * test that themePath can find paths for themes.
454
 *
455
 * @return void
456
 */
457
	public function testThemePath() {
458
		App::build(array(
459
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
460
		));
461
		$path = App::themePath('test_theme');
462
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
463
		$this->assertEquals($expected, $path);
464
 
465
		$path = App::themePath('TestTheme');
466
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
467
		$this->assertEquals($expected, $path);
468
 
469
		App::build();
470
	}
471
 
472
/**
473
 * testClassLoading method
474
 *
475
 * @return void
476
 */
477
	public function testClassLoading() {
478
		$file = App::import('Model', 'Model', false);
479
		$this->assertTrue($file);
480
		$this->assertTrue(class_exists('Model'));
481
 
482
		$file = App::import('Controller', 'Controller', false);
483
		$this->assertTrue($file);
484
		$this->assertTrue(class_exists('Controller'));
485
 
486
		$file = App::import('Component', 'Auth', false);
487
		$this->assertTrue($file);
488
		$this->assertTrue(class_exists('AuthComponent'));
489
 
490
		$file = App::import('Shell', 'Shell', false);
491
		$this->assertTrue($file);
492
		$this->assertTrue(class_exists('Shell'));
493
 
494
		$file = App::import('Configure', 'PhpReader');
495
		$this->assertTrue($file);
496
		$this->assertTrue(class_exists('PhpReader'));
497
 
498
		$file = App::import('Model', 'SomeRandomModelThatDoesNotExist', false);
499
		$this->assertFalse($file);
500
 
501
		$file = App::import('Model', 'AppModel', false);
502
		$this->assertTrue($file);
503
		$this->assertTrue(class_exists('AppModel'));
504
 
505
		$file = App::import('WrongType', null, true, array(), '');
506
		$this->assertFalse($file);
507
 
508
		$file = App::import('Model', 'NonExistingPlugin.NonExistingModel', false);
509
		$this->assertFalse($file);
510
 
511
		$file = App::import('Model', array('NonExistingPlugin.NonExistingModel'), false);
512
		$this->assertFalse($file);
513
 
514
		if (!class_exists('AppController', false)) {
515
			$classes = array_flip(get_declared_classes());
516
 
517
			$this->assertFalse(isset($classes['PagesController']));
518
			$this->assertFalse(isset($classes['AppController']));
519
 
520
			$file = App::import('Controller', 'Pages');
521
			$this->assertTrue($file);
522
			$this->assertTrue(class_exists('PagesController'));
523
 
524
			$classes = array_flip(get_declared_classes());
525
 
526
			$this->assertTrue(isset($classes['PagesController']));
527
			$this->assertTrue(isset($classes['AppController']));
528
 
529
			$file = App::import('Behavior', 'Containable');
530
			$this->assertTrue($file);
531
			$this->assertTrue(class_exists('ContainableBehavior'));
532
 
533
			$file = App::import('Component', 'RequestHandler');
534
			$this->assertTrue($file);
535
			$this->assertTrue(class_exists('RequestHandlerComponent'));
536
 
537
			$file = App::import('Helper', 'Form');
538
			$this->assertTrue($file);
539
			$this->assertTrue(class_exists('FormHelper'));
540
 
541
			$file = App::import('Model', 'NonExistingModel');
542
			$this->assertFalse($file);
543
 
544
			$file = App::import('Datasource', 'DboSource');
545
			$this->assertTrue($file);
546
			$this->assertTrue(class_exists('DboSource'));
547
		}
548
		App::build();
549
	}
550
 
551
/**
552
 * test import() with plugins
553
 *
554
 * @return void
555
 */
556
	public function testPluginImporting() {
557
		App::build(array(
558
			'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
559
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
560
		));
561
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
562
 
563
		$result = App::import('Controller', 'TestPlugin.Tests');
564
		$this->assertTrue($result);
565
		$this->assertTrue(class_exists('TestPluginAppController'));
566
		$this->assertTrue(class_exists('TestsController'));
567
 
568
		$result = App::import('Lib', 'TestPlugin.TestPluginLibrary');
569
		$this->assertTrue($result);
570
		$this->assertTrue(class_exists('TestPluginLibrary'));
571
 
572
		$result = App::import('Lib', 'Library');
573
		$this->assertTrue($result);
574
		$this->assertTrue(class_exists('Library'));
575
 
576
		$result = App::import('Helper', 'TestPlugin.OtherHelper');
577
		$this->assertTrue($result);
578
		$this->assertTrue(class_exists('OtherHelperHelper'));
579
 
580
		$result = App::import('Helper', 'TestPlugin.TestPluginApp');
581
		$this->assertTrue($result);
582
		$this->assertTrue(class_exists('TestPluginAppHelper'));
583
 
584
		$result = App::import('Datasource', 'TestPlugin.TestSource');
585
		$this->assertTrue($result);
586
		$this->assertTrue(class_exists('TestSource'));
587
 
588
		App::uses('ExampleExample', 'TestPlugin.Vendor/Example');
589
		$this->assertTrue(class_exists('ExampleExample'));
590
 
591
		App::build();
592
	}
593
 
594
/**
595
 * test that building helper paths actually works.
596
 *
597
 * @return void
598
 * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/410
599
 */
600
	public function testImportingHelpersFromAlternatePaths() {
601
		$this->assertFalse(class_exists('BananaHelper', false), 'BananaHelper exists, cannot test importing it.');
602
		App::build(array(
603
			'View/Helper' => array(
604
				CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Helper' . DS
605
			)
606
		));
607
		$this->assertFalse(class_exists('BananaHelper', false), 'BananaHelper exists, cannot test importing it.');
608
		App::import('Helper', 'Banana');
609
		$this->assertTrue(class_exists('BananaHelper', false), 'BananaHelper was not loaded.');
610
 
611
		App::build();
612
	}
613
 
614
/**
615
 * testFileLoading method
616
 *
617
 * @return void
618
 */
619
	public function testFileLoading() {
620
		$file = App::import('File', 'RealFile', false, array(), CAKE . 'Config' . DS . 'config.php');
621
		$this->assertTrue($file);
622
 
623
		$file = App::import('File', 'NoFile', false, array(), CAKE . 'Config' . DS . 'cake' . DS . 'config.php');
624
		$this->assertFalse($file);
625
	}
626
 
627
/**
628
 * testFileLoadingWithArray method
629
 *
630
 * @return void
631
 */
632
	public function testFileLoadingWithArray() {
633
		$type = array(
634
			'type' => 'File',
635
			'name' => 'SomeName',
636
			'parent' => false,
637
			'file' => CAKE . DS . 'Config' . DS . 'config.php'
638
		);
639
		$file = App::import($type);
640
		$this->assertTrue($file);
641
 
642
		$type = array(
643
			'type' => 'File',
644
			'name' => 'NoFile',
645
			'parent' => false,
646
			'file' => CAKE . 'Config' . DS . 'cake' . DS . 'config.php'
647
		);
648
		$file = App::import($type);
649
		$this->assertFalse($file);
650
	}
651
 
652
/**
653
 * testFileLoadingReturnValue method
654
 *
655
 * @return void
656
 */
657
	public function testFileLoadingReturnValue() {
658
		$file = App::import('File', 'Name', false, array(), CAKE . 'Config' . DS . 'config.php', true);
659
		$this->assertTrue(!empty($file));
660
 
661
		$this->assertTrue(isset($file['Cake.version']));
662
 
663
		$type = array(
664
			'type' => 'File',
665
			'name' => 'OtherName',
666
			'parent' => false,
667
			'file' => CAKE . 'Config' . DS . 'config.php', 'return' => true
668
		);
669
		$file = App::import($type);
670
		$this->assertTrue(!empty($file));
671
 
672
		$this->assertTrue(isset($file['Cake.version']));
673
	}
674
 
675
/**
676
 * testLoadingWithSearch method
677
 *
678
 * @return void
679
 */
680
	public function testLoadingWithSearch() {
681
		$file = App::import('File', 'NewName', false, array(CAKE . 'Config' . DS), 'config.php');
682
		$this->assertTrue($file);
683
 
684
		$file = App::import('File', 'AnotherNewName', false, array(CAKE), 'config.php');
685
		$this->assertFalse($file);
686
	}
687
 
688
/**
689
 * testLoadingWithSearchArray method
690
 *
691
 * @return void
692
 */
693
	public function testLoadingWithSearchArray() {
694
		$type = array(
695
			'type' => 'File',
696
			'name' => 'RandomName',
697
			'parent' => false,
698
			'file' => 'config.php',
699
			'search' => array(CAKE . 'Config' . DS)
700
		);
701
		$file = App::import($type);
702
		$this->assertTrue($file);
703
 
704
		$type = array(
705
			'type' => 'File',
706
			'name' => 'AnotherRandomName',
707
			'parent' => false,
708
			'file' => 'config.php',
709
			'search' => array(CAKE)
710
		);
711
		$file = App::import($type);
712
		$this->assertFalse($file);
713
	}
714
 
715
/**
716
 * testMultipleLoading method
717
 *
718
 * @return void
719
 */
720
	public function testMultipleLoading() {
721
		if (class_exists('PersisterOne', false) || class_exists('PersisterTwo', false)) {
722
			$this->markTestSkipped('Cannot test loading of classes that exist.');
723
		}
724
		App::build(array(
725
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS)
726
		));
727
		$toLoad = array('PersisterOne', 'PersisterTwo');
728
		$load = App::import('Model', $toLoad);
729
		$this->assertTrue($load);
730
 
731
		$classes = array_flip(get_declared_classes());
732
 
733
		$this->assertTrue(isset($classes['PersisterOne']));
734
		$this->assertTrue(isset($classes['PersisterTwo']));
735
 
736
		$load = App::import('Model', array('PersisterOne', 'SomeNotFoundClass', 'PersisterTwo'));
737
		$this->assertFalse($load);
738
	}
739
 
740
	public function testLoadingVendor() {
741
		App::build(array(
742
			'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
743
			'vendors' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS),
744
		), App::RESET);
745
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
746
 
747
		ob_start();
748
		$result = App::import('Vendor', 'css/TestAsset', array('ext' => 'css'));
749
		$text = ob_get_clean();
750
		$this->assertTrue($result);
751
		$this->assertEquals('/* this is the test asset css file */', trim($text));
752
 
753
		$result = App::import('Vendor', 'TestPlugin.sample/SamplePlugin');
754
		$this->assertTrue($result);
755
		$this->assertTrue(class_exists('SamplePluginClassTestName'));
756
 
757
		$result = App::import('Vendor', 'sample/ConfigureTestVendorSample');
758
		$this->assertTrue($result);
759
		$this->assertTrue(class_exists('ConfigureTestVendorSample'));
760
 
761
		ob_start();
762
		$result = App::import('Vendor', 'SomeNameInSubfolder', array('file' => 'somename/some.name.php'));
763
		$text = ob_get_clean();
764
		$this->assertTrue($result);
765
		$this->assertEquals('This is a file with dot in file name', $text);
766
 
767
		ob_start();
768
		$result = App::import('Vendor', 'TestHello', array('file' => 'Test' . DS . 'hello.php'));
769
		$text = ob_get_clean();
770
		$this->assertTrue($result);
771
		$this->assertEquals('This is the hello.php file in Test directory', $text);
772
 
773
		ob_start();
774
		$result = App::import('Vendor', 'MyTest', array('file' => 'Test' . DS . 'MyTest.php'));
775
		$text = ob_get_clean();
776
		$this->assertTrue($result);
777
		$this->assertEquals('This is the MyTest.php file', $text);
778
 
779
		ob_start();
780
		$result = App::import('Vendor', 'Welcome');
781
		$text = ob_get_clean();
782
		$this->assertTrue($result);
783
		$this->assertEquals('This is the welcome.php file in vendors directory', $text);
784
 
785
		ob_start();
786
		$result = App::import('Vendor', 'TestPlugin.Welcome');
787
		$text = ob_get_clean();
788
		$this->assertTrue($result);
789
		$this->assertEquals('This is the welcome.php file in test_plugin/vendors directory', $text);
790
	}
791
 
792
/**
793
 * Tests that the automatic class loader will also find in "libs" folder for both
794
 * app and plugins if it does not find the class in other configured paths
795
 *
796
 */
797
	public function testLoadClassInLibs() {
798
		App::build(array(
799
			'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
800
			'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
801
		), App::RESET);
802
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
803
 
804
		$this->assertFalse(class_exists('CustomLibClass', false));
805
		App::uses('CustomLibClass', 'TestPlugin.Custom/Package');
806
		$this->assertTrue(class_exists('CustomLibClass'));
807
 
808
		$this->assertFalse(class_exists('TestUtilityClass', false));
809
		App::uses('TestUtilityClass', 'Utility');
810
		$this->assertTrue(class_exists('TestUtilityClass'));
811
	}
812
 
813
/**
814
 * Tests that App::location() returns the defined path for a class
815
 *
816
 * @return void
817
 */
818
	public function testClassLocation() {
819
		App::uses('MyCustomClass', 'MyPackage/Name');
820
		$this->assertEquals('MyPackage/Name', App::location('MyCustomClass'));
821
	}
822
 
823
/**
824
 * Test that paths() works.
825
 *
826
 * @return void
827
 */
828
	public function testPaths() {
829
		$result = App::paths();
830
		$this->assertArrayHasKey('Plugin', $result);
831
		$this->assertArrayHasKey('Controller', $result);
832
		$this->assertArrayHasKey('Controller/Component', $result);
833
	}
834
 
835
/**
836
 * Proves that it is possible to load plugin libraries in top
837
 * level Lib dir for plugins
838
 *
839
 * @return void
840
 */
841
	public function testPluginLibClasses() {
842
		App::build(array(
843
			'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
844
		), App::RESET);
845
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
846
		$this->assertFalse(class_exists('TestPluginOtherLibrary', false));
847
		App::uses('TestPluginOtherLibrary', 'TestPlugin.Lib');
848
		$this->assertTrue(class_exists('TestPluginOtherLibrary'));
849
	}
850
}