統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / BasicsTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (34.962 KB)

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

890
EXPECTED;
891
                $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
892
893
                $this->assertEquals($expected, $result);
894
895
                ob_start();
896
                debug('<div>this-is-a-test</div>', true);
897
                $result = ob_get_clean();
898
                $expectedHtml = <<<EXPECTED
899
<div class="cake-debug-output">
900
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
901
<pre class="cake-debug">
902
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
903
</pre>
904
</div>
905
EXPECTED;
906
                $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
907
                $this->assertEquals($expected, $result);
908
909
                ob_start();
910
                debug('<div>this-is-a-test</div>', true, true);
911
                $result = ob_get_clean();
912
                $expected = <<<EXPECTED
913
<div class="cake-debug-output">
914
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
915
<pre class="cake-debug">
916
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
917
</pre>
918
</div>
919
EXPECTED;
920
                $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
921
                $this->assertEquals($expected, $result);
922
923
                ob_start();
924
                debug('<div>this-is-a-test</div>', true, false);
925
                $result = ob_get_clean();
926
                $expected = <<<EXPECTED
927
<div class="cake-debug-output">
928

929
<pre class="cake-debug">
930
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
931
</pre>
932
</div>
933
EXPECTED;
934
                $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
935
                $this->assertEquals($expected, $result);
936
937
                ob_start();
938
                debug('<div>this-is-a-test</div>', null);
939
                $result = ob_get_clean();
940
                $expectedHtml = <<<EXPECTED
941
<div class="cake-debug-output">
942
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
943
<pre class="cake-debug">
944
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
945
</pre>
946
</div>
947
EXPECTED;
948
                $expectedText = <<<EXPECTED
949
%s (line %d)
950
########## DEBUG ##########
951
'<div>this-is-a-test</div>'
952
###########################
953

954
EXPECTED;
955
                if (PHP_SAPI === 'cli') {
956
                        $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
957
                } else {
958
                        $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 20);
959
                }
960
                $this->assertEquals($expected, $result);
961
962
                ob_start();
963
                debug('<div>this-is-a-test</div>', null, false);
964
                $result = ob_get_clean();
965
                $expectedHtml = <<<EXPECTED
966
<div class="cake-debug-output">
967

968
<pre class="cake-debug">
969
&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
970
</pre>
971
</div>
972
EXPECTED;
973
                $expectedText = <<<EXPECTED
974

975
########## DEBUG ##########
976
'<div>this-is-a-test</div>'
977
###########################
978

979
EXPECTED;
980
                if (PHP_SAPI === 'cli') {
981
                        $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
982
                } else {
983
                        $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
984
                }
985
                $this->assertEquals($expected, $result);
986
987
                ob_start();
988
                debug('<div>this-is-a-test</div>', false);
989
                $result = ob_get_clean();
990
                $expected = <<<EXPECTED
991
%s (line %d)
992
########## DEBUG ##########
993
'<div>this-is-a-test</div>'
994
###########################
995

996
EXPECTED;
997
                $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
998
                $this->assertEquals($expected, $result);
999
1000
                ob_start();
1001
                debug('<div>this-is-a-test</div>', false, true);
1002
                $result = ob_get_clean();
1003
                $expected = <<<EXPECTED
1004
%s (line %d)
1005
########## DEBUG ##########
1006
'<div>this-is-a-test</div>'
1007
###########################
1008

1009
EXPECTED;
1010
                $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
1011
                $this->assertEquals($expected, $result);
1012
1013
                ob_start();
1014
                debug('<div>this-is-a-test</div>', false, false);
1015
                $result = ob_get_clean();
1016
                $expected = <<<EXPECTED
1017

1018
########## DEBUG ##########
1019
'<div>this-is-a-test</div>'
1020
###########################
1021

1022
EXPECTED;
1023
                $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
1024
                $this->assertEquals($expected, $result);
1025
1026
                ob_start();
1027
                debug(false, false, false);
1028
                $result = ob_get_clean();
1029
                $expected = <<<EXPECTED
1030

1031
########## DEBUG ##########
1032
false
1033
###########################
1034

1035
EXPECTED;
1036
                $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
1037
                $this->assertEquals($expected, $result);
1038
        }
1039
1040
/**
1041
 * test pr()
1042
 *
1043
 * @return void
1044
 */
1045
        public function testPr() {
1046
                $this->skipIf(PHP_SAPI === 'cli', 'Skipping web test in cli mode');
1047
                ob_start();
1048
                pr('this is a test');
1049
                $result = ob_get_clean();
1050
                $expected = "<pre>this is a test</pre>";
1051
                $this->assertEquals($expected, $result);
1052
1053
                ob_start();
1054
                pr(array('this' => 'is', 'a' => 'test'));
1055
                $result = ob_get_clean();
1056
                $expected = "<pre>Array\n(\n    [this] => is\n    [a] => test\n)\n</pre>";
1057
                $this->assertEquals($expected, $result);
1058
        }
1059
1060
/**
1061
 * test pr()
1062
 *
1063
 * @return void
1064
 */
