FileEngineTest.php 20 KB

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