function PollTestBase::assertPollChoiceOrder

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

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

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

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

See also

PollTestBase::_pollGenerateEdit()

1 call to PollTestBase::assertPollChoiceOrder()
PollTestBase::pollCreate in drupal/core/modules/poll/lib/Drupal/poll/Tests/PollTestBase.php
Creates a poll.

File

drupal/core/modules/poll/lib/Drupal/poll/Tests/PollTestBase.php, line 149
Definition of Drupal\poll\Tests\PollTestBase.

Class

PollTestBase
Defines a base class for testing the Poll module.

Namespace

Drupal\poll\Tests

Code

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

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

      // Directly assert the weight form element value for this choice.
      $this
        ->assertFieldByName('choice[chid:' . $id . '][weight]', $weight, format_string('Found choice @id with weight @weight.', array(
        '@id' => $id,
        '@weight' => $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, format_string('Found choice @label in preview.', array(
        '@label' => $next_label,
      )));
    }
  }
}