You are here

function FieldMultipleLimitTestCase::testFieldDisplay in Field multiple limit 7

Test that data added to the page shows up as expected.

File

./field_multiple_limit.test, line 99
Tests for Field Multiple Limit, based on examples from field.test

Class

FieldMultipleLimitTestCase
Test the field formatter settings work.

Code

function testFieldDisplay() {
  $this
    ->addTestDataToPage();

  // We now see a page containing the added fields.
  // Check that the fields are displayed in the normal order
  $pattern = '|Field data #0[\\s\\S]*Field data #1[\\s\\S]*Field data #2[\\s\\S]*Field data #3[\\s\\S]*Field data #4|';
  $this
    ->assertPattern($pattern, 'All fields are displayed in delta order on the page.');

  // Change the formatter setting to random order.
  $instance = field_read_instance($this->entity_type, $this->field_name, $this->bundle);
  $instance['display']['default']['settings']['field_multiple_limit_order'] = 2;
  field_update_instance($instance);

  // Reloading the page should show items in random order. In the extremely
  // unlikely case that the same random order comes up 10 times consecutively
  // this will be a false positive.
  $match = 0;
  for ($i = 1; $i <= 10; $i++) {
    $this
      ->drupalGet($this->url);
    if (preg_match($pattern, $this
      ->drupalGetContent())) {
      $match++;
    }
  }
  $this
    ->assertFalse($match == 10, 'All fields are displayed in random order on the page.');

  // Change the formatter setting to limit to just three items in original order.
  $instance = field_read_instance($this->entity_type, $this->field_name, $this->bundle);
  $instance['display']['default']['settings']['field_multiple_limit'] = 3;
  $instance['display']['default']['settings']['field_multiple_limit_order'] = 0;
  field_update_instance($instance);

  // Reloading the page should now NOT display #4
  $this
    ->drupalGet($this->url);
  $this
    ->assertNoPattern('|Field data #4|', 'When limiting fileds, some are now hidden.');

  // Now reverse the order
  $instance['display']['default']['settings']['field_multiple_limit_order'] = 1;
  field_update_instance($instance);

  // Reloading the page should now display #4, #3 #2 only
  $this
    ->drupalGet($this->url);
  $this
    ->assertPattern('|Field data #4[\\s\\S]*Field data #3[\\s\\S]*Field data #2|', 'Reversing fields worked');
  $this
    ->assertNoPattern('|Field data #1|', 'Reversing fields and limiting works');
}