Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 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
 * testTranslatePercent
402
 *
403
 * @return void
404
 */
405
	public function testTranslatePercent() {
406
		$result = __('%s are 100% real fruit', 'Apples');
407
		$expected = 'Apples are 100% real fruit';
408
		$this->assertEquals($expected, $result, 'Percent sign at end of word should be considered literal');
409
 
410
		$result = __('%s are %d% real fruit', 'Apples', 100);
411
		$expected = 'Apples are 100% real fruit';
412
		$this->assertEquals($expected, $result, 'A digit marker should not be misinterpreted');
413
 
414
		$result = __('%s are %s% real fruit', 'Apples', 100);
415
		$expected = 'Apples are 100% real fruit';
416
		$this->assertEquals($expected, $result, 'A string marker should not be misinterpreted');
417
 
418
		$result = __('%nonsense %s', 'Apples');
419
		$expected = '%nonsense Apples';
420
		$this->assertEquals($expected, $result, 'A percent sign at the start of the string should be considered literal');
421
 
422
		$result = __('%s are awesome%', 'Apples');
423
		$expected = 'Apples are awesome%';
424
		$this->assertEquals($expected, $result, 'A percent sign at the end of the string should be considered literal');
425
 
426
		$result = __('%2$d %1$s entered the bowl', 'Apples', 2);
427
		$expected = '2 Apples entered the bowl';
428
		$this->assertEquals($expected, $result, 'Positional replacement markers should not be misinterpreted');
429
 
430
		$result = __('%.2f% of all %s agree', 99.44444, 'Cats');
431
		$expected = '99.44% of all Cats agree';
432
		$this->assertEquals($expected, $result, 'significant-digit placeholder should not be misinterpreted');
433
	}
434
 
435
/**
436
 * testTranslateWithFormatSpecifiers
437
 *
438
 * @return void
439
 */
440
	public function testTranslateWithFormatSpecifiers() {
441
		$expected = 'Check,   one, two, three';
442
		$result = __('Check, %+10s, three', 'one, two');
443
		$this->assertEquals($expected, $result);
444
 
445
		$expected = 'Check,    +1, two, three';
446
		$result = __('Check, %+5d, two, three', 1);
447
		$this->assertEquals($expected, $result);
448
 
449
		$expected = 'Check, @@one, two, three';
450
		$result = __('Check, %\'@+10s, three', 'one, two');
451
		$this->assertEquals($expected, $result);
452
 
453
		$expected = 'Check, one, two  , three';
454
		$result = __('Check, %-10s, three', 'one, two');
455
		$this->assertEquals($expected, $result);
456
 
457
		$expected = 'Check, one, two##, three';
458
		$result = __('Check, %\'#-10s, three', 'one, two');
459
		$this->assertEquals($expected, $result);
460
 
461
		$expected = 'Check,   one, two, three';
462
		$result = __d('default', 'Check, %+10s, three', 'one, two');
463
		$this->assertEquals($expected, $result);
464
 
465
		$expected = 'Check, @@one, two, three';
466
		$result = __d('default', 'Check, %\'@+10s, three', 'one, two');
467
		$this->assertEquals($expected, $result);
468
 
469
		$expected = 'Check, one, two  , three';
470
		$result = __d('default', 'Check, %-10s, three', 'one, two');
471
		$this->assertEquals($expected, $result);
472
 
473
		$expected = 'Check, one, two##, three';
474
		$result = __d('default', 'Check, %\'#-10s, three', 'one, two');
475
		$this->assertEquals($expected, $result);
476
	}
477
 
478
/**
479
 * testTranslateDomainPluralWithFormatSpecifiers
480
 *
481
 * @return void
482
 */
483
	public function testTranslateDomainPluralWithFormatSpecifiers() {
484
		$result = __dn('core', '%+5d item.', '%+5d items.', 1, 1);
485
		$expected = '   +1 item.';
486
		$this->assertEquals($expected, $result);
487
 
488
		$result = __dn('core', '%-5d item.', '%-5d items.', 10, 10);
489
		$expected = '10    items.';
490
		$this->assertEquals($expected, $result);
491
 
492
		$result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 1, 1);
