diff --git a/Model/Config/Backend/ManualCapture.php b/Model/Config/Backend/ManualCapture.php index 73bfc12..bea11e4 100644 --- a/Model/Config/Backend/ManualCapture.php +++ b/Model/Config/Backend/ManualCapture.php @@ -104,7 +104,37 @@ private function saveValue(string $gateway, string $fieldKey, string $fieldValue { $this->writer->save( self::PAYMENT . DIRECTORY_SEPARATOR . $gateway . DIRECTORY_SEPARATOR . $fieldKey, - $fieldValue + $fieldValue, + $this->getScope() ?: ScopeConfigInterface::SCOPE_TYPE_DEFAULT, + (int)$this->getScopeId() ); } + + /** + * Remove the dependent values from the scope when the field falls back to the inherited value + * + * @return ManualCapture + */ + public function afterDelete(): ManualCapture + { + $scope = $this->getScope() ?: ScopeConfigInterface::SCOPE_TYPE_DEFAULT; + + if ($scope !== ScopeConfigInterface::SCOPE_TYPE_DEFAULT) { + $gateway = $this->getGroupId(); + + foreach ([ + [$gateway, self::PAYMENT_ACTION], + [$gateway . '_recurring', self::MANUAL_CAPTURE], + [$gateway . '_vault', self::MANUAL_CAPTURE] + ] as [$group, $field]) { + $this->writer->delete( + self::PAYMENT . DIRECTORY_SEPARATOR . $group . DIRECTORY_SEPARATOR . $field, + $scope, + (int)$this->getScopeId() + ); + } + } + + return parent::afterDelete(); + } } diff --git a/Test/Integration/Model/Config/Backend/ManualCaptureTest.php b/Test/Integration/Model/Config/Backend/ManualCaptureTest.php index 9ceadd5..5d81a62 100644 --- a/Test/Integration/Model/Config/Backend/ManualCaptureTest.php +++ b/Test/Integration/Model/Config/Backend/ManualCaptureTest.php @@ -127,4 +127,101 @@ public function testAfterSaveWithManualCaptureDisabledAndVaultEnabled() //phpcs:ignore @$this->manualCapture->afterSave(); } + + /** + * Test if every value is written to the scope the Manual Capture field was saved in + * + * @return void + */ + public function testAfterSaveWritesAtTheScopeOfTheField() + { + $this->manualCapture->addData(['fieldset_data' => [Vault::FIELD_VAULT_ENABLED => '1']]); + $this->manualCapture->setGroupId('multisafepay_creditcard'); + $this->manualCapture->setValue('1'); + $this->manualCapture->setScope('websites'); + $this->manualCapture->setScopeId(2); + + $this->writer->expects($this->exactly(3)) + ->method('save') + ->with( + $this->isString(), + $this->isString(), + 'websites', + 2 + ); + + //phpcs:ignore + @$this->manualCapture->afterSave(); + } + + /** + * Test if the default scope is used when the Manual Capture field was saved in the default scope + * + * @return void + */ + public function testAfterSaveWritesAtTheDefaultScope() + { + $this->manualCapture->setGroupId('multisafepay_creditcard'); + $this->manualCapture->setValue('0'); + $this->manualCapture->setScope('default'); + $this->manualCapture->setScopeId(0); + + $this->writer->expects($this->exactly(2)) + ->method('save') + ->with( + $this->isString(), + $this->isString(), + 'default', + 0 + ); + + //phpcs:ignore + @$this->manualCapture->afterSave(); + } + + /** + * Test if the dependent values are removed from the scope when the field is reset to the inherited value + * + * @return void + */ + public function testAfterDeleteRemovesDependentValuesFromTheScope() + { + $this->manualCapture->setGroupId('multisafepay_creditcard'); + $this->manualCapture->setScope('websites'); + $this->manualCapture->setScopeId(2); + + $deleted = []; + $this->writer->expects($this->exactly(3)) + ->method('delete') + ->willReturnCallback(function ($path, $scope, $scopeId) use (&$deleted) { + $deleted[] = [$path, $scope, $scopeId]; + }); + + //phpcs:ignore + @$this->manualCapture->afterDelete(); + + $this->assertSame( + [ + ['payment/multisafepay_creditcard/payment_action', 'websites', 2], + ['payment/multisafepay_creditcard_recurring/manual_capture', 'websites', 2], + ['payment/multisafepay_creditcard_vault/manual_capture', 'websites', 2], + ], + $deleted + ); + } + + /** + * Test if nothing is removed when the field is deleted in the default scope + * + * @return void + */ + public function testAfterDeleteKeepsDefaultScopeValues() + { + $this->manualCapture->setGroupId('multisafepay_creditcard'); + + $this->writer->expects($this->never())->method('delete'); + + //phpcs:ignore + @$this->manualCapture->afterDelete(); + } }