From 281c75c67ca75d84ed7130f558800e307f809718 Mon Sep 17 00:00:00 2001 From: John Oleksowicz Date: Thu, 19 Oct 2023 13:41:08 -0500 Subject: [PATCH] Add helper to check if settings exists in DB MAILPOET-5549 --- mailpoet/lib/Settings/SettingsController.php | 11 +++++++++++ .../integration/Settings/SettingsControllerTest.php | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/mailpoet/lib/Settings/SettingsController.php b/mailpoet/lib/Settings/SettingsController.php index 6be1121423..2ba6e49472 100644 --- a/mailpoet/lib/Settings/SettingsController.php +++ b/mailpoet/lib/Settings/SettingsController.php @@ -124,6 +124,17 @@ class SettingsController { unset($this->settings[$key]); } + /** + * Returns true if a value is stored in the database for the given key + * + * @param string $key + * + * @return bool + */ + public function hasSavedValue(string $key): bool { + return $this->get($key, 'unset') !== 'unset'; + } + private function ensureLoaded() { if ($this->loaded) { return; diff --git a/mailpoet/tests/integration/Settings/SettingsControllerTest.php b/mailpoet/tests/integration/Settings/SettingsControllerTest.php index 5105f95064..72d433ee36 100644 --- a/mailpoet/tests/integration/Settings/SettingsControllerTest.php +++ b/mailpoet/tests/integration/Settings/SettingsControllerTest.php @@ -120,6 +120,13 @@ class SettingsControllerTest extends \MailPoetTest { $this->assertEquals(true, true); } + public function testItCanCheckIfSavedValueExists(): void { + $this->assertFalse($this->controller->hasSavedValue('test_key')); + $this->createOrUpdateSetting('test_key', 'some value'); + $this->controller->resetCache(); // force reload from database + $this->assertTrue($this->controller->hasSavedValue('test_key')); + } + private function createOrUpdateSetting($name, $value) { $tableName = $this->entityManager->getClassMetadata(SettingEntity::class)->getTableName(); $this->connection->executeStatement("