FileEngineTest.php 18 KB

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