connection = Server::get(IDBConnection::class); $this->keyManager = new KeyManager($this->connection); $this->cleanUp(); } protected function tearDown(): void { $this->cleanUp(); parent::tearDown(); } private function cleanUp(): void { $this->connection->prepare( "DELETE FROM `*PREFIX*onlyoffice_filekey` WHERE `file_id` = ?" )->execute([self::FILE_ID]); } /** * Returns an empty string when no key exists for the given file. */ public function testGetReturnsEmptyStringWhenNoKeyExists(): void { $this->assertSame("", $this->keyManager->get(self::FILE_ID)); } /** * Returns the stored key after it has been set. */ public function testGetReturnsKeyAfterSet(): void { $this->keyManager->set(self::FILE_ID, "abc123"); $this->assertSame("abc123", $this->keyManager->get(self::FILE_ID)); } /** * Persists the key to the database and returns true on success. */ public function testSetReturnsTrueAndPersistsKey(): void { $result = $this->keyManager->set(self::FILE_ID, "key-value"); $this->assertTrue($result); $this->assertSame("key-value", $this->keyManager->get(self::FILE_ID)); } /** * Deletes an unlocked row and returns true; the key is no longer retrievable. */ public function testDeleteRemovesUnlockedRow(): void { $this->keyManager->set(self::FILE_ID, "key-value"); $result = $this->keyManager->delete(self::FILE_ID); $this->assertTrue($result); $this->assertSame("", $this->keyManager->get(self::FILE_ID)); } /** * Does not delete a locked row when $unlock is false (the default). */ public function testDeleteDoesNotRemoveLockedRowByDefault(): void { $this->keyManager->set(self::FILE_ID, "key-value"); $this->keyManager->lock(self::FILE_ID, true); $this->keyManager->delete(self::FILE_ID); $this->assertSame("key-value", $this->keyManager->get(self::FILE_ID)); } /** * Deletes a locked row when $unlock is explicitly true. */ public function testDeleteRemovesLockedRowWhenUnlockIsTrue(): void { $this->keyManager->set(self::FILE_ID, "key-value"); $this->keyManager->lock(self::FILE_ID, true); $result = $this->keyManager->delete(self::FILE_ID, true); $this->assertTrue($result); $this->assertSame("", $this->keyManager->get(self::FILE_ID)); } /** * Returns false for forcesave status when no row exists. */ public function testWasForcesaveReturnsFalseWhenNoRowExists(): void { $this->assertFalse($this->keyManager->wasForcesave(self::FILE_ID)); } /** * Returns false for forcesave status immediately after inserting a key. */ public function testWasForcesaveReturnsFalseByDefault(): void { $this->keyManager->set(self::FILE_ID, "key-value"); $this->assertFalse($this->keyManager->wasForcesave(self::FILE_ID)); } /** * Returns true for forcesave status after setForcesave is called with true. */ public function testWasForcesaveReturnsTrueAfterSetForcesave(): void { $this->keyManager->set(self::FILE_ID, "key-value"); $this->keyManager->setForcesave(self::FILE_ID, true); $this->assertTrue($this->keyManager->wasForcesave(self::FILE_ID)); } /** * Returns false for forcesave status after setForcesave is reset to false. */ public function testWasForcesaveReturnsFalseAfterReset(): void { $this->keyManager->set(self::FILE_ID, "key-value"); $this->keyManager->setForcesave(self::FILE_ID, true); $this->keyManager->setForcesave(self::FILE_ID, false); $this->assertFalse($this->keyManager->wasForcesave(self::FILE_ID)); } }