You are here

function PollTestBase::assertPollChoiceOrder in Poll 8

Asserts correct poll choice order in the node form after submission.

Verifies both the order in the DOM and in the 'weight' form elements.

Parameters

array $choices: An array containing poll choices, as generated by PollTestBase::generateChoices().

int $index: (optional) The amount/number of already submitted poll choices. Defaults to 0.

bool $preview: (optional) Whether to also check the poll preview. Defaults to FALSE.

See also

PollTestBase::pollGenerateEdit()

File

tests/src/Functional/PollTestBase.php, line 203

Class

PollTestBase
Defines a base class for testing the Poll module.

Namespace

Drupal\Tests\poll\Functional

Code

function assertPollChoiceOrder(array $choices, $index = 0, $preview = FALSE) {
  $expected = array();
  $weight = 0;
  foreach ($choices as $id => $label) {
    if ($id < $index) {

      // Directly assert the weight form element value for this choice.
      $this
        ->assertFieldByName('choice[' . $id . '][_weight]', $weight);

      // The expected weight of each choice is higher than the previous one.
      $weight++;

      // Append to our (to be reversed) stack of labels.
      $expected[$weight] = $label;
    }
  }
  ksort($expected);

  // Verify DOM order of poll choices (i.e., #weight of form elements).
  $elements = $this
    ->xpath('//input[starts-with(@name, :prefix) and contains(@name, :suffix)]', array(
    ':prefix' => 'choice[chid:',
    ':suffix' => '][chtext]',
  ));
  $expected_order = $expected;
  foreach ($elements as $element) {
    $next_label = array_shift($expected_order);
    $this
      ->assertEqual((string) $element['value'], $next_label);
  }

  // If requested, also verify DOM order in preview.
  if ($preview) {
    $elements = $this
      ->xpath('//div[contains(@class, :teaser)]/descendant::div[@class=:text]', array(
      ':teaser' => 'node-teaser',
      ':text' => 'text',
    ));
    $expected_order = $expected;
    foreach ($elements as $element) {
      $next_label = array_shift($expected_order);
      $this
        ->assertEqual((string) $element, $next_label, "Found choice {$next_label} in preview.");
    }
  }
}