You are here

ServicesEndpointTests.test in Services 6.3

Same filename and directory in other branches
  1. 7.3 tests/functional/ServicesEndpointTests.test

File

tests/functional/ServicesEndpointTests.test
View source
<?php

/**
 * @file
 * Call the endpoint tests when no authentication is being used.
 *
 */
require_once 'ServicesWebTestCase.php';

/**
 * Run test cases for the endpoint with no authentication turned on.
 *
 */
class ServicesEndpointTests extends ServicesWebTestCase {

  /**
   * Implementation of setUp().
   */
  public function setUp() {
    parent::setUp('autoload', 'ctools', 'services', 'rest_server', 'inputstream');
  }

  /**
   * Test adding an endpoint succeeds.
   */
  public function testEndpointForm() {

    // Create and log in our privileged user.
    $this->privilegedUser = $this
      ->drupalCreateUser(array(
      'administer services',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($this->privilegedUser);
    $edit = $this
      ->populateEndpointFAPI();
    $this
      ->drupalPost('admin/build/services/add', $edit, t('Save'));
    $this
      ->drupalGet($edit['path']);
  }

  /**
   * Test adding an endpoint succeeds.
   */
  public function testSuccessfulAddEndpoint() {
    $edit = $this
      ->populateEndpointFAPI();

    // Create and log in our privileged user.
    $this->privilegedUser = $this
      ->drupalCreateUser(array(
      'administer services',
      'administer nodes',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($this->privilegedUser);
    $this
      ->drupalPost('admin/build/services/add', $edit, t('Save'));
    $this
      ->assertResponse('200', t('expected 200'));
    $this
      ->drupalGet('admin/build/services/list');
    $this
      ->assertResponse('200', t('expected 200'));
    $this
      ->assertText($edit['name'], t('Endpoint path appears'));
    $this
      ->assertText('Normal', t('Storage is in database'));
  }

  /**
   * Test missing path to endpoint causes an error.
   */
  public function testMissingPath() {
    $edit = $this
      ->populateEndpointFAPI();
    unset($edit['path']);

    // Create and log in our privileged user.
    $this->privilegedUser = $this
      ->drupalCreateUser(array(
      'administer services',
      'administer nodes',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($this->privilegedUser);
    $this
      ->drupalPost('admin/build/services/add', $edit, t('Save'));
    $this
      ->assertResponse('200', 'expected 200');
    $this
      ->assertText('Path to endpoint field is required.', t('Endpoint path missing error message.'));
    $this
      ->assertFieldByName('server', 'rest_server', t('Server is rest server'));
    $this
      ->assertFieldChecked('edit-services-use-content-permissions', t('Storage use content permission is checked.'));
  }

  /**
   * Test missing server for endpoint causes an error.
   */
  public function testMissingServer() {
    $edit = $this
      ->populateEndpointFAPI();
    unset($edit['server']);

    // Create and log in our privileged user.
    $this->privilegedUser = $this
      ->drupalCreateUser(array(
      'administer services',
      'administer nodes',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($this->privilegedUser);
    $this
      ->drupalPost('admin/build/services/add', $edit, t('Save'));
    $this
      ->assertResponse('200', 'expected 200');
    $this
      ->assertText('Server field is required.', t('Server missing error message.'));
    $this
      ->assertFieldByName('name', $edit['name'], t('Name field remains.'));
    $this
      ->assertFieldChecked('edit-services-use-content-permissions', t('Storage use content permission is checked.'));
  }

  /*
   ------------------------------------
   HELPER METHODS
   ------------------------------------
  */

  /**
   * Creates a data array for populating an endpoint creation form.
   *
   * @return
   * An array of fields for fully populating an endpoint creation form.
   */
  public function populateEndpointFAPI() {
    return array(
      'name' => 'machinename',
      'path' => $this
        ->randomName(10),
      'server' => 'rest_server',
      'services_use_content_permissions' => TRUE,
    );
  }
  public function saveNewEndpoint() {
    $edit = $this
      ->populateEndpointFAPI();
    $endpoint = new stdClass();
    $endpoint->disabled = FALSE;

    /* Edit this to true to make a default endpoint disabled initially */
    $endpoint->api_version = 3;
    $endpoint->name = $edit['name'];
    $endpoint->server = $edit['server'];
    $endpoint->path = $edit['path'];
    $endpoint->authentication = array();
    $endpoint->resources = array(
      'node' => array(
        'alias' => '',
        'operations' => array(
          'create' => array(
            'enabled' => 1,
          ),
          'retrieve' => array(
            'enabled' => 1,
          ),
          'update' => array(
            'enabled' => 1,
          ),
          'delete' => array(
            'enabled' => 1,
          ),
          'index' => array(
            'enabled' => 1,
          ),
        ),
      ),
      'system' => array(
        'alias' => '',
        'actions' => array(
          'connect' => array(
            'enabled' => 1,
          ),
          'get_variable' => array(
            'enabled' => 1,
          ),
          'set_variable' => array(
            'enabled' => 1,
          ),
        ),
      ),
      'taxonomy_term' => array(
        'alias' => '',
        'operations' => array(
          'create' => array(
            'enabled' => 1,
          ),
          'retrieve' => array(
            'enabled' => 1,
          ),
          'update' => array(
            'enabled' => 1,
          ),
          'delete' => array(
            'enabled' => 1,
          ),
        ),
        'actions' => array(
          'selectNodes' => array(
            'enabled' => 1,
          ),
        ),
      ),
      'taxonomy_vocabulary' => array(
        'alias' => '',
        'operations' => array(
          'create' => array(
            'enabled' => 1,
          ),
          'retrieve' => array(
            'enabled' => 1,
          ),
          'update' => array(
            'enabled' => 1,
          ),
          'delete' => array(
            'enabled' => 1,
          ),
        ),
        'actions' => array(
          'getTree' => array(
            'enabled' => 1,
          ),
        ),
      ),
      'user' => array(
        'alias' => '',
        'operations' => array(
          'create' => array(
            'enabled' => 1,
          ),
          'retrieve' => array(
            'enabled' => 1,
          ),
          'update' => array(
            'enabled' => 1,
          ),
          'delete' => array(
            'enabled' => 1,
          ),
          'index' => array(
            'enabled' => 1,
          ),
        ),
        'actions' => array(
          'login' => array(
            'enabled' => 1,
          ),
          'logout' => array(
            'enabled' => 1,
          ),
        ),
      ),
      'comment' => array(
        'alias' => '',
        'operations' => array(
          'create' => array(
            'enabled' => 1,
          ),
          'retrieve' => array(
            'enabled' => 1,
          ),
          'update' => array(
            'enabled' => 1,
          ),
          'delete' => array(
            'enabled' => 1,
          ),
        ),
        'actions' => array(
          'loadNodeComments' => array(
            'enabled' => 1,
          ),
          'countAll' => array(
            'enabled' => 1,
          ),
          'countNew' => array(
            'enabled' => 1,
          ),
        ),
      ),
      'file' => array(
        'alias' => '',
        'operations' => array(
          'create' => array(
            'enabled' => 1,
          ),
          'retrieve' => array(
            'enabled' => 1,
          ),
          'delete' => array(
            'enabled' => 1,
          ),
        ),
        'relationships' => array(
          'files' => array(
            'enabled' => 1,
          ),
          'comments' => array(
            'enabled' => 1,
          ),
        ),
      ),
      'echo' => array(
        'alias' => '',
        'operations' => array(
          'index' => array(
            'enabled' => 1,
          ),
        ),
      ),
    );
    $endpoint->debug = 1;
    $endpoint->status = 1;
    services_endpoint_save($endpoint);
    $endpoint = services_endpoint_load($endpoint->name);
    $this
      ->assertEqual($endpoint->name, $edit['name'], t('Endpoint successfully created'));
    $this
      ->servicesGet($endpoint->path);
    return $endpoint;
  }

  /**
   * Builds out our post fields
   *
   */
  public function services_build_postfields($data = array()) {
    $post_data = '';
    if (is_array($data) && !empty($data)) {
      array_walk($data, array(
        $this,
        'services_flatten_fields',
      ));
      $post_data = implode('&', $data);
    }
    if (is_object($data) && !empty($data)) {
      array_walk(get_object_vars($data), array(
        $this,
        'services_flatten_fields',
      ));
      $post_data = implode('&', $data);
    }
    return $post_data;
  }

  /**
   * Modifies our array data so we can turn it into a querystring
   *
   * @param string $item - array value
   * @param string $key  - key of the array element
   */
  public function services_flatten_fields(&$item, $key) {
    $item = $key . '=' . $item;
  }

}

Classes

Namesort descending Description
ServicesEndpointTests Run test cases for the endpoint with no authentication turned on.