493
		$expected = '###+1 item.';
494
		$this->assertEquals($expected, $result);
495
 
496
		$result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 90, 90);
497
		$expected = '**+90 items.';
498
		$this->assertEquals($expected, $result);
499
 
500
		$result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 9000, 9000);
501
		$expected = '+9000 items.';
502
		$this->assertEquals($expected, $result);
503
	}
504
 
505
/**
506
 * test testTranslatePluralWithFormatSpecifiers
507
 *
508
 * @return void
509
 */
510
	public function testTranslatePluralWithFormatSpecifiers() {
511
		Configure::write('Config.language', 'rule_1_po');
512
 
513
		$result = __n('%-5d = 1', '%-5d = 0 or > 1', 10);
514
		$expected = '%-5d = 0 or > 1 (translated)';
515
		$this->assertEquals($expected, $result);
516
	}
517
 
518
/**
519
 * test testTranslateDomainCategoryWithFormatSpecifiers
520
 *
521
 * @return void
522
 */
523
	public function testTranslateDomainCategoryWithFormatSpecifiers() {
524
		Configure::write('Config.language', 'rule_1_po');
525
 
526
		$result = __dc('default', '%+10s world', 6, 'hello');
527
		$expected = '     hello world';
528
		$this->assertEquals($expected, $result);
529
 
530
		$result = __dc('default', '%-10s world', 6, 'hello');
531
		$expected = 'hello      world';
532
		$this->assertEquals($expected, $result);
533
 
534
		$result = __dc('default', '%\'@-10s world', 6, 'hello');
535
		$expected = 'hello@@@@@ world';
536
		$this->assertEquals($expected, $result);
537
	}
538
 
539
/**
540
 * test testTranslateDomainCategoryPluralWithFormatSpecifiers
541
 *
542
 * @return void
543
 */
544
	public function testTranslateDomainCategoryPluralWithFormatSpecifiers() {
545
		Configure::write('Config.language', 'rule_1_po');
546
 
547
		$result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 0, 6);
548
		$expected = '%-5d = 0 or > 1 (translated)';
549
		$this->assertEquals($expected, $result);
550
 
551
		$result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 1, 6);
552
		$expected = '%-5d = 1 (translated)';
553
		$this->assertEquals($expected, $result);
554
	}
555
 
556
/**
557
 * test testTranslateCategoryWithFormatSpecifiers
558
 *
559
 * @return void
560
 */
561
	public function testTranslateCategoryWithFormatSpecifiers() {
562
		$result = __c('Some string with %+10s', 6, 'arguments');
563
		$expected = 'Some string with  arguments';
564
		$this->assertEquals($expected, $result);
565
 
566
		$result = __c('Some string with %-10s: args', 6, 'arguments');
567
		$expected = 'Some string with arguments : args';
568
		$this->assertEquals($expected, $result);
569
 
570
		$result = __c('Some string with %\'*-10s: args', 6, 'arguments');
571
		$expected = 'Some string with arguments*: args';
572
		$this->assertEquals($expected, $result);
573
	}
574
 
575
/**
576
 * test __n()
577
 *
578
 * @return void
579
 */
580
	public function testTranslatePlural() {
581
		Configure::write('Config.language', 'rule_1_po');
582
 
583
		$result = __n('%d = 1', '%d = 0 or > 1', 0);
584
		$expected = '%d = 0 or > 1 (translated)';
585
		$this->assertEquals($expected, $result);
586
 
587
		$result = __n('%d = 1', '%d = 0 or > 1', 1);
588
		$expected = '%d = 1 (translated)';
589
		$this->assertEquals($expected, $result);
590
 
591
		$result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
592
		$expected = '%d = 0 or > 1 (from core translated)';
593
		$this->assertEquals($expected, $result);
594
 
595
		$result = __n('%d item.', '%d items.', 1, 1);
596
		$expected = '1 item.';
597
		$this->assertEquals($expected, $result);
598
 
599
		$result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
600
		$expected = '2 items for id 1234';
601
		$this->assertEquals($expected, $result);
602
 
603
		$result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
604
		$expected = '2 items for id 1234';
605
		$this->assertEquals($expected, $result);
606
	}
