You are here

public function PriceListItemTest::testImportPriceListItemsWithDelete in Commerce Pricelist 8.2

Tests importing price list items and deleting existing price list items.

File

tests/src/Functional/PriceListItemTest.php, line 211

Class

PriceListItemTest
Tests the price list item UI.

Namespace

Drupal\Tests\commerce_pricelist\Functional

Code

public function testImportPriceListItemsWithDelete() {

  // A price list item to be deleted.
  $price_list_item = $this
    ->createEntity('commerce_pricelist_item', [
    'type' => 'commerce_product_variation',
    'price_list_id' => $this->priceList
      ->id(),
    'purchasable_entity' => $this->firstVariation
      ->id(),
    'quantity' => '10',
    'price' => new Price('666', 'USD'),
  ]);
  $this
    ->drupalGet($this->priceListItemCollectionUri);
  $this
    ->clickLink('Import prices');
  $filepath = drupal_get_path('module', 'commerce_pricelist_test') . '/files/prices.csv';
  $this
    ->getSession()
    ->getPage()
    ->attachFileToField('files[csv]', $filepath);
  $this
    ->submitForm([
    'mapping[quantity_column]' => 'qty',
    'mapping[list_price_column]' => 'msrp',
    'mapping[currency_column]' => 'currency',
    'delete_existing' => TRUE,
  ], 'Import prices');
  $this
    ->assertSession()
    ->pageTextContains('Imported 2 prices.');
  $this
    ->assertSession()
    ->pageTextContains('Skipped 2 prices during import.');
  $this
    ->assertSession()
    ->pageTextContains('Red shirt');
  $this
    ->assertSession()
    ->pageTextContains('Blue shirt');

  /** @var \Drupal\Core\Entity\EntityStorageInterface $price_list_item_storage */
  $price_list_item_storage = \Drupal::service('entity_type.manager')
    ->getStorage('commerce_pricelist_item');

  // Confirm that the existing price list item was deleted.
  $price_list_item_storage
    ->resetCache([
    $price_list_item
      ->id(),
  ]);
  $price_list_item_exists = (bool) PriceListItem::load($price_list_item
    ->id());
  $this
    ->assertFalse($price_list_item_exists);

  // Confirm that two new price list items have been created.

  /** @var \Drupal\commerce_pricelist\Entity\PriceListItemInterface[] $price_list_items */
  $price_list_items = $price_list_item_storage
    ->loadMultiple();
  $this
    ->assertCount(2, $price_list_items);
  $first_price_list_item = reset($price_list_items);
  $this
    ->assertEquals($this->priceList
    ->id(), $first_price_list_item
    ->getPriceListId());
  $this
    ->assertEquals($this->firstVariation
    ->id(), $first_price_list_item
    ->getPurchasableEntityId());
  $this
    ->assertEquals('1', $first_price_list_item
    ->getQuantity());
  $this
    ->assertEquals(new Price('50', 'USD'), $first_price_list_item
    ->getListPrice());
  $this
    ->assertEquals(new Price('40', 'USD'), $first_price_list_item
    ->getPrice());
  $this
    ->assertTrue($first_price_list_item
    ->isEnabled());
  $second_price_list_item = end($price_list_items);
  $this
    ->assertEquals($this->priceList
    ->id(), $second_price_list_item
    ->getPriceListId());
  $this
    ->assertEquals($this->secondVariation
    ->id(), $second_price_list_item
    ->getPurchasableEntityId());
  $this
    ->assertEquals('3', $second_price_list_item
    ->getQuantity());
  $this
    ->assertEquals(new Price('99.99', 'USD'), $second_price_list_item
    ->getListPrice());
  $this
    ->assertEquals(new Price('89.99', 'USD'), $second_price_list_item
    ->getPrice());
  $this
    ->assertTrue($second_price_list_item
    ->isEnabled());
}