FileEngineTest.php 19 KB

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