You are here

public function AddressBookTest::testExtendedOverview in Commerce Core 8.2

Tests the address book overview page with several profile types.

File

modules/order/tests/src/FunctionalJavascript/AddressBookTest.php, line 373

Class

AddressBookTest
Tests the address book pages.

Namespace

Drupal\Tests\commerce_order\FunctionalJavascript

Code

public function testExtendedOverview() {
  $customer_profile_type = ProfileType::load('customer');
  $bundle_entity_duplicator = $this->container
    ->get('entity.bundle_entity_duplicator');
  $bundle_entity_duplicator
    ->duplicate($customer_profile_type, [
    'id' => 'customer_shipping',
    'label' => 'Customer (Shipping)',
    'display_label' => 'Shipping information',
    'multiple' => TRUE,
  ]);
  $customer_profile_type
    ->setDisplayLabel('Billing information');
  $customer_profile_type
    ->setMultiple(FALSE);
  $customer_profile_type
    ->save();
  $customer_profile_type = ProfileType::load('customer');
  $bundle_entity_duplicator = $this->container
    ->get('entity.bundle_entity_duplicator');
  $bundle_entity_duplicator
    ->duplicate($customer_profile_type, [
    'id' => 'customer_test',
    'label' => 'Customer (Test)',
    'display_label' => 'Test information',
  ]);
  $customer = $this
    ->createUser([
    'access user profiles',
    'create customer profile',
    'update own customer profile',
    'delete own customer profile',
    'delete own customer_shipping profile',
    'view own customer profile',
    'view own customer_shipping profile',
  ]);
  $this
    ->drupalLogin($customer);
  $this
    ->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
    'user' => $customer
      ->id(),
  ]));
  $this
    ->assertSession()
    ->pageTextContains('Billing information');

  // Confirm that there is no empty text for billing information, because
  // there is an add link.
  $container = $this
    ->getSession()
    ->getPage()
    ->find('css', '.address-book__container--customer');
  $this
    ->assertStringNotContainsString('There are no addresses yet.', $container
    ->getText());
  $add_link = $this
    ->getSession()
    ->getPage()
    ->find('css', '.address-book__container--customer .address-book__add-link');
  $this
    ->assertNotEmpty($add_link);
  $add_link
    ->click();
  $this
    ->assertSession()
    ->fieldExists('address[0][address][address_line1]');

  // Confirm that there is only an edit link after a profile has been created,
  // since the profile type doesn't allow multiple profiles per customer.
  $billing_profile = $this
    ->createEntity('profile', [
    'type' => 'customer',
    'uid' => $customer
      ->id(),
    'address' => $this->firstAddress,
  ]);
  $this
    ->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
    'user' => $customer
      ->id(),
  ]));
  $this
    ->assertSession()
    ->elementNotExists('css', '.address-book__container--customer .address-book__add-link');
  $this
    ->assertSession()
    ->elementExists('css', '.address-book__container--customer .address-book__edit-link');
  $this
    ->assertSession()
    ->elementNotExists('css', '.address-book__container--customer .address-book__delete-link');
  $this
    ->assertSession()
    ->elementNotExists('css', '.address-book__container--customer .address-book__set-default-link');

  // Confirm that the add form isn't available directly either.
  $this
    ->drupalGet(Url::fromRoute('commerce_order.address_book.add_form', [
    'user' => $customer
      ->id(),
    'profile_type' => 'customer',
  ]));
  $this
    ->assertSession()
    ->pageTextContains('Access Denied');
  $this
    ->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
    'user' => $customer
      ->id(),
  ]));
  $this
    ->assertSession()
    ->pageTextContains('Shipping information');

  // Confirm that there is empty text for shipping information, because
  // there is no add link (due to lack of access).
  $add_link = $this
    ->getSession()
    ->getPage()
    ->find('css', '.address-book__container--customer_shipping .address-book__add-link');
  $this
    ->assertEmpty($add_link);
  $container = $this
    ->getSession()
    ->getPage()
    ->find('css', '.address-book__container--customer_shipping');
  $this
    ->assertStringContainsString('There are no addresses yet.', $container
    ->getText());
  $this
    ->createEntity('profile', [
    'type' => 'customer_shipping',
    'uid' => $customer
      ->id(),
    'address' => $this->secondAddress,
  ]);
  $this
    ->createEntity('profile', [
    'type' => 'customer_shipping',
    'uid' => $customer
      ->id(),
    'address' => $this->thirdAddress,
  ]);
  $this
    ->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
    'user' => $customer
      ->id(),
  ]));

  // Confirm that the empty text is gone.
  $container = $this
    ->getSession()
    ->getPage()
    ->find('css', '.address-book__container--customer_shipping');
  $this
    ->assertStringNotContainsString('There are no addresses yet.', $container
    ->getText());

  // Confirm that there are no edit/set default links, due to lack of access.
  $edit_links = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '.address-book__container--customer_shipping .address-book__edit-link');
  $this
    ->assertEmpty($edit_links);
  $set_default_links = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '.address-book__container--customer_shipping .address-book__set-default-link');
  $this
    ->assertEmpty($set_default_links);
  $delete_links = $this
    ->getSession()
    ->getPage()
    ->findAll('css', '.address-book__container--customer_shipping .address-book__delete-link');
  $this
    ->assertNotEmpty($delete_links);

  // Confirm that the profile types are filtered by access, and that
  // the customer_test profile type is not displayed.
  $this
    ->createEntity('profile', [
    'type' => 'customer_test',
    'uid' => $customer
      ->id(),
    'address' => $this->fourthAddress,
  ]);
  $this
    ->assertSession()
    ->pageTextNotContains('Test information');
  $this
    ->assertSession()
    ->pageTextNotContains($this->fourthAddress['address_line1']);
}