Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * BasicsTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
require_once CAKE . 'basics.php';
20
 
21
App::uses('Folder', 'Utility');
22
App::uses('CakeResponse', 'Network');
23
 
24
/**
25
 * BasicsTest class
26
 *
27
 * @package       Cake.Test.Case
28
 */
29
class BasicsTest extends CakeTestCase {
30
 
31
/**
32
 * setUp method
33
 *
34
 * @return void
35
 */
36
	public function setUp() {
37
		parent::setUp();
38
		App::build(array(
39
			'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
40
		));
41
	}
42
 
43
/**
44
 * test the array_diff_key compatibility function.
45
 *
46
 * @return void
47
 */
48
	public function testArrayDiffKey() {
49
		$one = array('one' => 1, 'two' => 2, 'three' => 3);
50
		$two = array('one' => 'one', 'two' => 'two');
51
		$result = array_diff_key($one, $two);
52
		$expected = array('three' => 3);
53
		$this->assertEquals($expected, $result);
54
 
55
		$one = array('one' => array('value', 'value-two'), 'two' => 2, 'three' => 3);
56
		$two = array('two' => 'two');
57
		$result = array_diff_key($one, $two);
58
		$expected = array('one' => array('value', 'value-two'), 'three' => 3);
59
		$this->assertEquals($expected, $result);
60
 
61
		$one = array('one' => null, 'two' => 2, 'three' => '', 'four' => 0);
62
		$two = array('two' => 'two');
63
		$result = array_diff_key($one, $two);
64
		$expected = array('one' => null, 'three' => '', 'four' => 0);
65
		$this->assertEquals($expected, $result);
66
 
67
		$one = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
68
		$two = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
69
		$result = array_diff_key($one, $two);
70
		$this->assertSame(array(), $result);
71
	}
72
 
73
/**
74
 * testHttpBase method
75
 *
76
 * @return void
77
 */
78
	public function testEnv() {
79
		$this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
80
 
81
		$server = $_SERVER;
82
		$env = $_ENV;
83
 
84
		$_SERVER['HTTP_HOST'] = 'localhost';
85
		$this->assertEquals(env('HTTP_BASE'), '.localhost');
86
 
87
		$_SERVER['HTTP_HOST'] = 'com.ar';
88
		$this->assertEquals(env('HTTP_BASE'), '.com.ar');
89
 
90
		$_SERVER['HTTP_HOST'] = 'example.ar';
91
		$this->assertEquals(env('HTTP_BASE'), '.example.ar');
92
 
93
		$_SERVER['HTTP_HOST'] = 'example.com';
94
		$this->assertEquals(env('HTTP_BASE'), '.example.com');
95
 
96
		$_SERVER['HTTP_HOST'] = 'www.example.com';
97
		$this->assertEquals(env('HTTP_BASE'), '.example.com');
98
 
99
		$_SERVER['HTTP_HOST'] = 'subdomain.example.com';
100
		$this->assertEquals(env('HTTP_BASE'), '.example.com');
101
 
102
		$_SERVER['HTTP_HOST'] = 'example.com.ar';
103
		$this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
104
 
105
		$_SERVER['HTTP_HOST'] = 'www.example.com.ar';
106
		$this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
107
 
108
		$_SERVER['HTTP_HOST'] = 'subdomain.example.com.ar';
109
		$this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
110
 
111
		$_SERVER['HTTP_HOST'] = 'double.subdomain.example.com';
112
		$this->assertEquals(env('HTTP_BASE'), '.subdomain.example.com');
113
 
114
		$_SERVER['HTTP_HOST'] = 'double.subdomain.example.com.ar';
115
		$this->assertEquals(env('HTTP_BASE'), '.subdomain.example.com.ar');
116
 
117
		$_SERVER = $_ENV = array();
118
 
119
		$_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
120
		$this->assertEquals(env('SCRIPT_NAME'), '/a/test/test.php');
121
 
122
		$_SERVER = $_ENV = array();
123
 
124
		$_ENV['CGI_MODE'] = 'BINARY';
125
		$_ENV['SCRIPT_URL'] = '/a/test/test.php';
126
		$this->assertEquals(env('SCRIPT_NAME'), '/a/test/test.php');
127
 
128
		$_SERVER = $_ENV = array();
129
 
130
		$this->assertFalse(env('HTTPS'));
131
 
132
		$_SERVER['HTTPS'] = 'on';
133
		$this->assertTrue(env('HTTPS'));
134
 
135
		$_SERVER['HTTPS'] = '1';
136
		$this->assertTrue(env('HTTPS'));
137
 
138
		$_SERVER['HTTPS'] = 'I am not empty';
139
		$this->assertTrue(env('HTTPS'));
140
 
141
		$_SERVER['HTTPS'] = 1;
142
		$this->assertTrue(env('HTTPS'));
143
 
144
		$_SERVER['HTTPS'] = 'off';
145
		$this->assertFalse(env('HTTPS'));
146
 
147
		$_SERVER['HTTPS'] = false;
148
		$this->assertFalse(env('HTTPS'));
149
 
150
		$_SERVER['HTTPS'] = '';
151
		$this->assertFalse(env('HTTPS'));
152
 
153
		$_SERVER = array();
154
 
155
		$_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
156
		$this->assertTrue(env('HTTPS'));
157
 
158
		$_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
159
		$this->assertFalse(env('HTTPS'));
160
 
161
		$_SERVER = $_ENV = array();
162
 
163
		$this->assertNull(env('TEST_ME'));
164
 
165
		$_ENV['TEST_ME'] = 'a';
166
		$this->assertEquals(env('TEST_ME'), 'a');
167
 
168
		$_SERVER['TEST_ME'] = 'b';
169
		$this->assertEquals(env('TEST_ME'), 'b');
170
 
171
		unset($_ENV['TEST_ME']);
172
		$this->assertEquals(env('TEST_ME'), 'b');
173
 
174
		$_SERVER = $server;
175
		$_ENV = $env;
176
	}
177
 
178
/**
179
 * Test h()
180
 *
181
 * @return void
182
 */
183
	public function testH() {
184
		$string = '<foo>';
185
		$result = h($string);
186
		$this->assertEquals('&lt;foo&gt;', $result);
187
 
188
		$in = array('this & that', '<p>Which one</p>');
189
		$result = h($in);
190
		$expected = array('this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;');
191
		$this->assertEquals($expected, $result);
192
 
193
		$string = '<foo> & &nbsp;';
194
		$result = h($string);
195
		$this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
196
 
197
		$string = '<foo> & &nbsp;';
198
		$result = h($string, false);
199
		$this->assertEquals('&lt;foo&gt; &amp; &nbsp;', $result);
200
 
201
		$string = '<foo> & &nbsp;';
202
		$result = h($string, 'UTF-8');
203
		$this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
204
 
205
		$arr = array('<foo>', '&nbsp;');
206
		$result = h($arr);
207
		$expected = array(
208
			'&lt;foo&gt;',
209
			'&amp;nbsp;'
210
		);
211
		$this->assertEquals($expected, $result);
212
 
213
		$arr = array('<foo>', '&nbsp;');
214
		$result = h($arr, false);
215
		$expected = array(
216
			'&lt;foo&gt;',
217
			'&nbsp;'
218
		);
219
		$this->assertEquals($expected, $result);
220
 
221
		$arr = array('f' => '<foo>', 'n' => '&nbsp;');
222
		$result = h($arr, false);
223
		$expected = array(
224
			'f' => '&lt;foo&gt;',
225
			'n' => '&nbsp;'
226
		);
227
		$this->assertEquals($expected, $result);
228
 
229
		// Test that boolean values are not converted to strings
230
		$result = h(false);
231
		$this->assertFalse($result);
232
 
233
		$arr = array('foo' => false, 'bar' => true);
234
		$result = h($arr);
235
		$this->assertFalse($result['foo']);
236
		$this->assertTrue($result['bar']);
237
 
238
		$obj = new stdClass();
239
		$result = h($obj);
240
		$this->assertEquals('(object)stdClass', $result);
241
 
242
		$obj = new CakeResponse(array('body' => 'Body content'));
243
		$result = h($obj);
244
		$this->assertEquals('Body content', $result);
245
	}
246
 
247
/**
248
 * Test am()
249
 *
250
 * @return void
251
 */
252
	public function testAm() {
253
		$result = am(array('one', 'two'), 2, 3, 4);
254
		$expected = array('one', 'two', 2, 3, 4);
255
		$this->assertEquals($expected, $result);
256
 
257
		$result = am(array('one' => array(2, 3), 'two' => array('foo')), array('one' => array(4, 5)));
258
		$expected = array('one' => array(4, 5), 'two' => array('foo'));
259
		$this->assertEquals($expected, $result);
260
	}
261
 
262
/**
263
 * test cache()
264
 *
265
 * @return void
266
 */
267
	public function testCache() {
268
		$_cacheDisable = Configure::read('Cache.disable');
269
		$this->skipIf($_cacheDisable, 'Cache is disabled, skipping cache() tests.');
270
 
271
		Configure::write('Cache.disable', true);
272
		$result = cache('basics_test', 'simple cache write');
273
		$this->assertNull($result);
274
 
275
		$result = cache('basics_test');
276
		$this->assertNull($result);
277
 
278
		Configure::write('Cache.disable', false);
279
		$result = cache('basics_test', 'simple cache write');
280
		$this->assertTrue((bool)$result);
281
		$this->assertTrue(file_exists(CACHE . 'basics_test'));
282
 
283
		$result = cache('basics_test');
284
		$this->assertEquals('simple cache write', $result);
285
		if (file_exists(CACHE . 'basics_test')) {
286
			unlink(CACHE . 'basics_test');
287
		}
288
 
289
		cache('basics_test', 'expired', '+1 second');
290
		sleep(2);
291
		$result = cache('basics_test', null, '+1 second');
292
		$this->assertNull($result);
293
 
294
		Configure::write('Cache.disable', $_cacheDisable);
295
	}
296
 
297
/**
298
 * test clearCache()
299
 *
300
 * @return void
301
 */
302
	public function testClearCache() {
303
		$cacheOff = Configure::read('Cache.disable');
304
		$this->skipIf($cacheOff, 'Cache is disabled, skipping clearCache() tests.');
305
 
306
		cache('views' . DS . 'basics_test.cache', 'simple cache write');
307
		$this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
308
 
309
		cache('views' . DS . 'basics_test_2.cache', 'simple cache write 2');
310
		$this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_2.cache'));
311
 
312
		cache('views' . DS . 'basics_test_3.cache', 'simple cache write 3');
313
		$this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
314
 
315
		$result = clearCache(array('basics_test', 'basics_test_2'), 'views', '.cache');
316
		$this->assertTrue($result);
317
		$this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
318
		$this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
319
		$this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
320
 
321
		$result = clearCache(null, 'views', '.cache');
322
		$this->assertTrue($result);
323
		$this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
324
 
325
		// Different path from views and with prefix
326
		cache('models' . DS . 'basics_test.cache', 'simple cache write');
327
		$this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
328
 
329
		cache('models' . DS . 'basics_test_2.cache', 'simple cache write 2');
330
		$this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
331
 
332
		cache('models' . DS . 'basics_test_3.cache', 'simple cache write 3');
333
		$this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
334
 
335
		$result = clearCache('basics', 'models', '.cache');
336
		$this->assertTrue($result);
337
		$this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
338
		$this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
339
		$this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
340
 
341
		// checking if empty files were not removed
342
		$emptyExists = file_exists(CACHE . 'views' . DS . 'empty');
343
		if (!$emptyExists) {
344
			cache('views' . DS . 'empty', '');
345
		}
346
		cache('views' . DS . 'basics_test.php', 'simple cache write');
347
		$this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test.php'));
348
		$this->assertTrue(file_exists(CACHE . 'views' . DS . 'empty'));
349
 
350
		$result = clearCache();
351
		$this->assertTrue($result);
352
		$this->assertTrue(file_exists(CACHE . 'views' . DS . 'empty'));
353
		$this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.php'));
354
		if (!$emptyExists) {
355
			unlink(CACHE . 'views' . DS . 'empty');
356
		}
357
	}
358
 
359
/**
360
 * test __()
361
 *
362
 * @return void
363
 */
364
	public function testTranslate() {
365
		Configure::write('Config.language', 'rule_1_po');
366
 
367
		$result = __('Plural Rule 1');
368
		$expected = 'Plural Rule 1 (translated)';
369
		$this->assertEquals($expected, $result);
370
 
371
		$result = __('Plural Rule 1 (from core)');
372
		$expected = 'Plural Rule 1 (from core translated)';
373
		$this->assertEquals($expected, $result);
374
 
375
		$result = __('Some string with %s', 'arguments');
376
		$expected = 'Some string with arguments';
377
		$this->assertEquals($expected, $result);
378
 
379
		$result = __('Some string with %s %s', 'multiple', 'arguments');
380
		$expected = 'Some string with multiple arguments';
381
		$this->assertEquals($expected, $result);
382
 
383
		$result = __('Some string with %s %s', array('multiple', 'arguments'));
384
		$expected = 'Some string with multiple arguments';
385
		$this->assertEquals($expected, $result);
386
 
387
		$result = __('Testing %2$s %1$s', 'order', 'different');
388
		$expected = 'Testing different order';
389
		$this->assertEquals($expected, $result);
390
 
391
		$result = __('Testing %2$s %1$s', array('order', 'different'));
392
		$expected = 'Testing different order';
393
		$this->assertEquals($expected, $result);
394
 
395
		$result = __('Testing %.2f number', 1.2345);
396
		$expected = 'Testing 1.23 number';
397
		$this->assertEquals($expected, $result);
398
	}
399
 
400
/**
401
 * test __n()
402
 *
403
 * @return void
404
 */
405
	public function testTranslatePlural() {
406
		Configure::write('Config.language', 'rule_1_po');
407
 
408
		$result = __n('%d = 1', '%d = 0 or > 1', 0);
409
		$expected = '%d = 0 or > 1 (translated)';
410
		$this->assertEquals($expected, $result);
411
 
412
		$result = __n('%d = 1', '%d = 0 or > 1', 1);
413
		$expected = '%d = 1 (translated)';
414
		$this->assertEquals($expected, $result);
415
 
416
		$result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
417
		$expected = '%d = 0 or > 1 (from core translated)';
418
		$this->assertEquals($expected, $result);
419
 
420
		$result = __n('%d item.', '%d items.', 1, 1);
421
		$expected = '1 item.';
422
		$this->assertEquals($expected, $result);
423
 
424
		$result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
425
		$expected = '2 items for id 1234';
426
		$this->assertEquals($expected, $result);
427
 
428
		$result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
429
		$expected = '2 items for id 1234';
430
		$this->assertEquals($expected, $result);
431
	}
432
 
433
/**
434
 * test __d()
435
 *
436
 * @return void
437
 */
438
	public function testTranslateDomain() {
439
		Configure::write('Config.language', 'rule_1_po');
440
 
441
		$result = __d('default', 'Plural Rule 1');
442
		$expected = 'Plural Rule 1 (translated)';
443
		$this->assertEquals($expected, $result);
444
 
445
		$result = __d('core', 'Plural Rule 1');
446
		$expected = 'Plural Rule 1';
447
		$this->assertEquals($expected, $result);
448
 
449
		$result = __d('core', 'Plural Rule 1 (from core)');
450
		$expected = 'Plural Rule 1 (from core translated)';
451
		$this->assertEquals($expected, $result);
452
 
453
		$result = __d('core', 'Some string with %s', 'arguments');
454
		$expected = 'Some string with arguments';
455
		$this->assertEquals($expected, $result);
456
 
457
		$result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
458
		$expected = 'Some string with multiple arguments';
459
		$this->assertEquals($expected, $result);
460
 
461
		$result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
462
		$expected = 'Some string with multiple arguments';
463
		$this->assertEquals($expected, $result);
464
	}
465
 
466
/**
467
 * test __dn()
468
 *
469
 * @return void
470
 */
471
	public function testTranslateDomainPlural() {
472
		Configure::write('Config.language', 'rule_1_po');
473
 
474
		$result = __dn('default', '%d = 1', '%d = 0 or > 1', 0);
475
		$expected = '%d = 0 or > 1 (translated)';
476
		$this->assertEquals($expected, $result);
477
 
478
		$result = __dn('core', '%d = 1', '%d = 0 or > 1', 0);
479
		$expected = '%d = 0 or > 1';
480
		$this->assertEquals($expected, $result);
481
 
482
		$result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0);
483
		$expected = '%d = 0 or > 1 (from core translated)';
484
		$this->assertEquals($expected, $result);
485
 
486
		$result = __dn('default', '%d = 1', '%d = 0 or > 1', 1);
487
		$expected = '%d = 1 (translated)';
488
		$this->assertEquals($expected, $result);
489
 
490
		$result = __dn('core', '%d item.', '%d items.', 1, 1);
491
		$expected = '1 item.';
492
		$this->assertEquals($expected, $result);
493
 
494
		$result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
495
		$expected = '2 items for id 1234';
496
		$this->assertEquals($expected, $result);
497
 
498
		$result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
499
		$expected = '2 items for id 1234';
500
		$this->assertEquals($expected, $result);
501
	}
