View source
<?php
namespace Drupal\Tests\commerce_order\FunctionalJavascript;
use Drupal\Core\Url;
use Drupal\profile\Entity\Profile;
use Drupal\profile\Entity\ProfileType;
class AddressBookTest extends OrderWebDriverTestBase {
protected $firstAddress = [
'country_code' => 'US',
'administrative_area' => 'SC',
'locality' => 'Greenville',
'postal_code' => '29616',
'address_line1' => '9 Drupal Ave',
'given_name' => 'Bryan',
'family_name' => 'Centarro',
];
protected $secondAddress = [
'country_code' => 'US',
'administrative_area' => 'CA',
'locality' => 'Mountain View',
'postal_code' => '94043',
'address_line1' => '1098 Alta Ave',
'organization' => 'Google Inc.',
'given_name' => 'John',
'family_name' => 'Smith',
];
protected $thirdAddress = [
'country_code' => 'US',
'postal_code' => '53177',
'locality' => 'Milwaukee',
'address_line1' => 'Pabst Blue Ribbon Dr',
'administrative_area' => 'WI',
'given_name' => 'Frederick',
'family_name' => 'Pabst',
];
protected $fourthAddress = [
'country_code' => 'FR',
'locality' => 'Paris',
'postal_code' => '75002',
'address_line1' => '38 Rue du Sentier',
'given_name' => 'Leon',
'family_name' => 'Blum',
];
protected function getAdministratorPermissions() {
return [
'view the administration theme',
'access administration pages',
'access commerce administration pages',
'access user profiles',
'administer commerce_currency',
'administer commerce_store',
'administer commerce_store_type',
'administer profile',
'administer profile types',
];
}
protected function setUp() : void {
parent::setUp();
$this
->drupalLogout();
}
public function testOverviewAccess() {
$this
->drupalGet('user/0/address-book');
$this
->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
'user' => 0,
]));
$this
->assertSession()
->pageTextContains('Access Denied');
$customer = $this
->createUser([
'view any customer profile',
]);
$this
->drupalLogin($customer);
$this
->drupalGet($this->adminUser
->toUrl('canonical'));
$this
->assertSession()
->pageTextContains('Access denied');
$this
->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
'user' => $this->adminUser
->id(),
]));
$this
->assertSession()
->pageTextContains('Access Denied');
$customer = $this
->createUser([
'access user profiles',
]);
$this
->drupalLogin($customer);
$this
->drupalGet($this->adminUser
->toUrl('canonical'));
$this
->assertSession()
->pageTextNotContains('Access denied');
$this
->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
'user' => $this->adminUser
->id(),
]));
$this
->assertSession()
->pageTextContains('Access Denied');
$customer = $this
->createUser([
'access user profiles',
'view any customer profile',
]);
$this
->createEntity('profile', [
'type' => 'customer',
'uid' => $this->adminUser
->id(),
'address' => $this->secondAddress,
]);
$this
->drupalLogin($customer);
$this
->drupalGet($this->adminUser
->toUrl('canonical'));
$this
->assertSession()
->pageTextNotContains('Access denied');
$this
->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
'user' => $this->adminUser
->id(),
]));
$this
->assertSession()
->pageTextNotContains('Access Denied');
$this
->assertSession()
->pageTextContains('1098 Alta Ave');
$this
->assertSession()
->linkExists('Address book');
$this
->assertSession()
->linkNotExists('Billing information');
}
public function testCreateAccess() {
$first_user = $this
->createUser([
'view own customer profile',
]);
$second_user = $this
->createUser([
'create customer profile',
'view any profile',
'access user profiles',
]);
$third_user = $this
->createUser([
'administer profile',
'access user profiles',
]);
$this
->createEntity('profile', [
'type' => 'customer',
'uid' => $first_user
->id(),
'address' => $this->firstAddress,
'status' => TRUE,
]);
$overview_url = Url::fromRoute('commerce_order.address_book.overview', [
'user' => $first_user
->id(),
]);
$this
->drupalLogin($first_user);
$this
->drupalGet($overview_url);
$this
->assertSession()
->pageTextNotContains('Access Denied');
$this
->assertSession()
->pageTextContains('9 Drupal Ave');
$this
->assertSession()
->linkNotExists('Add address');
$add_url = Url::fromRoute('commerce_order.address_book.add_form', [
'user' => $first_user
->id(),
'profile_type' => 'customer',
]);
$this
->drupalGet($add_url);
$this
->assertSession()
->pageTextContains('Access denied');
$this
->drupalLogin($second_user);
$this
->drupalGet($overview_url);
$this
->assertSession()
->pageTextNotContains('Access Denied');
$this
->assertSession()
->pageTextContains('9 Drupal Ave');
$this
->assertSession()
->linkNotExists('Add address');
$this
->drupalGet($add_url);
$this
->assertSession()
->pageTextContains('Access denied');
$this
->drupalLogin($third_user);
$this
->drupalGet($overview_url);
$this
->assertSession()
->pageTextContains('9 Drupal Ave');
$this
->assertSession()
->linkExists('Add address');
$this
->getSession()
->getPage()
->clickLink('Add address');
$this
->getSession()
->getPage()
->fillField('address[0][address][country_code]', 'FR');
$this
->assertSession()
->assertWaitOnAjaxRequest();
foreach ($this->fourthAddress as $property => $value) {
$this
->getSession()
->getPage()
->fillField("address[0][address][{$property}]", $value);
}
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('Saved the 38 Rue du Sentier address.');
$profile = Profile::load('2');
$this
->assertNotEmpty($profile);
$this
->assertEquals('38 Rue du Sentier', $profile
->get('address')->address_line1);
$this
->assertEquals($first_user
->id(), $profile
->getOwnerId());
$this
->drupalGet($add_url);
$this
->assertSession()
->pageTextNotContains('Access denied');
$profile = ProfileType::load('customer');
$profile
->setMultiple(FALSE);
$profile
->save();
$this
->drupalGet($add_url);
$this
->assertSession()
->pageTextContains('Access denied');
}
public function testFallback() {
$profile = ProfileType::load('customer');
$profile
->setMultiple(FALSE);
$profile
->save();
\Drupal::service('router.builder')
->rebuild();
$customer = $this
->createUser([
'access user profiles',
'view own customer profile',
'update own customer profile',
]);
$profile = $this
->createEntity('profile', [
'type' => 'customer',
'uid' => $customer
->id(),
'address' => $this->firstAddress,
]);
$this
->drupalLogin($customer);
$this
->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
'user' => $customer
->id(),
]));
$this
->assertSession()
->pageTextContains('Access Denied');
$this
->drupalGet($customer
->toUrl());
$this
->assertSession()
->linkNotExists('Address book');
$this
->assertSession()
->linkExists('Customer information');
$this
->getSession()
->getPage()
->clickLink('Customer information');
$this
->saveHtmlOutput();
foreach ($this->firstAddress as $property => $value) {
$this
->assertSession()
->fieldValueEquals("address[0][address][{$property}]", $value);
}
$this
->submitForm([
'address[0][address][address_line1]' => '10 Drupal Ave',
], 'Save');
$this
->assertSession()
->pageTextContains('The profile has been saved.');
$profile = $this
->reloadEntity($profile);
$this
->assertEquals('10 Drupal Ave', $profile
->get('address')->address_line1);
}
public function testDefaultOverview() {
$customer = $this
->createUser([
'access user profiles',
'create customer profile',
'update own customer profile',
'delete own customer profile',
'view own customer profile',
'administer profile',
]);
$this
->drupalLogin($customer);
$this
->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
'user' => $customer
->id(),
]));
$this
->assertSession()
->pageTextContains('There are no addresses yet.');
$this
->getSession()
->getPage()
->clickLink('Add address');
$this
->getSession()
->getPage()
->fillField('address[0][address][country_code]', 'FR');
$this
->assertSession()
->assertWaitOnAjaxRequest();
foreach ($this->fourthAddress as $property => $value) {
$this
->getSession()
->getPage()
->fillField("address[0][address][{$property}]", $value);
}
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('Saved the 38 Rue du Sentier address.');
$rendered_address = $this
->getSession()
->getPage()
->find('css', 'p.address');
$this
->assertNotEmpty($rendered_address);
$this
->assertStringContainsString('38 Rue du Sentier', $rendered_address
->getText());
$this
->getSession()
->getPage()
->clickLink('Edit');
foreach ($this->fourthAddress as $property => $value) {
$this
->assertSession()
->fieldValueEquals("address[0][address][{$property}]", $value);
}
$this
->submitForm([
'address[0][address][address_line1]' => '39 Rue du Sentier',
], 'Save');
$this
->assertSession()
->pageTextContains('Saved the 39 Rue du Sentier address.');
$rendered_address = $this
->getSession()
->getPage()
->find('css', 'p.address');
$this
->assertNotEmpty($rendered_address);
$this
->assertStringContainsString('39 Rue du Sentier', $rendered_address
->getText());
$second_profile = $this
->createEntity('profile', [
'type' => 'customer',
'uid' => $customer
->id(),
'address' => $this->secondAddress,
]);
$this
->assertFalse($second_profile
->isDefault());
$this
->drupalGet(Url::fromRoute('commerce_order.address_book.overview', [
'user' => $customer
->id(),
]));
$this
->assertSession()
->pageTextContains('39 Rue du Sentier');
$this
->assertSession()
->pageTextContains($this->secondAddress['address_line1']);
$set_default_links = $this
->getSession()
->getPage()
->findAll('css', '.address-book__set-default-link');
$this
->assertCount(1, $set_default_links);
$set_default_link = reset($set_default_links);
$set_default_link
->click();
$this
->assertSession()
->pageTextContains($this->secondAddress['address_line1'] . ' is now the default address.');
$set_default_links = $this
->getSession()
->getPage()
->findAll('css', '.address-book__set-default-link');
$this
->assertCount(1, $set_default_links);
$set_default_link = reset($set_default_links);
$set_default_link
->click();
$this
->assertSession()
->pageTextContains('39 Rue du Sentier is now the default address.');
$delete_links = $this
->getSession()
->getPage()
->findAll('css', '.address-book__delete-link');
$this
->assertCount(2, $delete_links);
$delete_link = reset($delete_links);
$delete_link
->click();
$this
->assertSession()
->pageTextContains('Are you sure you want to delete the 39 Rue du Sentier address?');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('The 39 Rue du Sentier address has been deleted.');
}
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');
$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]');
$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');
$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');
$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(),
]));
$container = $this
->getSession()
->getPage()
->find('css', '.address-book__container--customer_shipping');
$this
->assertStringNotContainsString('There are no addresses yet.', $container
->getText());
$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);
$this
->createEntity('profile', [
'type' => 'customer_test',
'uid' => $customer
->id(),
'address' => $this->fourthAddress,
]);
$this
->assertSession()
->pageTextNotContains('Test information');
$this
->assertSession()
->pageTextNotContains($this->fourthAddress['address_line1']);
}
}