FileEngineTest.php 22 KB

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