FileEngineTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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')
  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. * testKeyPath method
  235. *
  236. * @return void
  237. */
  238. public function testKeyPath() {
  239. $result = Cache::write('views.countries.something', 'here', 'file_test');
  240. $this->assertTrue($result);
  241. $this->assertTrue(file_exists(CACHE . 'cake_views_countries_something'));
  242. $result = Cache::read('views.countries.something', 'file_test');
  243. $this->assertEquals('here', $result);
  244. $result = Cache::clear(false, 'file_test');
  245. $this->assertTrue($result);
  246. }
  247. /**
  248. * testRemoveWindowsSlashesFromCache method
  249. *
  250. * @return void
  251. */
  252. public function testRemoveWindowsSlashesFromCache() {
  253. Cache::config('windows_test', array('engine' => 'File', 'isWindows' => true, 'prefix' => null, 'path' => TMP));
  254. $expected = array(
  255. 'C:\dev\prj2\sites\cake\libs' => array(
  256. 0 => 'C:\dev\prj2\sites\cake\libs', 1 => 'C:\dev\prj2\sites\cake\libs\view',
  257. 2 => 'C:\dev\prj2\sites\cake\libs\view\scaffolds', 3 => 'C:\dev\prj2\sites\cake\libs\view\pages',
  258. 4 => 'C:\dev\prj2\sites\cake\libs\view\layouts', 5 => 'C:\dev\prj2\sites\cake\libs\view\layouts\xml',
  259. 6 => 'C:\dev\prj2\sites\cake\libs\view\layouts\rss', 7 => 'C:\dev\prj2\sites\cake\libs\view\layouts\js',
  260. 8 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email', 9 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\text',
  261. 10 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\html', 11 => 'C:\dev\prj2\sites\cake\libs\view\helpers',
  262. 12 => 'C:\dev\prj2\sites\cake\libs\view\errors', 13 => 'C:\dev\prj2\sites\cake\libs\view\elements',
  263. 14 => 'C:\dev\prj2\sites\cake\libs\view\elements\email', 15 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\text',
  264. 16 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\html', 17 => 'C:\dev\prj2\sites\cake\libs\model',
  265. 18 => 'C:\dev\prj2\sites\cake\libs\model\datasources', 19 => 'C:\dev\prj2\sites\cake\libs\model\datasources\dbo',
  266. 20 => 'C:\dev\prj2\sites\cake\libs\model\behaviors', 21 => 'C:\dev\prj2\sites\cake\libs\controller',
  267. 22 => 'C:\dev\prj2\sites\cake\libs\controller\components', 23 => 'C:\dev\prj2\sites\cake\libs\cache'),
  268. 'C:\dev\prj2\sites\main_site\vendors' => array(
  269. 0 => 'C:\dev\prj2\sites\main_site\vendors', 1 => 'C:\dev\prj2\sites\main_site\vendors\shells',
  270. 2 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates', 3 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates\cdc_project',
  271. 4 => 'C:\dev\prj2\sites\main_site\vendors\shells\tasks', 5 => 'C:\dev\prj2\sites\main_site\vendors\js',
  272. 6 => 'C:\dev\prj2\sites\main_site\vendors\css'),
  273. 'C:\dev\prj2\sites\vendors' => array(
  274. 0 => 'C:\dev\prj2\sites\vendors', 1 => 'C:\dev\prj2\sites\vendors\simpletest',
  275. 2 => 'C:\dev\prj2\sites\vendors\simpletest\test', 3 => 'C:\dev\prj2\sites\vendors\simpletest\test\support',
  276. 4 => 'C:\dev\prj2\sites\vendors\simpletest\test\support\collector', 5 => 'C:\dev\prj2\sites\vendors\simpletest\extensions',
  277. 6 => 'C:\dev\prj2\sites\vendors\simpletest\extensions\testdox', 7 => 'C:\dev\prj2\sites\vendors\simpletest\docs',
  278. 8 => 'C:\dev\prj2\sites\vendors\simpletest\docs\fr', 9 => 'C:\dev\prj2\sites\vendors\simpletest\docs\en'),
  279. 'C:\dev\prj2\sites\main_site\views\helpers' => array(
  280. 0 => 'C:\dev\prj2\sites\main_site\views\helpers')
  281. );
  282. Cache::write('test_dir_map', $expected, 'windows_test');
  283. $data = Cache::read('test_dir_map', 'windows_test');
  284. Cache::delete('test_dir_map', 'windows_test');
  285. $this->assertEquals($expected, $data);
  286. Cache::drop('windows_test');
  287. }
  288. /**
  289. * testWriteQuotedString method
  290. *
  291. * @return void
  292. */
  293. public function testWriteQuotedString() {
  294. Cache::config('file_test', array('engine' => 'File', 'path' => TMP . 'tests'));
  295. Cache::write('App.doubleQuoteTest', '"this is a quoted string"', 'file_test');
  296. $this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
  297. Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
  298. $this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
  299. Cache::config('file_test', array('isWindows' => true, 'path' => TMP . 'tests'));
  300. $this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
  301. Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
  302. $this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
  303. Cache::delete('App.singleQuoteTest', 'file_test');
  304. Cache::delete('App.doubleQuoteTest', 'file_test');
  305. }
  306. /**
  307. * check that FileEngine generates an error when a configured Path does not exist.
  308. *
  309. * @expectedException PHPUnit_Framework_Error_Warning
  310. * @return void
  311. */
  312. public function testErrorWhenPathDoesNotExist() {
  313. $this->skipIf(is_dir(TMP . 'tests' . DS . 'file_failure'), 'Cannot run test directory exists.');
  314. Cache::config('failure', array(
  315. 'engine' => 'File',
  316. 'path' => TMP . 'tests' . DS . 'file_failure'
  317. ));
  318. Cache::drop('failure');
  319. }
  320. /**
  321. * Testing the mask setting in FileEngine
  322. *
  323. * @return void
  324. */
  325. public function testMaskSetting() {
  326. if (DS === '\\') {
  327. $this->markTestSkipped('File permission testing does not work on Windows.');
  328. }
  329. Cache::config('mask_test', array('engine' => 'File', 'path' => TMP . 'tests'));
  330. $data = 'This is some test content';
  331. $write = Cache::write('masking_test', $data, 'mask_test');
  332. $result = substr(sprintf('%o',fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
  333. $expected = '0664';
  334. $this->assertEquals($expected, $result);
  335. Cache::delete('masking_test', 'mask_test');
  336. Cache::drop('mask_test');
  337. Cache::config('mask_test', array('engine' => 'File', 'mask' => 0666, 'path' => TMP . 'tests'));
  338. $write = Cache::write('masking_test', $data, 'mask_test');
  339. $result = substr(sprintf('%o',fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
  340. $expected = '0666';
  341. $this->assertEquals($expected, $result);
  342. Cache::delete('masking_test', 'mask_test');
  343. Cache::drop('mask_test');
  344. Cache::config('mask_test', array('engine' => 'File', 'mask' => 0644, 'path' => TMP . 'tests'));
  345. $write = Cache::write('masking_test', $data, 'mask_test');
  346. $result = substr(sprintf('%o',fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
  347. $expected = '0644';
  348. $this->assertEquals($expected, $result);
  349. Cache::delete('masking_test', 'mask_test');
  350. Cache::drop('mask_test');
  351. Cache::config('mask_test', array('engine' => 'File', 'mask' => 0640, 'path' => TMP . 'tests'));
  352. $write = Cache::write('masking_test', $data, 'mask_test');
  353. $result = substr(sprintf('%o',fileperms(TMP . 'tests' . DS . 'cake_masking_test')), -4);
  354. $expected = '0640';
  355. $this->assertEquals($expected, $result);
  356. Cache::delete('masking_test', 'mask_test');
  357. Cache::drop('mask_test');
  358. }
  359. /**
  360. * Tests that configuring groups for stored keys return the correct values when read/written
  361. *
  362. * @return void
  363. */
  364. public function testGroupsReadWrite() {
  365. Cache::config('file_groups', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b')));
  366. $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
  367. $this->assertEquals('value', Cache::read('test_groups', 'file_groups'));
  368. $this->assertTrue(Cache::write('test_groups2', 'value2', 'file_groups'));
  369. $this->assertTrue(Cache::write('test_groups3', 'value3', 'file_groups'));
  370. }
  371. /**
  372. * Test that clearing with repeat writes works properly
  373. */
  374. public function testClearingWithRepeatWrites() {
  375. Cache::config('repeat', array(
  376. 'engine' => 'File', 'groups' => array('users')
  377. ));
  378. $this->assertTrue(Cache::write('user', 'rchavik', 'repeat'));
  379. $this->assertEquals('rchavik', Cache::read('user', 'repeat'));
  380. Cache::delete('user', 'repeat');
  381. $this->assertEquals(false, Cache::read('user', 'repeat'));
  382. $this->assertTrue(Cache::write('user', 'ADmad', 'repeat'));
  383. $this->assertEquals('ADmad', Cache::read('user', 'repeat'));
  384. Cache::clearGroup('users', 'repeat');
  385. $this->assertEquals(false, Cache::read('user', 'repeat'));
  386. $this->assertTrue(Cache::write('user', 'markstory', 'repeat'));
  387. $this->assertEquals('markstory', Cache::read('user', 'repeat'));
  388. Cache::drop('repeat');
  389. }
  390. /**
  391. * Tests that deleting from a groups-enabled config is possible
  392. *
  393. * @return void
  394. */
  395. public function testGroupDelete() {
  396. Cache::config('file_groups', array(
  397. 'engine' => 'File',
  398. 'duration' => 3600,
  399. 'groups' => array('group_a', 'group_b')
  400. ));
  401. $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
  402. $this->assertEquals('value', Cache::read('test_groups', 'file_groups'));
  403. $this->assertTrue(Cache::delete('test_groups', 'file_groups'));
  404. $this->assertFalse(Cache::read('test_groups', 'file_groups'));
  405. }
  406. /**
  407. * Test clearing a cache group
  408. *
  409. * @return void
  410. */
  411. public function testGroupClear() {
  412. Cache::config('file_groups', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b')));
  413. Cache::config('file_groups2', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_b')));
  414. Cache::config('file_groups3', array(
  415. 'engine' => 'File',
  416. 'duration' => 3600,
  417. 'groups' => array('group_b'),
  418. 'prefix' => 'leading_',
  419. ));
  420. $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
  421. $this->assertTrue(Cache::write('test_groups2', 'value 2', 'file_groups2'));
  422. $this->assertTrue(Cache::write('test_groups3', 'value 3', 'file_groups3'));
  423. $this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
  424. $this->assertFalse(Cache::read('test_groups', 'file_groups'));
  425. $this->assertFalse(Cache::read('test_groups2', 'file_groups2'));
  426. $this->assertEquals('value 3', Cache::read('test_groups3', 'file_groups3'));
  427. $this->assertTrue(Cache::write('test_groups4', 'value', 'file_groups'));
  428. $this->assertTrue(Cache::write('test_groups5', 'value 2', 'file_groups2'));
  429. $this->assertTrue(Cache::write('test_groups6', 'value 3', 'file_groups3'));
  430. $this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
  431. $this->assertFalse(Cache::read('test_groups4', 'file_groups'));
  432. $this->assertFalse(Cache::read('test_groups5', 'file_groups2'));
  433. $this->assertEquals('value 3', Cache::read('test_groups6', 'file_groups3'));
  434. }
  435. }