FileEngineTest.php 17 KB

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