You are here

public function RestfulCreateEntityTestCase::testCreateEntity in RESTful 7.2

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

Test creating an entity (POST method).

File

tests/RestfulCreateEntityTestCase.test, line 60
Contains RestfulCreateEntityTestCase

Class

RestfulCreateEntityTestCase
Class RestfulCreateEntityTestCase.

Code

public function testCreateEntity() {
  $resource_manager = restful()
    ->getResourceManager();

  // Create test entities to be referenced.
  $ids = array();
  for ($i = 0; $i < 2; $i++) {

    /* @var \Entity $entity */
    $entity = entity_create('entity_test', array(
      'name' => 'main',
    ));
    $entity
      ->save();
    $ids[] = $entity->pid;
  }
  $images = array();
  foreach ($this
    ->drupalGetTestFiles('image') as $file) {
    $file = file_save($file);
    $images[] = $file->fid;
  }
  $resource_manager
    ->clearPluginCache('main:1.1');
  $handler = $resource_manager
    ->getPlugin('main:1.1');
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'taxonomy_term')
    ->entityCondition('bundle', 'test_vocab')
    ->execute();
  $tids = array_keys($result['taxonomy_term']);
  $text1 = $this
    ->randomName();
  $text2 = $this
    ->randomName();
  $request = array(
    'text_single' => $text1,
    'text_multiple' => array(
      $text1,
      $text2,
    ),
    'text_single_processing' => $text1,
    'text_multiple_processing' => array(
      $text1,
      $text2,
    ),
    'entity_reference_single' => $ids[0],
    'entity_reference_multiple' => $ids,
    'term_single' => $tids[0],
    'term_multiple' => array(
      $tids[0],
      $tids[1],
    ),
    'file_single' => $images[0],
    'file_multiple' => array(
      $images[0],
      $images[1],
    ),
    'image_single' => $images[0],
    'image_multiple' => array(
      $images[0],
      $images[1],
    ),
  );
  $response = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->doPost($request)));
  $response = $response['data'];
  $result = $response[0];
  $text_single = trim(strip_tags($result['text_single']));
  $text_multiple = array(
    trim(strip_tags($result['text_multiple'][0])),
    trim(strip_tags($result['text_multiple'][1])),
  );
  $expected_result = $request;

  // Strip some elements, and the text, for easier assertion.
  $striped_result = $result;
  unset($striped_result['id']);
  unset($striped_result['label']);
  unset($striped_result['self']);
  unset($striped_result['entity_reference_single_resource']);
  unset($striped_result['entity_reference_multiple_resource']);
  $striped_result['text_single'] = $text_single;
  $striped_result['text_multiple'] = $text_multiple;
  $striped_result['text_single_processing'] = $text_single;
  $striped_result['text_multiple_processing'] = $text_multiple;
  ksort($striped_result);
  ksort($expected_result);
  $this
    ->assertEqual($expected_result, $striped_result, 'Entity was created with correct values.');
  $this
    ->assertEqual($result['entity_reference_single_resource']['id'], $ids[0], ' Entity reference single resource was created correctly');
  $this
    ->assertEqual($result['entity_reference_multiple_resource'][0]['id'], $ids[0], ' Entity reference multiple resource was created correctly');

  // Create an entity with empty request.
  try {
    $handler
      ->setRequest(Request::create('', array(), RequestInterface::METHOD_POST));
    $handler
      ->setPath('');
    restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json');
    $this
      ->fail('User can create an entity with empty request.');
  } catch (BadRequestException $e) {
    $this
      ->pass('User cannot create an entity with empty request.');
  }

  // Create an entity with invalid property name.
  $request['invalid'] = 'wrong';
  try {
    $handler
      ->setRequest(Request::create('', array(), RequestInterface::METHOD_POST, NULL, FALSE, NULL, array(), array(), array(), $request));
    $handler
      ->setPath('');
    restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json');
    $this
      ->fail('User can create an entity with invalid property name.');
  } catch (BadRequestException $e) {
    $this
      ->pass('User cannot create an entity with invalid property name.');
  }

  // Create entity with comma separated multiple entity reference.
  $request = array(
    'entity_reference_multiple' => implode(',', $ids),
  );
  $handler
    ->setRequest(Request::create('', array(), RequestInterface::METHOD_POST, NULL, FALSE, NULL, array(), array(), array(), $request));
  $handler
    ->setPath('');
  $response = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $response = $response['data'];
  $result = $response[0];
  $this
    ->assertEqual($result['entity_reference_multiple'], $ids, 'Created entity with comma separated multiple entity reference.');

  // Create entity with comma separated multiple taxonomy term reference.
  $ids = array(
    $tids[0],
    $tids[1],
  );
  $request = array(
    'term_multiple' => implode(',', $ids),
  );
  $handler
    ->setRequest(Request::create('', array(), RequestInterface::METHOD_POST, NULL, FALSE, NULL, array(), array(), array(), $request));
  $handler
    ->setPath('');
  $response = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $response = $response['data'];
  $result = $response[0];
  $this
    ->assertEqual($result['term_multiple'], $ids, 'Created entity with comma separated multiple taxonomy term reference.');

  // Create entity with comma separated multiple file reference.
  $ids = array(
    $images[0],
    $images[1],
  );
  $request = array(
    'file_multiple' => implode(',', $ids),
  );
  $handler
    ->setRequest(Request::create('', array(), RequestInterface::METHOD_POST, NULL, FALSE, NULL, array(), array(), array(), $request));
  $handler
    ->setPath('');
  $response = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $response = $response['data'];
  $result = $response[0];
  $this
    ->assertEqual($result['file_multiple'], $ids, 'Created entity with comma separated multiple file reference.');

  // Create entity with comma separated multiple image reference.
  $ids = array(
    $images[0],
    $images[1],
  );
  $request = array(
    'image_multiple' => implode(',', $ids),
  );
  $handler
    ->setRequest(Request::create('', array(), RequestInterface::METHOD_POST, NULL, FALSE, NULL, array(), array(), array(), $request));
  $handler
    ->setPath('');
  $response = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json'));
  $response = $response['data'];
  $result = $response[0];
  $this
    ->assertEqual($result['image_multiple'], $ids, 'Created entity with comma separated multiple image reference.');
}