You are here

public function StockLevelWidgetsTest::testAbsoluteTransactionStockLevelWidgetSingleVariationMode in Commerce Stock 8

Tests the absolute stock level widget in single variation mode.

File

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

Class

StockLevelWidgetsTest
Provides tests for the stock level widget.

Namespace

Drupal\Tests\commerce_stock_field\Functional

Code

public function testAbsoluteTransactionStockLevelWidgetSingleVariationMode() {
  $entity_type = "commerce_product_variation";
  $bundle = 'default';
  $default_note = $this
    ->randomString(100);
  $widget_id = "commerce_stock_level_absolute";
  $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][stock_level]');
  $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);

  // 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][stock_level]');
  $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);
  $stock_level = 15;
  $edit = [
    'title[0][value]' => 'New title',
    'variations[entity][price][0][number]' => '199.99',
    'variations[entity][' . $this->fieldName . '][0][stock_level]' => $stock_level,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  \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(15, $transaction->qty);
  $this
    ->assertEquals($this->adminUser
    ->id(), $transaction->related_uid);
  $this
    ->drupalGet($product
    ->toUrl('edit-form'));
  $stock_level = 5;
  $edit = [
    'variations[entity][' . $this->fieldName . '][0][stock_level]' => $stock_level,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $transaction = $this
    ->getLastEntityTransaction($variation
    ->id());
  $this
    ->assertEquals(-10, $transaction->qty);
}