502
 
503
/**
504
 * test __c()
505
 *
506
 * @return void
507
 */
508
	public function testTranslateCategory() {
509
		Configure::write('Config.language', 'rule_1_po');
510
 
511
		$result = __c('Plural Rule 1', 6);
512
		$expected = 'Plural Rule 1 (translated)';
513
		$this->assertEquals($expected, $result);
514
 
515
		$result = __c('Plural Rule 1 (from core)', 6);
516
		$expected = 'Plural Rule 1 (from core translated)';
517
		$this->assertEquals($expected, $result);
518
 
519
		$result = __c('Some string with %s', 6, 'arguments');
520
		$expected = 'Some string with arguments';
521
		$this->assertEquals($expected, $result);
522
 
523
		$result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
524
		$expected = 'Some string with multiple arguments';
525
		$this->assertEquals($expected, $result);
526
 
527
		$result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
528
		$expected = 'Some string with multiple arguments';
529
		$this->assertEquals($expected, $result);
530
	}
531
 
532
/**
533
 * test __dc()
534
 *
535
 * @return void
536
 */
537
	public function testTranslateDomainCategory() {
538
		Configure::write('Config.language', 'rule_1_po');
539
 
540
		$result = __dc('default', 'Plural Rule 1', 6);
541
		$expected = 'Plural Rule 1 (translated)';
542
		$this->assertEquals($expected, $result);
543
 
544
		$result = __dc('default', 'Plural Rule 1 (from core)', 6);
545
		$expected = 'Plural Rule 1 (from core translated)';
546
		$this->assertEquals($expected, $result);
547
 
548
		$result = __dc('core', 'Plural Rule 1', 6);
549
		$expected = 'Plural Rule 1';
550
		$this->assertEquals($expected, $result);
551
 
552
		$result = __dc('core', 'Plural Rule 1 (from core)', 6);
553
		$expected = 'Plural Rule 1 (from core translated)';
554
		$this->assertEquals($expected, $result);
555
 
556
		$result = __dc('core', 'Some string with %s', 6, 'arguments');
557
		$expected = 'Some string with arguments';
558
		$this->assertEquals($expected, $result);
559
 
560
		$result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
561
		$expected = 'Some string with multiple arguments';
562
		$this->assertEquals($expected, $result);
563
 
564
		$result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
565
		$expected = 'Some string with multiple arguments';
566
		$this->assertEquals($expected, $result);
567
	}
