From 012545cd4ca2dafe7218082335dd0786a53ff689 Mon Sep 17 00:00:00 2001 From: Heihokon Date: Wed, 9 Jul 2025 14:02:38 -0500 Subject: [PATCH] fix: Prevent sending troubleshooting hits when not activated --- .../NoBatchingContinuousCachingStrategy.php | 55 +++++++++++++------ src/Enum/FlagshipConstant.php | 2 +- ...oBatchingContinuousCachingStrategyTest.php | 22 +++++--- 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/Api/NoBatchingContinuousCachingStrategy.php b/src/Api/NoBatchingContinuousCachingStrategy.php index 2eec3226..bc311fbc 100644 --- a/src/Api/NoBatchingContinuousCachingStrategy.php +++ b/src/Api/NoBatchingContinuousCachingStrategy.php @@ -51,8 +51,8 @@ protected function onError(HitAbstract $hit): void protected function sendHit(HitAbstract $hit): void { $header = [ - FlagshipConstant::HEADER_CONTENT_TYPE => FlagshipConstant::HEADER_APPLICATION_JSON, - ]; + FlagshipConstant::HEADER_CONTENT_TYPE => FlagshipConstant::HEADER_APPLICATION_JSON, + ]; $requestBody = $hit->toApiKeys(); $now = $this->getNow(); @@ -68,8 +68,8 @@ protected function sendHit(HitAbstract $hit): void FlagshipConstant::TRACKING_MANAGER, FlagshipConstant::HIT_SENT_SUCCESS, [ - FlagshipConstant::SEND_HIT, - $this->getLogFormat(null, $url, $requestBody, $header, $this->getNow() - $now), + FlagshipConstant::SEND_HIT, + $this->getLogFormat(null, $url, $requestBody, $header, $this->getNow() - $now), ] ); } catch (Exception $exception) { @@ -79,12 +79,23 @@ protected function sendHit(HitAbstract $hit): void FlagshipConstant::TRACKING_MANAGER, FlagshipConstant::UNEXPECTED_ERROR_OCCURRED, [ - FlagshipConstant::SEND_HIT, - $this->getLogFormat($exception->getMessage(), $url, $requestBody, $header, $this->getNow() - $now), + FlagshipConstant::SEND_HIT, + $this->getLogFormat($exception->getMessage(), $url, $requestBody, $header, $this->getNow() - $now), ] ); $troubleshooting = new Troubleshooting(); - $troubleshooting->setLabel(TroubleshootingLabel::SEND_HIT_ROUTE_ERROR)->setLogLevel(LogLevel::ERROR)->setVisitorId($this->flagshipInstanceId)->setFlagshipInstanceId($this->flagshipInstanceId)->setTraffic(100)->setConfig($this->config)->setHttpRequestBody($requestBody)->setHttpRequestHeaders($header)->setHttpRequestMethod("POST")->setHttpRequestUrl($url)->setHttpResponseBody($exception->getMessage())->setHttpResponseTime($this->getNow() - $now); + $troubleshooting->setLabel(TroubleshootingLabel::SEND_HIT_ROUTE_ERROR) + ->setLogLevel(LogLevel::ERROR) + ->setFlagshipInstanceId($this->flagshipInstanceId) + ->setHttpRequestBody($requestBody) + ->setHttpRequestHeaders($header) + ->setHttpRequestMethod("POST") + ->setHttpRequestUrl($url) + ->setHttpResponseBody($exception->getMessage()) + ->setHttpResponseTime($this->getNow() - $now) + ->setTraffic(100)->setConfig($this->config) + ->setVisitorId($this->flagshipInstanceId) + ; $this->addTroubleshootingHit($troubleshooting); $this->sendTroubleshootingQueue(); } @@ -117,8 +128,8 @@ public function activateFlag(Activate $hit): void FlagshipConstant::TRACKING_MANAGER, FlagshipConstant::HIT_SENT_SUCCESS, [ - FlagshipConstant::SEND_ACTIVATE, - $this->getLogFormat(null, $url, $requestBody, $headers, $this->getNow() - $now), + FlagshipConstant::SEND_ACTIVATE, + $this->getLogFormat(null, $url, $requestBody, $headers, $this->getNow() - $now), ] ); } catch (Exception $exception) { @@ -128,22 +139,29 @@ public function activateFlag(Activate $hit): void FlagshipConstant::TRACKING_MANAGER, FlagshipConstant::UNEXPECTED_ERROR_OCCURRED, [ - FlagshipConstant::SEND_ACTIVATE, - $this->getLogFormat($exception->getMessage(), $url, $requestBody, $headers, $this->getNow() - $now), + FlagshipConstant::SEND_ACTIVATE, + $this->getLogFormat($exception->getMessage(), $url, $requestBody, $headers, $this->getNow() - $now), ] ); $troubleshooting = new Troubleshooting(); - $troubleshooting->setLabel(TroubleshootingLabel::SEND_ACTIVATE_HIT_ROUTE_ERROR)->setLogLevel(LogLevel::ERROR)->setVisitorId($this->flagshipInstanceId)->setFlagshipInstanceId($this->flagshipInstanceId)->setTraffic(100)->setConfig($this->config)->setHttpRequestBody($requestBody)->setHttpRequestHeaders($headers)->setHttpRequestMethod("POST")->setHttpRequestUrl($url)->setHttpResponseBody($exception->getMessage())->setHttpResponseTime($this->getNow() - $now); + $troubleshooting->setLabel(TroubleshootingLabel::SEND_ACTIVATE_HIT_ROUTE_ERROR) + ->setFlagshipInstanceId($this->flagshipInstanceId) + ->setHttpRequestBody($requestBody) + ->setHttpRequestHeaders($headers) + ->setHttpRequestMethod("POST") + ->setHttpRequestUrl($url) + ->setHttpResponseBody($exception->getMessage()) + ->setHttpResponseTime($this->getNow() - $now) + ->setLogLevel(LogLevel::ERROR) + ->setTraffic(100)->setConfig($this->config) + ->setVisitorId($this->flagshipInstanceId); $this->addTroubleshootingHit($troubleshooting); $this->sendTroubleshootingQueue(); } } - /** - * @param string $visitorId - * @return void - */ + protected function notConsent(string $visitorId): void { $keysToFlush = $this->commonNotConsent($visitorId); @@ -157,7 +175,10 @@ protected function notConsent(string $visitorId): void public function addTroubleshootingHit(Troubleshooting $hit): void { - $this->sendTroubleshooting($hit); + if (!$this->isTroubleshootingActivated()) { + return; + } + $this->sendTroubleshooting($hit); } public function addUsageHit(UsageHit $hit): void diff --git a/src/Enum/FlagshipConstant.php b/src/Enum/FlagshipConstant.php index f964ff67..270fb673 100644 --- a/src/Enum/FlagshipConstant.php +++ b/src/Enum/FlagshipConstant.php @@ -54,7 +54,7 @@ class FlagshipConstant /** * SDK version */ - public const SDK_VERSION = "4.1.0"; + public const SDK_VERSION = "4.1.1"; public const GET_FLAG = 'GET_FLAG'; diff --git a/tests/Api/NoBatchingContinuousCachingStrategyTest.php b/tests/Api/NoBatchingContinuousCachingStrategyTest.php index 17f1d350..f48c44ad 100644 --- a/tests/Api/NoBatchingContinuousCachingStrategyTest.php +++ b/tests/Api/NoBatchingContinuousCachingStrategyTest.php @@ -3,18 +3,19 @@ namespace Flagship\Api; use Exception; -use Flagship\Config\DecisionApiConfig; -use Flagship\Enum\EventCategory; -use Flagship\Enum\FlagshipConstant; +use Flagship\Hit\Page; +use Flagship\Hit\Event; use Flagship\Hit\Activate; -use Flagship\Hit\ActivateBatch; use Flagship\Hit\UsageHit; -use Flagship\Hit\Event; use Flagship\Hit\HitAbstract; -use Flagship\Hit\Page; -use Flagship\Hit\Troubleshooting; use Flagship\Traits\LogTrait; +use Flagship\Hit\ActivateBatch; use PHPUnit\Framework\TestCase; +use Flagship\Enum\EventCategory; +use Flagship\Hit\Troubleshooting; +use Flagship\Enum\FlagshipConstant; +use Flagship\Config\DecisionApiConfig; +use PHPUnit\Framework\MockObject\MockObject; class NoBatchingContinuousCachingStrategyTest extends TestCase { @@ -527,6 +528,9 @@ public function testAddTroubleshootingHit() $httpClientMock = $this->getMockForAbstractClass('Flagship\Utils\HttpClientInterface'); + /** + * @var NoBatchingContinuousCachingStrategy| MockObject + */ $strategy = $this->getMockForAbstractClass( "Flagship\Api\NoBatchingContinuousCachingStrategy", [ @@ -537,12 +541,14 @@ public function testAddTroubleshootingHit() true, false, true, - ["sendTroubleshooting"] + ["sendTroubleshooting", 'isTroubleshootingActivated'] ); $troubleshooting = new Troubleshooting(); $troubleshooting->setConfig($config); + $strategy->expects($this->once())->method('isTroubleshootingActivated')->willReturn(true); + $strategy->expects($this->once())->method('sendTroubleshooting')->with($troubleshooting); $strategy->addTroubleshootingHit($troubleshooting);