You are here

pmpapi.test in Public Media Platform API Integration 7

File

tests/pmpapi.test
View source
<?php

/**
 * Tests the functionality of the PMPAPI module.
 */
class PMPAPIWebTestCase extends DrupalWebTestCase {
  protected $privileged_user;
  const SLEEP_TIME = 1;

  /**
   * Gives display information to the SimpleTest system.
   */
  public static function getInfo() {
    return array(
      'name' => 'PMPAPI Tests',
      'description' => 'Ensure that the basic PMPAPI functionality functions properly.',
      'group' => 'PMPAPI',
    );
  }

  /**
   * Sets up the test environment.
   *
   * @see DrupalWebTestCase::setUp()
   */
  public function setUp() {
    parent::setUp(array(
      'pmpapi',
    ));

    // Create and log in our privileged user.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer PMP API',
    ));
    $this
      ->drupalLogin($this->privileged_user);

    // Set up client_id, client_secret, host
    // @TODO: Throw this into a separate test -- using GUI?
    module_load_include('php', 'pmpapi', 'tests/settings');
    $vars = pmpapi_test_get_secret_vars();
    variable_set('pmpapi_auth_client_id', $vars['client_id']);
    variable_set('pmpapi_auth_client_secret', $vars['client_secret']);
    variable_set('pmpapi_base_url', $vars['host']);

    // Turn off caching
    variable_set('pmpapi_cache', FALSE);
  }

  /**
   * Gets a GUID (locally generated from the SDK).
   */
  public function testPmpAPIGetValidGUID() {
    $guid = pmpapi_guid();
    $valid_guid = preg_match("/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab]{1}[0-9a-f]{3}-[0-9a-f]{12}/", $guid);
    $this
      ->assertTrue($valid_guid, 'generated a valid guid.');
  }

  /**
   * Fetches a doc from the PMP.
   */
  public function testPmpAPIFetchDoc() {
    module_load_include('php', 'pmpapi', 'tests/settings');
    $vars = pmpapi_test_get_secret_vars();
    $doc = pmpapi_fetch($vars['story_guid']);
    $is_non_empty_object = is_object($doc) && !empty($doc) && empty($doc->errors['query']);
    $this
      ->assertTrue($is_non_empty_object, 'successfully pulled, without errors, object that is not empty.');
  }

  /**
   * Attempts to bogus doc from the PMP.
   */
  public function testPmpAPIFailToFetchDoc() {
    $doc = pmpapi_fetch('1234');
    $pull_error = !empty($doc->errors['query']);
    $this
      ->assertTrue($pull_error, 'pull error created when attempting to pull bogus doc.');
  }

  /**
   * Makes a basic query to the PMP.
   */
  public function testPmpAPIDoQuery() {
    module_load_include('php', 'pmpapi', 'tests/settings');
    $vars = pmpapi_test_get_secret_vars();
    $params = array(
      'tag' => $vars['sample_tag'],
    );
    $doc = pmpapi_query($params);
    $is_non_empty_object = is_object($doc) && !empty($doc) && empty($doc->errors['query']);
    $this
      ->assertTrue($is_non_empty_object, 'successfully pulled, without errors, query that is not empty.');
  }

  /**
   * Sends a simple doc to the PMP.
   */
  public function testPmpAPISendDoc() {
    $values = array(
      'attributes' => array(
        'title' => $this
          ->randomName(20),
        'teaser' => $this
          ->randomName(10),
      ),
    );
    $doc = pmpapi_send($values);
    sleep(self::SLEEP_TIME);
    pmpapi_remove($doc->attributes->guid);
    $this
      ->assertTrue(is_object($doc), 'successfully pushed a story to the PMP.');
  }

}

Classes

Namesort descending Description
PMPAPIWebTestCase Tests the functionality of the PMPAPI module.