FileEngineTest.php 19 KB

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