FileEngineTest.php 22 KB

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