FileEngineTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. Cache::enable();
  32. $this->_configCache();
  33. Cache::clear(false, 'file_test');
  34. }
  35. /**
  36. * tearDown method
  37. *
  38. * @return void
  39. */
  40. public function tearDown() {
  41. Cache::drop('file_test');
  42. Cache::drop('file_groups');
  43. Cache::drop('file_groups2');
  44. Cache::drop('file_groups3');
  45. parent::tearDown();
  46. }
  47. /**
  48. * Helper method for testing.
  49. *
  50. * @param array $config
  51. * @return void
  52. */
  53. protected function _configCache($config = []) {
  54. $defaults = [
  55. 'className' => 'File',
  56. 'path' => TMP . 'tests',
  57. ];
  58. Cache::drop('file_test');
  59. Cache::config('file_test', array_merge($defaults, $config));
  60. }
  61. /**
  62. * testReadAndWriteCache method
  63. *
  64. * @return void
  65. */
  66. public function testReadAndWriteCacheExpired() {
  67. $this->_configCache(['duration' => 1]);
  68. $result = Cache::read('test', 'file_test');
  69. $expecting = '';
  70. $this->assertEquals($expecting, $result);
  71. }
  72. /**
  73. * Test reading and writing to the cache.
  74. *
  75. * @return void
  76. */
  77. public function testReadAndwrite() {
  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(TMP . 'tests/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. $this->_configCache(['duration' => 1]);
  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. $this->_configCache(['duration' => '+1 second']);
  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/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. $this->_configCache(['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. $this->_configCache(['serialize' => false]);
  152. $read = Cache::read('serialize_test', 'file_test');
  153. $delete = Cache::delete('serialize_test', 'file_test');
  154. $this->assertSame($read, serialize($data));
  155. $this->assertSame(unserialize($read), $data);
  156. }
  157. /**
  158. * testClear method
  159. *
  160. * @return void
  161. */
  162. public function testClear() {
  163. $this->_configCache(['duration' => 0]);
  164. $data = 'this is a test of the emergency broadcasting system';
  165. Cache::write('serialize_test1', $data, 'file_test');
  166. Cache::write('serialize_test2', $data, 'file_test');
  167. Cache::write('serialize_test3', $data, 'file_test');
  168. $this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test1'));
  169. $this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test2'));
  170. $this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test3'));
  171. sleep(1);
  172. $result = Cache::clear(true, 'file_test');
  173. $this->assertTrue($result);
  174. $this->assertFalse(file_exists(TMP . 'tests/cake_serialize_test1'));
  175. $this->assertFalse(file_exists(TMP . 'tests/cake_serialize_test2'));
  176. $this->assertFalse(file_exists(TMP . 'tests/cake_serialize_test3'));
  177. $data = 'this is a test of the emergency broadcasting system';
  178. Cache::write('serialize_test1', $data, 'file_test');
  179. Cache::write('serialize_test2', $data, 'file_test');
  180. Cache::write('serialize_test3', $data, 'file_test');
  181. $this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test1'));
  182. $this->assertTrue(file_exists(TMP . 'tests/cake_serialize_test2'));
  183. $this->assertTrue(file_exists(TMP . 'tests/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. * Test that clear() also removes files with group tags.
  217. *
  218. * @return void
  219. */
  220. public function testClearWithGroups() {
  221. $engine = new FileEngine();
  222. $engine->init(array(
  223. 'prefix' => 'cake_test_',
  224. 'duration' => DAY,
  225. 'groups' => array('short', 'round')
  226. ));
  227. $key = 'cake_test_test_key';
  228. $engine->write($key, 'it works', DAY);
  229. $engine->clear(false);
  230. $this->assertFalse($engine->read($key), 'Key should have been removed');
  231. }
  232. /**
  233. * Test that clear() also removes files with group tags.
  234. *
  235. * @return void
  236. */
  237. public function testClearWithNoKeys() {
  238. $engine = new FileEngine();
  239. $engine->init(array(
  240. 'prefix' => 'cake_test_',
  241. 'duration' => DAY,
  242. 'groups' => array('one', 'two')
  243. ));
  244. $key = 'cake_test_test_key';
  245. $engine->clear(false);
  246. $this->assertFalse($engine->read($key), 'No errors should be found');
  247. }
  248. /**
  249. * testKeyPath method
  250. *
  251. * @return void
  252. */
  253. public function testKeyPath() {
  254. $result = Cache::write('views.countries.something', 'here', 'file_test');
  255. $this->assertTrue($result);
  256. $this->assertTrue(file_exists(TMP . 'tests/cake_views_countries_something'));
  257. $result = Cache::read('views.countries.something', 'file_test');
  258. $this->assertEquals('here', $result);
  259. $result = Cache::clear(false, 'file_test');
  260. $this->assertTrue($result);
  261. $result = Cache::write('domain.test.com:8080', 'here', 'file_test');
  262. $this->assertTrue($result);
  263. $this->assertTrue(file_exists(TMP . 'tests/cake_domain_test_com_8080'));
  264. $result = Cache::write('command>dir|more', 'here', 'file_test');
  265. $this->assertTrue($result);
  266. $this->assertTrue(file_exists(TMP . 'tests/cake_command_dir_more'));
  267. }
  268. /**
  269. * testRemoveWindowsSlashesFromCache method
  270. *
  271. * @return void
  272. */
  273. public function testRemoveWindowsSlashesFromCache() {
  274. Cache::config('windows_test', [
  275. 'engine' => 'File',
  276. 'isWindows' => true,
  277. 'prefix' => null,
  278. 'path' => TMP
  279. ]);
  280. $expected = array(
  281. 'C:\dev\prj2\sites\cake\libs' => array(
  282. 0 => 'C:\dev\prj2\sites\cake\libs', 1 => 'C:\dev\prj2\sites\cake\libs\view',
  283. 2 => 'C:\dev\prj2\sites\cake\libs\view\scaffolds', 3 => 'C:\dev\prj2\sites\cake\libs\view\pages',
  284. 4 => 'C:\dev\prj2\sites\cake\libs\view\layouts', 5 => 'C:\dev\prj2\sites\cake\libs\view\layouts\xml',
  285. 6 => 'C:\dev\prj2\sites\cake\libs\view\layouts\rss', 7 => 'C:\dev\prj2\sites\cake\libs\view\layouts\js',
  286. 8 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email', 9 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\text',
  287. 10 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\html', 11 => 'C:\dev\prj2\sites\cake\libs\view\helpers',
  288. 12 => 'C:\dev\prj2\sites\cake\libs\view\errors', 13 => 'C:\dev\prj2\sites\cake\libs\view\elements',
  289. 14 => 'C:\dev\prj2\sites\cake\libs\view\elements\email', 15 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\text',
  290. 16 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\html', 17 => 'C:\dev\prj2\sites\cake\libs\model',
  291. 18 => 'C:\dev\prj2\sites\cake\libs\model\datasources', 19 => 'C:\dev\prj2\sites\cake\libs\model\datasources\dbo',
  292. 20 => 'C:\dev\prj2\sites\cake\libs\model\behaviors', 21 => 'C:\dev\prj2\sites\cake\libs\controller',
  293. 22 => 'C:\dev\prj2\sites\cake\libs\controller\components', 23 => 'C:\dev\prj2\sites\cake\libs\cache'),
  294. 'C:\dev\prj2\sites\main_site\vendors' => array(
  295. 0 => 'C:\dev\prj2\sites\main_site\vendors', 1 => 'C:\dev\prj2\sites\main_site\vendors\shells',
  296. 2 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates', 3 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates\cdc_project',
  297. 4 => 'C:\dev\prj2\sites\main_site\vendors\shells\tasks', 5 => 'C:\dev\prj2\sites\main_site\vendors\js',
  298. 6 => 'C:\dev\prj2\sites\main_site\vendors\css'),
  299. 'C:\dev\prj2\sites\vendors' => array(
  300. 0 => 'C:\dev\prj2\sites\vendors', 1 => 'C:\dev\prj2\sites\vendors\simpletest',
  301. 2 => 'C:\dev\prj2\sites\vendors\simpletest\test', 3 => 'C:\dev\prj2\sites\vendors\simpletest\test\support',
  302. 4 => 'C:\dev\prj2\sites\vendors\simpletest\test\support\collector', 5 => 'C:\dev\prj2\sites\vendors\simpletest\extensions',
  303. 6 => 'C:\dev\prj2\sites\vendors\simpletest\extensions\testdox', 7 => 'C:\dev\prj2\sites\vendors\simpletest\docs',
  304. 8 => 'C:\dev\prj2\sites\vendors\simpletest\docs\fr', 9 => 'C:\dev\prj2\sites\vendors\simpletest\docs\en'),
  305. 'C:\dev\prj2\sites\main_site\views\helpers' => array(
  306. 0 => 'C:\dev\prj2\sites\main_site\views\helpers')
  307. );
  308. Cache::write('test_dir_map', $expected, 'windows_test');
  309. $data = Cache::read('test_dir_map', 'windows_test');
  310. Cache::delete('test_dir_map', 'windows_test');
  311. $this->assertEquals($expected, $data);
  312. Cache::drop('windows_test');
  313. }
  314. /**
  315. * testWriteQuotedString method
  316. *
  317. * @return void
  318. */
  319. public function testWriteQuotedString() {
  320. Cache::write('App.doubleQuoteTest', '"this is a quoted string"', 'file_test');
  321. $this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
  322. Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
  323. $this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
  324. Cache::drop('file_test');
  325. Cache::config('file_test', array(
  326. 'className' => 'File',
  327. 'isWindows' => true,
  328. 'path' => TMP . 'tests'
  329. ));
  330. $this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
  331. Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
  332. $this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
  333. Cache::delete('App.singleQuoteTest', 'file_test');
  334. Cache::delete('App.doubleQuoteTest', 'file_test');
  335. }
  336. /**
  337. * check that FileEngine does not generate an error when a configured Path does not exist in debug mode.
  338. *
  339. * @return void
  340. */
  341. public function testPathDoesNotExist() {
  342. Configure::write('debug', true);
  343. $dir = TMP . 'tests/autocreate-' . microtime(true);
  344. Cache::drop('file_test');
  345. Cache::config('file_test', array(
  346. 'engine' => 'File',
  347. 'path' => $dir
  348. ));
  349. Cache::read('Test', 'file_test');
  350. $this->assertTrue(file_exists($dir), 'Dir should exist.');
  351. // Cleanup
  352. rmdir($dir);
  353. }
  354. /**
  355. * Test that under debug 0 directories do get made.
  356. *
  357. * @return void
  358. */
  359. public function testPathDoesNotExistDebugOff() {
  360. Configure::write('debug', false);
  361. $dir = TMP . 'tests/autocreate-' . microtime(true);
  362. Cache::drop('file_test');
  363. Cache::config('file_test', array(
  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. * Testing the mask setting in FileEngine
  374. *
  375. * @return void
  376. */
  377. public function testMaskSetting() {
  378. if (DS === '\\') {
  379. $this->markTestSkipped('File permission testing does not work on Windows.');
  380. }
  381. Cache::config('mask_test', ['engine' => 'File', 'path' => TMP . 'tests']);
  382. $data = 'This is some test content';
  383. $write = Cache::write('masking_test', $data, 'mask_test');
  384. $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4);
  385. $expected = '0664';
  386. $this->assertEquals($expected, $result);
  387. Cache::delete('masking_test', 'mask_test');
  388. Cache::drop('mask_test');
  389. Cache::config('mask_test', ['engine' => 'File', 'mask' => 0666, 'path' => TMP . 'tests']);
  390. Cache::write('masking_test', $data, 'mask_test');
  391. $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4);
  392. $expected = '0666';
  393. $this->assertEquals($expected, $result);
  394. Cache::delete('masking_test', 'mask_test');
  395. Cache::drop('mask_test');
  396. Cache::config('mask_test', ['engine' => 'File', 'mask' => 0644, 'path' => TMP . 'tests']);
  397. Cache::write('masking_test', $data, 'mask_test');
  398. $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4);
  399. $expected = '0644';
  400. $this->assertEquals($expected, $result);
  401. Cache::delete('masking_test', 'mask_test');
  402. Cache::drop('mask_test');
  403. Cache::config('mask_test', ['engine' => 'File', 'mask' => 0640, 'path' => TMP . 'tests']);
  404. Cache::write('masking_test', $data, 'mask_test');
  405. $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4);
  406. $expected = '0640';
  407. $this->assertEquals($expected, $result);
  408. Cache::delete('masking_test', 'mask_test');
  409. Cache::drop('mask_test');
  410. }
  411. /**
  412. * Tests that configuring groups for stored keys return the correct values when read/written
  413. *
  414. * @return void
  415. */
  416. public function testGroupsReadWrite() {
  417. Cache::config('file_groups', [
  418. 'engine' => 'File',
  419. 'duration' => 3600,
  420. 'groups' => array('group_a', 'group_b')
  421. ]);
  422. $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
  423. $this->assertEquals('value', Cache::read('test_groups', 'file_groups'));
  424. $this->assertTrue(Cache::write('test_groups2', 'value2', 'file_groups'));
  425. $this->assertTrue(Cache::write('test_groups3', 'value3', 'file_groups'));
  426. }
  427. /**
  428. * Test that clearing with repeat writes works properly
  429. *
  430. * @return void
  431. */
  432. public function testClearingWithRepeatWrites() {
  433. Cache::config('repeat', [
  434. 'engine' => 'File',
  435. 'groups' => array('users')
  436. ]);
  437. $this->assertTrue(Cache::write('user', 'rchavik', 'repeat'));
  438. $this->assertEquals('rchavik', Cache::read('user', 'repeat'));
  439. Cache::delete('user', 'repeat');
  440. $this->assertEquals(false, Cache::read('user', 'repeat'));
  441. $this->assertTrue(Cache::write('user', 'ADmad', 'repeat'));
  442. $this->assertEquals('ADmad', Cache::read('user', 'repeat'));
  443. Cache::clearGroup('users', 'repeat');
  444. $this->assertEquals(false, Cache::read('user', 'repeat'));
  445. $this->assertTrue(Cache::write('user', 'markstory', 'repeat'));
  446. $this->assertEquals('markstory', Cache::read('user', 'repeat'));
  447. Cache::drop('repeat');
  448. }
  449. /**
  450. * Tests that deleting from a groups-enabled config is possible
  451. *
  452. * @return void
  453. */
  454. public function testGroupDelete() {
  455. Cache::config('file_groups', [
  456. 'engine' => 'File',
  457. 'duration' => 3600,
  458. 'groups' => array('group_a', 'group_b')
  459. ]);
  460. $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
  461. $this->assertEquals('value', Cache::read('test_groups', 'file_groups'));
  462. $this->assertTrue(Cache::delete('test_groups', 'file_groups'));
  463. $this->assertFalse(Cache::read('test_groups', 'file_groups'));
  464. }
  465. /**
  466. * Test clearing a cache group
  467. *
  468. * @return void
  469. */
  470. public function testGroupClear() {
  471. Cache::config('file_groups', [
  472. 'engine' => 'File',
  473. 'duration' => 3600,
  474. 'groups' => array('group_a', 'group_b')
  475. ]);
  476. Cache::config('file_groups2', [
  477. 'engine' => 'File',
  478. 'duration' => 3600,
  479. 'groups' => array('group_b')
  480. ]);
  481. Cache::config('file_groups3', [
  482. 'engine' => 'File',
  483. 'duration' => 3600,
  484. 'groups' => array('group_b'),
  485. 'prefix' => 'leading_',
  486. ]);
  487. $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
  488. $this->assertTrue(Cache::write('test_groups2', 'value 2', 'file_groups2'));
  489. $this->assertTrue(Cache::write('test_groups3', 'value 3', 'file_groups3'));
  490. $this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
  491. $this->assertFalse(Cache::read('test_groups', 'file_groups'));
  492. $this->assertFalse(Cache::read('test_groups2', 'file_groups2'));
  493. $this->assertEquals('value 3', Cache::read('test_groups3', 'file_groups3'));
  494. $this->assertTrue(Cache::write('test_groups4', 'value', 'file_groups'));
  495. $this->assertTrue(Cache::write('test_groups5', 'value 2', 'file_groups2'));
  496. $this->assertTrue(Cache::write('test_groups6', 'value 3', 'file_groups3'));
  497. $this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
  498. $this->assertFalse(Cache::read('test_groups4', 'file_groups'));
  499. $this->assertFalse(Cache::read('test_groups5', 'file_groups2'));
  500. $this->assertEquals('value 3', Cache::read('test_groups6', 'file_groups3'));
  501. }
  502. /**
  503. * Test that clearGroup works with no prefix.
  504. *
  505. * @return void
  506. */
  507. public function testGroupClearNoPrefix() {
  508. Cache::config('file_groups', array(
  509. 'className' => 'File',
  510. 'duration' => 3600,
  511. 'prefix' => '',
  512. 'groups' => array('group_a', 'group_b')
  513. ));
  514. Cache::write('key_1', 'value', 'file_groups');
  515. Cache::write('key_2', 'value', 'file_groups');
  516. Cache::clearGroup('group_a', 'file_groups');
  517. $this->assertFalse(Cache::read('key_1', 'file_groups'), 'Did not delete');
  518. $this->assertFalse(Cache::read('key_2', 'file_groups'), 'Did not delete');
  519. }
  520. }