public function StockLevelWidgetsTest::testSimpleTransactionStockLevelWidget in Commerce Stock 8
Tests the default simple transaction widget.
File
- modules/
field/ tests/ src/ Functional/ StockLevelWidgetsTest.php, line 24
Class
- StockLevelWidgetsTest
- Provides tests for the stock level widget.
Namespace
Drupal\Tests\commerce_stock_field\FunctionalCode
public function testSimpleTransactionStockLevelWidget() {
$entity_type = "commerce_product_variation";
$bundle = 'default';
$widget_id = "commerce_stock_level_simple_transaction";
$default_note = $this
->randomString(200);
$widget_settings = [
'custom_transaction_note' => FALSE,
'default_transaction_note' => $default_note,
'step' => '1',
];
$this
->configureFormDisplay($widget_id, $widget_settings, $entity_type, $bundle);
// Test adding a new variation on the variations tab.
$this
->drupalGet('admin/commerce/products');
$this
->getSession()
->getPage()
->clickLink('Add product');
// Create a product.
$store_ids = EntityHelper::extractIds($this->stores);
$title = $this
->randomMachineName();
$edit = [
'title[0][value]' => $title,
];
foreach ($store_ids as $store_id) {
$edit['stores[target_id][value][' . $store_id . ']'] = $store_id;
}
$this
->submitForm($edit, 'Save and add variations');
$this
->assertNotEmpty($this
->getSession()
->getPage()
->hasLink('Add variation'));
$this
->getSession()
->getPage()
->clickLink('Add variation');
$this
->assertSession()
->pageTextContains(t('Add variation'));
// Ensure the stock part of the form is healty.
$this
->assertSession()
->fieldExists('commerce_stock_always_in_stock[value]');
$this
->assertSession()
->checkboxNotChecked('commerce_stock_always_in_stock[value]');
$this
->assertSession()
->fieldExists($this->fieldName . '[0][adjustment]');
$this
->assertSession()
->fieldExists($this->fieldName . '[0][stock_transaction_note]');
$this
->assertSession()
->fieldDisabled($this->fieldName . '[0][stock_transaction_note]');
$this
->assertSession()
->fieldValueEquals($this->fieldName . '[0][stock_transaction_note]', $default_note);
$this
->assertSession()
->buttonExists('Save');
$variation_sku = $this
->randomMachineName();
$title = $this
->randomString();
$this
->getSession()
->getPage()
->fillField('sku[0][value]', $variation_sku);
$this
->getSession()
->getPage()
->fillField('price[0][number]', '9.99');
$this
->getSession()
->getPage()
->fillField('title[0][value]', $title);
$adjustment = 2;
$this
->getSession()
->getPage()
->fillField($this->fieldName . '[0][adjustment]', $adjustment);
$this
->submitForm([], t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$variation_in_table = $this
->getSession()
->getPage()
->find('xpath', '//table/tbody/tr/td[text()="' . $variation_sku . '"]');
$this
->assertNotEmpty($variation_in_table);
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()
->getStorage('commerce_product_variation');
$result = $storage
->loadByProperties([
'sku' => $variation_sku,
]);
$variation = array_shift($result);
$transaction = $this
->getLastEntityTransaction($variation
->id());
$this
->assertEquals($adjustment, $transaction->qty);
$this
->assertEquals($this->adminUser
->id(), $transaction->related_uid);
// Test the widget on variation edit form.
$this
->drupalGet($this->variation
->toUrl('edit-form'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists('commerce_stock_always_in_stock[value]');
$this
->assertSession()
->checkboxNotChecked('commerce_stock_always_in_stock[value]');
// Check the defaults.
$this
->assertSession()
->fieldExists($this->fieldName . '[0][adjustment]');
$this
->assertSession()
->fieldExists($this->fieldName . '[0][stock_transaction_note]');
$this
->assertSession()
->fieldValueEquals($this->fieldName . '[0][stock_transaction_note]', $default_note);
$adjustment = 6;
$new_price_amount = '1.11';
$variations_edit = [
'price[0][number]' => $new_price_amount,
$this->fieldName . '[0][adjustment]' => $adjustment,
];
$this
->submitForm($variations_edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$transaction = $this
->getLastEntityTransaction($this->variation
->id());
$data = unserialize($transaction->data);
$this
->assertEquals($adjustment, $transaction->qty);
$this
->assertEquals($this->adminUser
->id(), $transaction->related_uid);
$this
->assertEquals($default_note, $data['message']);
$widget_settings = [
'custom_transaction_note' => TRUE,
'default_transaction_note' => $default_note,
'step' => '1',
];
$this
->configureFormDisplay($widget_id, $widget_settings, $entity_type, $bundle);
$this
->drupalGet($this->variation
->toUrl('edit-form'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists($this->fieldName . '[0][adjustment]');
$this
->assertSession()
->fieldExists($this->fieldName . '[0][stock_transaction_note]');
self::expectException(ExpectationException::class);
$this
->assertSession()
->fieldDisabled($this->fieldName . '[0][stock_transaction_note]');
$this
->assertSession()
->fieldValueEquals($this->fieldName . '[0][stock_transaction_note]', $default_note);
$adjustment = -3;
$edit = [
$this->fieldName . '[0][adjustment]' => $adjustment,
$this->fieldName . '[0][stock_transaction_note]' => 'CustomNote',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$transaction = $this
->getLastEntityTransaction($this->variation
->id());
$data = unserialize($transaction->data);
$this
->assertEquals($adjustment, $transaction->qty);
$this
->assertEquals($this->adminUser
->id(), $transaction->related_uid);
$this
->assertEquals('CustomNote', $data['message']);
}