You are here

wsclient_examples.test in Web service client 7

Web service client SOAP/REST Tests.

File

wsclient_examples/wsclient_examples.test
View source
<?php

/**
 * @file
 * Web service client SOAP/REST Tests.
 */
class WSClientSOAPTestCase extends DrupalWebTestCase {
  static function getInfo() {
    return array(
      'name' => 'Web service client SOAP',
      'description' => 'Tests invoking a SOAP service.',
      'group' => 'Services',
    );
  }
  function setUp() {
    parent::setUp('wsclient_examples');
  }

  /**
   * Tests invoking the geocoder.us service.
   */
  function testServiceInvocation() {
    $service = wsclient_service_load('geocoder');
    $result = $service
      ->geocode_address('1600 Pennsylvania Av, Washington, DC');
    $this
      ->assertEqual($result[0]->zip, '20502', 'SOAP service invocation was successful.');
  }

}
class WSClientSOAPRulesTestCase extends DrupalWebTestCase {
  static function getInfo() {
    return array(
      'name' => 'Web service client SOAP Rules',
      'description' => 'Tests the Rules integration of the SOAP endpoint.',
      'group' => 'Services',
      'dependencies' => array(
        'rules',
      ),
    );
  }
  function setUp() {
    parent::setUp('wsclient_examples', 'rules');
  }

  /**
   * Tests invoking the geocoder.us service.
   */
  function testRulesIntegration() {
    list($result) = rules_action('wsclient_geocoder_geocode_address', array())
      ->execute('1600 Pennsylvania Av, Washington, DC');
    $this
      ->assertEqual($result[0]->zip, '20502', 'SOAP service invoked via the action.');
    $rule = rule(array(
      'address' => array(
        'type' => 'text',
      ),
    ));
    $rule
      ->action('wsclient_geocoder_geocode_address', array(
      'param_address:select' => 'address',
    ))
      ->action('drupal_message', array(
      'message:select' => 'result:0:zip',
    ))
      ->execute('1600 Pennsylvania Av, Washington, DC');
    RulesLog::logger()
      ->checkLog();
    $msg = drupal_get_messages();
    $this
      ->assertEqual(array_pop($msg['status']), '20502', 'SOAP service has been invoked.');
  }

}
class WSClientRESTTestCase extends DrupalWebTestCase {
  static function getInfo() {
    return array(
      'name' => 'Web service client REST',
      'description' => 'Tests invoking a RESTful service.',
      'group' => 'Services',
    );
  }
  function setUp() {
    parent::setUp('wsclient_examples');
  }

  /**
   * Tests invoking the google translation service.
   */
  function testServiceInvocation() {
    $service = wsclient_service_load('twitter_search');
    $result = $service
      ->search('drupal');
    $this
      ->assertEqual($result['query'], 'drupal', 'REST service invocation was successful.');
  }

}
class WSClientRESTRulesTestCase extends DrupalWebTestCase {
  static function getInfo() {
    return array(
      'name' => 'Web service client REST Rules',
      'description' => 'Tests the Rules integration of the REST endpoint.',
      'group' => 'Services',
      'dependencies' => array(
        'rules',
      ),
    );
  }
  function setUp() {
    parent::setUp('wsclient_examples', 'rules');
  }

  /**
   * Tests invoking the google translation service.
   */
  function testRulesIntegration() {
    list($result) = rules_action('wsclient_twitter_search_search', array(
      'param_q' => 'drupal',
    ))
      ->execute();
    $this
      ->assertEqual($result['query'], 'drupal', 'REST service invoked via the action.');

    // Test making use of the provided variable.
    $rule = rule(array(
      'text' => array(
        'type' => 'text',
      ),
    ));
    $rule
      ->action('wsclient_twitter_search_search', array(
      'param_q:select' => 'text',
    ))
      ->action('drupal_message', array(
      'message:select' => 'result:query',
    ))
      ->execute('drupal');
    RulesLog::logger()
      ->checkLog();
    $msg = drupal_get_messages();
    $this
      ->assertEqual(drupal_strtolower(array_pop($msg['status'])), 'drupal', 'REST service has been invoked.');
  }

}
class WSClientRestWSTestCase extends DrupalWebTestCase {
  static function getInfo() {
    return array(
      'name' => 'Web service client RestWS (2.x)',
      'description' => 'Tests invoking the local RestWS service.',
      'group' => 'Services',
      'dependencies' => array(
        'restws',
        'restws_basic_auth',
      ),
    );
  }
  function setUp() {
    parent::setUp('restws', 'restws_basic_auth', 'wsclient_examples');

    // Allow any user name to use HTTP basic auth.
    variable_set('restws_basic_auth_user_regex', '/^.*/');
  }

  /**
   * Test the RestWS node service.
   */
  function testCRUD() {

    // Get.
    $title = $this
      ->randomName(8);
    $node = $this
      ->drupalCreateNode(array(
      'title' => $title,
    ));
    $account = $this
      ->drupalCreateUser(array(
      'access resource node',
    ));
    $service = wsclient_service_load('restws_node');
    $this
      ->addSimpletestCurlOptions($service, $account);
    $result = $service
      ->get($node->nid);
    $this
      ->assertEqual($node->title, $result['title'], 'Node title received correctly');

    // Create.
    $account = $this
      ->drupalCreateUser(array(
      'access resource node',
      'access content',
      'bypass node access',
    ));
    $this
      ->addSimpletestCurlOptions($service, $account);
    $service
      ->clearCache();
    $node = array(
      'type' => $node->title,
      'title' => $title,
      'author' => $node->uid,
    );
    $result = $service
      ->create($node);
    $nid = $result['id'];

    // Clear the static cache, otherwise we won't see the update.
    $created_node = node_load($nid, NULL, TRUE);
    $this
      ->assertEqual($created_node->title, $title, 'Created node title is correct.');
    $this
      ->assertEqual($result['uri'], url('node/' . $nid, array(
      'absolute' => TRUE,
    )), 'Returned URI is correct.');
    $this
      ->assertEqual($result['resource'], 'node', 'Returned resource is correct.');

    // Update.
    $new_title = $this
      ->randomName();
    $node = array(
      'title' => $new_title,
    );
    $service
      ->update($node, $nid);

    // Clear the static cache, otherwise we won't see the update.
    $updated_node = node_load($nid, NULL, TRUE);
    $this
      ->assertEqual($new_title, $updated_node->title, 'Node title has been updated sucessfully');

    // Delete.
    // We cannot directly call the method delete() as it is used for another
    // purpose.
    $service
      ->invoke('delete', array(
      $nid,
    ));

    // Clear the static cache, otherwise we won't see the update.
    $deleted_node = node_load($nid, NULL, TRUE);
    $this
      ->assertFalse($deleted_node, 'Node is not in the DB anymore.');
  }

  /**
   * Helper function to add the right cURL headers so that we connect to the simpletest DB.
   */
  protected function addSimpletestCurlOptions($service, $account) {
    $curl_options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
    $curl_options[CURLOPT_USERPWD] = $account->name . ':' . $account->pass_raw;
    if (preg_match('/simpletest\\d+/', $this->databasePrefix, $matches)) {
      $curl_options[CURLOPT_USERAGENT] = drupal_generate_test_ua($matches[0]);
    }
    $service->settings['curl options'] = $curl_options;
  }

}