public function PromotionTest::testCreatePromotion in Commerce Core 8.2
Tests creating a promotion.
File
- modules/
promotion/ tests/ src/ FunctionalJavascript/ PromotionTest.php, line 75
Class
- PromotionTest
- Tests the admin UI for promotions.
Namespace
Drupal\Tests\commerce_promotion\FunctionalJavascriptCode
public function testCreatePromotion() {
$this
->drupalGet('admin/commerce/promotions');
$this
->getSession()
->getPage()
->clickLink('Add promotion');
// Check the integrity of the form.
$this
->assertSession()
->fieldExists('name[0][value]');
$this
->assertSession()
->fieldExists('display_name[0][value]');
$name = $this
->randomMachineName(8);
$this
->getSession()
->getPage()
->fillField('name[0][value]', $name);
$this
->getSession()
->getPage()
->fillField('display_name[0][value]', 'Discount');
$this
->getSession()
->getPage()
->selectFieldOption('offer[0][target_plugin_id]', 'order_item_percentage_off');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$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
->assertSession()
->assertWaitOnAjaxRequest();
$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',
'products',
'customer',
] as $condition_group) {
$tab_matches = $this
->xpath('//a[@href="#edit-conditions-form-' . $condition_group . '"]');
$this
->assertNotEmpty($tab_matches);
}
$vertical_tab_elements = $this
->xpath('//a[@href="#edit-conditions-form-order"]');
$vertical_tab_element = reset($vertical_tab_elements);
$vertical_tab_element
->click();
$this
->getSession()
->getPage()
->checkField('Current order total');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->getSession()
->getPage()
->fillField('conditions[form][order][order_total_price][configuration][form][amount][number]', '50.00');
// Confirm that the usage limit widget works properly.
$this
->assertSession()
->fieldExists('usage_limit[0][limit]');
$this
->assertSession()
->fieldValueEquals('usage_limit[0][limit]', 0);
$usage_limit_xpath = '//input[@type="number" and @name="usage_limit[0][usage_limit]"]';
$this
->assertFalse($this
->getSession()
->getDriver()
->isVisible($usage_limit_xpath));
// Select 'Limited number of uses'.
$this
->getSession()
->getPage()
->selectFieldOption('usage_limit[0][limit]', '1');
$this
->assertTrue($this
->getSession()
->getDriver()
->isVisible($usage_limit_xpath));
$this
->getSession()
->getPage()
->fillField('usage_limit[0][usage_limit]', '99');
// Confirm that the customer usage limit widget works properly.
$this
->assertSession()
->fieldExists('usage_limit_customer[0][limit_customer]');
$this
->assertSession()
->fieldValueEquals('usage_limit_customer[0][limit_customer]', 0);
$customer_usage_limit_xpath = '//input[@type="number" and @name="usage_limit_customer[0][usage_limit_customer]"]';
$this
->assertFalse($this
->getSession()
->getDriver()
->isVisible($customer_usage_limit_xpath));
$this
->getSession()
->getPage()
->selectFieldOption('usage_limit_customer[0][limit_customer]', '1');
$this
->assertTrue($this
->getSession()
->getDriver()
->isVisible($customer_usage_limit_xpath));
$this
->getSession()
->getPage()
->fillField('usage_limit_customer[0][usage_limit_customer]', '5');
$this
->setRawFieldValue('start_date[0][value][date]', '2019-11-29');
$this
->setRawFieldValue('start_date[0][value][time]', '10:30:00');
$this
->submitForm([], t('Save'));
$this
->assertSession()
->pageTextContains("Saved the {$name} promotion.");
$rows = $this
->getSession()
->getPage()
->findAll('xpath', '//table/tbody/tr/td[text()="' . $name . '"]');
$this
->assertCount(1, $rows);
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
$promotion = Promotion::load(1);
$this
->assertNotEmpty($promotion
->getCreatedTime());
$this
->assertNotEmpty($promotion
->getChangedTime());
$this
->assertEquals($name, $promotion
->getName());
$this
->assertEquals('Discount', $promotion
->getDisplayName());
$offer = $promotion
->getOffer();
$this
->assertEquals('0.10', $offer
->getConfiguration()['percentage']);
$conditions = $promotion
->getConditions();
$condition = reset($conditions);
$this
->assertEquals('50.00', $condition
->getConfiguration()['amount']['number']);
$this
->assertEquals('99', $promotion
->getUsageLimit());
$this
->assertEquals('5', $promotion
->getCustomerUsageLimit());
$this
->assertEquals('2019-11-29 10:30:00', $promotion
->getStartDate()
->format('Y-m-d H:i:s'));
$this
->assertNull($promotion
->getEndDate());
$this
->drupalGet($promotion
->toUrl('edit-form'));
$this
->assertSession()
->fieldExists('usage_limit[0][limit]');
$this
->assertSession()
->fieldValueEquals('usage_limit[0][limit]', 1);
$this
->assertTrue($this
->getSession()
->getDriver()
->isVisible($usage_limit_xpath));
$this
->assertSession()
->fieldExists('usage_limit_customer[0][limit_customer]');
$this
->assertSession()
->fieldValueEquals('usage_limit_customer[0][limit_customer]', 1);
$this
->assertTrue($this
->getSession()
->getDriver()
->isVisible($customer_usage_limit_xpath));
}