Skip to content

Commit

Permalink
Issue #2901322 by bojanz: Rename PercentageOffBase setting 'amount' t…
Browse files Browse the repository at this point in the history
…o 'percentage'
  • Loading branch information
bojanz committed Aug 10, 2017
1 parent 6380291 commit 5b3779b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
Expand Up @@ -14,18 +14,31 @@ 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.
*
* @return string
* The percentage.
*/
public function getPercentage() {
return (string) $this->configuration['amount'];
return (string) $this->configuration['percentage'];
}

/**
Expand All @@ -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;
Expand All @@ -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.'));
}
}

Expand All @@ -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);
}

}
Expand Up @@ -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',
Expand Down
Expand Up @@ -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',
Expand Down
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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');

Expand Down
8 changes: 4 additions & 4 deletions modules/promotion/tests/src/Kernel/PromotionOfferTest.php
Expand Up @@ -112,15 +112,15 @@ public function testOrderPercentageOff() {
'offer' => [
'target_plugin_id' => 'order_percentage_off',
'target_plugin_configuration' => [
'amount' => '0.10',
'percentage' => '0.10',
],
],
]);
$promotion->save();

/** @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()));
Expand Down Expand Up @@ -218,15 +218,15 @@ public function testProductPercentageOff() {
'offer' => [
'target_plugin_id' => 'order_item_percentage_off',
'target_plugin_configuration' => [
'amount' => '0.50',
'percentage' => '0.50',
],
],
]);
$promotion->save();

/** @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);
Expand Down
2 changes: 1 addition & 1 deletion modules/tax/src/Plugin/Commerce/TaxType/Custom.php
Expand Up @@ -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']);
}
Expand Down

0 comments on commit 5b3779b

Please sign in to comment.