568
 
569
/**
570
 * test __dcn()
571
 *
572
 * @return void
573
 */
574
	public function testTranslateDomainCategoryPlural() {
575
		Configure::write('Config.language', 'rule_1_po');
576
 
577
		$result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 6);
578
		$expected = '%d = 0 or > 1 (translated)';
579
		$this->assertEquals($expected, $result);
580
 
581
		$result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6);
582
		$expected = '%d = 1 (from core translated)';
583
		$this->assertEquals($expected, $result);
584
 
585
		$result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
586
		$expected = '%d = 0 or > 1';
587
		$this->assertEquals($expected, $result);
588
 
589
		$result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
590
		$expected = '1 item.';
591
		$this->assertEquals($expected, $result);
592
 
593
		$result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
594
		$expected = '2 items for id 1234';
595
		$this->assertEquals($expected, $result);
596
 
597
		$result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
598
		$expected = '2 items for id 1234';
599
		$this->assertEquals($expected, $result);
600
	}
601
 
602
/**
603
 * test LogError()
604
 *
605
 * @return void
606
 */
607
	public function testLogError() {
608
		if (file_exists(LOGS . 'error.log')) {
609
			unlink(LOGS . 'error.log');
610
		}
611
 
612
		// disable stderr output for this test
613
		if (CakeLog::stream('stderr')) {
614
			CakeLog::disable('stderr');
615
		}
616
 
617
		LogError('Testing LogError() basic function');
618
		LogError("Testing with\nmulti-line\nstring");
619
 
620
		if (CakeLog::stream('stderr')) {
621
			CakeLog::enable('stderr');
622
		}
623
 
624
		$result = file_get_contents(LOGS . 'error.log');
625
		$this->assertRegExp('/Error: Testing LogError\(\) basic function/', $result);
626
		$this->assertNotRegExp("/Error: Testing with\nmulti-line\nstring/", $result);
627
		$this->assertRegExp('/Error: Testing with multi-line string/', $result);
628
	}
