Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * FileEngineTest 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.Cache.Engine
15
 * @since         CakePHP(tm) v 1.2.0.5434
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Cache', 'Cache');
20
 
21
/**
22
 * FileEngineTest class
23
 *
24
 * @package       Cake.Test.Case.Cache.Engine
25
 */
26
class FileEngineTest extends CakeTestCase {
27
 
28
/**
29
 * config property
30
 *
31
 * @var array
32
 */
33
	public $config = array();
34
 
35
/**
36
 * setUp method
37
 *
38
 * @return void
39
 */
40
	public function setUp() {
41
		parent::setUp();
42
		Configure::write('Cache.disable', false);
43
		Cache::config('file_test', array('engine' => 'File', 'path' => CACHE));
44
	}
45
 
46
/**
47
 * tearDown method
48
 *
49
 * @return void
50
 */
51
	public function tearDown() {
52
		parent::tearDown();
53
		// Cache::clear(false, 'file_test');
54
		Cache::drop('file_test');
55
		Cache::drop('file_groups');
56
		Cache::drop('file_groups2');
57
		Cache::drop('file_groups3');
58
	}
59
 
60
/**
61
 * testCacheDirChange method
62
 *
63
 * @return void
64
 */
65
	public function testCacheDirChange() {
66
		$result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'sessions'));
67
		$this->assertEquals(Cache::settings('sessions'), $result['settings']);
68
 
69
		$result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'tests'));
70
		$this->assertEquals(Cache::settings('sessions'), $result['settings']);
71
		$this->assertNotEquals(Cache::settings('default'), $result['settings']);
72
	}
73
 
74
/**
75
 * testReadAndWriteCache method
76
 *
77
 * @return void
78
 */
79
	public function testReadAndWriteCache() {
80
		Cache::config('default');
81
 
82
		$result = Cache::write(null, 'here', 'file_test');
83
		$this->assertFalse($result);
84
 
85
		Cache::set(array('duration' => 1), 'file_test');
86
 
87
		$result = Cache::read('test', 'file_test');
88
		$expecting = '';
89
		$this->assertEquals($expecting, $result);
90
 
91
		$data = 'this is a test of the emergency broadcasting system';
92
		$result = Cache::write('test', $data, 'file_test');
93
		$this->assertTrue(file_exists(CACHE . 'cake_test'));
94
 
95
		$result = Cache::read('test', 'file_test');
96
		$expecting = $data;
97
		$this->assertEquals($expecting, $result);
98
 
99
		Cache::delete('test', 'file_test');
100
	}
101
 
102
/**
103
 * Test read/write on the same cache key. Ensures file handles are re-wound.
104
 *
105
 * @return void
106
 */
107
	public function testConsecutiveReadWrite() {
108
		Cache::write('rw', 'first write', 'file_test');
109
		$result = Cache::read('rw', 'file_test');
110
 
111
		Cache::write('rw', 'second write', 'file_test');
112
		$resultB = Cache::read('rw', 'file_test');
113
 
114
		Cache::delete('rw', 'file_test');
115
		$this->assertEquals('first write', $result);
116
		$this->assertEquals('second write', $resultB);
117
	}
118
 
119
/**
120
 * testExpiry method
121
 *
122
 * @return void
123
 */
124
	public function testExpiry() {
125
		Cache::set(array('duration' => 1), 'file_test');
126
 
127
		$result = Cache::read('test', 'file_test');
128
		$this->assertFalse($result);
129
 
130
		$data = 'this is a test of the emergency broadcasting system';
131
		$result = Cache::write('other_test', $data, 'file_test');
132
		$this->assertTrue($result);
133
 
134
		sleep(2);
135
		$result = Cache::read('other_test', 'file_test');
136
		$this->assertFalse($result);
137
 
138
		Cache::set(array('duration' => "+1 second"), 'file_test');
139
 
140
		$data = 'this is a test of the emergency broadcasting system';
141
		$result = Cache::write('other_test', $data, 'file_test');
142
		$this->assertTrue($result);
143
 
144
		sleep(2);
145
		$result = Cache::read('other_test', 'file_test');
146
		$this->assertFalse($result);
147
	}
148
 
149
/**
150
 * testDeleteCache method
151
 *
152
 * @return void
153
 */
