You are here

public function ServicesEntityNodeResourceTest::testCRUD in Services Entity API 7.2

Tests basic CRUD and index actions of a node via the entity_node service.

1 call to ServicesEntityNodeResourceTest::testCRUD()
ServicesEntityNodeResourceTest::testCRUDClean in tests/services_entity.test
Tests node services using the 'clean' resource controller.

File

tests/services_entity.test, line 781
Services Entity Tests

Class

ServicesEntityNodeResourceTest
Tests entity_node services for both the generic and clean controller.

Code

public function testCRUD($clean = FALSE) {
  $resource = 'entity_node';

  // Certain things are different depending on whether or not we are using
  // the 'clean' resource controller.
  if ($clean) {
    variable_set('services_entity_resource_class', 'ServicesEntityResourceControllerClean');
    $node = array(
      'type' => 'page',
      'title' => $this
        ->randomName(10),
      'body' => array(
        'value' => $this
          ->randomName(50),
        'format' => 'full_html',
        'summary' => '',
      ),
    );
  }
  else {
    variable_set('services_entity_resource_class', 'ServicesEntityResourceController');
    $node = array(
      'type' => 'page',
      'title' => $this
        ->randomName(10),
      'body' => array(
        'und' => array(
          '0' => array(
            'value' => $this
              ->randomName(50),
            'format' => 'full_html',
            'summary' => '',
          ),
        ),
      ),
    );
  }

  // Test retrieving a specific revision.
  $revisions = $this
    ->createNodeRevisions();
  foreach ($revisions as $revision) {
    $retrieved = $this
      ->retrieve($resource, $revision->nid, array(
      'revision' => $revision->vid,
    ), 200);
    $this
      ->assertEqual($retrieved['title'], $revision->title, 'Revision ' . $revision->vid . ' has the correct title.');
  }

  // Ensure that retrieving the node without specifying a revision gets the
  // most recent revision.
  $retrieved = $this
    ->retrieve($resource, $revision->nid);
  $this
    ->assertEqual($retrieved['title'], $revision->title, 'Plain retrieve gets the lastest revision.');

  // We are only going to test properties that we set explicitly.
  $test_properties = array_keys($node);

  // Test node access
  $account = $this
    ->drupalCreateUser(array());
  $this
    ->serviceLogin($account->name, $account->pass_raw);
  $this
    ->create($resource, $node, 401);
  $this
    ->serviceLogout();

  // Test format access.
  $unpriv_account = $this
    ->drupalCreateUser(array(
    'create page content',
    'edit any page content',
    'delete any page content',
  ));
  $this
    ->serviceLogin($unpriv_account->name, $unpriv_account->pass_raw);
  $this
    ->create($resource, $node, 403);
  $this
    ->serviceLogout();

  // User with format access should be able to create the node.
  $account = $this
    ->drupalCreateUser(array(
    'bypass node access',
    'use text format full_html',
  ));
  $this
    ->serviceLogin($account->name, $account->pass_raw);

  // Set the 'author' or 'uid' property, depending on which controller we're using.
  if ($clean) {
    $node['author'] = $account->uid;
  }
  else {
    $node['uid'] = $account->uid;
  }

  // We cannot create a node with the clean controller because of [issue link]
  // @todo remove this conditional once that lands.
  $created_node = $this
    ->create($resource, $node);
  $this
    ->assertTrue($created_node['nid'], t('node create response has a node id'));
  $nid = $created_node['nid'];

  // Verify that the actual node was created.
  $drupal_node = $this
    ->nodeLoad($nid, $clean);
  $this
    ->assertNodeProperties($created_node, $drupal_node, $test_properties, 'Node CREATE response');
  $this
    ->assertNodeProperties($node, $drupal_node, $test_properties, 'Created node');

  // Test an update of the created node.
  // Change our title and body.
  $created_node['title'] = $this
    ->randomName(10);
  if ($clean) {
    $created_node['body']['value'] = $this
      ->randomname(50);
  }
  else {
    $created_node['body']['und'][0]['value'] = $this
      ->randomname(50);
  }
  $update_result = $this
    ->update($resource, $nid, $created_node, 200);
  $drupal_node = $this
    ->nodeLoad($nid, $clean);

  // @todo restore this once [insert issue -- generic-update-entity] lands
  if (!$clean) {
    $this
      ->assertNodeProperties($update_result, $drupal_node, $test_properties, 'Created node UPDATE response');
  }
  $this
    ->assertNodeProperties($created_node, $drupal_node, $test_properties, 'Updated created node');

  // Test retrieving the new node via services.
  $fetched_node = $this
    ->retrieve($resource, $nid);
  $this
    ->assertNodeProperties($fetched_node, $drupal_node, $test_properties, 'Node RETRIEVE response');

  // Test an update of the fetched node.
  // Change our title and body.
  $fetched_node['title'] = $this
    ->randomName(10);
  if ($clean) {
    $fetched_node['body']['value'] = $this
      ->randomname(50);
  }
  else {
    $fetched_node['body']['und'][0]['value'] = $this
      ->randomname(50);
  }
  $update_result = $this
    ->update($resource, $nid, $fetched_node, 200);
  $drupal_node = $this
    ->nodeLoad($nid, $clean);
  $this
    ->assertNodeProperties($update_result, $drupal_node, $test_properties, 'Fetched node UPDATE response');
  $this
    ->assertNodeProperties($fetched_node, $drupal_node, $test_properties, 'Updated fetched node');

  // Test updating with a disallowed text format in the body.
  $drupal_node = node_load($nid, NULL, TRUE);
  $drupal_node->body['und'][0]['format'] = filter_default_format();
  node_save($drupal_node);
  $this
    ->serviceLogout();
  $this
    ->serviceLogin($unpriv_account->name, $unpriv_account->pass_raw);
  $fetched_node['body']['format'] = 'full_html';
  $this
    ->update($resource, $nid, $fetched_node, 403);
  $drupal_node = node_load($nid, NULL, TRUE);
  $this
    ->assertEqual($drupal_node->body['und'][0]['format'], filter_default_format(), 'Unprivileged user was unable to update with disallowed text format.');

  // Test a delete of a node.
  $delete_result = $this
    ->delete($resource, $nid);

  // Confirm the node was deleted.
  $deleted_node = $this
    ->retrieve($resource, $nid, array(), 404);
  $drupal_node = node_load($nid, NULL, TRUE);
  $this
    ->assertFalse($drupal_node, 'The deleted node was removed from the db.');

  // Test creating a node without a body field.
  $node_sans_body = array(
    'type' => 'page',
    'title' => $this
      ->randomName(10),
  );

  // We need an author to create a node via clean controller
  // See https://drupal.org/node/1237014
  if ($clean) {
    $node_sans_body['author'] = $account->uid;
  }
  $created_node_sans_body = $this
    ->create($resource, $node_sans_body);
  $this
    ->assertTrue($created_node_sans_body['nid'], 'Created Node without body has a response with a node id');
  $drupal_node = $this
    ->nodeLoad($created_node_sans_body['nid'], $clean);
  $this
    ->assertNodeProperties($node_sans_body, $drupal_node, array_keys($node_sans_body), 'Created node without body');
}