629
 
630
/**
631
 * test fileExistsInPath()
632
 *
633
 * @return void
634
 */
635
	public function testFileExistsInPath() {
636
		if (!function_exists('ini_set')) {
637
			$this->markTestSkipped('%s ini_set function not available');
638
		}
639
 
640
		$_includePath = ini_get('include_path');
641
 
642
		$path = TMP . 'basics_test';
643
		$folder1 = $path . DS . 'folder1';
644
		$folder2 = $path . DS . 'folder2';
645
		$file1 = $path . DS . 'file1.php';
646
		$file2 = $folder1 . DS . 'file2.php';
647
		$file3 = $folder1 . DS . 'file3.php';
648
		$file4 = $folder2 . DS . 'file4.php';
649
 
650
		new Folder($path, true);
651
		new Folder($folder1, true);
652
		new Folder($folder2, true);
653
		touch($file1);
654
		touch($file2);
655
		touch($file3);
656
		touch($file4);
657
 
658
		ini_set('include_path', $path . PATH_SEPARATOR . $folder1);
659
 
660
		$this->assertEquals(fileExistsInPath('file1.php'), $file1);
661
		$this->assertEquals(fileExistsInPath('file2.php'), $file2);
662
		$this->assertEquals(fileExistsInPath('folder1' . DS . 'file2.php'), $file2);
663
		$this->assertEquals(fileExistsInPath($file2), $file2);
664
		$this->assertEquals(fileExistsInPath('file3.php'), $file3);
665
		$this->assertEquals(fileExistsInPath($file4), $file4);
666
 
667
		$this->assertFalse(fileExistsInPath('file1'));
668
		$this->assertFalse(fileExistsInPath('file4.php'));
669
 
670
		$Folder = new Folder($path);
671
		$Folder->delete();
672
 
673
		ini_set('include_path', $_includePath);
674
	}
