FileEngineTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 = 'content to cache';
  228. $dataTwo = 'content to cache';
  229. $expected = 'content to cache';
  230. $FileOne->set('prefix_one_key_one', $dataOne);
  231. $FileTwo->set('prefix_two_key_two', $dataTwo);
  232. $this->assertSame($expected, $FileOne->get('prefix_one_key_one'));
  233. $this->assertSame($expected, $FileTwo->get('prefix_two_key_two'));
  234. $FileOne->clear();
  235. $this->assertSame($expected, $FileTwo->get('prefix_two_key_two'), 'secondary config was cleared by accident.');
  236. $FileTwo->clear();
  237. }
  238. /**
  239. * Test that clear() also removes files with group tags.
  240. */
  241. public function testClearWithGroups(): void
  242. {
  243. $engine = new FileEngine();
  244. $engine->init([
  245. 'prefix' => 'cake_test_',
  246. 'duration' => 3600,
  247. 'groups' => ['short', 'round'],
  248. ]);
  249. $key = 'cake_test_test_key';
  250. $engine->set($key, 'it works');
  251. $engine->clear();
  252. $this->assertNull($engine->get($key), 'Key should have been removed');
  253. }
  254. /**
  255. * Test that clear() also removes files with group tags.
  256. */
  257. public function testClearWithNoKeys(): void
  258. {
  259. $engine = new FileEngine();
  260. $engine->init([
  261. 'prefix' => 'cake_test_',
  262. 'duration' => 3600,
  263. 'groups' => ['one', 'two'],
  264. ]);
  265. $key = 'cake_test_test_key';
  266. $engine->clear();
  267. $this->assertNull($engine->get($key), 'No errors should be found');
  268. }
  269. /**
  270. * testKeyPath method
  271. */
  272. public function testKeyPath(): void
  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->assertSame('here', $result);
  279. $key = 'colon:quote"slash/brackets[]';
  280. $result = Cache::write($key, 'here', 'file_test');
  281. $this->assertTrue($result);
  282. $this->assertFileExists(TMP . 'tests/cake_colon%3Aquote%22slash%2Fbrackets%5B%5D');
  283. $result = Cache::read($key, 'file_test');
  284. $this->assertSame('here', $result);
  285. $result = Cache::clear('file_test');
  286. $this->assertTrue($result);
  287. }
  288. /**
  289. * testRemoveWindowsSlashesFromCache method
  290. */
  291. public function testRemoveWindowsSlashesFromCache(): void
  292. {
  293. Cache::setConfig('windows_test', [
  294. 'engine' => 'File',
  295. 'prefix' => null,
  296. 'path' => CACHE,
  297. ]);
  298. $expected = [
  299. 'C:\dev\prj2\sites\cake\libs' => [
  300. 0 => 'C:\dev\prj2\sites\cake\libs', 1 => 'C:\dev\prj2\sites\cake\libs\view',
  301. 2 => 'C:\dev\prj2\sites\cake\libs\view\scaffolds', 3 => 'C:\dev\prj2\sites\cake\libs\view\pages',
  302. 4 => 'C:\dev\prj2\sites\cake\libs\view\layouts', 5 => 'C:\dev\prj2\sites\cake\libs\view\layouts\xml',
  303. 6 => 'C:\dev\prj2\sites\cake\libs\view\layouts\rss', 7 => 'C:\dev\prj2\sites\cake\libs\view\layouts\js',
  304. 8 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email', 9 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\text',
  305. 10 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\html', 11 => 'C:\dev\prj2\sites\cake\libs\view\helpers',
  306. 12 => 'C:\dev\prj2\sites\cake\libs\view\errors', 13 => 'C:\dev\prj2\sites\cake\libs\view\elements',
  307. 14 => 'C:\dev\prj2\sites\cake\libs\view\elements\email', 15 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\text',
  308. 16 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\html', 17 => 'C:\dev\prj2\sites\cake\libs\model',
  309. 18 => 'C:\dev\prj2\sites\cake\libs\model\datasources', 19 => 'C:\dev\prj2\sites\cake\libs\model\datasources\dbo',
  310. 20 => 'C:\dev\prj2\sites\cake\libs\model\behaviors', 21 => 'C:\dev\prj2\sites\cake\libs\controller',
  311. 22 => 'C:\dev\prj2\sites\cake\libs\controller\components', 23 => 'C:\dev\prj2\sites\cake\libs\cache'],
  312. 'C:\dev\prj2\sites\main_site\vendors' => [
  313. 0 => 'C:\dev\prj2\sites\main_site\vendors', 1 => 'C:\dev\prj2\sites\main_site\vendors\shells',
  314. 2 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates', 3 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates\cdc_project',
  315. 4 => 'C:\dev\prj2\sites\main_site\vendors\shells\tasks', 5 => 'C:\dev\prj2\sites\main_site\vendors\js',
  316. 6 => 'C:\dev\prj2\sites\main_site\vendors\css'],
  317. 'C:\dev\prj2\sites\vendors' => [
  318. 0 => 'C:\dev\prj2\sites\vendors', 1 => 'C:\dev\prj2\sites\vendors\simpletest',
  319. 2 => 'C:\dev\prj2\sites\vendors\simpletest\test', 3 => 'C:\dev\prj2\sites\vendors\simpletest\test\support',
  320. 4 => 'C:\dev\prj2\sites\vendors\simpletest\test\support\collector', 5 => 'C:\dev\prj2\sites\vendors\simpletest\extensions',
  321. 6 => 'C:\dev\prj2\sites\vendors\simpletest\extensions\testdox', 7 => 'C:\dev\prj2\sites\vendors\simpletest\docs',
  322. 8 => 'C:\dev\prj2\sites\vendors\simpletest\docs\fr', 9 => 'C:\dev\prj2\sites\vendors\simpletest\docs\en'],
  323. 'C:\dev\prj2\sites\main_site\views\helpers' => [
  324. 0 => 'C:\dev\prj2\sites\main_site\views\helpers'],
  325. ];
  326. Cache::write('test_dir_map', $expected, 'windows_test');
  327. $data = Cache::read('test_dir_map', 'windows_test');
  328. Cache::delete('test_dir_map', 'windows_test');
  329. $this->assertEquals($expected, $data);
  330. Cache::drop('windows_test');
  331. }
  332. /**
  333. * testWriteQuotedString method
  334. */
  335. public function testWriteQuotedString(): void
  336. {
  337. Cache::write('App.doubleQuoteTest', '"this is a quoted string"', 'file_test');
  338. $this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
  339. Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
  340. $this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
  341. Cache::drop('file_test');
  342. Cache::setConfig('file_test', [
  343. 'className' => 'File',
  344. 'isWindows' => true,
  345. 'path' => TMP . 'tests',
  346. ]);
  347. $this->assertSame(Cache::read('App.doubleQuoteTest', 'file_test'), '"this is a quoted string"');
  348. Cache::write('App.singleQuoteTest', "'this is a quoted string'", 'file_test');
  349. $this->assertSame(Cache::read('App.singleQuoteTest', 'file_test'), "'this is a quoted string'");
  350. Cache::delete('App.singleQuoteTest', 'file_test');
  351. Cache::delete('App.doubleQuoteTest', 'file_test');
  352. }
  353. /**
  354. * check that FileEngine does not generate an error when a configured Path does not exist in debug mode.
  355. */
  356. public function testPathDoesNotExist(): void
  357. {
  358. Configure::write('debug', true);
  359. $dir = TMP . 'tests/autocreate-' . microtime(true);
  360. Cache::drop('file_test');
  361. Cache::setConfig('file_test', [
  362. 'engine' => 'File',
  363. 'path' => $dir,
  364. ]);
  365. Cache::read('Test', 'file_test');
  366. $this->assertFileExists($dir, 'Dir should exist.');
  367. // Cleanup
  368. rmdir($dir);
  369. }
  370. /**
  371. * Test that under debug 0 directories do get made.
  372. */
  373. public function testPathDoesNotExistDebugOff(): void
  374. {
  375. Configure::write('debug', false);
  376. $dir = TMP . 'tests/autocreate-' . microtime(true);
  377. Cache::drop('file_test');
  378. Cache::setConfig('file_test', [
  379. 'engine' => 'File',
  380. 'path' => $dir,
  381. ]);
  382. Cache::read('Test', 'file_test');
  383. $this->assertFileExists($dir, 'Dir should exist.');
  384. // Cleanup
  385. rmdir($dir);
  386. }
  387. /**
  388. * Testing the mask setting in FileEngine
  389. */
  390. public function testMaskSetting(): void
  391. {
  392. if (DS === '\\') {
  393. $this->markTestSkipped('File permission testing does not work on Windows.');
  394. }
  395. Cache::setConfig('mask_test', ['engine' => 'File', 'path' => TMP . 'tests']);
  396. $data = 'This is some test content';
  397. Cache::write('masking_test', $data, 'mask_test');
  398. $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4);
  399. $expected = '0664';
  400. $this->assertSame($expected, $result);
  401. Cache::delete('masking_test', 'mask_test');
  402. Cache::drop('mask_test');
  403. Cache::setConfig('mask_test', ['engine' => 'File', 'mask' => 0666, 'path' => TMP . 'tests']);
  404. Cache::write('masking_test', $data, 'mask_test');
  405. $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4);
  406. $expected = '0666';
  407. $this->assertSame($expected, $result);
  408. Cache::delete('masking_test', 'mask_test');
  409. Cache::drop('mask_test');
  410. Cache::setConfig('mask_test', ['engine' => 'File', 'mask' => 0644, 'path' => TMP . 'tests']);
  411. Cache::write('masking_test', $data, 'mask_test');
  412. $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4);
  413. $expected = '0644';
  414. $this->assertSame($expected, $result);
  415. Cache::delete('masking_test', 'mask_test');
  416. Cache::drop('mask_test');
  417. Cache::setConfig('mask_test', ['engine' => 'File', 'mask' => 0640, 'path' => TMP . 'tests']);
  418. Cache::write('masking_test', $data, 'mask_test');
  419. $result = substr(sprintf('%o', fileperms(TMP . 'tests/cake_masking_test')), -4);
  420. $expected = '0640';
  421. $this->assertSame($expected, $result);
  422. Cache::delete('masking_test', 'mask_test');
  423. Cache::drop('mask_test');
  424. }
  425. /**
  426. * Tests that configuring groups for stored keys return the correct values when read/written
  427. */
  428. public function testGroupsReadWrite(): void
  429. {
  430. Cache::setConfig('file_groups', [
  431. 'engine' => 'File',
  432. 'duration' => 3600,
  433. 'groups' => ['group_a', 'group_b'],
  434. ]);
  435. $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
  436. $this->assertSame('value', Cache::read('test_groups', 'file_groups'));
  437. $this->assertTrue(Cache::write('test_groups2', 'value2', 'file_groups'));
  438. $this->assertTrue(Cache::write('test_groups3', 'value3', 'file_groups'));
  439. }
  440. /**
  441. * Test that clearing with repeat writes works properly
  442. */
  443. public function testClearingWithRepeatWrites(): void
  444. {
  445. Cache::setConfig('repeat', [
  446. 'engine' => 'File',
  447. 'groups' => ['users'],
  448. ]);
  449. $this->assertTrue(Cache::write('user', 'rchavik', 'repeat'));
  450. $this->assertSame('rchavik', Cache::read('user', 'repeat'));
  451. Cache::delete('user', 'repeat');
  452. $this->assertNull(Cache::read('user', 'repeat'));
  453. $this->assertTrue(Cache::write('user', 'ADmad', 'repeat'));
  454. $this->assertSame('ADmad', Cache::read('user', 'repeat'));
  455. Cache::clearGroup('users', 'repeat');
  456. $this->assertNull(Cache::read('user', 'repeat'));
  457. $this->assertTrue(Cache::write('user', 'markstory', 'repeat'));
  458. $this->assertSame('markstory', Cache::read('user', 'repeat'));
  459. Cache::drop('repeat');
  460. }
  461. /**
  462. * Tests that deleting from a groups-enabled config is possible
  463. */
  464. public function testGroupDelete(): void
  465. {
  466. Cache::setConfig('file_groups', [
  467. 'engine' => 'File',
  468. 'duration' => 3600,
  469. 'groups' => ['group_a', 'group_b'],
  470. ]);
  471. $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
  472. $this->assertSame('value', Cache::read('test_groups', 'file_groups'));
  473. $this->assertTrue(Cache::delete('test_groups', 'file_groups'));
  474. $this->assertNull(Cache::read('test_groups', 'file_groups'));
  475. }
  476. /**
  477. * Test clearing a cache group
  478. */
  479. public function testGroupClear(): void
  480. {
  481. Cache::setConfig('file_groups', [
  482. 'engine' => 'File',
  483. 'duration' => 3600,
  484. 'groups' => ['group_a', 'group_b'],
  485. ]);
  486. Cache::setConfig('file_groups2', [
  487. 'engine' => 'File',
  488. 'duration' => 3600,
  489. 'groups' => ['group_b'],
  490. ]);
  491. Cache::setConfig('file_groups3', [
  492. 'engine' => 'File',
  493. 'duration' => 3600,
  494. 'groups' => ['group_b'],
  495. 'prefix' => 'leading_',
  496. ]);
  497. $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
  498. $this->assertTrue(Cache::write('test_groups2', 'value 2', 'file_groups2'));
  499. $this->assertTrue(Cache::write('test_groups3', 'value 3', 'file_groups3'));
  500. $this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
  501. $this->assertNull(Cache::read('test_groups', 'file_groups'));
  502. $this->assertNull(Cache::read('test_groups2', 'file_groups2'));
  503. $this->assertSame('value 3', Cache::read('test_groups3', 'file_groups3'));
  504. $this->assertTrue(Cache::write('test_groups4', 'value', 'file_groups'));
  505. $this->assertTrue(Cache::write('test_groups5', 'value 2', 'file_groups2'));
  506. $this->assertTrue(Cache::write('test_groups6', 'value 3', 'file_groups3'));
  507. $this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
  508. $this->assertNull(Cache::read('test_groups4', 'file_groups'));
  509. $this->assertNull(Cache::read('test_groups5', 'file_groups2'));
  510. $this->assertSame('value 3', Cache::read('test_groups6', 'file_groups3'));
  511. }
  512. /**
  513. * Test that clearGroup works with no prefix.
  514. */
  515. public function testGroupClearNoPrefix(): void
  516. {
  517. Cache::setConfig('file_groups', [
  518. 'className' => 'File',
  519. 'duration' => 3600,
  520. 'prefix' => '',
  521. 'groups' => ['group_a', 'group_b'],
  522. ]);
  523. Cache::write('key_1', 'value', 'file_groups');
  524. Cache::write('key_2', 'value', 'file_groups');
  525. Cache::clearGroup('group_a', 'file_groups');
  526. $this->assertNull(Cache::read('key_1', 'file_groups'), 'Did not delete');
  527. $this->assertNull(Cache::read('key_2', 'file_groups'), 'Did not delete');
  528. }
  529. /**
  530. * Test that failed add write return false.
  531. */
  532. public function testAdd(): void
  533. {
  534. Cache::delete('test_add_key', 'file_test');
  535. $result = Cache::add('test_add_key', 'test data', 'file_test');
  536. $this->assertTrue($result);
  537. $expected = 'test data';
  538. $result = Cache::read('test_add_key', 'file_test');
  539. $this->assertSame($expected, $result);
  540. $result = Cache::add('test_add_key', 'test data 2', 'file_test');
  541. $this->assertFalse($result);
  542. }
  543. /**
  544. * Tests that only files inside of the configured path are being deleted.
  545. */
  546. public function testClearIsRestrictedToConfiguredPath(): void
  547. {
  548. $this->_configCache([
  549. 'prefix' => '',
  550. 'path' => TMP . 'tests',
  551. ]);
  552. $unrelatedFile = tempnam(TMP, 'file_test');
  553. file_put_contents($unrelatedFile, 'data');
  554. $this->assertFileExists($unrelatedFile);
  555. Cache::write('key', 'data', 'file_test');
  556. $this->assertFileExists(TMP . 'tests/key');
  557. $result = Cache::clear('file_test');
  558. $this->assertTrue($result);
  559. $this->assertFileDoesNotExist(TMP . 'tests/key');
  560. $this->assertFileExists($unrelatedFile);
  561. $this->assertTrue(unlink($unrelatedFile));
  562. }
  563. }