FileEngineTest.php 23 KB

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