From 5b3779bdf035144602433185c7c530daddebb2b0 Mon Sep 17 00:00:00 2001 From: Bojan Zivanovic Date: Thu, 10 Aug 2017 19:07:45 +0200 Subject: [PATCH] Issue #2901322 by bojanz: Rename PercentageOffBase setting 'amount' to 'percentage' --- .../PromotionOffer/PercentageOffBase.php | 29 ++++++++++++++----- .../CouponRedemptionElementTest.php | 2 +- .../CouponRedemptionPaneTest.php | 2 +- .../FunctionalJavascript/PromotionTest.php | 10 +++---- .../tests/src/Kernel/PromotionOfferTest.php | 8 ++--- .../src/Plugin/Commerce/TaxType/Custom.php | 2 +- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/PercentageOffBase.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/PercentageOffBase.php index ac1fdb785b..70c6addb67 100644 --- a/modules/promotion/src/Plugin/Commerce/PromotionOffer/PercentageOffBase.php +++ b/modules/promotion/src/Plugin/Commerce/PromotionOffer/PercentageOffBase.php @@ -14,10 +14,23 @@ abstract class PercentageOffBase extends PromotionOfferBase { */ public function defaultConfiguration() { return [ - 'amount' => '0', + 'percentage' => '0', ] + parent::defaultConfiguration(); } + /** + * {@inheritdoc} + */ + public function setConfiguration(array $configuration) { + parent::setConfiguration($configuration); + + if (isset($this->configuration['amount'])) { + // The 'amount' key was renamed to 'percentage' in 2.0-rc2. + $this->configuration['percentage'] = $this->configuration['amount']; + unset($this->configuration['amount']); + } + } + /** * Gets the percentage. * @@ -25,7 +38,7 @@ public function defaultConfiguration() { * The percentage. */ public function getPercentage() { - return (string) $this->configuration['amount']; + return (string) $this->configuration['percentage']; } /** @@ -34,16 +47,16 @@ public function getPercentage() { public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form += parent::buildConfigurationForm($form, $form_state); - $form['amount'] = [ + $form['percentage'] = [ '#type' => 'commerce_number', '#title' => $this->t('Percentage'), - '#default_value' => $this->configuration['amount'] * 100, + '#default_value' => $this->configuration['percentage'] * 100, '#maxlength' => 255, - '#required' => TRUE, '#min' => 0, '#max' => 100, '#size' => 4, '#field_suffix' => $this->t('%'), + '#required' => TRUE, ]; return $form; @@ -54,8 +67,8 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { $values = $form_state->getValue($form['#parents']); - if (empty($values['amount'])) { - $form_state->setError($form, $this->t('Percentage amount cannot be empty.')); + if (empty($values['percentage'])) { + $form_state->setError($form, $this->t('Percentage must be a positive number.')); } } @@ -66,7 +79,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s parent::submitConfigurationForm($form, $form_state); $values = $form_state->getValue($form['#parents']); - $this->configuration['amount'] = (string) ($values['amount'] / 100); + $this->configuration['percentage'] = (string) ($values['percentage'] / 100); } } diff --git a/modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionElementTest.php b/modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionElementTest.php index f5b731d260..6b58f26421 100644 --- a/modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionElementTest.php +++ b/modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionElementTest.php @@ -82,7 +82,7 @@ protected function setUp() { 'offer' => [ 'target_plugin_id' => 'order_percentage_off', 'target_plugin_configuration' => [ - 'amount' => '0.10', + 'percentage' => '0.10', ], ], 'start_date' => '2017-01-01', diff --git a/modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionPaneTest.php b/modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionPaneTest.php index e87eb79abf..83cfd3a6d8 100644 --- a/modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionPaneTest.php +++ b/modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionPaneTest.php @@ -86,7 +86,7 @@ protected function setUp() { 'offer' => [ 'target_plugin_id' => 'order_percentage_off', 'target_plugin_configuration' => [ - 'amount' => '0.10', + 'percentage' => '0.10', ], ], 'start_date' => '2017-01-01', diff --git a/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php b/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php index b42ed76561..ea55c05c49 100644 --- a/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php +++ b/modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php @@ -50,13 +50,13 @@ public function testCreatePromotion() { $this->getSession()->getPage()->fillField('name[0][value]', $name); $this->getSession()->getPage()->selectFieldOption('offer[0][target_plugin_id]', 'order_item_percentage_off'); $this->waitForAjaxToFinish(); - $this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][order_item_percentage_off][amount]', '10.0'); + $this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][order_item_percentage_off][percentage]', '10.0'); // Change, assert any values reset. $this->getSession()->getPage()->selectFieldOption('offer[0][target_plugin_id]', 'order_percentage_off'); $this->waitForAjaxToFinish(); - $this->assertSession()->fieldValueNotEquals('offer[0][target_plugin_configuration][order_percentage_off][amount]', '10.0'); - $this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][order_percentage_off][amount]', '10.0'); + $this->assertSession()->fieldValueNotEquals('offer[0][target_plugin_configuration][order_percentage_off][percentage]', '10.0'); + $this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][order_percentage_off][percentage]', '10.0'); // Confirm the integrity of the conditions UI. foreach (['order', 'product', 'customer'] as $condition_group) { @@ -115,7 +115,7 @@ public function testCreatePromotionWithEndDate() { $name = $this->randomMachineName(8); $edit = [ 'name[0][value]' => $name, - 'offer[0][target_plugin_configuration][order_percentage_off][amount]' => '10.0', + 'offer[0][target_plugin_configuration][order_percentage_off][percentage]' => '10.0', ]; // Set an end date. @@ -170,7 +170,7 @@ public function testEditPromotion() { $new_promotion_name = $this->randomMachineName(8); $edit = [ 'name[0][value]' => $new_promotion_name, - 'offer[0][target_plugin_configuration][order_item_percentage_off][amount]' => '20', + 'offer[0][target_plugin_configuration][order_item_percentage_off][percentage]' => '20', ]; $this->submitForm($edit, 'Save'); diff --git a/modules/promotion/tests/src/Kernel/PromotionOfferTest.php b/modules/promotion/tests/src/Kernel/PromotionOfferTest.php index a1d68fa7c0..489c6bc71d 100644 --- a/modules/promotion/tests/src/Kernel/PromotionOfferTest.php +++ b/modules/promotion/tests/src/Kernel/PromotionOfferTest.php @@ -112,7 +112,7 @@ public function testOrderPercentageOff() { 'offer' => [ 'target_plugin_id' => 'order_percentage_off', 'target_plugin_configuration' => [ - 'amount' => '0.10', + 'percentage' => '0.10', ], ], ]); @@ -120,7 +120,7 @@ public function testOrderPercentageOff() { /** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $offer_field */ $offer_field = $promotion->get('offer')->first(); - $this->assertEquals('0.10', $offer_field->target_plugin_configuration['amount']); + $this->assertEquals('0.10', $offer_field->target_plugin_configuration['percentage']); $promotion->apply($this->order); $this->assertEquals(1, count($this->order->getAdjustments())); @@ -218,7 +218,7 @@ public function testProductPercentageOff() { 'offer' => [ 'target_plugin_id' => 'order_item_percentage_off', 'target_plugin_configuration' => [ - 'amount' => '0.50', + 'percentage' => '0.50', ], ], ]); @@ -226,7 +226,7 @@ public function testProductPercentageOff() { /** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $offer_field */ $offer_field = $promotion->get('offer')->first(); - $this->assertEquals('0.50', $offer_field->target_plugin_configuration['amount']); + $this->assertEquals('0.50', $offer_field->target_plugin_configuration['percentage']); $this->container->get('commerce_order.order_refresh')->refresh($this->order); $this->order = $this->reloadEntity($this->order); diff --git a/modules/tax/src/Plugin/Commerce/TaxType/Custom.php b/modules/tax/src/Plugin/Commerce/TaxType/Custom.php index 54b157c92d..2a4565faff 100644 --- a/modules/tax/src/Plugin/Commerce/TaxType/Custom.php +++ b/modules/tax/src/Plugin/Commerce/TaxType/Custom.php @@ -91,7 +91,7 @@ public function setConfiguration(array $configuration) { foreach ($this->configuration['rates'] as &$rate) { if (isset($rate['amount'])) { - // The 'amount' property was renamed to 'percentage' in 2.0-rc2. + // The 'amount' key was renamed to 'percentage' in 2.0-rc2. $rate['percentage'] = $rate['amount']; unset($rate['amount']); }