/** * @param integer $value * @return $this */ protected function setStatus($value) { $status = $this->form->get('field_issue_status[und]'); $status->setValue($value); $this->form->set($status); return $this; }
/** * Go to the previous form page * * @param \Symfony\Component\DomCrawler\Form $form * @return \TYPO3\Flow\Http\Response */ protected function gotoPreviousFormPage(\Symfony\Component\DomCrawler\Form $form) { $previousButton = $this->browser->getCrawler()->filterXPath('//nav[@class="form-navigation"]/*/*[contains(@class, "previous")]/button'); $previousButton->rewind(); $form->set(new InputFormField($previousButton->current())); return $this->browser->submit($form); }
public function testGetFormNode() { $dom = new \DOMDocument(); $dom->loadHTML('<html><form><input type="submit" /></form></html>'); $form = new Form($dom->getElementsByTagName('input')->item(0)); $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form'); }
protected function makeRequestUsingForm(Form $form) { $files = []; $plainFiles = $form->getFiles(); foreach ($plainFiles as $key => $file) { $files[$key] = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error'], true); } return $this->makeRequest($form->getMethod(), $form->getUri(), $form->getValues(), [], $files); }
public function isValid(Form $form) { if ($form->has('token')) { $values = $form->getValues(); $values['token'] = 'modified by csrf scanner'; $form->setValues($values); $this->client->submit($form); $status = $this->client->getResponse()->getStatus(); if (403 == $status) { return true; } $this->message = "403 response expected, but got a {$status}"; } else { $this->message = "No 'token' input field found"; } return false; }
/** * @param Form $form * * @return ModuleList */ public static function createFromForm(Form $form) { $node = new Crawler($form->getFormNode()); // Example: <input type="checkbox" id="edit-modules-other-oxygen-enable" name="modules[Other][oxygen][enable]" value="1" class="form-checkbox" /> $inputs = $node->filter('input[type="checkbox"][id^="edit-modules-"]'); $modules = []; foreach ($inputs as $input) { /** @var \DOMElement $input */ if (!$input->hasAttribute('name')) { continue; } $enabled = $input->hasAttribute('checked'); $match = preg_match('{^modules\\[([^\\]]+)\\]\\[([^\\]]+)\\]\\[enable\\]$}', $input->getAttribute('name'), $matches); if (!$match) { continue; } list(, $package, $slug) = $matches; $modules[] = new ModuleListItem($package, $slug, $enabled); } return new self($modules); }
/** * Merges second form values into first one. * * @param Form $to merging target * @param Form $from merging source */ private function mergeForms(Form $to, Form $from) { foreach ($from->all() as $name => $field) { $fieldReflection = new \ReflectionObject($field); $nodeReflection = $fieldReflection->getProperty('node'); $valueReflection = $fieldReflection->getProperty('value'); $nodeReflection->setAccessible(true); $valueReflection->setAccessible(true); $isIgnoredField = $field instanceof InputFormField && in_array($nodeReflection->getValue($field)->getAttribute('type'), array('submit', 'button', 'image'), true); if (!$isIgnoredField) { $valueReflection->setValue($to[$name], $valueReflection->getValue($field)); } } }
public function testSubmitWithoutAFormButton() { $dom = new \DOMDocument(); $dom->loadHTML(' <html> <form> <input type="foo" /> </form> </html> '); $nodes = $dom->getElementsByTagName('form'); $form = new Form($nodes->item(0), 'http://example.com'); $this->assertSame($nodes->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form'); }
public function getSonataAdminFormName(Form $form) { parse_str(parse_url($form->getUri(), PHP_URL_QUERY), $query); return $query['uniqid']; }
/** * Edits the data of a form * * @param Client $client * @param Form $form * @param array $data */ protected function submitEditForm(Client $client, Form $form, array $data = array()) { $originalData = array(); foreach ($form->all() as $fieldName => $formField) { $originalData[$fieldName] = $formField->getValue(); } $data = array_merge($originalData, $data); $this->submitForm($client, $form, $data); }
/** * Fill form for address tests (update test) * * @param Form $form * @return Form */ protected function fillFormForUpdateTest(Form $form) { $formNode = $form->getNode(); $formNode->setAttribute('action', $formNode->getAttribute('action') . '?_widgetContainer=dialog'); $form['orob2b_account_typed_address[types]'] = [AddressType::TYPE_BILLING, AddressType::TYPE_SHIPPING]; $form['orob2b_account_typed_address[defaults][default]'] = [false, AddressType::TYPE_SHIPPING]; $doc = new \DOMDocument("1.0"); $doc->loadHTML('<select name="orob2b_account_typed_address[country]" id="orob2b_account_typed_address_country" ' . 'tabindex="-1" class="select2-offscreen"> ' . '<option value="" selected="selected"></option> ' . '<option value="ZW">Zimbabwe</option> </select>'); $field = new ChoiceFormField($doc->getElementsByTagName('select')->item(0)); $form->set($field); $form['orob2b_account_typed_address[country]'] = 'ZW'; $doc->loadHTML('<select name="orob2b_account_typed_address[region]" id="orob2b_account_typed_address_region" ' . 'tabindex="-1" class="select2-offscreen"> ' . '<option value="" selected="selected"></option> ' . '<option value="ZW-MA">Manicaland</option> </select>'); $field = new ChoiceFormField($doc->getElementsByTagName('select')->item(0)); $form->set($field); $form['orob2b_account_typed_address[region]'] = 'ZW-MA'; return $form; }
/** * @param Form $form * * @return Crawler */ protected function submitForm(Form $form) { $action = $form->getFormNode()->getAttribute('data-action'); $form->getFormNode()->setAttribute('action', $action); return $this->client->submit($form); }
/** * Convert the given uploads to UploadedFile instances. * * @param \Symfony\Component\DomCrawler\Form $form * @param array $uploads * @return array */ protected function convertUploadsForTesting(Form $form, array $uploads) { $files = $form->getFiles(); $names = array_keys($files); $files = array_map(function (array $file, $name) use($uploads) { return isset($uploads[$name]) ? $this->getUploadedFileForTesting($file, $uploads, $name) : $file; }, $files, $names); $uploads = array_combine($names, $files); foreach ($uploads as $key => $file) { if (preg_match('/.*?(?:\\[.*?\\])+/', $key)) { $this->prepareArrayBasedFileInput($uploads, $key, $file); } } return $uploads; }
/** * Returns an array of name => value pairs for the passed form. * * For form fields containing a name ending in [], an array is * created out of all field values with the given name. * * @param \Symfony\Component\DomCrawler\Form the form * @return array an array of name => value pairs */ protected function getFormValuesFor(Form $form) { $values = []; $fields = $form->all(); foreach ($fields as $field) { if ($field->isDisabled() || !$field->hasValue() || $field instanceof FileFormField) { continue; } $fieldName = $this->getSubmissionFormFieldName($field->getName()); if (substr($field->getName(), -2) === '[]') { if (!isset($values[$fieldName])) { $values[$fieldName] = []; } $values[$fieldName][] = $field->getValue(); } else { $values[$fieldName] = $field->getValue(); } } return $values; }
/** * Returns an array of name => value pairs for the passed form. * * The function calls getPhpValues on the passed form object, then * resets numeric array indexes for array field names without array * keys specified (i.e. 'fieldname[]' but not 'fieldname[keyname]') * as expected by setCheckboxBoolValues. * * @param \Symfony\Component\DomCrawler\Form the form * @return array an array of name => value pairs */ protected function getFormValuesFor(Form $form) { $values = $form->getPhpValues(); $fields = $form->all(); foreach ($fields as $field) { $name = $this->getSubmissionFormFieldName($field->getName()); if (!empty($values[$name]) && substr($field->getName(), -2) === '[]') { $values[$name] = array_values($values[$name]); } } return $values; }
/** * * @param \Symfony\Component\DomCrawler\Form $form * @param string $method The Request Method */ public function submitForm(Form $form, $method = "POST") { $this->getClient()->request($method, $form->getUri(), $form->getPhpValues()); }
/** * @param Form $form * @param Quote $quote * @param int $customQuantity * @return array */ protected function setFormData(Form $form, Quote $quote, $customQuantity) { $selectedOffers = []; foreach ($quote->getQuoteProducts() as $quoteProduct) { /** @var \OroB2B\Bundle\SaleBundle\Entity\QuoteProductOffer $quoteProductOffer */ $quoteProductOffer = $quoteProduct->getQuoteProductOffers()->first(); foreach ($form->get('orob2b_sale_quote_to_order') as $key => $row) { if (!is_array($row)) { continue; } /** @var ChoiceFormField $offer */ $offer = $form->get('offer_choice_' . $key); if ((int) $offer->getValue() !== (int) $quoteProductOffer->getId()) { continue; } if ($quoteProductOffer->isAllowIncrements()) { $form['orob2b_sale_quote_to_order[' . $key . '][quantity]'] = $customQuantity; } else { $form['orob2b_sale_quote_to_order[' . $key . '][quantity]'] = $quoteProductOffer->getQuantity(); } $selectedOffers[] = $quoteProductOffer; } } return $selectedOffers; }
/** * @param Form $form * @param $code * @param $elementId * @return Crawler */ private function addMenuItemForm(Form $form, $code, $elementId) { $ajaxClient = static::createClient(); $ajaxCrawler = $ajaxClient->request('POST', '/admin/core/append-form-field-element', array_merge($form->getPhpValues(), array('code' => $code, 'elementId' => 'page_' . $elementId, 'uniqid' => 'page'))); foreach ($ajaxCrawler->filter('input') as $node) { if ($node->attributes->getNamedItem('type')) { if ($node->attributes->getNamedItem('type')->nodeValue == 'checkbox' || $node->attributes->getNamedItem('type')->nodeValue == 'radio') { $form->set(new ChoiceFormField($node)); continue; } if ($node->attributes->getNamedItem('type') == 'file') { $form->set(new FileFormField($node)); continue; } } $form->set(new InputFormField($node)); } foreach ($ajaxCrawler->filter('select') as $node) { $form->set(new ChoiceFormField($node)); } foreach ($ajaxCrawler->filter('textarea') as $node) { $form->set(new TextareaFormField($node)); } return $ajaxCrawler; }
/** * Make a request to a URL using form parameters. * * @param Form $form * @return static */ protected function makeRequestUsingForm(Form $form) { return $this->makeRequest($form->getMethod(), $form->getUri(), $form->getValues(), [], $form->getFiles()); }
/** * Convert the given uploads to UploadedFile instances. * * @param \Symfony\Component\DomCrawler\Form $form * @param array $uploads * @return array */ protected function convertUploadsForTesting(Form $form, array $uploads) { $files = $form->getFiles(); $names = array_keys($files); $files = array_map(function (array $file, $name) use($uploads) { return isset($uploads[$name]) ? $this->getUploadedFileForTesting($file, $uploads, $name) : $file; }, $files, $names); return array_combine($names, $files); }
/** * Extract the parameters from the given form. * * @param \Symfony\Component\DomCrawler\Form $form * @return array */ protected function extractParametersFromForm(Form $form) { parse_str(http_build_query($form->getValues()), $parameters); return $parameters; }
/** * Submit a form * * @param \Symfony\Component\DomCrawler\Form $form * @return \TYPO3\Flow\Http\Response * @api */ public function submit(Form $form) { return $this->request($form->getUri(), $form->getMethod(), $form->getPhpValues(), $form->getPhpFiles()); }
/** * Returns a Form object for the first node in the list. * * @param array $values An array of values for the form fields * @param string $method The method for the form * * @return Form A Form instance * * @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement */ public function form(array $values = null, $method = null) { if (!$this->nodes) { throw new \InvalidArgumentException('The current node list is empty.'); } $node = $this->getNode(0); if (!$node instanceof \DOMElement) { throw new \InvalidArgumentException(sprintf('The selected node should be instance of DOMElement, got "%s".', get_class($node))); } $form = new Form($node, $this->uri, $method, $this->baseHref); if (null !== $values) { $form->setValues($values); } return $form; }
/** * Converts form files to UploadedFile instances. * * @param Form $form * @return array */ protected function convertFormFiles(Form $form) { $files = $form->getFiles(); $names = array_keys($files); $files = array_map(function ($file, $name) { if (isset($this->files[$name])) { $absolutePath = $this->files[$name]; $file = new UploadedFile($file['tmp_name'], basename($absolutePath), $file['type'], $file['size'], $file['error'], true); } return $file; }, $files, $names); return array_combine($names, $files); }
/** * Submits a form. * * @param Form $form A Form instance * @param array $values An array of form field values * * @api */ public function submit(Form $form, array $values = array()) { $form->setValues($values); return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles()); }
public function testBase() { $dom = new \DOMDocument(); $dom->loadHTML('<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>'); $nodes = $dom->getElementsByTagName('input'); $form = new Form($nodes->item($nodes->length - 1), null, 'http://www.bar.com/foobar/', '/', 'http://www.foo.com/'); $this->assertEquals('http://www.foo.com/foo.php', $form->getUri()); }
public function testgetPhpValuesWithEmptyTextarea() { $dom = new \DOMDocument(); $dom->loadHTML(' <html> <form> <textarea name="example"></textarea> </form> </html> '); $nodes = $dom->getElementsByTagName('form'); $form = new Form($nodes->item(0), 'http://example.com'); $this->assertEquals($form->getPhpValues(), array('example' => '')); }
public function isValid(Form $form) { return $form->has('token'); }
/** * Returns a Form object for the first node in the list. * * @param array $values An array of values for the form fields * @param string $method The method for the form * * @return Form A Form instance * * @throws \InvalidArgumentException If the current node list is empty * * @api */ public function form(array $values = null, $method = null) { if (!count($this)) { throw new \InvalidArgumentException('The current node list is empty.'); } $form = new Form($this->getNode(0), $this->uri, $method); if (null !== $values) { $form->setValues($values); } return $form; }
/** * @param Form $form * @param string $package * @param string $slug * @param Session $session * * @return Promise * * @rejects RequestException */ public function disableModuleAsync(Form $form, $package, $slug, Session $session) { $formData = $form->getPhpValues(); if (empty($formData['modules'][$package][$slug]['enable'])) { // The module is already disabled. return new FulfilledPromise(null); } unset($formData['modules'][$package][$slug]['enable']); return $this->client->requestAsync($form->getMethod(), $form->getUri(), [RequestOptions::COOKIES => $session->getCookieJar(), RequestOptions::AUTH => $session->getAuthData(), RequestOptions::FORM_PARAMS => $formData, RequestOptions::HEADERS => ['referer' => $form->getUri()], RequestOptions::ALLOW_REDIRECTS => false])->then(function () { // Resolve to null. }); }