675
 
676
/**
677
 * test convertSlash()
678
 *
679
 * @return void
680
 */
681
	public function testConvertSlash() {
682
		$result = convertSlash('\path\to\location\\');
683
		$expected = '\path\to\location\\';
684
		$this->assertEquals($expected, $result);
685
 
686
		$result = convertSlash('/path/to/location/');
687
		$expected = 'path_to_location';
688
		$this->assertEquals($expected, $result);
689
	}
690
 
691
/**
692
 * test debug()
693
 *
694
 * @return void
695
 */
696
	public function testDebug() {
697
		ob_start();
698
		debug('this-is-a-test', false);
699
		$result = ob_get_clean();
700
		$expectedText = <<<EXPECTED
701
%s (line %d)
702
########## DEBUG ##########
703
'this-is-a-test'
704
###########################
705
 
706
EXPECTED;
707
		$expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
708
 
709
		$this->assertEquals($expected, $result);
710
 
711
		ob_start();
712
		debug('<div>this-is-a-test</div>', true);
713
		$result = ob_get_clean();
714
		$expectedHtml = <<<EXPECTED
715
<div class="cake-debug-output">
716
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
717
<pre class="cake-debug">
718
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
719
</pre>
720
</div>
721
EXPECTED;
722
		$expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
723
		$this->assertEquals($expected, $result);
724
 
725
		ob_start();
726
		debug('<div>this-is-a-test</div>', true, true);
727
		$result = ob_get_clean();
728
		$expected = <<<EXPECTED
729
<div class="cake-debug-output">
730
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
731
<pre class="cake-debug">
732
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
733
</pre>
734
</div>
735
EXPECTED;
736
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
737
		$this->assertEquals($expected, $result);
738
 
739
		ob_start();
740
		debug('<div>this-is-a-test</div>', true, false);
741
		$result = ob_get_clean();
742
		$expected = <<<EXPECTED
743
<div class="cake-debug-output">
744
 
745
<pre class="cake-debug">
746
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
747
</pre>
748
</div>
749
EXPECTED;
750
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
751
		$this->assertEquals($expected, $result);
752
 
753
		ob_start();
754
		debug('<div>this-is-a-test</div>', null);
755
		$result = ob_get_clean();
756
		$expectedHtml = <<<EXPECTED
757
<div class="cake-debug-output">
758
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
759
<pre class="cake-debug">
760
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
761
</pre>
762
</div>
763
EXPECTED;
764
		$expectedText = <<<EXPECTED
765
%s (line %d)
766
########## DEBUG ##########
767
'<div>this-is-a-test</div>'
768
###########################
769
 
770
EXPECTED;
771
		if (php_sapi_name() === 'cli') {
772
			$expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
773
		} else {
774
			$expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
775
		}
776
		$this->assertEquals($expected, $result);
777
 
778
		ob_start();
779
		debug('<div>this-is-a-test</div>', null, false);
780
		$result = ob_get_clean();
781
		$expectedHtml = <<<EXPECTED
782
<div class="cake-debug-output">
783
 
784
<pre class="cake-debug">
785
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
786
</pre>
787
</div>
788
EXPECTED;
789
		$expectedText = <<<EXPECTED
790
 
791
########## DEBUG ##########
792
'<div>this-is-a-test</div>'
793
###########################
794
 
795
EXPECTED;
796
		if (php_sapi_name() === 'cli') {
797
			$expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
798
		} else {
799
			$expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
800
		}
801
		$this->assertEquals($expected, $result);
802
 
803
		ob_start();
804
		debug('<div>this-is-a-test</div>', false);
805
		$result = ob_get_clean();
806
		$expected = <<<EXPECTED
807
%s (line %d)
808
########## DEBUG ##########
809
'<div>this-is-a-test</div>'
810
###########################
811
 
812
EXPECTED;
813
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
814
		$this->assertEquals($expected, $result);
815
 
816
		ob_start();
817
		debug('<div>this-is-a-test</div>', false, true);
818
		$result = ob_get_clean();
819
		$expected = <<<EXPECTED
820
%s (line %d)
821
########## DEBUG ##########
822
'<div>this-is-a-test</div>'
823
###########################
824
 
825
EXPECTED;
826
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
827
		$this->assertEquals($expected, $result);
828
 
829
		ob_start();
830
		debug('<div>this-is-a-test</div>', false, false);
831
		$result = ob_get_clean();
832
		$expected = <<<EXPECTED
833
 
834
########## DEBUG ##########
835
'<div>this-is-a-test</div>'
836
###########################
837
 
838
EXPECTED;
839
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
840
		$this->assertEquals($expected, $result);
841
 
842
		ob_start();
843
		debug(false, false, false);
844
		$result = ob_get_clean();
845
		$expected = <<<EXPECTED
846
 
847
########## DEBUG ##########
848
false
849
###########################
850
 
851
EXPECTED;
852
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
853
		$this->assertEquals($expected, $result);
854
	}
