From 81352d502747e9829bd9d963d4dda2549d566e3f Mon Sep 17 00:00:00 2001 From: Piyush Garg <62426177+il-coder@users.noreply.github.com> Date: Sun, 26 Nov 2023 02:41:12 +0530 Subject: [PATCH] Fix double prefix for increment in cache FileHandler --- system/Cache/Handlers/FileHandler.php | 4 +-- .../system/Cache/Handlers/FileHandlerTest.php | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/system/Cache/Handlers/FileHandler.php b/system/Cache/Handlers/FileHandler.php index 54cafdc5f35b..0a39c3610ba0 100644 --- a/system/Cache/Handlers/FileHandler.php +++ b/system/Cache/Handlers/FileHandler.php @@ -150,8 +150,8 @@ public function deleteMatching(string $pattern) */ public function increment(string $key, int $offset = 1) { - $key = static::validateKey($key, $this->prefix); - $tmp = $this->getItem($key); + $prefixedKey = static::validateKey($key, $this->prefix); + $tmp = $this->getItem($prefixedKey); if ($tmp === false) { $tmp = ['data' => 0, 'ttl' => 60]; diff --git a/tests/system/Cache/Handlers/FileHandlerTest.php b/tests/system/Cache/Handlers/FileHandlerTest.php index 11d1c93b9c1c..0bcaccf817c3 100644 --- a/tests/system/Cache/Handlers/FileHandlerTest.php +++ b/tests/system/Cache/Handlers/FileHandlerTest.php @@ -67,6 +67,10 @@ protected function tearDown(): void chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key, 0777); unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key); } + if (is_file($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key)) { + chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key, 0777); + unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key); + } } rmdir($this->config->file['storePath']); @@ -233,6 +237,22 @@ public function testIncrement(): void $this->assertSame(10, $this->handler->increment(self::$key3, 10)); } + public function testIncrementWithDefaultPrefix(): void + { + $this->config->prefix = 'test_'; + $this->handler = new FileHandler($this->config); + $this->handler->initialize(); + + $this->handler->save(self::$key1, 1); + $this->handler->save(self::$key2, 'value'); + + $this->assertSame(11, $this->handler->increment(self::$key1, 10)); + $this->assertSame($this->handler->increment(self::$key1, 10), $this->handler->get(self::$key1)); + $this->assertFalse($this->handler->increment(self::$key2, 10)); + $this->assertSame(10, $this->handler->increment(self::$key3, 10)); + $this->assertSame($this->handler->increment(self::$key3, 10), $this->handler->get(self::$key3)); + } + public function testDecrement(): void { $this->handler->save(self::$key1, 10); @@ -246,6 +266,21 @@ public function testDecrement(): void $this->assertSame(-1, $this->handler->decrement(self::$key3, 1)); } + public function testDecrementWithDefaultPrefix(): void + { + $this->handler->save(self::$key1, 10); + $this->handler->save(self::$key2, 'value'); + + // Line following commented out to force the cache to add a zero entry for key3 + // $this->fileHandler->save(self::$key3, 0); + + $this->assertSame(9, $this->handler->decrement(self::$key1, 1)); + $this->assertSame($this->handler->decrement(self::$key1, 1), $this->handler->get(self::$key1)); + $this->assertFalse($this->handler->decrement(self::$key2, 1)); + $this->assertSame(-1, $this->handler->decrement(self::$key3, 1)); + $this->assertSame($this->handler->decrement(self::$key3, 1), $this->handler->get(self::$key3)); + } + public function testClean(): void { $this->handler->save(self::$key1, 1);