154
	public function testDeleteCache() {
155
		$data = 'this is a test of the emergency broadcasting system';
156
		$result = Cache::write('delete_test', $data, 'file_test');
157
		$this->assertTrue($result);
158
 
159
		$result = Cache::delete('delete_test', 'file_test');
160
		$this->assertTrue($result);
161
		$this->assertFalse(file_exists(TMP . 'tests' . DS . 'delete_test'));
162
 
163
		$result = Cache::delete('delete_test', 'file_test');
164
		$this->assertFalse($result);
165
	}
166
 
167
/**
168
 * testSerialize method
169
 *
170
 * @return void
171
 */
172
	public function testSerialize() {
173
		Cache::config('file_test', array('engine' => 'File', 'serialize' => true));
174
		$data = 'this is a test of the emergency broadcasting system';
175
		$write = Cache::write('serialize_test', $data, 'file_test');
176
		$this->assertTrue($write);
177
 
178
		Cache::config('file_test', array('serialize' => false));
179
		$read = Cache::read('serialize_test', 'file_test');
180
 
181
		$newread = Cache::read('serialize_test', 'file_test');
182
 
183
		Cache::delete('serialize_test', 'file_test');
184
 
185
		$this->assertSame($read, serialize($data));
186
 
187
		$this->assertSame(unserialize($newread), $data);
188
	}
189
 
190
/**
191
 * testClear method
192
 *
193
 * @return void
194
 */
195
	public function testClear() {
196
		Cache::config('file_test', array('engine' => 'File', 'duration' => 1));
197
 
198
		$data = 'this is a test of the emergency broadcasting system';
199
		Cache::write('serialize_test1', $data, 'file_test');
200
		Cache::write('serialize_test2', $data, 'file_test');
201
		Cache::write('serialize_test3', $data, 'file_test');
202
		$this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
203
		$this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
204
		$this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
205
		sleep(2);
206
		$result = Cache::clear(true, 'file_test');
207
		$this->assertTrue($result);
208
		$this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
209
		$this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
210
		$this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
211
 
212
		$data = 'this is a test of the emergency broadcasting system';
213
		Cache::write('serialize_test1', $data, 'file_test');
214
		Cache::write('serialize_test2', $data, 'file_test');
215
		Cache::write('serialize_test3', $data, 'file_test');
216
		$this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
217
		$this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
218
		$this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
219
 
220
		$result = Cache::clear(false, 'file_test');
221
		$this->assertTrue($result);
222
		$this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
223
		$this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
224
		$this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
225
	}
226
 
227
/**
228
 * test that clear() doesn't wipe files not in the current engine's prefix.
229
 *
230
 * @return void
231
 */
232
	public function testClearWithPrefixes() {
233
		$FileOne = new FileEngine();
234
		$FileOne->init(array(
235
			'prefix' => 'prefix_one_',
236
			'duration' => DAY
237
		));
238
		$FileTwo = new FileEngine();
239
		$FileTwo->init(array(
240
			'prefix' => 'prefix_two_',
241
			'duration' => DAY
242
		));
243
 
244
		$dataOne = $dataTwo = $expected = 'content to cache';
245
		$FileOne->write('prefix_one_key_one', $dataOne, DAY);
246
		$FileTwo->write('prefix_two_key_two', $dataTwo, DAY);
247
 
248
		$this->assertEquals($expected, $FileOne->read('prefix_one_key_one'));
249
		$this->assertEquals($expected, $FileTwo->read('prefix_two_key_two'));
250
 
251
		$FileOne->clear(false);
252
		$this->assertEquals($expected, $FileTwo->read('prefix_two_key_two'), 'secondary config was cleared by accident.');
253
		$FileTwo->clear(false);
254
	}
255
 
256
/**
257
 * Test that clear() also removes files with group tags.
258
 *
259
 * @return void
260
 */
261
	public function testClearWithGroups() {
262
		$engine = new FileEngine();
263
		$engine->init(array(
264
			'prefix' => 'cake_test_',
265
			'duration' => DAY,
266
			'groups' => array('short', 'round')
267
		));
268
		$key = 'cake_test_test_key';
269
		$engine->write($key, 'it works', DAY);
270
		$engine->clear(false);
271
		$this->assertFalse($engine->read($key), 'Key should have been removed');
272
	}
273
 
274
/**
275
 * Test that clear() also removes files with group tags.
276
 *
277
 * @return void
278
 */
