FileEngineTest.php 22 KB

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