You are here

function RestfulViewEntityTestCase::testViewEntity in RESTful 7

Same name and namespace in other branches
  1. 7.2 tests/RestfulViewEntityTestCase.test \RestfulViewEntityTestCase::testViewEntity()

Test viewing an entity (GET method).

v1.0 - Simple entity view (id, label, self). v1.1 - Text and entity reference fields. v1.2 - "callback" and "process callback". v1.3 - Non-existing "callback" property. v1.4 - Non-existing "process callback" property. v1.6 - XML output format.

File

tests/RestfulViewEntityTestCase.test, line 34
Contains RestfulViewEntityTestCase

Class

RestfulViewEntityTestCase
@file Contains RestfulViewEntityTestCase

Code

function testViewEntity() {
  $user1 = $this
    ->drupalCreateUser();
  $entity1 = entity_create('entity_test', array(
    'name' => 'main',
    'uid' => $user1->uid,
  ));
  $entity1
    ->save();
  $entity2 = entity_create('entity_test', array(
    'name' => 'main',
    'uid' => $user1->uid,
  ));
  $entity2
    ->save();
  $entity3 = entity_create('entity_test', array(
    'name' => 'main',
    'uid' => $user1->uid,
  ));
  $wrapper = entity_metadata_wrapper('entity_test', $entity3);
  $text1 = $this
    ->randomName();
  $text2 = $this
    ->randomName();
  $wrapper->text_single
    ->set($text1);
  $wrapper->text_multiple
    ->set(array(
    $text1,
    $text2,
  ));
  $wrapper->entity_reference_single
    ->set($entity1);
  $wrapper->entity_reference_multiple[] = $entity1;
  $wrapper->entity_reference_multiple[] = $entity2;
  $wrapper
    ->save();
  $id = $entity3->pid;
  $base_expected_result = array(
    'id' => $id,
    'label' => 'Main test type',
  );

  // v1.0 - Simple entity view (id, label, self).
  $handler = restful_get_restful_handler('main', 1, 0);
  $base_expected_result['self'] = $handler
    ->versionedUrl($id);
  $expected_result = $base_expected_result;
  $response = $handler
    ->get($id);
  $result = $response[0];
  $this
    ->assertEqual($result, $expected_result, 'Entity view has expected result for "main" resource v1');

  // v1.1 - Text and entity reference field.
  $handler = restful_get_restful_handler('main', 1, 1);
  $base_expected_result['self'] = $handler
    ->versionedUrl($id);
  $response = $handler
    ->get($id);
  $result = $response[0];
  $base_expected_result_v1 = $base_expected_result;

  // NULL fields.
  $base_expected_result_v1 += array(
    'text_single_processing' => NULL,
    'text_multiple_processing' => NULL,
    'term_single' => NULL,
    'term_multiple' => NULL,
    'file_single' => NULL,
    'file_multiple' => NULL,
    'image_single' => NULL,
    'image_multiple' => NULL,
  );
  $expected_result = $base_expected_result_v1;
  $expected_result['text_single'] = $text1;
  $expected_result['text_multiple'] = array(
    $text1,
    $text2,
  );
  $expected_result['entity_reference_single'] = $entity1->pid;
  $expected_result['entity_reference_multiple'] = array(
    $entity1->pid,
    $entity2->pid,
  );
  $response = $handler
    ->get($entity1->pid);
  $expected_result['entity_reference_single_resource'] = $response[0];
  $response1 = $handler
    ->get($entity1->pid);
  $response2 = $handler
    ->get($entity2->pid);
  $expected_result['entity_reference_multiple_resource'] = array(
    $response1[0],
    $response2[0],
  );
  $stripped_result = $result;
  $stripped_result['text_single'] = trim(strip_tags($result['text_single']));
  $stripped_result['text_multiple'][0] = trim(strip_tags($result['text_multiple'][0]));
  $stripped_result['text_multiple'][1] = trim(strip_tags($result['text_multiple'][1]));
  ksort($stripped_result);
  ksort($expected_result);
  $this
    ->assertEqual($stripped_result, $expected_result, 'Entity view has correct result for "main" resource v1.1');

  // Test the "full_view" property on a referenced entity.
  // We change the definition via the handler instead of creating another
  // plugin.
  $public_fields = $handler
    ->getPublicFields();

  // Single entity reference field with "resource".
  $public_fields['entity_reference_single_resource']['resource'] = array(
    'main' => array(
      'name' => 'main',
      'full_view' => FALSE,
    ),
  );
  $handler
    ->setPublicFields($public_fields);
  $result = $handler
    ->get($id);
  $this
    ->assertEqual($result[0]['entity_reference_single_resource'], $entity1->pid, '"full_view" property is working properly.');

  // Empty the text and entity reference fields.
  $wrapper->text_single
    ->set(NULL);
  $wrapper->text_multiple
    ->set(NULL);
  $wrapper->entity_reference_single
    ->set(NULL);
  $wrapper->entity_reference_multiple
    ->set(NULL);
  $wrapper
    ->save();
  $response = $handler
    ->get($id);
  $result = $response[0];
  $expected_result = $base_expected_result_v1;
  $expected_result['text_single'] = NULL;
  $expected_result['text_multiple'] = NULL;
  $expected_result['text_single'] = NULL;
  $expected_result['text_multiple'] = NULL;
  $expected_result['entity_reference_single'] = NULL;
  $expected_result['entity_reference_multiple'] = NULL;
  $expected_result['entity_reference_single_resource'] = NULL;
  $expected_result['entity_reference_multiple_resource'] = NULL;
  ksort($result);
  ksort($expected_result);
  $this
    ->assertEqual($result, $expected_result, 'Entity view has correct result for "main" resource v1.1 with empty entity reference.');

  // Load an entity by an alternate field.
  $entity4 = entity_create('entity_test', array(
    'name' => 'main',
    'uid' => $user1->uid,
  ));
  $wrapper = entity_metadata_wrapper('entity_test', $entity4);
  $text = $this
    ->randomName();
  $wrapper->text_single
    ->set($text);
  $wrapper
    ->save();
  $request = array(
    'loadByFieldName' => 'text_single',
  );
  $result = $handler
    ->get($text, $request);
  $this
    ->assertNotNull($result[0]);

  // Make sure canonical header is added.
  $headers = $handler
    ->getHttpHeaders();
  $this
    ->assertEqual($headers['Link'], $handler
    ->versionedUrl($wrapper
    ->getIdentifier(), array(), FALSE) . '; rel="canonical"');

  // v1.2 - "callback" and "process callback".
  $handler = restful_get_restful_handler('main', 1, 2);
  $base_expected_result['self'] = $handler
    ->versionedUrl($id);
  $response = $handler
    ->get($id);
  $result = $response[0];
  $expected_result = $base_expected_result;
  $expected_result['callback'] = 'callback';
  $expected_result['process_callback_from_callback'] = 'callback processed from callback';
  $expected_result['process_callback_from_value'] = $id . ' processed from value';
  $this
    ->assertEqual($result, $expected_result, 'Entity view has correct result for "main" resource v1.2');

  // v1.3 - Non-existing "callback" property.
  $handler = restful_get_restful_handler('main', 1, 3);
  try {
    $handler
      ->get($id);
    $this
      ->fail('Non-existing "callback" property did not trigger an exception.');
  } catch (Exception $e) {
    $this
      ->pass('Non-existing "callback" property triggered an exception.');
  }

  // v1.4 - Non-existing "process callback" property.
  $handler = restful_get_restful_handler('main', 1, 4);
  try {
    $handler
      ->get($id);
    $this
      ->fail('Non-existing "process callback" property did not trigger an exception.');
  } catch (Exception $e) {
    $this
      ->pass('Non-existing "process callback" property triggered an exception.');
  }

  // v1.6 - XML output format.
  $settings = array(
    'type' => 'article',
    'uid' => $user1->uid,
  );
  $node = $this
    ->drupalCreateNode($settings);
  $response = $this
    ->httpRequest('api/v1.6/articles', \RestfulInterface::GET);

  // Returns something like:
  // <api>
  //   <data>
  //     <item0>
  //       <id>1</id>
  //       <label>Foo</label>
  //       <self>http://example.com/node/1</self>
  //       <body>...</body>
  //     </item0>
  //   </data>
  //   <count>50</count>
  //   <_links>
  //     <next>https://example.com/api/v1.0/articles</next>
  //   </_links>
  // </api>
  $xml = new SimpleXMLElement($response['body']);
  $results = $xml
    ->xpath('/api/articles/item0/label');
  $result = reset($results);
  $this
    ->assertEqual($node->title, $result
    ->__toString(), 'XML parsed correctly.');

  // Test Image variations based on image styles.
  // Add the multiple images field to the article bundle.
  $field = array(
    'field_name' => 'field_images',
    'type' => 'image',
    'settings' => array(),
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_images',
    'entity_type' => 'node',
    'label' => 'Image multiple',
    'bundle' => 'article',
  );
  field_create_instance($instance);
  $images = $this
    ->drupalGetTestFiles('image');
  $image = array_shift($images);
  $image = file_save((object) $image);
  $article = $this
    ->drupalCreateNode(array(
    'type' => 'article',
    'field_image' => array(
      LANGUAGE_NONE => array(
        array(
          'fid' => $image->fid,
        ),
      ),
    ),
  ));
  $handler = restful_get_restful_handler('articles', 1, 5);
  $result = $handler
    ->get($article->nid);
  $this
    ->assertEqual(array_keys($result[0]['image']['styles']) == array(
    'thumbnail',
    'medium',
    'large',
  ), 'The selected image styles are present.');
  $this
    ->assertEqual(count(array_filter(array_values($result[0]['image']['styles']))) == 3, 'The image styles are populated.');

  // Test multiple Image variations based on image styles.
  $article = array(
    'type' => 'article',
    'field_images' => array(
      LANGUAGE_NONE => array(),
    ),
  );
  foreach ($images as $image) {
    $image = file_save((object) $image);
    $article['field_images'][LANGUAGE_NONE][] = array(
      'fid' => $image->fid,
    );
  }
  $article = $this
    ->drupalCreateNode($article);
  $handler = restful_get_restful_handler('articles', 1, 5);
  $result = $handler
    ->get($article->nid);
  $this
    ->assertEqual(count($result[0]['images']), count($images), 'The number of images is correct.');
  $this
    ->assertEqual(array_keys($result[0]['images'][0]['styles']) == array(
    'thumbnail',
    'medium',
    'large',
  ), 'The selected image styles are present.');
  $this
    ->assertEqual(count(array_filter(array_values($result[0]['images'][0]['styles']))) == 3, 'The image styles are populated.');
}