607
 
608
/**
609
 * test __d()
610
 *
611
 * @return void
612
 */
613
	public function testTranslateDomain() {
614
		Configure::write('Config.language', 'rule_1_po');
615
 
616
		$result = __d('default', 'Plural Rule 1');
617
		$expected = 'Plural Rule 1 (translated)';
618
		$this->assertEquals($expected, $result);
619
 
620
		$result = __d('core', 'Plural Rule 1');
621
		$expected = 'Plural Rule 1';
622
		$this->assertEquals($expected, $result);
623
 
624
		$result = __d('core', 'Plural Rule 1 (from core)');
625
		$expected = 'Plural Rule 1 (from core translated)';
626
		$this->assertEquals($expected, $result);
627
 
628
		$result = __d('core', 'Some string with %s', 'arguments');
629
		$expected = 'Some string with arguments';
630
		$this->assertEquals($expected, $result);
631
 
632
		$result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
633
		$expected = 'Some string with multiple arguments';
634
		$this->assertEquals($expected, $result);
635
 
636
		$result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
637
		$expected = 'Some string with multiple arguments';
638
		$this->assertEquals($expected, $result);
639
	}
640
 
641
/**
642
 * test __dn()
643
 *
644
 * @return void
645
 */
646
	public function testTranslateDomainPlural() {
647
		Configure::write('Config.language', 'rule_1_po');
648
 
649
		$result = __dn('default', '%d = 1', '%d = 0 or > 1', 0);
650
		$expected = '%d = 0 or > 1 (translated)';
651
		$this->assertEquals($expected, $result);
652
 
653
		$result = __dn('core', '%d = 1', '%d = 0 or > 1', 0);
654
		$expected = '%d = 0 or > 1';
655
		$this->assertEquals($expected, $result);
656
 
657
		$result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0);
658
		$expected = '%d = 0 or > 1 (from core translated)';
659
		$this->assertEquals($expected, $result);
660
 
661
		$result = __dn('default', '%d = 1', '%d = 0 or > 1', 1);
662
		$expected = '%d = 1 (translated)';
663
		$this->assertEquals($expected, $result);
664
 
665
		$result = __dn('core', '%d item.', '%d items.', 1, 1);
666
		$expected = '1 item.';
667
		$this->assertEquals($expected, $result);
668
 
669
		$result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
670
		$expected = '2 items for id 1234';
671
		$this->assertEquals($expected, $result);
672
 
673
		$result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
674
		$expected = '2 items for id 1234';
675
		$this->assertEquals($expected, $result);
676
	}
677
 
678
/**
679
 * test __c()
680
 *
681
 * @return void
682
 */
683
	public function testTranslateCategory() {
684
		Configure::write('Config.language', 'rule_1_po');
685
 
686
		$result = __c('Plural Rule 1', 6);
687
		$expected = 'Plural Rule 1 (translated)';
688
		$this->assertEquals($expected, $result);
689
 
690
		$result = __c('Plural Rule 1 (from core)', 6);
691
		$expected = 'Plural Rule 1 (from core translated)';
692
		$this->assertEquals($expected, $result);
693
 
694
		$result = __c('Some string with %s', 6, 'arguments');
695
		$expected = 'Some string with arguments';
696
		$this->assertEquals($expected, $result);
697
 
698
		$result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
699
		$expected = 'Some string with multiple arguments';
700
		$this->assertEquals($expected, $result);
701
 
702
		$result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
703
		$expected = 'Some string with multiple arguments';
704
		$this->assertEquals($expected, $result);
705
	}