855
 
856
/**
857
 * test pr()
858
 *
859
 * @return void
860
 */
861
	public function testPr() {
862
		$this->skipIf(php_sapi_name() == 'cli', 'Skipping web test in cli mode');
863
		ob_start();
864
		pr('this is a test');
865
		$result = ob_get_clean();
866
		$expected = "<pre>this is a test</pre>";
867
		$this->assertEquals($expected, $result);
868
 
869
		ob_start();
870
		pr(array('this' => 'is', 'a' => 'test'));
871
		$result = ob_get_clean();
872
		$expected = "<pre>Array\n(\n    [this] => is\n    [a] => test\n)\n</pre>";
873
		$this->assertEquals($expected, $result);
874
	}
875
 
876
/**
877
 * test pr()
878
 *
879
 * @return void
880
 */
881
	public function testPrCli() {
882
		$this->skipIf(php_sapi_name() != 'cli', 'Skipping cli test in web mode');
883
		ob_start();
884
		pr('this is a test');
885
		$result = ob_get_clean();
886
		$expected = "\nthis is a test\n";
887
		$this->assertEquals($expected, $result);
888
 
889
		ob_start();
890
		pr(array('this' => 'is', 'a' => 'test'));
891
		$result = ob_get_clean();
892
		$expected = "\nArray\n(\n    [this] => is\n    [a] => test\n)\n\n";
893
		$this->assertEquals($expected, $result);
894
	}