279
	public function testClearWithNoKeys() {
280
		$engine = new FileEngine();
281
		$engine->init(array(
282
			'prefix' => 'cake_test_',
283
			'duration' => DAY,
284
			'groups' => array('one', 'two')
285
		));
286
		$key = 'cake_test_test_key';
287
		$engine->clear(false);
288
		$this->assertFalse($engine->read($key), 'No errors should be found');
289
	}
290
 
291
/**
292
 * testKeyPath method
293
 *
294
 * @return void
295
 */
296
	public function testKeyPath() {
297
		$result = Cache::write('views.countries.something', 'here', 'file_test');
298
		$this->assertTrue($result);
299
		$this->assertTrue(file_exists(CACHE . 'cake_views_countries_something'));
300
 
301
		$result = Cache::read('views.countries.something', 'file_test');
302
		$this->assertEquals('here', $result);
303
 
304
		$result = Cache::clear(false, 'file_test');
305
		$this->assertTrue($result);
306
 
307
		$result = Cache::write('domain.test.com:8080', 'here', 'file_test');
308
		$this->assertTrue($result);
309
		$this->assertTrue(file_exists(CACHE . 'cake_domain_test_com_8080'));
310
 
311
		$result = Cache::write('command>dir|more', 'here', 'file_test');
312
		$this->assertTrue($result);
313
		$this->assertTrue(file_exists(CACHE . 'cake_command_dir_more'));
314
	}
315
 
316
/**
317
 * testRemoveWindowsSlashesFromCache method
318
 *
319
 * @return void
320
 */
