You are here

protected function QuoteTest::selectQuote in Ubercart 8.4

Simulates selection of a quote on the checkout page.

Parameters

int $n: The index of the quote to select.

File

shipping/uc_quote/src/Tests/QuoteTest.php, line 115

Class

QuoteTest
Tests shipping quote functionality.

Namespace

Drupal\uc_quote\Tests

Code

protected function selectQuote($n) {

  // Get the list of available quotes.
  $xpath = '//*[@name="panes[quotes][quotes][quote_option]"]';
  $elements = $this
    ->xpath($xpath);
  $vals = [];
  foreach ($elements as $element) {
    $vals[(string) $element['id']] = (string) $element['value'];
  }

  // Set the checked attribute of the chosen quote.
  $dom = new \DOMDocument();
  $dom
    ->loadHTML($this->content);
  $i = 0;
  $selected = '';
  foreach ($vals as $id => $value) {
    if ($i == $n) {
      $dom
        ->getElementById($id)
        ->setAttribute('checked', 'checked');
      $selected = $value;
    }
    else {
      $dom
        ->getElementById($id)
        ->removeAttribute('checked');
    }
    $i++;
  }
  $this
    ->setRawContent($dom
    ->saveHTML());

  // Post the selection via Ajax.
  $option = [
    'panes[quotes][quotes][quote_option]' => $selected,
  ];
  return $this
    ->drupalPostAjaxForm(NULL, [], $option);
}