895
 
896
/**
897
 * test stripslashes_deep()
898
 *
899
 * @return void
900
 */
901
	public function testStripslashesDeep() {
902
		$this->skipIf(ini_get('magic_quotes_sybase') === '1', 'magic_quotes_sybase is on.');
903
 
904
		$this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
905
		$this->assertEquals(stripslashes_deep('tes\\' . chr(0) . 't'), 'tes' . chr(0) . 't');
906
		$this->assertEquals(stripslashes_deep('tes\"t'), 'tes"t');
907
		$this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
908
		$this->assertEquals(stripslashes_deep('te\\st'), 'test');
909
 
910
		$nested = array(
911
			'a' => "tes\'t",
912
			'b' => 'tes\\' . chr(0) . 't',
913
			'c' => array(
914
				'd' => 'tes\"t',
915
				'e' => "te\'s\'t",
916
				array('f' => "tes\'t")
917
				),
918
			'g' => 'te\\st'
919
		);
920
		$expected = array(
921
			'a' => "tes't",
922
			'b' => 'tes' . chr(0) . 't',
923
			'c' => array(
924
				'd' => 'tes"t',
925
				'e' => "te's't",
926
				array('f' => "tes't")
927
				),
928
			'g' => 'test'
929
		);
930
		$this->assertEquals($expected, stripslashes_deep($nested));
931
	}