321
	public function testRemoveWindowsSlashesFromCache() {
322
		Cache::config('windows_test', array('engine' => 'File', 'isWindows' => true, 'prefix' => null, 'path' => TMP));
323
 
324
		$expected = array(
325
			'C:\dev\prj2\sites\cake\libs' => array(
326
 
327
				2 => 'C:\dev\prj2\sites\cake\libs\view\scaffolds', 3 => 'C:\dev\prj2\sites\cake\libs\view\pages',
328
				4 => 'C:\dev\prj2\sites\cake\libs\view\layouts', 5 => 'C:\dev\prj2\sites\cake\libs\view\layouts\xml',
329
				6 => 'C:\dev\prj2\sites\cake\libs\view\layouts\rss', 7 => 'C:\dev\prj2\sites\cake\libs\view\layouts\js',
330
				8 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email', 9 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\text',
331
				10 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\html', 11 => 'C:\dev\prj2\sites\cake\libs\view\helpers',
332
				12 => 'C:\dev\prj2\sites\cake\libs\view\errors', 13 => 'C:\dev\prj2\sites\cake\libs\view\elements',
333
				14 => 'C:\dev\prj2\sites\cake\libs\view\elements\email', 15 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\text',
334
				16 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\html', 17 => 'C:\dev\prj2\sites\cake\libs\model',
335
				18 => 'C:\dev\prj2\sites\cake\libs\model\datasources', 19 => 'C:\dev\prj2\sites\cake\libs\model\datasources\dbo',
336
				20 => 'C:\dev\prj2\sites\cake\libs\model\behaviors', 21 => 'C:\dev\prj2\sites\cake\libs\controller',
337
				22 => 'C:\dev\prj2\sites\cake\libs\controller\components', 23 => 'C:\dev\prj2\sites\cake\libs\cache'),
338
			'C:\dev\prj2\sites\main_site\vendors' => array(
339
 
340
				2 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates', 3 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates\cdc_project',
341
				4 => 'C:\dev\prj2\sites\main_site\vendors\shells\tasks', 5 => 'C:\dev\prj2\sites\main_site\vendors\js',
342
				6 => 'C:\dev\prj2\sites\main_site\vendors\css'),
343
			'C:\dev\prj2\sites\vendors' => array(
344
 
345
				2 => 'C:\dev\prj2\sites\vendors\simpletest\test', 3 => 'C:\dev\prj2\sites\vendors\simpletest\test\support',
346
				4 => 'C:\dev\prj2\sites\vendors\simpletest\test\support\collector', 5 => 'C:\dev\prj2\sites\vendors\simpletest\extensions',
347
				6 => 'C:\dev\prj2\sites\vendors\simpletest\extensions\testdox', 7 => 'C:\dev\prj2\sites\vendors\simpletest\docs',
348
				8 => 'C:\dev\prj2\sites\vendors\simpletest\docs\fr', 9 => 'C:\dev\prj2\sites\vendors\simpletest\docs\en'),
349
			'C:\dev\prj2\sites\main_site\views\helpers' => array(
350
 
351
		);
352
 
353
		Cache::write('test_dir_map', $expected, 'windows_test');
354
		$data = Cache::read('test_dir_map', 'windows_test');
355
		Cache::delete('test_dir_map', 'windows_test');
356
		$this->assertEquals($expected, $data);
357
 
358
		Cache::drop('windows_test');
359
	}
360
 
361
/**
362
 * testWriteQuotedString method
363
 *
364
 * @return void
365
 */
366
	public function testWriteQuotedString() {
367
		Cache::config('file_test', array('engine' => 'File', 'path' => TMP . 'tests'));
368
		Cache::write('App.doubleQuoteTest', '"this is a quoted string"', 'file_test');
369
		$this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
370
		Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
371
		$this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
372
 
373
		Cache::config('file_test', array('isWindows' => true, 'path' => TMP . 'tests'));
374
		$this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
375
		Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
376
		$this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
377
		Cache::delete('App.singleQuoteTest', 'file_test');
378
		Cache::delete('App.doubleQuoteTest', 'file_test');
379
	}
380
 
381
/**
382
 * check that FileEngine does not generate an error when a configured Path does not exist in debug mode.
383
 *
384
 * @return void
385
 */
386
	public function testPathDoesNotExist() {
387
		$this->skipIf(is_dir(TMP . 'tests' . DS . 'autocreate'), 'Cannot run if test directory exists.');
388
 
389
		Cache::config('autocreate', array(
390
			'engine' => 'File',
391
			'path' => TMP . 'tests' . DS . 'autocreate'
392
		));
393
 
394
		Cache::drop('autocreate');
395
	}
396
 
397
/**
398
 * Testing the mask setting in FileEngine
399
 *
400
 * @return void
401
 */
402
	public function testMaskSetting() {
403
		if (DS === '\\') {
404
			$this->markTestSkipped('File permission testing does not work on Windows.');
405
		}
406
		Cache::config('mask_test', array('engine' => 'File', 'path' => TMP . 'tests'));
407
		$data = 'This is some test content';
408
		$write = Cache::write('masking_test', $data, 'mask_test');
409
		$result = substr(sprintf('%o', fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
410
		$expected = '0664';
411
		$this->assertEquals($expected, $result);
412
		Cache::delete('masking_test', 'mask_test');
413
		Cache::drop('mask_test');
414
 
415
		Cache::config('mask_test', array('engine' => 'File', 'mask' => 0666, 'path' => TMP . 'tests'));
416
		Cache::write('masking_test', $data, 'mask_test');
417
		$result = substr(sprintf('%o', fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
418
		$expected = '0666';
419
		$this->assertEquals($expected, $result);
420
		Cache::delete('masking_test', 'mask_test');
421
		Cache::drop('mask_test');
422
 
423
		Cache::config('mask_test', array('engine' => 'File', 'mask' => 0644, 'path' => TMP . 'tests'));
424
		Cache::write('masking_test', $data, 'mask_test');
425
		$result = substr(sprintf('%o', fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
426
		$expected = '0644';
427
		$this->assertEquals($expected, $result);
428
		Cache::delete('masking_test', 'mask_test');
429
		Cache::drop('mask_test');
430
 
431
		Cache::config('mask_test', array('engine' => 'File', 'mask' => 0640, 'path' => TMP . 'tests'));
432
		Cache::write('masking_test', $data, 'mask_test');
433
		$result = substr(sprintf('%o', fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
434
		$expected = '0640';
435
		$this->assertEquals($expected, $result);
436
		Cache::delete('masking_test', 'mask_test');
437
		Cache::drop('mask_test');
438
	}
439
 
440
/**
441
 * Tests that configuring groups for stored keys return the correct values when read/written
442
 *
443
 * @return void
444
 */
445
	public function testGroupsReadWrite() {
446
		Cache::config('file_groups', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b')));
447
		$this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
448
		$this->assertEquals('value', Cache::read('test_groups', 'file_groups'));
449
 
450
		$this->assertTrue(Cache::write('test_groups2', 'value2', 'file_groups'));
451
		$this->assertTrue(Cache::write('test_groups3', 'value3', 'file_groups'));
452
	}
453
 
454
/**
455
 * Test that clearing with repeat writes works properly
456
 *
457
 * @return void
458
 */
459
	public function testClearingWithRepeatWrites() {
460
		Cache::config('repeat', array(
461
			'engine' => 'File', 'groups' => array('users')
462
		));
463
 
464
		$this->assertTrue(Cache::write('user', 'rchavik', 'repeat'));
465
		$this->assertEquals('rchavik', Cache::read('user', 'repeat'));
466
 
467
		Cache::delete('user', 'repeat');
468
		$this->assertEquals(false, Cache::read('user', 'repeat'));
469
 
470
		$this->assertTrue(Cache::write('user', 'ADmad', 'repeat'));
471
		$this->assertEquals('ADmad', Cache::read('user', 'repeat'));
472
 
473
		Cache::clearGroup('users', 'repeat');
474
		$this->assertEquals(false, Cache::read('user', 'repeat'));
475
 
476
		$this->assertTrue(Cache::write('user', 'markstory', 'repeat'));
477
		$this->assertEquals('markstory', Cache::read('user', 'repeat'));
478
 
479
		Cache::drop('repeat');
480
	}
481
 
482
/**
483
 * Tests that deleting from a groups-enabled config is possible
484
 *
485
 * @return void
486
 */
487
	public function testGroupDelete() {
488
		Cache::config('file_groups', array(
489
			'engine' => 'File',
490
			'duration' => 3600,
491
			'groups' => array('group_a', 'group_b')
492
		));
493
		$this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
494
		$this->assertEquals('value', Cache::read('test_groups', 'file_groups'));
495
		$this->assertTrue(Cache::delete('test_groups', 'file_groups'));
496
 
497
		$this->assertFalse(Cache::read('test_groups', 'file_groups'));
498
	}
499
 
500
/**
501
 * Test clearing a cache group
502
 *
503
 * @return void
504
 */
505
	public function testGroupClear() {
506
		Cache::config('file_groups', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b')));
507
		Cache::config('file_groups2', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_b')));
508
		Cache::config('file_groups3', array(
509
			'engine' => 'File',
510
			'duration' => 3600,
511
			'groups' => array('group_b'),
512
			'prefix' => 'leading_',
513
		));
514
 
515
		$this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
516
		$this->assertTrue(Cache::write('test_groups2', 'value 2', 'file_groups2'));
517
		$this->assertTrue(Cache::write('test_groups3', 'value 3', 'file_groups3'));
518
 
519
		$this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
520
		$this->assertFalse(Cache::read('test_groups', 'file_groups'));
521
		$this->assertFalse(Cache::read('test_groups2', 'file_groups2'));
522
		$this->assertEquals('value 3', Cache::read('test_groups3', 'file_groups3'));
523
 
524
		$this->assertTrue(Cache::write('test_groups4', 'value', 'file_groups'));
525
		$this->assertTrue(Cache::write('test_groups5', 'value 2', 'file_groups2'));
526
		$this->assertTrue(Cache::write('test_groups6', 'value 3', 'file_groups3'));
527
 
528
		$this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
529
		$this->assertFalse(Cache::read('test_groups4', 'file_groups'));
530
		$this->assertFalse(Cache::read('test_groups5', 'file_groups2'));
531
		$this->assertEquals('value 3', Cache::read('test_groups6', 'file_groups3'));
532
	}
533
 
534
/**
535
 * Test that clearGroup works with no prefix.
536
 *
537
 * @return void
538
 */
539
	public function testGroupClearNoPrefix() {
540
		Cache::config('file_groups', array(
541
			'engine' => 'File',
542
			'duration' => 3600,
543
			'prefix' => '',
544
			'groups' => array('group_a', 'group_b')
545
		));
546
		Cache::write('key_1', 'value', 'file_groups');
547
		Cache::write('key_2', 'value', 'file_groups');
548
		Cache::clearGroup('group_a', 'file_groups');
549
		$this->assertFalse(Cache::read('key_1', 'file_groups'), 'Did not delete');
550
		$this->assertFalse(Cache::read('key_2', 'file_groups'), 'Did not delete');
551
	}
552
 
553
}