Example #1
1
  /**
   * Build all necessary things for child form (form state, etc.).
   *
   * @param \Drupal\Core\Entity\EntityFormInterface $controller
   *   Entity form controller for child form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Parent form state object.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   Entity object.
   * @param string $operation
   *   Operation that is to be performed in inline form.
   * @param array $parents
   *   Entity form #parents.
   *
   * @return \Drupal\Core\Form\FormStateInterface
   *   Child form state object.
   */
  public static function buildChildFormState(EntityFormInterface $controller, FormStateInterface $form_state, EntityInterface $entity, $operation, $parents) {
    $child_form_state = new FormState();

    $child_form_state->addBuildInfo('callback_object', $controller);
    $child_form_state->addBuildInfo('base_form_id', $controller->getBaseFormID());
    $child_form_state->addBuildInfo('form_id', $controller->getFormID());
    $child_form_state->addBuildInfo('args', array());

    // Copy values to child form.
    $child_form_state->setCompleteForm($form_state->getCompleteForm());
    $child_form_state->setUserInput($form_state->getUserInput());

    // Filter out all submitted values that are not directly relevant for this
    // IEF. Otherwise they might mess things up.
    $form_state_values = $form_state->getValues();
    $form_state_values = static::extractArraySequence($form_state_values, $parents);

    $child_form_state->setValues($form_state_values);
    $child_form_state->setStorage($form_state->getStorage());
    $value = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.' . $operation);
    $child_form_state->set('form_display', $value);

    // Since some of the submit handlers are run, redirects need to be disabled.
    $child_form_state->disableRedirect();

    // When a form is rebuilt after Ajax processing, its #build_id and #action
    // should not change.
    // @see drupal_rebuild_form()
    $rebuild_info = $child_form_state->getRebuildInfo();
    $rebuild_info['copy']['#build_id'] = TRUE;
    $rebuild_info['copy']['#action'] = TRUE;
    $child_form_state->setRebuildInfo($rebuild_info);

    $child_form_state->set('inline_entity_form', $form_state->get('inline_entity_form'));
    $child_form_state->set('langcode', $entity->language()->getId());

    $child_form_state->set('field', $form_state->get('field'));
    $child_form_state->setTriggeringElement($form_state->getTriggeringElement());
    $child_form_state->setSubmitHandlers($form_state->getSubmitHandlers());

    return $child_form_state;
  }
 /**
  * Page that triggers a programmatic form submission.
  *
  * Returns the validation errors triggered by the form submission as json.
  */
 public function submitFormPage()
 {
     $form_state = new FormState();
     $values = ['name' => 'robo-user', 'mail' => '*****@*****.**', 'op' => t('Submit')];
     $form_state->setValues($values);
     \Drupal::formBuilder()->submitForm('\\Drupal\\user\\Form\\UserPasswordForm', $form_state);
     return new JsonResponse($form_state->getErrors());
 }
 /**
  * @covers ::submitForm
  * @covers ::getPluginSelector
  */
 public function testSubmitForm()
 {
     $form = ['plugin_selector' => ['foo' => $this->randomMachineName()]];
     $form_state = new FormState();
     $form_state->setValues(['plugin_selector_id' => $this->configFactoryConfiguration['payment_form.payment_type']['plugin_selector_id'], 'allowed_plugin_ids' => $this->configFactoryConfiguration['payment_form.payment_type']['allowed_plugin_ids'], 'limit_allowed_plugins' => $this->configFactoryConfiguration['payment_form.payment_type']['limit_allowed_plugins']]);
     $map = [['payment_radios', [], $this->pluginSelector], [$this->configFactoryConfiguration['payment_form.payment_type']['plugin_selector_id'], [], $this->selectedPluginSelector]];
     $this->pluginSelectorManager->expects($this->atLeast(count($map)))->method('createInstance')->willReturnMap($map);
     $this->pluginSelector->expects($this->once())->method('submitSelectorForm')->with($form['plugin_selector'], $form_state);
     $this->pluginSelector->expects($this->once())->method('getSelectedPlugin')->willReturn($this->selectedPluginSelector);
     $this->sut->submitForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $form_values = $form_state->getValues();
     $plugin_settings = $form_state->get('plugin_settings');
     $field_types = $form_values['fields'];
     // Remove from configuration the keys of the field types which have no
     // plugin selected. We need to clear this keys from configuration first
     // and then save the settings for the fields which have a plugin selected.
     // If we do both writing and clearing in the same for, the values won't get
     // saved.
     foreach ($field_types as $field_type => $field_type_values) {
         // If there is no plugin selected remove the key from config file.
         if ($field_type_values['plugin']['type'] == 'hidden') {
             $this->config->clear($field_type);
         }
     }
     $this->config->save();
     // For field types that have a plugin selected save the settings.
     foreach ($field_types as $field_type => $field_type_values) {
         if ($field_type_values['plugin']['type'] != 'hidden') {
             // Get plugin settings. They lie either directly in submitted form
             // values (if the whole form was submitted while some plugin settings
             // were being edited), or have been persisted in $form_state.
             $plugin = $this->diffBuilderManager->createInstance($field_type_values['plugin']['type']);
             // Form submitted without pressing update button on plugin settings form.
             if (isset($field_type_values['settings_edit_form']['settings'])) {
                 $settings = $field_type_values['settings_edit_form']['settings'];
             } elseif (isset($plugin_settings[$field_type]['settings'])) {
                 $settings = $plugin_settings[$field_type]['settings'];
             } else {
                 $settings = $plugin->defaultConfiguration();
             }
             // Build a FormState object and call the plugin submit handler.
             $state = new FormState();
             $state->setValues($settings);
             $state->set('field_type', $field_type);
             $plugin->submitConfigurationForm($form, $state);
         }
     }
     drupal_set_message($this->t('Your settings have been saved.'));
 }