706
 
707
/**
708
 * test __dc()
709
 *
710
 * @return void
711
 */
712
	public function testTranslateDomainCategory() {
713
		Configure::write('Config.language', 'rule_1_po');
714
 
715
		$result = __dc('default', 'Plural Rule 1', 6);
716
		$expected = 'Plural Rule 1 (translated)';
717
		$this->assertEquals($expected, $result);
718
 
719
		$result = __dc('default', 'Plural Rule 1 (from core)', 6);
720
		$expected = 'Plural Rule 1 (from core translated)';
721
		$this->assertEquals($expected, $result);
722
 
723
		$result = __dc('core', 'Plural Rule 1', 6);
724
		$expected = 'Plural Rule 1';
725
		$this->assertEquals($expected, $result);
726
 
727
		$result = __dc('core', 'Plural Rule 1 (from core)', 6);
728
		$expected = 'Plural Rule 1 (from core translated)';
729
		$this->assertEquals($expected, $result);
730
 
731
		$result = __dc('core', 'Some string with %s', 6, 'arguments');
732
		$expected = 'Some string with arguments';
733
		$this->assertEquals($expected, $result);
734
 
735
		$result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
736
		$expected = 'Some string with multiple arguments';
737
		$this->assertEquals($expected, $result);
738
 
739
		$result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
740
		$expected = 'Some string with multiple arguments';
741
		$this->assertEquals($expected, $result);
742
	}
743
 
744
/**
745
 * test __dcn()
746
 *
747
 * @return void
748
 */
749
	public function testTranslateDomainCategoryPlural() {
750
		Configure::write('Config.language', 'rule_1_po');
751
 
752
		$result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 6);
753
		$expected = '%d = 0 or > 1 (translated)';
754
		$this->assertEquals($expected, $result);
755
 
756
		$result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6);
757
		$expected = '%d = 1 (from core translated)';
758
		$this->assertEquals($expected, $result);
759
 
760
		$result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
761
		$expected = '%d = 0 or > 1';
762
		$this->assertEquals($expected, $result);
763
 
764
		$result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
765
		$expected = '1 item.';
766
		$this->assertEquals($expected, $result);
767
 
768
		$result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
769
		$expected = '2 items for id 1234';
770
		$this->assertEquals($expected, $result);
771
 
772
		$result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
773
		$expected = '2 items for id 1234';
774
		$this->assertEquals($expected, $result);
775
	}
776
 
777
/**
778
 * test LogError()
779
 *
780
 * @return void
781
 */
782
	public function testLogError() {
783
		if (file_exists(LOGS . 'error.log')) {
784
			unlink(LOGS . 'error.log');
785
		}
786
 
787
		// disable stderr output for this test
788
		if (CakeLog::stream('stderr')) {
789
			CakeLog::disable('stderr');
790
		}
791
 
792
		LogError('Testing LogError() basic function');
793
		LogError("Testing with\nmulti-line\nstring");
794
 
795
		if (CakeLog::stream('stderr')) {
796
			CakeLog::enable('stderr');
797
		}
798
 
799
		$result = file_get_contents(LOGS . 'error.log');
800
		$this->assertRegExp('/Error: Testing LogError\(\) basic function/', $result);
801
		$this->assertNotRegExp("/Error: Testing with\nmulti-line\nstring/", $result);
802
		$this->assertRegExp('/Error: Testing with multi-line string/', $result);
803
	}
804
 
805
/**
806
 * test fileExistsInPath()
807
 *
808
 * @return void
809
 */
