You are here

public function PriceListTest::testEdit in Commerce Pricelist 8.2

Tests editing a price list.

File

tests/src/Functional/PriceListTest.php, line 64

Class

PriceListTest
Tests the price list UI.

Namespace

Drupal\Tests\commerce_pricelist\Functional

Code

public function testEdit() {
  $roles = $this->adminUser
    ->getRoles();
  $role = reset($roles);
  $price_list = $this
    ->createEntity('commerce_pricelist', [
    'type' => 'commerce_product_variation',
    'name' => $this
      ->randomMachineName(8),
    'start_date' => '2018-07-07T13:37:00',
    'customer_role' => $role,
  ]);
  $this
    ->drupalGet($price_list
    ->toUrl('edit-form'));
  $prices_tab_uri = Url::fromRoute('entity.commerce_pricelist_item.collection', [
    'commerce_pricelist' => $price_list
      ->id(),
  ])
    ->toString();
  $this
    ->assertSession()
    ->linkByHrefExists($price_list
    ->toUrl('edit-form')
    ->toString());
  $this
    ->assertSession()
    ->linkByHrefExists($price_list
    ->toUrl('duplicate-form')
    ->toString());
  $this
    ->assertSession()
    ->linkByHrefExists($prices_tab_uri);
  $this
    ->submitForm([
    'name[0][value]' => 'Random list',
    'start_date[0][value][date]' => '2018-08-08',
    'start_date[0][value][time]' => '13:37:15',
    'customer_eligibility' => 'customers',
    'customers[0][target_id]' => $this->adminUser
      ->label() . ' (' . $this->adminUser
      ->id() . ')',
    // The role should not be persisted due to the customer being used.
    "customer_roles[{$role}]" => $role,
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Saved the Random list price list.');
  \Drupal::service('entity_type.manager')
    ->getStorage('commerce_pricelist')
    ->resetCache([
    $price_list
      ->id(),
  ]);

  /** @var \Drupal\commerce_pricelist\Entity\PriceListInterface $price_list */
  $price_list = PriceList::load(1);
  $this
    ->assertEquals('Random list', $price_list
    ->getName());
  $this
    ->assertEquals('2018-08-08 13:37:15', $price_list
    ->getStartDate()
    ->format('Y-m-d H:i:s'));
  $this
    ->assertEmpty($price_list
    ->getCustomerRoles());
  $this->adminUser = $this
    ->reloadEntity($this->adminUser);
  $this
    ->assertEquals([
    $this->adminUser,
  ], $price_list
    ->getCustomers());
}