FileEngineTest.php 19 KB

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