You are here

public function StockLevelWidgetsTest::testAbsoluteStockLevelWidget in Commerce Stock 8

Testing the commerce_stock_level_absolute widget.

File

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

Class

StockLevelWidgetsTest
Provides tests for the stock level widget.

Namespace

Drupal\Tests\commerce_stock_field\Functional

Code

public function testAbsoluteStockLevelWidget() {
  $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($this->variation
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldExists($this->fieldName . '[0][stock_level]');
  $this
    ->assertSession()
    ->fieldNotExists($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);
  $stock_level = 15;
  $new_price_amount = '1.11';
  $variations_edit = [
    'price[0][number]' => $new_price_amount,
    $this->fieldName . '[0][stock_level]' => $stock_level,
  ];
  $this
    ->submitForm($variations_edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $transaction = $this
    ->getLastEntityTransaction($this->variation
    ->id());
  $data = unserialize($transaction->data);

  // We setup our variation with an initial stock of 10. So setting the
  // absolute level to 15 should result in a transaction with 5.
  $this
    ->assertEquals(5, $transaction->qty);
  $this
    ->assertEquals($this->adminUser
    ->id(), $transaction->related_uid);
  $this
    ->assertEquals($default_note, $data['message']);

  // If the absolute stock level is the same as before, it shouldn't trigger
  // any transaction.
  $this
    ->drupalGet($this->variation
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $stock_level = 15;
  $variations_edit = [
    $this->fieldName . '[0][stock_level]' => $stock_level,
  ];
  $this
    ->submitForm($variations_edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $transaction2 = $this
    ->getLastEntityTransaction($this->variation
    ->id());
  $this
    ->assertEquals($transaction->id, $transaction2->id);
  $this
    ->drupalGet($this->variation
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Empty absolute stock_level shoudn't trigger any transaction.
  $stock_level = '';
  $edit = [
    $this->fieldName . '[0][stock_level]' => $stock_level,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $transaction3 = $this
    ->getLastEntityTransaction($this->variation
    ->id());
  $this
    ->assertEquals($transaction->id, $transaction3->id);
  $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][stock_level]');
  $this
    ->assertSession()
    ->fieldNotExists($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);
  $stock_level = 5;
  $edit = [
    $this->fieldName . '[0][stock_level]' => $stock_level,
    $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(-10, $transaction->qty);
  $this
    ->assertEquals($this->adminUser
    ->id(), $transaction->related_uid);
  $this
    ->assertEquals('CustumNote', $data['message']);

  // Testing that zero value, results in a transaction.
  $this
    ->drupalGet($this->variation
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $stock_level = 0;
  $edit = [
    $this->fieldName . '[0][stock_level]' => $stock_level,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $transaction = $this
    ->getLastEntityTransaction($this->variation
    ->id());
  $this
    ->assertEquals(-5, $transaction->qty);
}