810
	public function testFileExistsInPath() {
811
		if (!function_exists('ini_set')) {
812
			$this->markTestSkipped('%s ini_set function not available');
813
		}
814
 
815
		$_includePath = ini_get('include_path');
816
 
817
		$path = TMP . 'basics_test';
818
		$folder1 = $path . DS . 'folder1';
819
		$folder2 = $path . DS . 'folder2';
820
		$file1 = $path . DS . 'file1.php';
821
		$file2 = $folder1 . DS . 'file2.php';
822
		$file3 = $folder1 . DS . 'file3.php';
823
		$file4 = $folder2 . DS . 'file4.php';
824
 
825
		new Folder($path, true);
826
		new Folder($folder1, true);
827
		new Folder($folder2, true);
828
		touch($file1);
829
		touch($file2);
830
		touch($file3);
831
		touch($file4);
832
 
833
		ini_set('include_path', $path . PATH_SEPARATOR . $folder1);
834
 
835
		$this->assertEquals(fileExistsInPath('file1.php'), $file1);
836
		$this->assertEquals(fileExistsInPath('file2.php'), $file2);
837
		$this->assertEquals(fileExistsInPath('folder1' . DS . 'file2.php'), $file2);
838
		$this->assertEquals(fileExistsInPath($file2), $file2);
839
		$this->assertEquals(fileExistsInPath('file3.php'), $file3);
840
		$this->assertEquals(fileExistsInPath($file4), $file4);
841
 
842
		$this->assertFalse(fileExistsInPath('file1'));
843
		$this->assertFalse(fileExistsInPath('file4.php'));
844
 
845
		$Folder = new Folder($path);
846
		$Folder->delete();
847
 
848
		ini_set('include_path', $_includePath);
849
	}
850
 
851
/**
852
 * test convertSlash()
853
 *
854
 * @return void
855
 */
856
	public function testConvertSlash() {
857
		$result = convertSlash('\path\to\location\\');
858
		$expected = '\path\to\location\\';
859
		$this->assertEquals($expected, $result);
860
 
861
		$result = convertSlash('/path/to/location/');
862
		$expected = 'path_to_location';
863
		$this->assertEquals($expected, $result);
864
	}
865
 
866
/**
867
 * test debug()
868
 *
869
 * @return void
870
 */