Example #5
0
 /**
  * @covers ::setValues
  *
  * @dataProvider providerTestSetValues
  */
 public function testSetValues($parents, $new_values, $expected)
 {
     $parent_form_state = new FormState();
     $parent_form_state->setValues($this->formStateValues);
     $subform = NestedArray::getValue($this->parentForm, $parents);
     $subform_state = SubformState::createForSubform($subform, $this->parentForm, $parent_form_state);
     $this->assertSame($subform_state, $subform_state->setValues($new_values));
     $this->assertSame($expected, $parent_form_state->getValues());
 }
Example #6
0
 /**
  * Builds the form state passed to zone members.
  *
  * @param array $member_parents
  *   The parents array indicating the position of the member form.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The parent form state.
  *
  * @return \Drupal\Core\Form\FormStateInterface
  *   The new member form state.
  */
 protected function buildMemberFormState($member_parents, FormStateInterface $form_state)
 {
     $member_values = $form_state->getValue($member_parents, []);
     $member_user_input = (array) NestedArray::getValue($form_state->getUserInput(), $member_parents);
     $member_form_state = new FormState();
     $member_form_state->setValues($member_values);
     $member_form_state->setUserInput($member_user_input);
     return $member_form_state;
 }
 /**
  * @covers ::submitForm
  */
 public function testSubmitForm()
 {
     $plugin_id = $this->randomMachineName();
     $values = ['default_plugin_id' => $plugin_id];
     $form = [];
     $form_state = new FormState();
     $form_state->setValues($values);
     $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
     $config->expects($this->atLeastOnce())->method('set')->with('plugin_id', $plugin_id);
     $config->expects($this->atLeastOnce())->method('save');
     $this->configFactory->expects($this->atLeastOnce())->method('getEditable')->with('currency.amount_formatting')->willReturn($config);
     $this->controller->submitForm($form, $form_state);
 }
 /**
  * @covers ::submitForm
  */
 public function testSubmitForm()
 {
     $plugin_id_a = $this->randomMachineName();
     $plugin_enabled_a = (bool) mt_rand(0, 1);
     $plugin_id_b = $this->randomMachineName();
     $plugin_enabled_b = (bool) mt_rand(0, 1);
     $plugin_id_c = $this->randomMachineName();
     $plugin_enabled_c = (bool) mt_rand(0, 1);
     $configuration = [$plugin_id_c => $plugin_enabled_c, $plugin_id_a => $plugin_enabled_a, $plugin_id_b => $plugin_enabled_b];
     $values = ['exchange_rate_providers' => [$plugin_id_c => ['enabled' => $plugin_enabled_c, 'weight' => mt_rand(9, 99)], $plugin_id_a => ['enabled' => $plugin_enabled_a, 'weight' => mt_rand(999, 9999)], $plugin_id_b => ['enabled' => $plugin_enabled_b, 'weight' => mt_rand(99999, 999999)]]];
     $form = [];
     $form_state = new FormState();
     $form_state->setValues($values);
     $this->exchangeRateProvider->expects($this->once())->method('saveConfiguration')->with(new \PHPUnit_Framework_Constraint_IsIdentical($configuration));
     $this->sut->submitForm($form, $form_state);
 }
 /**
  * Build all necessary things for child form (form state, etc.).
  *
  * @param \Drupal\Core\Entity\EntityFormInterface $controller
  *   Entity form controller for child form.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   Parent form state object.
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   Entity object.
  * @param string $operation
  *   Operation that is to be performed in inline form.
  *
  * @return \Drupal\Core\Form\FormStateInterface
  *   Child form state object.
  */
 public static function buildChildFormState(EntityFormInterface $controller, FormStateInterface $form_state, EntityInterface $entity, $operation)
 {
     $child_form_state = new FormState();
     $child_form_state->addBuildInfo('callback_object', $controller);
     $child_form_state->addBuildInfo('base_form_id', $controller->getBaseFormID());
     $child_form_state->addBuildInfo('form_id', $controller->getFormID());
     $child_form_state->addBuildInfo('args', array());
     // Copy values to child form.
     $child_form_state->setUserInput($form_state->getUserInput());
     $child_form_state->setValues($form_state->getValues());
     $child_form_state->setStorage($form_state->getStorage());
     $child_form_state->set('form_display', entity_get_form_display($entity->getEntityTypeId(), $entity->bundle(), $operation));
     // Since some of the submit handlers are run, redirects need to be disabled.
     $child_form_state->disableRedirect();
     // When a form is rebuilt after Ajax processing, its #build_id and #action
     // should not change.
     // @see drupal_rebuild_form()
     $rebuild_info = $child_form_state->getRebuildInfo();
     $rebuild_info['copy']['#build_id'] = TRUE;
     $rebuild_info['copy']['#action'] = TRUE;
     $child_form_state->setRebuildInfo($rebuild_info);
     $child_form_state->set('inline_entity_form', $form_state->get('inline_entity_form'));
     $child_form_state->set('langcode', $entity->language()->getId());
     $child_form_state->set('field', $form_state->get('field'));
     $child_form_state->setTriggeringElement($form_state->getTriggeringElement());
     $child_form_state->setSubmitHandlers($form_state->getSubmitHandlers());
     return $child_form_state;
 }
 /**
  * Testing the form save function of the values.
  *
  * Checking that values are stored correctly in the configuration.
  */
 public function testSaveSettingsForm()
 {
     $admin_form = new AdminForm($this->configFactory, $this->mailManager, $this->moduleHandler, $this->themeHandler);
     $config = $this->configFactory->getEditable('mailsystem.settings');
     $form = array();
     // Global configuration.
     $form_state = new FormState();
     $form_state->setValues(array('mailsystem' => array('default_formatter' => 'mailsystem_test', 'default_sender' => 'mailsystem_demo', 'default_theme' => 'test_theme')));
     $admin_form->submitForm($form, $form_state);
     $this->assertEquals('mailsystem_test', $config->get('defaults.formatter'), 'Default formatter changed');
     $this->assertEquals('mailsystem_demo', $config->get('defaults.sender'), 'Default sender changed');
     $this->assertEquals('test_theme', $config->get('theme'), 'Default theme changed');
     // Override a custom module setting with no mail key.
     $form_state = new FormState();
     $form_state->setValues(array('custom' => array('custom_module' => 'module_one', 'custom_module_key' => 'mail_key', 'custom_formatter' => 'mailsystem_test', 'custom_sender' => 'mailsystem_demo')));
     $admin_form->submitForm($form, $form_state);
     $base = MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.module_one.mail_key';
     $this->assertEquals('mailsystem_test', $config->get($base . '.' . MailsystemManager::MAILSYSTEM_TYPE_FORMATTING), 'Module one formatter changed');
     $this->assertEquals('mailsystem_demo', $config->get($base . '.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING), 'Module one sender changed');
     // Override a custom module setting with a mail key and no sender.
     $form_state = new FormState();
     $form_state->setValues(array('custom' => array('custom_module' => 'module_two', 'custom_module_key' => '', 'custom_formatter' => 'mailsystem_test', 'custom_sender' => 'none')));
     $admin_form->submitForm($form, $form_state);
     $base = MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.module_two.none';
     $this->assertEquals('mailsystem_test', $config->get($base . '.' . MailsystemManager::MAILSYSTEM_TYPE_FORMATTING), 'Module two with no key formatter changed');
     $this->assertEquals(NULL, $config->get($base . '.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING), 'Module two with no key sender changed to nothing');
     // Add a custom module setting with a mail key and no sender.
     $form_state = new FormState();
     $form_state->setValues(array('custom' => array('custom_module' => 'module_three', 'custom_module_key' => 'mail_key', 'custom_formatter' => 'none', 'custom_sender' => 'mailsystem_test')));
     $admin_form->submitForm($form, $form_state);
     $base = MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.module_three.mail_key';
     $this->assertEquals(NULL, $config->get($base . '.' . MailsystemManager::MAILSYSTEM_TYPE_FORMATTING), 'Module three no formatter added');
     $this->assertEquals('mailsystem_test', $config->get($base . '.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING), 'Module three sender added');
     // Clear the configuration for some modules.
     $form_state = new FormState();
     $form_state->setValues(array('custom' => array('modules' => array('module_two' => 'module_two', 'module_one' => 'not_clean'))));
     $admin_form->submitForm($form, $form_state);
     $this->assertEquals('mailsystem_test', $config->get(MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.module_three.mail_key.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING), 'After clean, module three exists');
     $this->assertEquals('mailsystem_demo', $config->get(MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.module_one.mail_key.' . MailsystemManager::MAILSYSTEM_TYPE_SENDING), 'After clean, module one exists');
     $this->assertEquals(NULL, $config->get(MailsystemManager::MAILSYSTEM_MODULES_CONFIG . '.module_two'), 'After clean, module two does not exists');
 }
 /**
  * @covers ::validate
  */
 public function testValidate()
 {
     $line_item_name_a = $this->randomMachineName();
     $line_item_name_b = $this->randomMachineName();
     $line_item_name_c = $this->randomMachineName();
     $root_element_name = $this->randomMachineName();
     $form_build = array('foo' => array('#name' => $root_element_name, '#parents' => array('foo'), 'line_items' => []));
     $line_item_a = $this->getMock(PaymentLineItemInterface::class);
     $line_item_b = $this->getMock(PaymentLineItemInterface::class);
     $line_item_c = $this->getMock(PaymentLineItemInterface::class);
     /** @var \PHPUnit_Framework_MockObject_MockObject[] $line_items */
     $line_items = array($line_item_name_a => $line_item_a, $line_item_name_b => $line_item_b, $line_item_name_c => $line_item_c);
     foreach ($line_items as $line_item_name => $line_item) {
         $form_build['foo']['line_items'][$line_item_name] = array('plugin_form' => array('#foo' => $this->randomMachineName()));
         $line_item->expects($this->atLeastOnce())->method('getName')->willReturn($line_item_name);
         $line_item->expects($this->once())->method('validateConfigurationForm')->with($form_build['foo']['line_items'][$line_item_name]['plugin_form']);
         $line_item->expects($this->once())->method('submitConfigurationForm')->with($form_build['foo']['line_items'][$line_item_name]['plugin_form']);
     }
     $form_state = new FormState();
     $form_state->set('payment.element.payment_line_items_input.configured.' . $root_element_name, array_values($line_items));
     $form_state->setValues(array('foo' => array('line_items' => array($line_item_name_a => array('weight' => 3), $line_item_name_b => array('weight' => 1), $line_item_name_c => array('weight' => 2)))));
     $this->sut->validate($form_build['foo'], $form_state, $form_build);
     $element = $this->sut;
     $line_items = $element::getLineItems($form_build['foo'], $form_state);
     $this->assertSame(array($line_item_b, $line_item_c, $line_item_a), $line_items);
 }
Example #12
0
 /**
  * @covers ::setValues
  * @covers ::getValues
  */
 public function testGetValues()
 {
     $values = ['foo' => 'bar'];
     $form_state = new FormState();
     $form_state->setValues($values);
     $this->assertSame($values, $form_state->getValues());
 }
 /**
  * @covers ::submitConfigurationForm
  * @covers ::getExecutePaymentStatusSelector
  * @covers ::getCapturePaymentStatusSelector
  * @covers ::getRefundPaymentStatusSelector
  * @covers ::getPaymentStatusSelector
  */
 public function testSubmitConfigurationForm()
 {
     $brand_label = $this->randomMachineName();
     $message = $this->randomMachineName();
     $execute_status_id = $this->randomMachineName();
     $capture = TRUE;
     $capture_status_id = $this->randomMachineName();
     $refund = TRUE;
     $refund_status_id = $this->randomMachineName();
     $payment_status = $this->getMock(PaymentStatusInterface::class);
     $this->paymentStatusManager->expects($this->atLeastOnce())->method('createInstance')->willReturn($payment_status);
     $payment_status_selector = $this->getMock(PluginSelectorInterface::class);
     $payment_status_selector->expects($this->atLeastOnce())->method('getSelectedPlugin')->willReturn($payment_status);
     $this->pluginSelectorManager->expects($this->atLeastOnce())->method('createInstance')->willReturn($payment_status_selector);
     $form = array('message' => array('#parents' => array('foo', 'bar', 'message')), 'plugin_form' => array('brand_label' => array('#parents' => array('foo', 'bar', 'status')), 'execute' => ['execute_status' => ['#foo' => $this->randomMachineName()]], 'capture' => ['plugin_form' => ['capture_status' => ['#foo' => $this->randomMachineName()]]], 'refund' => ['plugin_form' => ['refund_status' => ['#foo' => $this->randomMachineName()]]]));
     $form_state = new FormState();
     $form_state->setValues(['foo' => array('bar' => array('brand_label' => $brand_label, 'message' => $message, 'execute' => array('execute_status_id' => $execute_status_id), 'capture' => array('capture' => $capture, 'capture_status_id' => $capture_status_id), 'refund' => array('refund' => $refund, 'refund_status_id' => $refund_status_id)))]);
     $this->sut->submitConfigurationForm($form, $form_state);
     $this->assertSame($brand_label, $this->sut->getBrandLabel());
     $this->assertSame($capture, $this->sut->getCapture());
     $this->assertSame($refund, $this->sut->getRefund());
 }
 /**
  * Test submitForm with an fid, ensures values are set correctly.
  *
  * @covers ::ajaxSave
  *
  * @test
  */
 public function ajaxSaveWithFidLoadsEntitiesAndSetsFormStateValues()
 {
     $form = [];
     $catalog_id = 'test_catalog';
     $app_id = 'test_application_id';
     $source_url = 'www.example.com/test_application_id/test.jpg';
     $form['asset']['#catalog_id'] = $catalog_id;
     $form_state = new FormState();
     $intial_values = ['asset' => [self::MOCK_ASSET_ID], 'attributes' => ['data-conversion' => 'thumb']];
     $form_state->setValues($intial_values);
     $mock_asset = $this->getMockBuilder(EmbridgeAssetEntity::class)->disableOriginalConstructor()->getMock();
     $mock_asset->expects($this->once())->method('uuid')->willReturn(self::MOCK_ASSET_UUID);
     $mock_asset->expects($this->once())->method('isTemporary')->willReturn(TRUE);
     $mock_asset->expects($this->once())->method('setPermanent');
     $mock_asset->expects($this->once())->method('save');
     $mock_asset_storage = $this->getMockBuilder(EntityStorageInterface::class)->disableOriginalConstructor()->getMock();
     $mock_asset_storage->expects($this->once())->method('load')->with(self::MOCK_ASSET_ID)->willReturn($mock_asset);
     $mock_catalog = $this->getMockBuilder(EmbridgeCatalog::class)->disableOriginalConstructor()->getMock();
     $mock_catalog->expects($this->once())->method('getApplicationId')->willReturn($app_id);
     $mock_catalog_storage = $this->getMockBuilder(EntityStorageInterface::class)->disableOriginalConstructor()->getMock();
     $mock_catalog_storage->expects($this->once())->method('load')->with($catalog_id)->willReturn($mock_catalog);
     $this->entityTypeManager->expects($this->exactly(2))->method('getStorage')->will($this->returnValueMap([['embridge_asset_entity', $mock_asset_storage], ['embridge_catalog', $mock_catalog_storage]]));
     $this->assetHelper->expects($this->once())->method('getAssetConversionUrl')->with($mock_asset, $app_id, 'thumb')->willReturn($source_url);
     $this->form->ajaxSave($form, $form_state);
     $expected_values = array_merge_recursive(['attributes' => ['src' => $source_url, 'data-entity-uuid' => self::MOCK_ASSET_UUID, 'data-entity-type' => 'embridge_asset_entity']], $intial_values);
     $actual_values = $form_state->getValues();
     $this->assertEquals($expected_values, $actual_values);
 }