public function RestfulReferenceTestCase::testEntityReference in RESTful 7.2
Same name and namespace in other branches
- 7 tests/RestfulReferenceTestCase.test \RestfulReferenceTestCase::testEntityReference()
Test field reference.
File
- tests/
RestfulReferenceTestCase.test, line 76 - Contains RestfulReferenceTestCase.
Class
- RestfulReferenceTestCase
- Class RestfulReferenceTestCase.
Code
public function testEntityReference() {
$resource_manager = restful()
->getResourceManager();
restful_test_add_fields();
foreach (array(
'entity_reference_single',
'entity_reference_multiple',
) as $field_name) {
$field = field_info_field($field_name);
$field['settings']['target_type'] = 'node';
field_update_field($field);
}
$resource_manager
->clearPluginCache('main:1.6');
$resource_manager
->clearPluginCache('test_articles:1.1');
$handler = $resource_manager
->getPlugin('main:1.6');
$node_handler = $resource_manager
->getPlugin('test_articles:1.1');
$user1 = $this
->drupalCreateUser();
$settings = array(
'type' => 'article',
'uid' => $user1->uid,
);
$node1 = $this
->drupalCreateNode($settings);
$node2 = $this
->drupalCreateNode($settings);
$user1 = $this
->drupalCreateUser();
$entity1 = entity_create('entity_test', array(
'name' => 'main',
'uid' => $user1->uid,
));
$entity1
->save();
$wrapper = entity_metadata_wrapper('entity_test', $entity1);
$wrapper->entity_reference_single
->set($node1);
$wrapper->entity_reference_multiple
->set(array(
$node1,
$node2,
));
$wrapper
->save();
$handler
->setRequest(Request::create('api/v1.6/main/' . $entity1->pid));
$handler
->setPath($entity1->pid);
$result = drupal_json_decode(restful()
->getFormatterManager()
->format($handler
->process(), 'json'));
$result = $result['data'];
$node_handler
->setRequest(Request::create('api/v1.1/test_articles' . $node1->nid));
$node_handler
->setPath($node1->nid);
$response1 = drupal_json_decode(restful()
->getFormatterManager()
->format($node_handler
->process(), 'json'));
$response1 = $response1['data'];
$node_handler
->setRequest(Request::create('api/v1.1/test_articles' . $node2->nid));
$node_handler
->setPath($node2->nid);
$response2 = drupal_json_decode(restful()
->getFormatterManager()
->format($node_handler
->process(), 'json'));
$response2 = $response2['data'];
$expected_result = $response1[0];
$this
->assertEqual($result[0]['entity_reference_single_resource'], $expected_result, 'Single reference with resource of another entity has correct response.');
$expected_result = array(
$response1[0],
$response2[0],
);
$this
->assertEqual($result[0]['entity_reference_multiple_resource'], $expected_result, 'Multiple reference with resource of another entity has correct response.');
// Test the "fullView" property on a referenced entity.
// We change the definition via the handler instead of creating another
// plugin.
$resource_field = $handler
->getFieldDefinitions()
->get('entity_reference_single_resource');
$resource_info = $resource_field
->getResource();
$resource_info['fullView'] = FALSE;
$resource_field
->setResource($resource_info);
$handler
->getFieldDefinitions()
->set('entity_reference_single_resource', $resource_field);
// Clear cache.
$handler
->setRequest(Request::create('api/v1.6/main/' . $entity1->pid));
$handler
->setPath($entity1->pid);
$result = drupal_json_decode(restful()
->getFormatterManager()
->format($handler
->process(), 'json'));
$result = $result['data'];
$this
->assertEqual($result[0]['entity_reference_single_resource'], $node1->nid, '"fullView" disabled is showing only the entity ID.');
}