You are here

function ServicesEntityBasic::testPropertyFetching in Services Entity API 7

File

tests/services_entity.test, line 23

Class

ServicesEntityBasic

Code

function testPropertyFetching() {
  $node = (object) array(
    'title' => 'Test of title',
    'type' => 'article',
    'uid' => $this->adminUser->uid,
    'status' => 1,
    'body' => array(
      LANGUAGE_NONE => array(
        0 => array(
          'value' => 'Test of body',
          'format' => 'filtered_html',
        ),
      ),
    ),
  );
  node_save($node);

  // Test the base properties of the node.
  $expected['nid'] = array(
    'cardinality' => 1,
    'type' => 'integer',
    'translatable' => 0,
    LANGUAGE_NONE => array(
      0 => array(
        'value' => $node->nid,
      ),
    ),
  );
  $expected['title'] = array(
    'cardinality' => 1,
    'type' => 'text',
    'translatable' => 0,
    LANGUAGE_NONE => array(
      0 => array(
        'value' => 'Test of title',
      ),
    ),
  );
  $expected['type'] = array(
    'cardinality' => 1,
    'type' => 'token',
    'translatable' => 0,
    LANGUAGE_NONE => array(
      0 => array(
        'value' => 'article',
      ),
    ),
  );
  $expected['status'] = array(
    'cardinality' => 1,
    'type' => 'integer',
    'translatable' => 0,
    LANGUAGE_NONE => array(
      0 => array(
        'value' => 1,
      ),
    ),
  );

  // Test accessing properties across relationships.
  $expected['author:name'] = array(
    'cardinality' => 1,
    'type' => 'user',
    'translatable' => 0,
    LANGUAGE_NONE => array(
      0 => array(
        'name' => array(
          'cardinality' => 1,
          'type' => 'text',
          'translatable' => 0,
          LANGUAGE_NONE => array(
            0 => array(
              'value' => $this->adminUser->name,
            ),
          ),
        ),
      ),
    ),
  );
  $expected['author:uid'] = array(
    'cardinality' => 1,
    'type' => 'user',
    'translatable' => 0,
    LANGUAGE_NONE => array(
      0 => array(
        'uid' => array(
          'cardinality' => 1,
          'type' => 'integer',
          'translatable' => 0,
          LANGUAGE_NONE => array(
            0 => array(
              'value' => $this->adminUser->uid,
            ),
          ),
        ),
      ),
    ),
  );
  $expected['author:created'] = array(
    'cardinality' => 1,
    'type' => 'user',
    'translatable' => 0,
    LANGUAGE_NONE => array(
      0 => array(
        'created' => array(
          'cardinality' => 1,
          'type' => 'date',
          'translatable' => 0,
          LANGUAGE_NONE => array(
            0 => array(
              'value' => $this->adminUser->created,
            ),
          ),
        ),
      ),
    ),
  );
  foreach ($expected as $property => $expected) {
    $fields = _services_entity_transform_fields(array(
      $property => $property,
    ));
    $entity = clone $node;
    $wrapper = entity_metadata_wrapper('node', $entity);
    $result = services_entity_prepare_structure($wrapper, $fields);
    $path = explode(':', $property);
    $base_element = reset($path);
    $this
      ->verbose("<pre>Expected: " . print_r($expected, TRUE) . "\n\nReturned: " . print_r($result[$base_element], TRUE) . "</pre>");
    $this
      ->assertEqual($expected, $result[$base_element], t('Property @property returned expected result', array(
      '@property' => $property,
    )));

    // Force to reset the cache of entities involved to simulate a fresh
    // request.
    entity_get_controller('node')
      ->resetCache();
    entity_get_controller('user')
      ->resetCache();
  }
}