1065
        public function testPrCli() {
1066
                $this->skipIf(PHP_SAPI !== 'cli', 'Skipping cli test in web mode');
1067
                ob_start();
1068
                pr('this is a test');
1069
                $result = ob_get_clean();
1070
                $expected = "\nthis is a test\n";
1071
                $this->assertEquals($expected, $result);
1072
1073
                ob_start();
1074
                pr(array('this' => 'is', 'a' => 'test'));
1075
                $result = ob_get_clean();
1076
                $expected = "\nArray\n(\n    [this] => is\n    [a] => test\n)\n\n";
1077
                $this->assertEquals($expected, $result);
1078
        }
1079
1080
/**
1081
 * test stripslashes_deep()
1082
 *
1083
 * @return void
1084
 */
1085
        public function testStripslashesDeep() {
1086
                $this->skipIf(ini_get('magic_quotes_sybase') === '1', 'magic_quotes_sybase is on.');
1087
1088
                $this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
1089
                $this->assertEquals(stripslashes_deep('tes\\' . chr(0) . 't'), 'tes' . chr(0) . 't');
1090
                $this->assertEquals(stripslashes_deep('tes\"t'), 'tes"t');
1091
                $this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
1092
                $this->assertEquals(stripslashes_deep('te\\st'), 'test');
1093
1094
                $nested = array(
1095
                        'a' => "tes\'t",
1096
                        'b' => 'tes\\' . chr(0) . 't',
1097
                        'c' => array(
1098
                                'd' => 'tes\"t',
1099
                                'e' => "te\'s\'t",
1100
                                array('f' => "tes\'t")
1101
                                ),
1102
                        'g' => 'te\\st'
1103
                );
1104
                $expected = array(
1105
                        'a' => "tes't",
1106
                        'b' => 'tes' . chr(0) . 't',
1107
                        'c' => array(
1108
                                'd' => 'tes"t',
1109
                                'e' => "te's't",
1110
                                array('f' => "tes't")
1111
                                ),
1112
                        'g' => 'test'
1113
                );
1114
                $this->assertEquals($expected, stripslashes_deep($nested));
1115
        }
1116
1117
/**
1118
 * test stripslashes_deep() with magic_quotes_sybase on
1119
 *
1120
 * @return void
1121
 */
1122
        public function testStripslashesDeepSybase() {
1123
                if (!(ini_get('magic_quotes_sybase') === '1')) {
1124
                        $this->markTestSkipped('magic_quotes_sybase is off');
1125
                }
1126
1127
                $this->assertEquals(stripslashes_deep("tes\'t"), "tes\'t");
1128
1129
                $nested = array(
1130
                        'a' => "tes't",
1131
                        'b' => "tes''t",
1132
                        'c' => array(
1133
                                'd' => "tes'''t",
1134
                                'e' => "tes''''t",
1135
                                array('f' => "tes''t")
1136
                                ),
1137
                        'g' => "te'''''st"
1138
                        );
1139
                $expected = array(
1140
                        'a' => "tes't",
1141
                        'b' => "tes't",
1142
                        'c' => array(
1143
                                'd' => "tes''t",
1144
                                'e' => "tes''t",
1145
                                array('f' => "tes't")
1146
                                ),
1147
                        'g' => "te'''st"
1148
                        );
1149
                $this->assertEquals($expected, stripslashes_deep($nested));
1150
        }
1151
1152
/**
1153
 * Tests that the stackTrace() method is a shortcut for Debugger::trace()
1154
 *
1155
 * @return void
1156
 */
1157
        public function testStackTrace() {
1158
                ob_start();
1159
                list(, $expected) = array(stackTrace(), Debugger::trace());
1160
                $result = ob_get_clean();
1161
                $this->assertEquals($expected, $result);
1162
1163
                $opts = array('args' => true);
1164
                ob_start();
1165
                list(, $expected) = array(stackTrace($opts), Debugger::trace($opts));
1166
                $result = ob_get_clean();
1167
                $this->assertEquals($expected, $result);
1168
        }
1169
1170
/**
1171
 * test pluginSplit
1172
 *
1173
 * @return void
1174
 */
1175
        public function testPluginSplit() {
1176
                $result = pluginSplit('Something.else');
1177
                $this->assertEquals(array('Something', 'else'), $result);
1178
1179
                $result = pluginSplit('Something.else.more.dots');
1180
                $this->assertEquals(array('Something', 'else.more.dots'), $result);
1181
1182
                $result = pluginSplit('Somethingelse');
1183
                $this->assertEquals(array(null, 'Somethingelse'), $result);
1184
1185
                $result = pluginSplit('Something.else', true);
1186
                $this->assertEquals(array('Something.', 'else'), $result);
1187
1188
                $result = pluginSplit('Something.else.more.dots', true);
1189
                $this->assertEquals(array('Something.', 'else.more.dots'), $result);
1190
1191
                $result = pluginSplit('Post', false, 'Blog');
1192
                $this->assertEquals(array('Blog', 'Post'), $result);
1193
1194
                $result = pluginSplit('Blog.Post', false, 'Ultimate');
1195
                $this->assertEquals(array('Blog', 'Post'), $result);
1196
        }
1197
}