diff -u b/modules/order/src/Element/ProfileSelect.php b/modules/order/src/Element/ProfileSelect.php --- b/modules/order/src/Element/ProfileSelect.php +++ b/modules/order/src/Element/ProfileSelect.php @@ -141,6 +141,9 @@ if ($user) { $profiles = $profile_storage->loadMultipleByUser($user, $element['#profile_type'], TRUE); } + elseif ($profile = $form_state->get('profile')) { + $profiles[] = $profile; + } $profile_options = []; foreach ($profiles as $profile) { $option_id = self::buildOptionId($profile, $element['#profile_latest_revision']); @@ -199,7 +202,7 @@ $element['profile_selection'] = [ '#type' => 'select', '#title' => $element['#title'], - '#options' => $profile_options + ['_new' => $element['#create_title']], + '#options' => $profile_options, '#default_value' => $selected_option, '#ajax' => [ 'callback' => [get_called_class(), 'ajaxRefresh'], @@ -209,6 +212,9 @@ '#weight' => -1, '#access' => !empty($profile_options), ]; + if (\Drupal::currentUser()->isAuthenticated()) { + $element['profile_selection']['#options'] += ['_new' => $element['#create_title']]; + } $view_builder = \Drupal::entityTypeManager()->getViewBuilder('profile'); $element['profile'] = $view_builder->view($element['#profile'], 'default'); @@ -325,8 +331,9 @@ $form_display->extractFormValues($element['#profile'], $element['form'], $form_state); $form_display->validateFormValues($element['#profile'], $element['form'], $form_state); if (!FormState::hasAnyErrors() && $element['#profile']->hasTranslationChanges()) { - $element['#profile']->save(); + $foo = $element['#profile']->save(); $form_state->set('return_to_view', TRUE); + $form_state->set('profile', $element['#profile']); } } if (!empty($triggering_element['#cancel'])) { diff -u b/modules/order/tests/src/FunctionalJavascript/ProfileSelectTest.php b/modules/order/tests/src/FunctionalJavascript/ProfileSelectTest.php --- b/modules/order/tests/src/FunctionalJavascript/ProfileSelectTest.php +++ b/modules/order/tests/src/FunctionalJavascript/ProfileSelectTest.php @@ -4,17 +4,14 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Url; -use Drupal\Tests\commerce\Functional\CommerceBrowserTestBase; -use Drupal\Tests\commerce\FunctionalJavascript\JavascriptTestTrait; +use Drupal\FunctionalJavascriptTests\JavascriptTestBase; /** * Tests the profile select form element. * * @group commerce */ -class ProfileSelectTest extends CommerceBrowserTestBase { - - use JavascriptTestTrait; +class ProfileSelectTest extends JavascriptTestBase { /** * Profile address values. @@ -57,15 +54,27 @@ * @var array */ public static $modules = [ + 'system', + 'commerce', + 'commerce_order', 'commerce_order_test', ]; /** + * A test user with normal privileges. + * + * @var \Drupal\user\UserInterface + */ + protected $user; + + /** * @inheritDoc */ protected function setUp() { parent::setUp(); + $this->user = $this->drupalCreateUser(); + $this->drupalLogin($this->user); $this->profileStorage = $this->container->get('entity_type.manager')->getStorage('profile'); } @@ -76,13 +85,13 @@ // Create a profile for a different user to make sure it's not shown. $profile1 = $this->createEntity('profile', [ 'type' => 'customer', - 'uid' => $this->adminUser->id(), + 'uid' => $this->user->id(), 'address' => $this->address1, ]); $this->drupalLogout(); $this->drupalGet(Url::fromRoute('commerce_order_test.profile_select_form')); - $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->pageTextContains('Profile select test form'); $this->assertSession()->fieldNotExists('Select a profile'); $this->getSession()->getPage()->fillField('profile[form][address][0][address][country_code]', $this->address1['country_code']); $this->waitForAjaxToFinish(); @@ -98,7 +107,7 @@ /** @var \Drupal\profile\Entity\ProfileInterface $profile */ $profile = $this->profileStorage->load(1); - $this->submitForm($edit, 'Submit'); + $this->submitForm([], 'Submit'); $this->assertSession()->responseContains(new FormattableMarkup('Profile selected: :label', [':label' => $profile->label()])); /** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $address */ @@ -118,19 +127,19 @@ /** @var \Drupal\profile\Entity\ProfileInterface $profile1 */ $profile1 = $this->createEntity('profile', [ 'type' => 'customer', - 'uid' => $this->adminUser->id(), + 'uid' => $this->user->id(), 'address' => $this->address1, ]); /** @var \Drupal\profile\Entity\ProfileInterface $profile2 */ $profile2 = $this->createEntity('profile', [ 'type' => 'customer', - 'uid' => $this->adminUser->id(), + 'uid' => $this->user->id(), 'address' => $this->address2, 'is_default' => TRUE, ]); $this->drupalGet(Url::fromRoute('commerce_order_test.profile_select_form')); - $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->pageTextContains('Profile select test form'); $this->assertSession()->fieldExists('Select a profile'); // The last created profile should be selected by default. $this->assertSession()->pageTextContains($this->address2['locality']); @@ -153,7 +162,7 @@ $this->assertEquals($this->address1['locality'], $address->getLocality()); $this->assertEquals($this->address1['postal_code'], $address->getPostalCode()); - $profiles = $this->profileStorage->loadMultipleByUser($this->adminUser, 'customer', TRUE); + $profiles = $this->profileStorage->loadMultipleByUser($this->user, 'customer', TRUE); $this->assertCount(2, $profiles); } @@ -162,7 +171,7 @@ */ public function testAuthenticatedCreate() { $this->drupalGet(Url::fromRoute('commerce_order_test.profile_select_form')); - $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->pageTextContains('Profile select test form'); $this->assertSession()->fieldNotExists('Select a profile'); $this->getSession()->getPage()->fillField('Country', $this->address1['country_code']); $this->waitForAjaxToFinish(); @@ -190,7 +199,7 @@ $this->assertEquals($this->address1['locality'], $address->getLocality()); $this->assertEquals($this->address1['postal_code'], $address->getPostalCode()); - $profiles = $this->profileStorage->loadMultipleByUser($this->adminUser, 'customer', TRUE); + $profiles = $this->profileStorage->loadMultipleByUser($this->user, 'customer', TRUE); $this->assertCount(1, $profiles); } @@ -203,21 +212,21 @@ /** @var \Drupal\profile\Entity\ProfileInterface $profile1 */ $profile1 = $this->createEntity('profile', [ 'type' => 'customer', - 'uid' => $this->adminUser->id(), + 'uid' => $this->user->id(), 'address' => $this->address1, ]); $profile1->save(); /** @var \Drupal\profile\Entity\ProfileInterface $profile2 */ $profile2 = $this->createEntity('profile', [ 'type' => 'customer', - 'uid' => $this->adminUser->id(), + 'uid' => $this->user->id(), 'address' => $this->address2, 'is_default' => TRUE, ]); $profile2->save(); $this->drupalGet(Url::fromRoute('commerce_order_test.profile_select_form')); - $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->pageTextContains('Profile select test form'); $this->assertSession()->fieldExists('Select a profile'); // The last created profile should be selected by default. $this->assertSession()->pageTextContains($this->address2['locality']); @@ -248,6 +257,42 @@ - $profiles = $this->profileStorage->loadMultipleByUser($this->adminUser, 'customer', TRUE); + $profiles = $this->profileStorage->loadMultipleByUser($this->user, 'customer', TRUE); $this->assertCount(2, $profiles); } + /** + * Creates a new entity. + * + * @param string $entity_type + * The entity type to be created. + * @param array $values + * An array of settings. + * Example: 'id' => 'foo'. + * + * @return \Drupal\Core\Entity\EntityInterface + * A new entity. + */ + protected function createEntity($entity_type, array $values) { + /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */ + $storage = \Drupal::service('entity_type.manager')->getStorage($entity_type); + $entity = $storage->create($values); + $status = $entity->save(); + $this->assertEquals(SAVED_NEW, $status, new FormattableMarkup('Created %label entity %type.', [ + '%label' => $entity->getEntityType()->getLabel(), + '%type' => $entity->id(), + ])); + // The newly saved entity isn't identical to a loaded one, and would fail + // comparisons. + $entity = $storage->load($entity->id()); + + return $entity; + } + + /** + * Waits for jQuery to become active and animations to complete. + */ + protected function waitForAjaxToFinish() { + $condition = "(0 === jQuery.active && 0 === jQuery(':animated').length)"; + $this->assertJsCondition($condition, 10000); + } + }