View source
<?php
namespace Drupal\Tests\commerce_order\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\profile\Entity\Profile;
use Drupal\profile\Entity\ProfileType;
class CustomerProfileTypeTest extends OrderBrowserTestBase {
public static $modules = [
'field_ui',
];
protected function getAdministratorPermissions() {
return array_merge([
'administer profile types',
'administer profile fields',
'administer profile',
], parent::getAdministratorPermissions());
}
protected function setUp() : void {
parent::setUp();
$user_form_display = EntityFormDisplay::load('user.user.default');
if (!$user_form_display) {
$user_form_display = EntityFormDisplay::create([
'targetEntityType' => 'user',
'bundle' => 'user',
'mode' => 'default',
'status' => TRUE,
]);
$user_form_display
->save();
}
}
public function testProfileTypeUi() {
$profile_type = ProfileType::load('customer');
$this
->assertFalse($profile_type
->access('delete'));
$this
->assertTrue($profile_type
->getThirdPartySetting('commerce_order', 'customer_profile_type'));
$this
->drupalGet($profile_type
->toUrl('edit-form'));
$checkbox = $this
->getSession()
->getPage()
->findField('commerce_order[customer_profile_type]');
$this
->assertNotEmpty($checkbox);
$this
->assertNotEmpty($checkbox
->getAttribute('checked'));
$this
->assertNotEmpty($checkbox
->getAttribute('disabled'));
$this
->submitForm([], 'Save');
$profile_type = $this
->reloadEntity($profile_type);
$this
->assertTrue($profile_type
->getThirdPartySetting('commerce_order', 'customer_profile_type'));
$this
->drupalGet('admin/config/people/profile-types/add');
$checkbox = $this
->getSession()
->getPage()
->findField('commerce_order[customer_profile_type]');
$this
->assertNotEmpty($checkbox);
$this
->assertEmpty($checkbox
->getAttribute('checked'));
$this
->assertEmpty($checkbox
->getAttribute('disabled'));
$this
->submitForm([
'label' => 'Customer (Shipping information)',
'display_label' => 'Shipping information',
'id' => 'customer_shipping',
], 'Save');
$profile_type = ProfileType::load('customer_shipping');
$this
->assertFalse($profile_type
->getThirdPartySetting('commerce_order', 'customer_profile_type'));
$address_field = FieldConfig::loadByName('profile', 'customer_shipping', 'address');
$this
->assertEmpty($address_field);
$this
->drupalGet($profile_type
->toUrl('edit-form'));
$this
->submitForm([
'commerce_order[customer_profile_type]' => TRUE,
], 'Save');
$profile_type = $this
->reloadEntity($profile_type);
$this
->assertTrue($profile_type
->getThirdPartySetting('commerce_order', 'customer_profile_type'));
$address_field = FieldConfig::loadByName('profile', 'customer_shipping', 'address');
$this
->assertNotEmpty($address_field);
$this
->assertEquals('Address', $address_field
->getLabel());
$this
->assertTrue($address_field
->isRequired());
$this
->drupalGet($profile_type
->toUrl('edit-form'));
$checkbox = $this
->getSession()
->getPage()
->findField('commerce_order[customer_profile_type]');
$this
->assertNotEmpty($checkbox);
$this
->assertNotEmpty($checkbox
->getAttribute('checked'));
$this
->assertEmpty($checkbox
->getAttribute('disabled'));
$profile = Profile::create([
'type' => 'customer_shipping',
'address' => [
'country_code' => 'US',
'administrative_area' => 'SC',
'locality' => 'Greenville',
'postal_code' => '29616',
'address_line1' => '9 Drupal Ave',
'given_name' => 'Bryan',
'family_name' => 'Centarro',
],
]);
$profile
->save();
$this
->drupalGet($profile_type
->toUrl('edit-form'));
$checkbox = $this
->getSession()
->getPage()
->findField('commerce_order[customer_profile_type]');
$this
->assertNotEmpty($checkbox);
$this
->assertNotEmpty($checkbox
->getAttribute('checked'));
$this
->assertNotEmpty($checkbox
->getAttribute('disabled'));
$profile
->delete();
$this
->drupalGet($profile_type
->toUrl('edit-form'));
$this
->submitForm([
'commerce_order[customer_profile_type]' => FALSE,
], 'Save');
$address_field = FieldConfig::loadByName('profile', 'customer_shipping', 'address');
$this
->assertEmpty($address_field);
}
public function testAddressFieldUi() {
$profile_type = ProfileType::load('customer');
$this
->drupalGet($profile_type
->toUrl('duplicate-form'));
$this
->submitForm([
'label' => 'Customer (Shipping information)',
'display_label' => 'Shipping information',
'id' => 'customer_shipping',
], 'Save');
$new_profile_type = ProfileType::load('customer_shipping');
$this
->assertNotEmpty($new_profile_type);
$fields_ui_url = Url::fromRoute('entity.profile.field_ui_fields', [
'profile_type' => 'customer_shipping',
]);
$this
->drupalGet($fields_ui_url);
$operation_links = $this
->xpath('//tr[@id="address"]//ul[@class = "dropbutton"]/li/a');
$link_labels = [];
foreach ($operation_links as $link) {
$link_labels[] = $link
->getText();
}
$this
->assertNotContains('Delete', $link_labels);
$this
->assertNotContains('Storage settings', $link_labels);
$field_config = FieldConfig::loadByName('profile', 'customer_shipping', 'address');
$this
->drupalGet($field_config
->toUrl('profile-field-edit-form'));
$this
->assertSession()
->fieldNotExists('required');
$this
->assertSession()
->fieldNotExists('settings[available_countries][]');
}
}