FileEngineTest.php 16 KB

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