FileEngineTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /**
  3. * FileEngineTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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. }
  52. /**
  53. * testCacheDirChange method
  54. *
  55. * @return void
  56. */
  57. public function testCacheDirChange() {
  58. $result = Cache::config('sessions', array('engine'=> 'File', 'path' => TMP . 'sessions'));
  59. $this->assertEqual($result['settings'], Cache::settings('sessions'));
  60. $result = Cache::config('sessions', array('engine'=> 'File', 'path' => TMP . 'tests'));
  61. $this->assertEqual($result['settings'], Cache::settings('sessions'));
  62. $this->assertNotEqual($result['settings'], Cache::settings('default'));
  63. }
  64. /**
  65. * testReadAndWriteCache method
  66. *
  67. * @return void
  68. */
  69. public function testReadAndWriteCache() {
  70. Cache::config('default');
  71. $result = Cache::write(null, 'here', 'file_test');
  72. $this->assertFalse($result);
  73. Cache::set(array('duration' => 1), 'file_test');
  74. $result = Cache::read('test', 'file_test');
  75. $expecting = '';
  76. $this->assertEqual($result, $expecting);
  77. $data = 'this is a test of the emergency broadcasting system';
  78. $result = Cache::write('test', $data, 'file_test');
  79. $this->assertTrue(file_exists(CACHE . 'cake_test'));
  80. $result = Cache::read('test', 'file_test');
  81. $expecting = $data;
  82. $this->assertEqual($result, $expecting);
  83. Cache::delete('test', 'file_test');
  84. }
  85. /**
  86. * testExpiry method
  87. *
  88. * @return void
  89. */
  90. public function testExpiry() {
  91. Cache::set(array('duration' => 1), 'file_test');
  92. $result = Cache::read('test', 'file_test');
  93. $this->assertFalse($result);
  94. $data = 'this is a test of the emergency broadcasting system';
  95. $result = Cache::write('other_test', $data, 'file_test');
  96. $this->assertTrue($result);
  97. sleep(2);
  98. $result = Cache::read('other_test', 'file_test');
  99. $this->assertFalse($result);
  100. Cache::set(array('duration' => "+1 second"), 'file_test');
  101. $data = 'this is a test of the emergency broadcasting system';
  102. $result = Cache::write('other_test', $data, 'file_test');
  103. $this->assertTrue($result);
  104. sleep(2);
  105. $result = Cache::read('other_test', 'file_test');
  106. $this->assertFalse($result);
  107. }
  108. /**
  109. * testDeleteCache method
  110. *
  111. * @return void
  112. */
  113. public function testDeleteCache() {
  114. $data = 'this is a test of the emergency broadcasting system';
  115. $result = Cache::write('delete_test', $data, 'file_test');
  116. $this->assertTrue($result);
  117. $result = Cache::delete('delete_test', 'file_test');
  118. $this->assertTrue($result);
  119. $this->assertFalse(file_exists(TMP . 'tests' . DS . 'delete_test'));
  120. $result = Cache::delete('delete_test', 'file_test');
  121. $this->assertFalse($result);
  122. }
  123. /**
  124. * testSerialize method
  125. *
  126. * @return void
  127. */
  128. public function testSerialize() {
  129. Cache::config('file_test', array('engine' => 'File', 'serialize' => true));
  130. $data = 'this is a test of the emergency broadcasting system';
  131. $write = Cache::write('serialize_test', $data, 'file_test');
  132. $this->assertTrue($write);
  133. Cache::config('file_test', array('serialize' => false));
  134. $read = Cache::read('serialize_test', 'file_test');
  135. $newread = Cache::read('serialize_test', 'file_test');
  136. $delete = Cache::delete('serialize_test', 'file_test');
  137. $this->assertIdentical($read, serialize($data));
  138. $this->assertIdentical(unserialize($newread), $data);
  139. }
  140. /**
  141. * testClear method
  142. *
  143. * @return void
  144. */
  145. public function testClear() {
  146. Cache::config('file_test', array('engine' => 'File', 'duration' => 1));
  147. $data = 'this is a test of the emergency broadcasting system';
  148. $write = Cache::write('serialize_test1', $data, 'file_test');
  149. $write = Cache::write('serialize_test2', $data, 'file_test');
  150. $write = Cache::write('serialize_test3', $data, 'file_test');
  151. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
  152. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
  153. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
  154. sleep(2);
  155. $result = Cache::clear(true, 'file_test');
  156. $this->assertTrue($result);
  157. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
  158. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
  159. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
  160. $data = 'this is a test of the emergency broadcasting system';
  161. $write = Cache::write('serialize_test1', $data, 'file_test');
  162. $write = Cache::write('serialize_test2', $data, 'file_test');
  163. $write = Cache::write('serialize_test3', $data, 'file_test');
  164. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
  165. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
  166. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
  167. $result = Cache::clear(false, 'file_test');
  168. $this->assertTrue($result);
  169. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
  170. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
  171. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
  172. }
  173. /**
  174. * test that clear() doesn't wipe files not in the current engine's prefix.
  175. *
  176. * @return void
  177. */
  178. public function testClearWithPrefixes() {
  179. $FileOne = new FileEngine();
  180. $FileOne->init(array(
  181. 'prefix' => 'prefix_one_',
  182. 'duration' => DAY
  183. ));
  184. $FileTwo = new FileEngine();
  185. $FileTwo->init(array(
  186. 'prefix' => 'prefix_two_',
  187. 'duration' => DAY
  188. ));
  189. $data1 = $data2 = $expected = 'content to cache';
  190. $FileOne->write('prefix_one_key_one', $data1, DAY);
  191. $FileTwo->write('prefix_two_key_two', $data2, DAY);
  192. $this->assertEqual($FileOne->read('prefix_one_key_one'), $expected);
  193. $this->assertEqual($FileTwo->read('prefix_two_key_two'), $expected);
  194. $FileOne->clear(false);
  195. $this->assertEqual($FileTwo->read('prefix_two_key_two'), $expected, 'secondary config was cleared by accident.');
  196. $FileTwo->clear(false);
  197. }
  198. /**
  199. * testKeyPath method
  200. *
  201. * @return void
  202. */
  203. public function testKeyPath() {
  204. $result = Cache::write('views.countries.something', 'here', 'file_test');
  205. $this->assertTrue($result);
  206. $this->assertTrue(file_exists(CACHE . 'cake_views_countries_something'));
  207. $result = Cache::read('views.countries.something', 'file_test');
  208. $this->assertEqual($result, 'here');
  209. $result = Cache::clear(false, 'file_test');
  210. $this->assertTrue($result);
  211. }
  212. /**
  213. * testRemoveWindowsSlashesFromCache method
  214. *
  215. * @return void
  216. */
  217. public function testRemoveWindowsSlashesFromCache() {
  218. Cache::config('windows_test', array('engine' => 'File', 'isWindows' => true, 'prefix' => null, 'path' => TMP));
  219. $expected = array (
  220. 'C:\dev\prj2\sites\cake\libs' => array(
  221. 0 => 'C:\dev\prj2\sites\cake\libs', 1 => 'C:\dev\prj2\sites\cake\libs\view',
  222. 2 => 'C:\dev\prj2\sites\cake\libs\view\scaffolds', 3 => 'C:\dev\prj2\sites\cake\libs\view\pages',
  223. 4 => 'C:\dev\prj2\sites\cake\libs\view\layouts', 5 => 'C:\dev\prj2\sites\cake\libs\view\layouts\xml',
  224. 6 => 'C:\dev\prj2\sites\cake\libs\view\layouts\rss', 7 => 'C:\dev\prj2\sites\cake\libs\view\layouts\js',
  225. 8 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email', 9 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\text',
  226. 10 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\html', 11 => 'C:\dev\prj2\sites\cake\libs\view\helpers',
  227. 12 => 'C:\dev\prj2\sites\cake\libs\view\errors', 13 => 'C:\dev\prj2\sites\cake\libs\view\elements',
  228. 14 => 'C:\dev\prj2\sites\cake\libs\view\elements\email', 15 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\text',
  229. 16 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\html', 17 => 'C:\dev\prj2\sites\cake\libs\model',
  230. 18 => 'C:\dev\prj2\sites\cake\libs\model\datasources', 19 => 'C:\dev\prj2\sites\cake\libs\model\datasources\dbo',
  231. 20 => 'C:\dev\prj2\sites\cake\libs\model\behaviors', 21 => 'C:\dev\prj2\sites\cake\libs\controller',
  232. 22 => 'C:\dev\prj2\sites\cake\libs\controller\components', 23 => 'C:\dev\prj2\sites\cake\libs\cache'),
  233. 'C:\dev\prj2\sites\main_site\vendors' => array(
  234. 0 => 'C:\dev\prj2\sites\main_site\vendors', 1 => 'C:\dev\prj2\sites\main_site\vendors\shells',
  235. 2 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates', 3 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates\cdc_project',
  236. 4 => 'C:\dev\prj2\sites\main_site\vendors\shells\tasks', 5 => 'C:\dev\prj2\sites\main_site\vendors\js',
  237. 6 => 'C:\dev\prj2\sites\main_site\vendors\css'),
  238. 'C:\dev\prj2\sites\vendors' => array(
  239. 0 => 'C:\dev\prj2\sites\vendors', 1 => 'C:\dev\prj2\sites\vendors\simpletest',
  240. 2 => 'C:\dev\prj2\sites\vendors\simpletest\test', 3 => 'C:\dev\prj2\sites\vendors\simpletest\test\support',
  241. 4 => 'C:\dev\prj2\sites\vendors\simpletest\test\support\collector', 5 => 'C:\dev\prj2\sites\vendors\simpletest\extensions',
  242. 6 => 'C:\dev\prj2\sites\vendors\simpletest\extensions\testdox', 7 => 'C:\dev\prj2\sites\vendors\simpletest\docs',
  243. 8 => 'C:\dev\prj2\sites\vendors\simpletest\docs\fr', 9 => 'C:\dev\prj2\sites\vendors\simpletest\docs\en'),
  244. 'C:\dev\prj2\sites\main_site\views\helpers' => array(
  245. 0 => 'C:\dev\prj2\sites\main_site\views\helpers')
  246. );
  247. Cache::write('test_dir_map', $expected, 'windows_test');
  248. $data = Cache::read('test_dir_map', 'windows_test');
  249. Cache::delete('test_dir_map', 'windows_test');
  250. $this->assertEqual($expected, $data);
  251. Cache::drop('windows_test');
  252. }
  253. /**
  254. * testWriteQuotedString method
  255. *
  256. * @return void
  257. */
  258. public function testWriteQuotedString() {
  259. Cache::config('file_test', array('engine' => 'File', 'path' => TMP . 'tests'));
  260. Cache::write('App.doubleQuoteTest', '"this is a quoted string"', 'file_test');
  261. $this->assertIdentical(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
  262. Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
  263. $this->assertIdentical(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
  264. Cache::config('file_test', array('isWindows' => true, 'path' => TMP . 'tests'));
  265. $this->assertIdentical(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
  266. Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
  267. $this->assertIdentical(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
  268. Cache::delete('App.singleQuoteTest', 'file_test');
  269. Cache::delete('App.doubleQuoteTest', 'file_test');
  270. }
  271. /**
  272. * check that FileEngine generates an error when a configured Path does not exist.
  273. *
  274. * @expectedException PHPUnit_Framework_Error_Warning
  275. * @return void
  276. */
  277. public function testErrorWhenPathDoesNotExist() {
  278. $this->skipIf(is_dir(TMP . 'tests' . DS . 'file_failure'), 'Cannot run test directory exists.');
  279. Cache::config('failure', array(
  280. 'engine' => 'File',
  281. 'path' => TMP . 'tests' . DS . 'file_failure'
  282. ));
  283. Cache::drop('failure');
  284. }
  285. /**
  286. * Testing the mask setting in FileEngine
  287. *
  288. * @return void
  289. */
  290. public function testMaskSetting() {
  291. Cache::config('mask_test', array('engine' => 'File', 'path' => TMP . 'tests'));
  292. $data = 'This is some test content';
  293. $write = Cache::write('masking_test', $data, 'mask_test');
  294. $result = substr(sprintf('%o',fileperms(TMP . 'tests' . DS .'cake_masking_test')), -4);
  295. $expected = '0664';
  296. $this->assertEqual($result, $expected);
  297. Cache::delete('masking_test', 'mask_test');
  298. Cache::drop('mask_test');
  299. Cache::config('mask_test', array('engine' => 'File', 'mask' => 0666, 'path' => TMP . 'tests'));
  300. $write = Cache::write('masking_test', $data, 'mask_test');
  301. $result = substr(sprintf('%o',fileperms(TMP . 'tests' . DS .'cake_masking_test')), -4);
  302. $expected = '0666';
  303. $this->assertEqual($result, $expected);
  304. Cache::delete('masking_test', 'mask_test');
  305. Cache::drop('mask_test');
  306. Cache::config('mask_test', array('engine' => 'File', 'mask' => 0644, 'path' => TMP . 'tests'));
  307. $write = Cache::write('masking_test', $data, 'mask_test');
  308. $result = substr(sprintf('%o',fileperms(TMP . 'tests' . DS .'cake_masking_test')), -4);
  309. $expected = '0644';
  310. $this->assertEqual($result, $expected);
  311. Cache::delete('masking_test', 'mask_test');
  312. Cache::drop('mask_test');
  313. Cache::config('mask_test', array('engine' => 'File', 'mask' => 0640, 'path' => TMP . 'tests'));
  314. $write = Cache::write('masking_test', $data, 'mask_test');
  315. $result = substr(sprintf('%o',fileperms(TMP . 'tests' . DS .'cake_masking_test')), -4);
  316. $expected = '0640';
  317. $this->assertEqual($result, $expected);
  318. Cache::delete('masking_test', 'mask_test');
  319. Cache::drop('mask_test');
  320. }
  321. }