932
 
933
/**
934
 * test stripslashes_deep() with magic_quotes_sybase on
935
 *
936
 * @return void
937
 */
938
	public function testStripslashesDeepSybase() {
939
		if (!(ini_get('magic_quotes_sybase') === '1')) {
940
			$this->markTestSkipped('magic_quotes_sybase is off');
941
		}
942
 
943
		$this->assertEquals(stripslashes_deep("tes\'t"), "tes\'t");
944
 
945
		$nested = array(
946
			'a' => "tes't",
947
			'b' => "tes''t",
948
			'c' => array(
949
				'd' => "tes'''t",
950
				'e' => "tes''''t",
951
				array('f' => "tes''t")
952
				),
953
			'g' => "te'''''st"
954
			);
955
		$expected = array(
956
			'a' => "tes't",
957
			'b' => "tes't",
958
			'c' => array(
959
				'd' => "tes''t",
960
				'e' => "tes''t",
961
				array('f' => "tes't")
962
				),
963
			'g' => "te'''st"
964
			);
965
		$this->assertEquals($expected, stripslashes_deep($nested));
966
	}
967
 
968
/**
969
 * test pluginSplit
970
 *
971
 * @return void
972
 */
973
	public function testPluginSplit() {
974
		$result = pluginSplit('Something.else');
975
		$this->assertEquals(array('Something', 'else'), $result);
976
 
977
		$result = pluginSplit('Something.else.more.dots');
978
		$this->assertEquals(array('Something', 'else.more.dots'), $result);
979
 
980
		$result = pluginSplit('Somethingelse');
981
		$this->assertEquals(array(null, 'Somethingelse'), $result);
982
 
983
		$result = pluginSplit('Something.else', true);
984
		$this->assertEquals(array('Something.', 'else'), $result);
985
 
986
		$result = pluginSplit('Something.else.more.dots', true);
987
		$this->assertEquals(array('Something.', 'else.more.dots'), $result);
988
 
989
		$result = pluginSplit('Post', false, 'Blog');
990
		$this->assertEquals(array('Blog', 'Post'), $result);
991
 
992
		$result = pluginSplit('Blog.Post', false, 'Ultimate');
993
		$this->assertEquals(array('Blog', 'Post'), $result);
994
	}
995
}