You are here

public function StockLevelWidgetsTest::testSimpleTransactionStockLevelWidgetSingleVariationMode in Commerce Stock 8

Tests the default simple transaction widget in single variation mode.

File

modules/field/tests/src/Functional/StockLevelWidgetsTest.php, line 155

Class

StockLevelWidgetsTest
Provides tests for the stock level widget.

Namespace

Drupal\Tests\commerce_stock_field\Functional

Code

public function testSimpleTransactionStockLevelWidgetSingleVariationMode() {
  $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);
  $this
    ->drupalGet('admin/commerce/config/product-types/default/edit');
  $edit = [
    'multipleVariations' => FALSE,
  ];
  $this
    ->submitForm($edit, t('Save'));
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add product');
  $this
    ->assertSession()
    ->buttonNotExists('Save and add variations');
  $this
    ->assertSession()
    ->fieldExists('variations[entity][sku][0][value]');

  // On product add form, there should be no stock level input field.
  $this
    ->assertSession()
    ->fieldNotExists('variations[entity][' . $this->fieldName . '][0][adjustment]');
  $this
    ->assertSession()
    ->fieldNotExists('variations[entity][' . $this->fieldName . '][0][stock_transaction_note]');
  $store_id = $this->stores[0]
    ->id();
  $title = $this
    ->randomMachineName();
  $sku = strtolower($this
    ->randomMachineName());
  $edit = [
    'title[0][value]' => $title,
    'stores[target_id][value][' . $store_id . ']' => $store_id,
    'variations[entity][sku][0][value]' => $sku,
    'variations[entity][price][0][number]' => '99.99',
    'variations[entity][title][0][value]' => $title . '_testvariation',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->saveHtmlOutput();

  // Check if product was created. Remember we created
  // product 1 in test setup.
  $product = Product::load(2);
  $this
    ->assertNotEmpty($product);
  $this
    ->assertEquals($title, $product
    ->getTitle());

  // On product edit form, we should have a stock widget.
  $this
    ->drupalGet($product
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->fieldExists('variations[entity][commerce_stock_always_in_stock][value]');
  $this
    ->assertSession()
    ->checkboxNotChecked('variations[entity][commerce_stock_always_in_stock][value]');
  $this
    ->assertSession()
    ->fieldExists('variations[entity][' . $this->fieldName . '][0][adjustment]');
  $this
    ->assertSession()
    ->fieldExists('variations[entity][' . $this->fieldName . '][0][stock_transaction_note]');
  $this
    ->assertSession()
    ->fieldDisabled('variations[entity][' . $this->fieldName . '][0][stock_transaction_note]');
  $this
    ->assertSession()
    ->fieldValueEquals('variations[entity][' . $this->fieldName . '][0][stock_transaction_note]', $default_note);
  $adjustment = 6;
  $edit = [
    'title[0][value]' => 'New title',
    'variations[entity][price][0][number]' => '199.99',
    'variations[entity][' . $this->fieldName . '][0][adjustment]' => $adjustment,
    'variations[entity][title][0][value]' => $title . '_testvariation',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->saveHtmlOutput();
  \Drupal::entityTypeManager()
    ->getStorage('commerce_product')
    ->resetCache([
    2,
  ]);
  \Drupal::entityTypeManager()
    ->getStorage('commerce_product_variation')
    ->resetCache([
    1,
  ]);
  $product = Product::load(2);
  $this
    ->assertNotEmpty($product);
  $variation = $product
    ->getDefaultVariation();
  $this
    ->assertNotEmpty($variation);
  $transaction = $this
    ->getLastEntityTransaction($variation
    ->id());
  $this
    ->assertEquals($adjustment, $transaction->qty);
  $this
    ->assertEquals($this->adminUser
    ->id(), $transaction->related_uid);
}