871
	public function testDebug() {
872
		ob_start();
873
		debug('this-is-a-test', false);
874
		$result = ob_get_clean();
875
		$expectedText = <<<EXPECTED
876
%s (line %d)
877
########## DEBUG ##########
878
'this-is-a-test'
879
###########################
880
 
881
EXPECTED;
882
		$expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
883
 
884
		$this->assertEquals($expected, $result);
885
 
886
		ob_start();
887
		debug('<div>this-is-a-test</div>', true);
888
		$result = ob_get_clean();
889
		$expectedHtml = <<<EXPECTED
890
<div class="cake-debug-output">
891
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
892
<pre class="cake-debug">
893
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
894
</pre>
895
</div>
896
EXPECTED;
897
		$expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
898
		$this->assertEquals($expected, $result);
899
 
900
		ob_start();
901
		debug('<div>this-is-a-test</div>', true, true);
902
		$result = ob_get_clean();
903
		$expected = <<<EXPECTED
904
<div class="cake-debug-output">
905
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
906
<pre class="cake-debug">
907
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
908
</pre>
909
</div>
910
EXPECTED;
911
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
912
		$this->assertEquals($expected, $result);
913
 
914
		ob_start();
915
		debug('<div>this-is-a-test</div>', true, false);
916
		$result = ob_get_clean();
917
		$expected = <<<EXPECTED
918
<div class="cake-debug-output">
919
 
920
<pre class="cake-debug">
921
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
922
</pre>
923
</div>
924
EXPECTED;
925
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
926
		$this->assertEquals($expected, $result);
927
 
928
		ob_start();
929
		debug('<div>this-is-a-test</div>', null);
930
		$result = ob_get_clean();
931
		$expectedHtml = <<<EXPECTED
932
<div class="cake-debug-output">
933
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
934
<pre class="cake-debug">
935
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
936
</pre>
937
</div>
938
EXPECTED;
939
		$expectedText = <<<EXPECTED
940
%s (line %d)
941
########## DEBUG ##########
942
'<div>this-is-a-test</div>'
943
###########################
944
 
945
EXPECTED;
946
		if (php_sapi_name() === 'cli') {
947
			$expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
948
		} else {
949
			$expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
950
		}
951
		$this->assertEquals($expected, $result);
952
 
953
		ob_start();
954
		debug('<div>this-is-a-test</div>', null, false);
955
		$result = ob_get_clean();
956
		$expectedHtml = <<<EXPECTED
957
<div class="cake-debug-output">
958
 
959
<pre class="cake-debug">
960
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
961
</pre>
962
</div>
963
EXPECTED;
964
		$expectedText = <<<EXPECTED
965
 
966
########## DEBUG ##########
967
'<div>this-is-a-test</div>'
968
###########################
969
 
970
EXPECTED;
971
		if (php_sapi_name() === 'cli') {
972
			$expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
973
		} else {
974
			$expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
975
		}
976
		$this->assertEquals($expected, $result);
977
 
978
		ob_start();
979
		debug('<div>this-is-a-test</div>', false);
980
		$result = ob_get_clean();
981
		$expected = <<<EXPECTED
982
%s (line %d)
983
########## DEBUG ##########
984
'<div>this-is-a-test</div>'
985
###########################
986
 
987
EXPECTED;
988
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
989
		$this->assertEquals($expected, $result);
990
 
991
		ob_start();
992
		debug('<div>this-is-a-test</div>', false, true);
993
		$result = ob_get_clean();
994
		$expected = <<<EXPECTED
995
%s (line %d)
996
########## DEBUG ##########
997
'<div>this-is-a-test</div>'
998
###########################
999
 
1000
EXPECTED;
1001
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
1002
		$this->assertEquals($expected, $result);
1003
 
1004
		ob_start();
1005
		debug('<div>this-is-a-test</div>', false, false);
1006
		$result = ob_get_clean();
1007
		$expected = <<<EXPECTED
1008
 
1009
########## DEBUG ##########
1010
'<div>this-is-a-test</div>'
1011
###########################
1012
 
1013
EXPECTED;
1014
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
1015
		$this->assertEquals($expected, $result);
1016
 
1017
		ob_start();
1018
		debug(false, false, false);
1019
		$result = ob_get_clean();
1020
		$expected = <<<EXPECTED
1021
 
1022
########## DEBUG ##########
1023
false
1024
###########################
1025
 
1026
EXPECTED;
1027
		$expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
1028
		$this->assertEquals($expected, $result);
1029
	}
1030
 
1031
/**
1032
 * test pr()
1033
 *
1034
 * @return void
1035
 */
1036
	public function testPr() {
1037
		$this->skipIf(php_sapi_name() === 'cli', 'Skipping web test in cli mode');
1038
		ob_start();
1039
		pr('this is a test');
1040
		$result = ob_get_clean();
1041
		$expected = "<pre>this is a test</pre>";
1042
		$this->assertEquals($expected, $result);
1043
 
1044
		ob_start();
1045
		pr(array('this' => 'is', 'a' => 'test'));
1046
		$result = ob_get_clean();
1047
		$expected = "<pre>Array\n(\n    [this] => is\n    [a] => test\n)\n</pre>";
1048
		$this->assertEquals($expected, $result);
1049
	}
1050
 
1051
/**
1052
 * test pr()
1053
 *
1054
 * @return void
1055
 */
1056
	public function testPrCli() {
1057
		$this->skipIf(php_sapi_name() != 'cli', 'Skipping cli test in web mode');
1058
		ob_start();
1059
		pr('this is a test');
1060
		$result = ob_get_clean();
1061
		$expected = "\nthis is a test\n";
1062
		$this->assertEquals($expected, $result);
1063
 
1064
		ob_start();
1065
		pr(array('this' => 'is', 'a' => 'test'));
1066
		$result = ob_get_clean();
1067
		$expected = "\nArray\n(\n    [this] => is\n    [a] => test\n)\n\n";
1068
		$this->assertEquals($expected, $result);
1069
	}
