You are here

TestServicesModule.test in Services 6.3

Unit tests for the functions in the 'services.module' file.

File

tests/unit/TestServicesModule.test
View source
<?php

/**
 * @file
 * Unit tests for the functions in the 'services.module' file.
 */

/**
 * Run test cases for the functions in the 'services.module' file.
 */
class ServicesModuleTests extends DrupalWebTestCase {

  // Class variables.
  protected $privilegedUser = NULL;

  /**
   * Implementation of getInfo.
   */
  public static function getInfo() {
    return array(
      'name' => 'Unit tests for services.module file.',
      'description' => 'These are the unit tests for the services.module file functions.',
      'group' => 'Services',
    );
  }

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

    // Create and logi in our privileged user.
    $this->privilegedUser = $this
      ->drupalCreateUser(array(
      'administer services',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($this->privilegedUser);
  }

  /**
   * Test services_help function.
   */
  public function testServicesHelp() {
    $result = services_help('admin/help#services', array());
    $message = t('There should be a help message returned from services help.');
    $this
      ->assertNotNull($result, $message);
    $result = services_help('admin/build/services', array());
    $message = t('There should be help text when going to the services endpoint administration page.');
    $this
      ->assertNotNull($result, $message);
  }

  /**
   * Test thta adding a menu endpoint creates an menu path for that item.
   */
  public function testEndpointMenu() {

    // Create the endpoint.
    $endpoint_settings = array(
      'name' => drupal_strtolower($this
        ->randomName(10)),
      'path' => drupal_strtolower($this
        ->randomName(10)),
      'server' => 'rest_server',
      'services_use_content_permissions' => TRUE,
    );
    $this
      ->drupalPost('admin/build/services/add', $endpoint_settings, t('Save'));
    $this
      ->assertResponse('200', t('Endpoint created successfully.'));

    // Enable node resource index method.
    $resource_settings = array(
      'resources[node][operations][index][enabled]' => '1',
    );
    $this
      ->drupalPost('admin/build/services/list/' . $endpoint_settings['name'] . '/resources', $resource_settings, t('Save'));
    $this
      ->assertResponse('200', t('Node resource index method enabled successfully.'));

    // Check path.
    $this
      ->drupalGet($endpoint_settings['path'] . '/node');
    $this
      ->assertResponse('200', t('Accessed endpoint menu path node index method.'));

    // After accessing node resource we got logged out. So we login again.
    $this
      ->drupalLogin($this->privilegedUser);

    // Check edit.
    $this
      ->drupalGet('admin/build/services/list/' . $endpoint_settings['name']);
    $this
      ->assertTitle(t('Edit endpoint !name | Drupal', array(
      '!name' => $endpoint_settings['name'],
    )), t('Accessed endpoint edit path.'));

    // Check export.
    $this
      ->drupalGet('admin/build/services/list/' . $endpoint_settings['name'] . '/export');
    $this
      ->assertTitle(t('Export endpoint !name | Drupal', array(
      '!name' => $endpoint_settings['name'],
    )), t('Accessed endpoint export path.'));

    // Check delete.
    $this
      ->drupalPost('admin/build/services/list/' . $endpoint_settings['name'], array(), t('Delete'));
    $this
      ->assertTitle(t('Are you sure you want to delete !name? | Drupal', array(
      '!name' => $endpoint_settings['name'],
    )), t('Can delete endpoint.'));
  }

  /**
   * Test that services_access_menu() returns TRUE.
   */
  public function testServicesAccessMenu() {
    $message = t('services_access_menu should return TRUE');
    $this
      ->assertTrue(services_access_menu(), $message);
  }

  // function

  /**
   * Verify services_get_servers() returs the REST server.
   */
  public function testServicesGetServers() {
    $results = services_get_servers();
    $message = t('\'services_get_servers\' should return an array.');
    $this
      ->assertTrue(gettype($results) == 'array', $message);
    $message = t('There should only be one element in the array.');
    $this
      ->assertEqual(count($results), 1, $message);
    $message = t('The key to the one element should be \'rest_server\'.');
    $this
      ->assertTrue(isset($results['rest_server']), $message);
    $message = t('Server name should be \'REST\'.');
    $this
      ->assertTrue(isset($results['rest_server']['name']) && $results['rest_server']['name'] == 'REST', $message);
    $message = t('Server path should be \'rest\'.');
    $this
      ->assertTrue(isset($results['rest_server']['path']) && $results['rest_server']['path'] == 'rest', $message);
  }

  /**
   * Test services_endpoint_new().
   */
  public function testServicesEndpointNew() {
    $results = services_endpoint_new();
    $results_type = gettype($results);
    $message = t('services_endpoints_new() should return an object.');
    $this
      ->assertEqual($results_type, 'object', $message);
    $string = 'New Service object should have property ';
    $this
      ->assertTrue(property_exists($results, 'eid'), t($string . 'eid.'));
    $this
      ->assertTrue(property_exists($results, 'name'), t($string . 'name.'));
    $this
      ->assertTrue(property_exists($results, 'server'), t($string . 'server.'));
    $this
      ->assertTrue(property_exists($results, 'path'), t($string . 'path.'));
  }

}

// class

Classes

Namesort descending Description
ServicesModuleTests Run test cases for the functions in the 'services.module' file.