You are here

public function RestfulDiscoveryTestCase::testFieldDiscovery in RESTful 7.2

Same name and namespace in other branches
  1. 7 tests/RestfulDiscoveryTestCase.test \RestfulDiscoveryTestCase::testFieldDiscovery()

Field discovery.

File

tests/RestfulDiscoveryTestCase.test, line 91
Contains RestfulDiscoveryTestCase

Class

RestfulDiscoveryTestCase
Class RestfulDiscoveryTestCase.

Code

public function testFieldDiscovery() {

  // Add common fields, vocabulary and terms.
  restful_test_add_fields();

  // Create an entity.
  $entity = entity_create('entity_test', array(
    'name' => 'main',
    'label' => $this
      ->randomName(),
  ));
  $pid = $entity
    ->save();
  $handler = restful()
    ->getResourceManager()
    ->getPlugin('main:1.1');
  $handler
    ->setRequest(Request::create('api/v1.1/main/' . $pid, array(), RequestInterface::METHOD_OPTIONS));
  $handler
    ->setPath($pid);
  $formatter = restful()
    ->getFormatterManager()
    ->negotiateFormatter(NULL);
  $formatter
    ->setResource($handler);
  $result = $formatter
    ->prepare($handler
    ->process());
  $expected = array(
    'id' => array(
      'data' => array(
        'cardinality' => 1,
        'read_only' => TRUE,
        'type' => 'integer',
        'required' => TRUE,
      ),
      'info' => array(
        'description' => t('Base ID for the entity.'),
        'label' => t('ID'),
      ),
    ),
    'label' => array(
      'data' => array(
        'cardinality' => 1,
        'read_only' => FALSE,
        'type' => 'string',
        'required' => FALSE,
        'size' => 255,
      ),
      'form_element' => array(
        'allowed_values' => NULL,
        'type' => 'textfield',
        'default_value' => '',
        'placeholder' => '',
        'size' => 255,
        'description' => 'The label of the resource.',
        'title' => 'label',
      ),
      'info' => array(
        'description' => t('The label of the resource.'),
        'label' => t('Label'),
      ),
    ),
    'text_multiple' => array(
      'data' => array(
        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
        'read_only' => FALSE,
        'type' => 'string',
        'size' => 255,
        'required' => FALSE,
      ),
      'form_element' => array(
        'allowed_values' => NULL,
        'default_value' => '',
        'placeholder' => t('This is helpful.'),
        'size' => 255,
        'type' => 'textfield',
        'description' => 'This field holds different text inputs.',
        'title' => 'text_multiple',
      ),
      'info' => array(
        'description' => t('This field holds different text inputs.'),
        'label' => t('Text multiple'),
      ),
    ),
  );
  foreach ($expected as $public_field => $discovery_info) {
    foreach (array(
      'data',
      'form_element',
      'info',
    ) as $section_name) {
      if (empty($result['data'][0][$public_field][$section_name]) && empty($expected[$public_field][$section_name])) {
        continue;
      }
      $this
        ->assertEqual($result['data'][0][$public_field][$section_name], $expected[$public_field][$section_name], format_string('The "@section" information is properly described for @field.', array(
        '@field' => $public_field,
        '@section' => $section_name,
      )));
    }
  }
}