You are here

public function RestfulJsonApiTestCase::testAlternativeIdField in RESTful 7.2

Test alternative ID field.

File

tests/RestfulJsonApiTestCase.test, line 596
Contains \RestfulJsonApiTestCase.

Class

RestfulJsonApiTestCase
Class RestfulJsonApiTestCase.

Code

public function testAlternativeIdField() {
  $entities = $this
    ->createEntities();
  $resource_manager = restful()
    ->getResourceManager();
  $format_manager = restful()
    ->getFormatterManager();
  $formatter = $format_manager
    ->negotiateFormatter(NULL, 'json_api');
  $resource_manager
    ->clearPluginCache('main:1.7');
  $handler = $resource_manager
    ->getPlugin('main:1.7');

  // Add files and taxonomy term references.
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'taxonomy_term')
    ->entityCondition('bundle', 'test_vocab')
    ->execute();
  $tids = array_keys($result['taxonomy_term']);
  $images = array();
  foreach ($this
    ->drupalGetTestFiles('image') as $file) {
    $file = file_save($file);
    $images[] = $file->fid;
  }
  $entities[2]->term_single[LANGUAGE_NONE][] = array(
    'tid' => $tids[0],
  );
  $entities[2]->term_multiple[LANGUAGE_NONE][] = array(
    'tid' => $tids[1],
  );
  $entities[2]->term_multiple[LANGUAGE_NONE][] = array(
    'tid' => $tids[2],
  );
  $entities[2]->file_single[LANGUAGE_NONE][] = array(
    'fid' => $images[0],
    'display' => TRUE,
  );
  $entities[2]->file_multiple[LANGUAGE_NONE][] = array(
    'fid' => $images[1],
    'display' => TRUE,
  );
  $entities[2]->file_multiple[LANGUAGE_NONE][] = array(
    'fid' => $images[2],
    'display' => TRUE,
  );
  entity_save('entity_test', $entities[2]);
  $handler
    ->setRequest(Request::create('api/main/v1.7/' . $entities[2]->uuid));
  $handler
    ->setPath($entities[2]->uuid);
  $formatter
    ->setResource($handler);
  $response = $formatter
    ->prepare($handler
    ->process());
  $result = $response['data']['attributes'];

  // Make sure that IDs are UUID.
  $this
    ->assertEqual(count(entity_uuid_load('file', array(
    $result['file_single'],
  ))), 1, 'UUID correctly loaded for: file_single');
  $this
    ->assertEqual(count(entity_uuid_load('file', $result['file_multiple'])), 2, 'UUID correctly loaded for: file_multiple');
  $this
    ->assertEqual(count(entity_uuid_load('taxonomy_term', array(
    $result['term_single'],
  ))), 1, 'UUID correctly loaded for: term_single');
  $this
    ->assertEqual(count(entity_uuid_load('taxonomy_term', $result['term_multiple'])), 2, 'UUID correctly loaded for: term_multiple');
  $this
    ->assertEqual(count(entity_uuid_load('entity_test', array(
    $result['entity_reference_single'],
  ))), 1, 'UUID correctly loaded for: entity_reference_single');
  $this
    ->assertEqual(count(entity_uuid_load('entity_test', $result['entity_reference_multiple'])), 2, 'UUID correctly loaded for: entity_reference_multiple');

  // Assert relationship.
  $relationships = $response['data']['relationships'];
  $this
    ->assertEqual($relationships['entity_reference_resource']['data']['id'], $entities[0]->uuid);
  $this
    ->assertEqual($relationships['entity_reference_resource']['data']['type'], 'main');

  // Assert that the self link contains the UUID.
  $this
    ->assertTrue(strpos($response['links']['self'], $entities[2]->uuid) !== FALSE, 'UUID found in self link.');

  // Test that if referencedIdProperty and idField don't match, embedding does
  // not happen.
  variable_set('restful_test_alternative_id_error', TRUE);
  $resource_manager
    ->clearPluginCache('main:1.7');
  $handler = $resource_manager
    ->getPlugin('main:1.7');
  $handler
    ->setRequest(Request::create('api/main/v1.7/' . $entities[2]->uuid, array(
    'include' => 'entity_reference_resource_error',
  )));
  $handler
    ->setPath($entities[2]->uuid);
  $format_manager
    ->setResource($handler);
  $response = $format_manager
    ->negotiateFormatter(NULL, 'json_api')
    ->prepare($handler
    ->process());
  $result = $response['data']['attributes'];

  // Make sure the entity_reference_resource_error is NULL.
  $this
    ->assertNull($result['entity_reference_resource_error'], 'Entity cannot be loaded with uuid when there is no idField.');
  variable_del('restful_test_alternative_id_error');

  // Clear caches to remove error field.
  $resource_manager
    ->clearPluginCache('main:1.7');
  $handler = $resource_manager
    ->getPlugin('main:1.7');

  // Filter the entities by the reference.
  $handler
    ->setRequest(Request::create('api/main/v1.7', array(
    'filter' => array(
      'entity_reference_single' => $entities[0]->uuid,
    ),
  )));
  $handler
    ->setPath('');
  $formatter
    ->setResource($handler);
  $response = $formatter
    ->prepare($handler
    ->process());

  // Assert that $entities[2] points to $entities[0]
  $this
    ->assertEqual($response['data'][0]['id'], $entities[2]->uuid);

  // Filter the entities by the reference.
  $handler
    ->setRequest(Request::create('api/main/v1.7', array(
    'filter' => array(
      'entity_reference_multiple' => $entities[1]->uuid,
    ),
  )));
  $handler
    ->setPath('');
  $formatter
    ->setResource($handler);
  $response = $formatter
    ->prepare($handler
    ->process());

  // Assert that $entities[2] points to $entities[0]
  $this
    ->assertEqual($response['data'][0]['id'], $entities[2]->uuid);
}