public function RestfulJsonApiTestCase::testReading in RESTful 7.2
Test requesting resources.
File
- tests/
RestfulJsonApiTestCase.test, line 112 - Contains \RestfulJsonApiTestCase.
Class
- RestfulJsonApiTestCase
- Class RestfulJsonApiTestCase.
Code
public function testReading() {
/* @var \EntityDrupalWrapper $wrapper */
$wrapper = entity_metadata_wrapper('entity_test', $this->entities[2]);
$this->handler
->setRequest(Request::create('api/v1.1/main/' . $wrapper
->getIdentifier()));
$this->handler
->setPath($wrapper
->getIdentifier());
$resource_field_collections = $this->handler
->process();
$result = drupal_json_decode($this->formatter
->format($resource_field_collections));
// Assert the basic properties of the resource.
$this
->assertEqual('main', $result['data']['type']);
$this
->assertEqual($wrapper
->getIdentifier(), $result['data']['id']);
// 1. Assert the "attributes" key.
// Remove the NULL keys since we're not adding them to the $expected array.
$attributes = array_filter($result['data']['attributes']);
$expected = array(
// The following fields don't have a resource in their definition,
// therefore they are treated as regular fields and not relationships.
'entity_reference_multiple' => array(
$this->entities[0]->pid,
$this->entities[1]->pid,
),
'entity_reference_single' => $this->entities[0]->pid,
'id' => $wrapper
->getIdentifier(),
'label' => $wrapper
->label(),
'self' => $this->handler
->versionedUrl($wrapper
->getIdentifier()),
// Create it empty and fill it later.
'text_multiple' => array(),
'text_single' => $wrapper->text_single
->value(),
);
foreach ($wrapper->text_multiple as $text_multiple_wrapper) {
/* @var \EntityStructureWrapper $text_multiple_wrapper */
$expected['text_multiple'][] = $text_multiple_wrapper
->value();
}
$this
->assertEqual(array_filter($expected), array_filter($attributes));
$this
->assertEqual($this->handler
->versionedUrl($wrapper
->getIdentifier(), array(
'query' => array(
'page' => array(
'number' => 1,
),
),
)), $result['links']['self']);
// 2. Assert the relationships.
$relationships = $result['data']['relationships'];
// Make sure that the entity_reference_*_resource are included as
// relationships.
$this
->assertEqual($relationships['entity_reference_multiple_resource']['links'], array(
'self' => $this->handler
->versionedUrl($wrapper
->getIdentifier() . '/relationships/entity_reference_multiple_resource'),
'related' => $this->handler
->versionedUrl('', array(
'query' => array(
'filter' => array(
'entity_reference_multiple_resource' => $wrapper->entity_reference_multiple[0]
->getIdentifier(),
),
),
)),
));
$this
->assertEqual($relationships['entity_reference_single_resource']['data']['type'], 'main');
$this
->assertEqual($relationships['entity_reference_single_resource']['data']['id'], $wrapper->entity_reference_single
->getIdentifier());
$this
->assertEqual($relationships['entity_reference_multiple_resource']['data'][0]['type'], 'main');
$this
->assertEqual($relationships['entity_reference_multiple_resource']['data'][0]['id'], $wrapper->entity_reference_multiple[0]
->getIdentifier());
$this
->assertEqual($relationships['entity_reference_multiple_resource']['data'][1]['type'], 'main');
$this
->assertEqual($relationships['entity_reference_multiple_resource']['data'][1]['id'], $wrapper->entity_reference_multiple[1]
->getIdentifier());
// Make sure that using fields + includes lists all the fields in the
// embedded entity if there is no subfield added.
$this->handler
->setRequest(Request::create('api/v1.1/main/' . $wrapper
->getIdentifier(), array(
'include' => 'entity_reference_multiple_resource,entity_reference_single_resource',
'fields' => 'entity_reference_multiple_resource,entity_reference_single_resource',
)));
$this->handler
->setPath($wrapper
->getIdentifier());
$this->formatter
->setResource($this->handler);
$resource_field_collections = $this->handler
->process();
$result = drupal_json_decode($this->formatter
->format($resource_field_collections));
$this
->assertEqual(count($result['included'][0]['attributes']), 16);
// Now make sure that including fields only lists those fields.
$this->handler
->setRequest(Request::create('api/v1.1/main/' . $wrapper
->getIdentifier(), array(
'include' => 'entity_reference_single_resource',
'fields' => 'entity_reference_single_resource.label',
)));
$this->handler
->setPath($wrapper
->getIdentifier());
$this->formatter
->setResource($this->handler);
$resource_field_collections = $this->handler
->process();
$result = drupal_json_decode($this->formatter
->format($resource_field_collections));
$this
->assertEqual(count($result['included'][0]['attributes']), 1);
// Make a request with the include query string.
$this->handler
->setRequest(Request::create('api/v1.1/main/' . $wrapper
->getIdentifier(), array(
'include' => 'entity_reference_multiple_resource,entity_reference_single_resource',
)));
$this->handler
->setPath($wrapper
->getIdentifier());
$this->formatter
->setResource($this->handler);
$resource_field_collections = $this->handler
->process();
$result = drupal_json_decode($this->formatter
->format($resource_field_collections));
$included = $result['included'];
// Make sure there are no repeated includes.
$this
->assertEqual(2, count($included));
$expected_includes = array(
array(
'attributes' => array(
'entity_reference_single' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
'id' => $wrapper->entity_reference_multiple[0]
->getIdentifier(),
'label' => $wrapper->entity_reference_multiple[0]
->label(),
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[0]
->getIdentifier()),
),
'id' => $wrapper->entity_reference_multiple[0]
->getIdentifier(),
'links' => array(
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[0]
->getIdentifier()),
),
'type' => 'main',
'relationships' => array(
'entity_reference_single_resource' => array(
'data' => array(
'id' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
'type' => 'main',
),
'links' => array(
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[0]
->getIdentifier() . '/relationships/entity_reference_single_resource'),
'related' => $this->handler
->versionedUrl('', array(
'query' => array(
'filter' => array(
'entity_reference_single_resource' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
),
),
)),
),
),
),
),
array(
'attributes' => array(
'id' => $wrapper->entity_reference_multiple[1]
->getIdentifier(),
'label' => $wrapper->entity_reference_multiple[1]
->label(),
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[1]
->getIdentifier()),
),
'id' => $wrapper->entity_reference_multiple[1]
->getIdentifier(),
'links' => array(
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[1]
->getIdentifier()),
),
'type' => 'main',
),
);
// Remove the empty fields from the actual response for easier comparison.
$included[0]['attributes'] = array_filter($included[0]['attributes']);
$included[1]['attributes'] = array_filter($included[1]['attributes']);
$this
->assertEqual($expected_includes, $included);
// 3. Assert the nested relationships.
$relationships = $result['data']['relationships'];
// Make sure that the entity_reference_*_resource are included as
// relationships.
$this
->assertEqual($relationships['entity_reference_single_resource']['data']['type'], 'main');
$this
->assertEqual($relationships['entity_reference_single_resource']['data']['id'], $wrapper->entity_reference_single
->getIdentifier());
$this
->assertEqual($relationships['entity_reference_multiple_resource']['data'][0]['type'], 'main');
$this
->assertEqual($relationships['entity_reference_multiple_resource']['data'][0]['id'], $wrapper->entity_reference_multiple[0]
->getIdentifier());
$this
->assertEqual($relationships['entity_reference_multiple_resource']['data'][1]['type'], 'main');
$this
->assertEqual($relationships['entity_reference_multiple_resource']['data'][1]['id'], $wrapper->entity_reference_multiple[1]
->getIdentifier());
// Make a request with the include query string.
$this->handler
->setRequest(Request::create('api/v1.1/main/' . $wrapper
->getIdentifier(), array(
'include' => 'entity_reference_single_resource.entity_reference_single_resource',
)));
$this->handler
->setPath($wrapper
->getIdentifier());
$this->formatter
->setResource($this->handler);
$resource_field_collections = $this->handler
->process();
$result = drupal_json_decode($this->formatter
->format($resource_field_collections));
$included = $result['included'];
// Make sure there are no repeated includes.
$this
->assertEqual(2, count($included));
$expected_includes = array(
array(
'attributes' => array(
'entity_reference_single' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
'id' => $wrapper->entity_reference_multiple[0]
->getIdentifier(),
'label' => $wrapper->entity_reference_multiple[0]
->label(),
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[0]
->getIdentifier()),
),
'id' => $wrapper->entity_reference_multiple[0]
->getIdentifier(),
'links' => array(
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[0]
->getIdentifier()),
),
'type' => 'main',
'relationships' => array(
'entity_reference_single_resource' => array(
'data' => array(
'id' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
'type' => 'main',
),
'links' => array(
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[0]
->getIdentifier() . '/relationships/entity_reference_single_resource'),
'related' => $this->handler
->versionedUrl('', array(
'query' => array(
'filter' => array(
'entity_reference_single_resource' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
),
),
)),
),
),
),
),
array(
'attributes' => array(
'id' => $wrapper->entity_reference_multiple[1]
->getIdentifier(),
'label' => $wrapper->entity_reference_multiple[1]
->label(),
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[1]
->getIdentifier()),
),
'id' => $wrapper->entity_reference_multiple[1]
->getIdentifier(),
'links' => array(
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_multiple[1]
->getIdentifier()),
),
'type' => 'main',
),
);
// Remove the empty fields from the actual response for easier comparison.
$included[0]['attributes'] = array_filter($included[0]['attributes']);
$included[1]['attributes'] = array_filter($included[1]['attributes']);
sort($expected_includes);
sort($included);
$this
->assertEqual($expected_includes, $included);
// 4. Assert the nested sparse fieldsets.
// Make a request with the include query string.
$this->handler
->setRequest(Request::create('api/v1.1/main/' . $wrapper
->getIdentifier(), array(
'include' => 'entity_reference_single_resource.entity_reference_single_resource',
'fields' => implode(',', array(
// Request a field in the first include and another one in the second.
'entity_reference_single_resource.text_single',
'entity_reference_single_resource.entity_reference_single_resource.text_multiple',
)),
)));
$this->handler
->setPath($wrapper
->getIdentifier());
$this->formatter
->setResource($this->handler);
$result = $this->formatter
->prepare($this->handler
->process());
$this
->assertEqual(2, count($result['included']));
// Make sure there is only one field in the nested includes.
$this
->assertTrue(empty($result['data']['attributes']));
$this
->assertFalse(empty($result['data']['relationships']));
$this
->assertEqual(array(
'text_single',
), array_keys($result['included'][0]['attributes']));
$this
->assertFalse(empty($result['included'][0]['relationships']));
$this
->assertEqual(array(
'text_multiple',
), array_keys($result['included'][1]['attributes']));
$this
->assertTrue(empty($result['included'][1]['relationships']));
// Make a request with the include query string.
$this->handler
->setRequest(Request::create('api/v1.1/main/' . $wrapper
->getIdentifier(), array(
'include' => 'entity_reference_single_resource.entity_reference_single_resource',
'fields' => implode(',', array(
// Request a field in the first include and another one in the second.
'entity_reference_single_resource.entity_reference_single_resource.text_multiple',
)),
)));
$this->handler
->setPath($wrapper
->getIdentifier());
$this->formatter
->setResource($this->handler);
$result = $this->formatter
->prepare($this->handler
->process());
$this
->assertEqual(2, count($result['included']));
// Make sure there is only one field in the nested includes.
$this
->assertTrue(empty($result['data']['attributes']));
$this
->assertFalse(empty($result['data']['relationships']));
$this
->assertTrue(empty($result['included'][0]['attributes']));
$this
->assertFalse(empty($result['included'][0]['relationships']));
$this
->assertTrue(empty($result['included'][1]['relationships']));
$this
->assertEqual(array(
'text_multiple',
), array_keys($result['included'][1]['attributes']));
// 6. Assert the attributes for a list.
// Make a list request and check the attributes.
$this->handler
->setRequest(Request::create(''));
$this->handler
->setPath('');
$this->formatter
->setResource($this->handler);
$resource_field_collections = $this->handler
->process();
$result = drupal_json_decode($this->formatter
->format($resource_field_collections));
$this
->assertTrue(\Drupal\restful\Plugin\resource\Field\ResourceFieldBase::isArrayNumeric($result['data']));
$this
->assertTrue(count($result['data']), count($this->entities));
foreach ($result['data'] as $delta => $row) {
// Assert the basic properties of the resource.
$this
->assertEqual('main', $row['type']);
$this
->assertEqual($this->entities[$delta]->pid, $row['id']);
}
$expected = array_filter($expected);
$attributes = array_filter($result['data'][2]['attributes']);
$this
->assertEqual($expected, $attributes);
// 5. Assert the relationships for a list.
$this->handler
->setRequest(Request::create('', array(
'include' => 'entity_reference_single_resource',
)));
$this->handler
->setPath('');
$this->formatter
->setResource($this->handler);
$resource_field_collections = $this->handler
->process();
$result = drupal_json_decode($this->formatter
->format($resource_field_collections));
$included = $result['included'];
$this
->assertEqual(2, count($included));
$expected_includes = array(
array(
'type' => 'main',
'id' => (string) $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
'links' => array(
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_single->entity_reference_single
->getIdentifier()),
),
'attributes' => array(
'id' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
'label' => $wrapper->entity_reference_single->entity_reference_single
->label(),
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_single->entity_reference_single
->getIdentifier()),
),
),
array(
'type' => 'main',
'id' => (string) $wrapper->entity_reference_single
->getIdentifier(),
'links' => array(
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_single
->getIdentifier()),
),
'attributes' => array(
'entity_reference_single' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
'id' => $wrapper->entity_reference_single
->getIdentifier(),
'label' => $wrapper->entity_reference_single
->label(),
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_single
->getIdentifier()),
),
'relationships' => array(
'entity_reference_single_resource' => array(
'data' => array(
'id' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
'type' => 'main',
),
'links' => array(
'self' => $this->handler
->versionedUrl($wrapper->entity_reference_single
->getIdentifier() . '/relationships/entity_reference_single_resource'),
'related' => $this->handler
->versionedUrl('', array(
'query' => array(
'filter' => array(
'entity_reference_single_resource' => $wrapper->entity_reference_single->entity_reference_single
->getIdentifier(),
),
),
)),
),
),
),
),
);
// Remove the empty fields from the actual response for easier comparison.
$included[0]['attributes'] = array_filter($included[0]['attributes']);
$included[1]['attributes'] = array_filter($included[1]['attributes']);
$this
->assertEqual($expected_includes, $included);
// 7. Assert request processing.
$request = Request::create('', array(
'fields' => 'first.second.third.fourth,first.third.fourth',
'include' => 'fifth.sixth.seventh',
));
$input = $request
->getParsedInput();
$this
->assertEqual($input['fields'], 'first,first.second,first.second.third,first.second.third.fourth,first.third,first.third.fourth');
$this
->assertEqual($input['include'], 'fifth,fifth.sixth,fifth.sixth.seventh');
// 8. Assert generic reference resource field.
// Add some data to the DB table.
$record = array(
'str_field' => $this
->randomName(),
'int_field' => mt_rand(1, 10),
'serialized_field' => serialize(array()),
);
drupal_write_record('restful_test_db_query', $record);
$handler = restful()
->getResourceManager()
->getPlugin('main:1.8');
$handler
->setRequest(Request::create('', array(
'include' => 'random_rel',
'range' => 1,
)));
$handler
->setPath(1);
$formatter = restful()
->getFormatterManager()
->getPlugin('json_api');
$formatter
->setResource($handler);
$result = $formatter
->prepare($handler
->process());
$this
->assertEqual($result['included'][0]['attributes']['id'], 1);
}