FileEngineTest.php 23 KB

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