1070
 
1071
/**
1072
 * test stripslashes_deep()
1073
 *
1074
 * @return void
1075
 */
1076
	public function testStripslashesDeep() {
1077
		$this->skipIf(ini_get('magic_quotes_sybase') === '1', 'magic_quotes_sybase is on.');
1078
 
1079
		$this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
1080
		$this->assertEquals(stripslashes_deep('tes\\' . chr(0) . 't'), 'tes' . chr(0) . 't');
1081
		$this->assertEquals(stripslashes_deep('tes\"t'), 'tes"t');
1082
		$this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
1083
		$this->assertEquals(stripslashes_deep('te\\st'), 'test');
1084
 
1085
		$nested = array(
1086
			'a' => "tes\'t",
1087
			'b' => 'tes\\' . chr(0) . 't',
1088
			'c' => array(
1089
				'd' => 'tes\"t',
1090
				'e' => "te\'s\'t",
1091
				array('f' => "tes\'t")
1092
				),
1093
			'g' => 'te\\st'
1094
		);
1095
		$expected = array(
1096
			'a' => "tes't",
1097
			'b' => 'tes' . chr(0) . 't',
1098
			'c' => array(
1099
				'd' => 'tes"t',
1100
				'e' => "te's't",
1101
				array('f' => "tes't")
1102
				),
1103
			'g' => 'test'
1104
		);
1105
		$this->assertEquals($expected, stripslashes_deep($nested));
1106
	}
1107
 
1108
/**
1109
 * test stripslashes_deep() with magic_quotes_sybase on
1110
 *
1111
 * @return void
1112
 */
1113
	public function testStripslashesDeepSybase() {
1114
		if (!(ini_get('magic_quotes_sybase') === '1')) {
1115
			$this->markTestSkipped('magic_quotes_sybase is off');
1116
		}
1117
 
1118
		$this->assertEquals(stripslashes_deep("tes\'t"), "tes\'t");
1119
 
1120
		$nested = array(
1121
			'a' => "tes't",
1122
			'b' => "tes''t",
1123
			'c' => array(
1124
				'd' => "tes'''t",
1125
				'e' => "tes''''t",
1126
				array('f' => "tes''t")
1127
				),
1128
			'g' => "te'''''st"
1129
			);
1130
		$expected = array(
1131
			'a' => "tes't",
1132
			'b' => "tes't",
1133
			'c' => array(
1134
				'd' => "tes''t",
1135
				'e' => "tes''t",
1136
				array('f' => "tes't")
1137
				),
1138
			'g' => "te'''st"
1139
			);
1140
		$this->assertEquals($expected, stripslashes_deep($nested));
1141
	}
1142
 
1143
/**
1144
 * test pluginSplit
1145
 *
1146
 * @return void
1147
 */
1148
	public function testPluginSplit() {
1149
		$result = pluginSplit('Something.else');
1150
		$this->assertEquals(array('Something', 'else'), $result);
1151
 
1152
		$result = pluginSplit('Something.else.more.dots');
1153
		$this->assertEquals(array('Something', 'else.more.dots'), $result);
1154
 
1155
		$result = pluginSplit('Somethingelse');
1156
		$this->assertEquals(array(null, 'Somethingelse'), $result);
1157
 
1158
		$result = pluginSplit('Something.else', true);
1159
		$this->assertEquals(array('Something.', 'else'), $result);
1160
 
1161
		$result = pluginSplit('Something.else.more.dots', true);
1162
		$this->assertEquals(array('Something.', 'else.more.dots'), $result);
1163
 
1164
		$result = pluginSplit('Post', false, 'Blog');
1165
		$this->assertEquals(array('Blog', 'Post'), $result);
1166
 
1167
		$result = pluginSplit('Blog.Post', false, 'Ultimate');
1168
		$this->assertEquals(array('Blog', 'Post'), $result);
1169
	}
1170
}