/** * @covers ::extractFormValues */ public function testExtractFormValues() { $menu_link_manager = $this->prophesize(MenuLinkManagerInterface::class); $menu_parent_form_selector = $this->prophesize(MenuParentFormSelectorInterface::class); $module_handler = $this->prophesize(ModuleHandlerInterface::class); $menu_link_form = new MenuLinkDefaultForm($menu_link_manager->reveal(), $menu_parent_form_selector->reveal(), $this->getStringTranslationStub(), $module_handler->reveal()); $static_override = $this->prophesize(StaticMenuLinkOverridesInterface::class); $menu_link = new MenuLinkDefault([], 'my_plugin_id', [], $static_override->reveal()); $menu_link_form->setMenuLinkInstance($menu_link); $form_state = new FormState(); $form_state->setValue('id', 'my_plugin_id'); $form_state->setValue('enabled', FALSE); $form_state->setValue('weight', 5); $form_state->setValue('expanded', TRUE); $form_state->setValue('menu_parent', 'foo:bar'); $form = []; $result = $menu_link_form->extractFormValues($form, $form_state); $this->assertEquals(['id' => 'my_plugin_id', 'enabled' => 0, 'weight' => 5, 'expanded' => 1, 'parent' => 'bar', 'menu_name' => 'foo'], $result); }
/** * Tests that #limit_validation_errors of the only submit button takes effect. */ function testLimitValidationErrors() { // Programmatically submit the form. $form_state = new FormState(); $form_state->setValue('section', 'one'); $form_builder = $this->container->get('form_builder'); $form_builder->submitForm($this, $form_state); // Verify that only the specified section was validated. $errors = $form_state->getErrors(); $this->assertTrue(isset($errors['one']), "Section 'one' was validated."); $this->assertFalse(isset($errors['two']), "Section 'two' was not validated."); // Verify that there are only values for the specified section. $this->assertTrue($form_state->hasValue('one'), "Values for section 'one' found."); $this->assertFalse($form_state->hasValue('two'), "Values for section 'two' not found."); }
/** * @covers ::copyFormValuesToEntity */ public function testCopyFormValuesToEntity() { $description = $this->randomMachineName(); $id = $this->randomMachineName(); $label = $this->randomMachineName(); $parent_id = $this->randomMachineName(); $this->paymentStatus->expects($this->once())->method('setDescription')->with($description); $this->paymentStatus->expects($this->once())->method('setId')->with($id); $this->paymentStatus->expects($this->once())->method('setLabel')->with($label); $this->paymentStatus->expects($this->once())->method('setParentId')->with($parent_id); $parent_status = $this->getMock(PluginSelectorInterface::class); $parent_status->expects($this->atLeastOnce())->method('getPluginId')->willReturn($parent_id); $parent_selector = $this->getMock(PluginSelectorInterface::class); $parent_selector->expects($this->atLeastOnce())->method('getSelectedPlugin')->willReturn($parent_status); $this->pluginSelectorManager->expects($this->atLeastOnce())->method('createInstance')->willReturn($parent_selector); $form = []; $form_state = new FormState(); $form_state->setValue('description', $description); $form_state->setValue('id', $id); $form_state->setValue('label', $label); $form_state->setValue('parent_id', $parent_id); $method = new \ReflectionMethod($this->sut, 'copyFormValuesToEntity'); $method->setAccessible(TRUE); $method->invokeArgs($this->sut, array($this->paymentStatus, $form, $form_state)); }
/** * Form submit handler to flush Mollom session and form information from cache. * * This is necessary as the entity forms will no longer automatically save * the data with the entity. * * @todo: Possible problems: * - This submit handler is invoked too late; the primary submit handler might * send out e-mails directly after saving the entity (e.g., * user_register_form_submit()), so mollom_mail_alter() is invoked before * Mollom session data has been saved. */ public static function submitForm($form, FormState $form_state) { // Some modules are implementing multi-step forms without separate form // submit handlers. In case we reach here and the form will be rebuilt, we // need to defer our submit handling until final submission. $is_rebuilding = $form_state->isRebuilding(); if ($is_rebuilding) { return; } $mollom = $form_state->getValue('mollom'); $form_object = $form_state->getFormObject(); // If an 'entity' and a 'post_id' mapping was provided via // hook_mollom_form_info(), try to automatically store Mollom session data. if (empty($mollom) || empty($mollom['entity']) || !$form_state->getFormObject() instanceof EntityFormInterface) { return; } /* @var $form_object \Drupal\Core\Entity\EntityFormInterface */ $entity_id = $form_object->getEntity()->id(); $data = (object) $mollom; $data->id = $entity_id; $data->moderate = $mollom['require_moderation'] ? 1 : 0; $stored_data = ResponseDataStorage::save($data); $form_state->setValue(['mollom', 'data'], $stored_data); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $pass = $form_state->getValue('filter_pass') ? explode(',', $form_state->getValue('filter_pass')) : array(); $fail = $form_state->getValue('filter_fail') ? explode(',', $form_state->getValue('filter_fail')) : array(); if ($form_state->getValue('filter') == 'all') { $classes = array_merge($pass, $fail); } elseif ($form_state->getValue('filter') == 'pass') { $classes = $pass; } else { $classes = $fail; } if (!$classes) { $form_state->setRedirect('simpletest.test_form'); return; } $form_execute = array(); $form_state_execute = new FormState(); foreach ($classes as $class) { $form_state_execute->setValue(['tests', $class], $class); } // Submit the simpletest test form to rerun the tests. // Under normal circumstances, a form object's submitForm() should never be // called directly, FormBuilder::submitForm() should be called instead. // However, it calls $form_state->setProgrammed(), which disables the Batch API. $simpletest_test_form = SimpletestTestForm::create(\Drupal::getContainer()); $simpletest_test_form->buildForm($form_execute, $form_state_execute); $simpletest_test_form->submitForm($form_execute, $form_state_execute); if ($redirect = $form_state_execute->getRedirect()) { $form_state->setRedirectUrl($redirect); } }
/** * Tests the submitConfigurationForm() method. * * @covers ::submitConfigurationForm */ public function testSubmitConfigurationForm() { $display_variant = $this->setUpDisplayVariant(); $this->assertSame('', $display_variant->label()); $form = array(); $label = $this->randomMachineName(); $form_state = new FormState(); $form_state->setValue('label', $label); $display_variant->submitConfigurationForm($form, $form_state); $this->assertSame($label, $display_variant->label()); }
/** * @covers ::addCleanValueKey */ public function testAddCleanValueKey() { $form_state = new FormState(); $form_state->setValue('value_to_clean', 'rainbow_sprinkles'); $form_state->addCleanValueKey('value_to_clean'); $this->assertSame($form_state->getCleanValueKeys(), ['form_id', 'form_token', 'form_build_id', 'op', 'value_to_clean']); return $form_state; }