You are here

public function UuidReferenceTest::testField in UUID reference field 7

Tests the uuidreference field.

All assertions are in this test run, to speed things up.

File

./uuidreference.test, line 115
Test classes for uuidreference.

Class

UuidReferenceTest
Tests the uuidreference field.

Code

public function testField() {

  // Test the uuidreference_field_is_empty() function.
  $this
    ->assertTrue(uuidreference_field_is_empty(array(
    'target_uuid' => '',
  ), array()));
  $this
    ->assertTrue(uuidreference_field_is_empty(array(
    'target_uuid' => NULL,
  ), array()));
  $this
    ->assertTrue(uuidreference_field_is_empty(array(
    'target_uuid' => NULL,
  ), array()));

  // Test an invalid uuid.
  $this
    ->assertTrue(uuidreference_field_is_empty(array(
    'target_uuid' => 'a340-4b3c-a363-865397307630',
  ), array()));

  // Valid uuid, this should return false - i.e. field is not empty.
  $this
    ->assertFalse(uuidreference_field_is_empty(array(
    'target_uuid' => '61dd3b36-a340-4b3c-a363-865397307630',
  ), array()));

  // Test the uuidreference_autocomplete() function.
  $user = $this->users[0];
  $expected = array(
    "{$user->name} [{$user->uuid}]" => $user->name,
  );
  $json = $this
    ->drupalGetAJAX('uuidreference/autocomplete/node/' . $this->contentType->type . '/uuidreference_test/' . $user->name);
  $this
    ->assertIdentical($json, $expected);

  // Test the formatter on a node_view.
  $this
    ->drupalGet("node/{$this->node->nid}");
  $field_items = field_get_items('node', $this->node, 'uuidreference_test');

  // Check the field label is present.
  $this
    ->assertText($this->fieldInstance['label']);

  // Check both user names are present from the formatter.
  foreach ($this->users as $delta => $user) {
    $this
      ->assertIdentical($field_items[$delta]['target_uuid'], $user->uuid);
    $this
      ->assertIdentical($field_items[$delta]['target_type'], 'user');
    $this
      ->assertText($user->name);
  }

  // Save the field formatter options to use a link.
  $instance = field_info_instance('node', 'uuidreference_test', $this->contentType->type);
  $instance['display']['default']['settings']['link'] = TRUE;
  field_update_instance($instance);
  $this
    ->drupalGet("node/{$this->node->nid}");

  // Check both user links are present from the formatter.
  foreach ($this->users as $user) {
    $this
      ->assertLinkByHref("user/{$user->uid}");
  }
}