You are here

pmpapi_push.test in Public Media Platform API Integration 7

File

pmpapi_push/tests/pmpapi_push.test
View source
<?php

/**
 * Tests the functionality of the PMPAPI push module.
 */
class PMPAPIPushWebTestCase extends DrupalWebTestCase {
  protected $privileged_user;
  protected $files = array();
  const SLEEP_TIME = 1;

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

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

    // 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);
    $this
      ->assertTrue(variable_get('pmpapi_push_push_active'), 'Push is active.');
    $this
      ->setUpFiles();

    // Set up node mapping
    $this->nodetype = 'testnode';
    $node_settings = array(
      'entity_type' => 'node',
      'bundle_name' => $this->nodetype,
      'target_profile' => 'story',
      'mapping' => array(
        'title' => 'title',
        'body' => 'contentencoded',
        'field_image' => 'item-image',
      ),
    );
    $this
      ->setUpEntityMapping($node_settings);

    // Set up image mapping
    $image_settings = array(
      'entity_type' => 'file',
      'bundle_name' => 'image',
      'target_profile' => 'image',
      'mapping' => array(
        'filename' => 'title',
      ),
    );
    $this
      ->setUpEntityMapping($image_settings);
  }

  /**
   * Sets up the entity/doc mapping.
   *
   * @param array $settings
   *   Seetings needed for the mappings
   */
  protected function setUpEntityMapping($settings) {
    $uname = $settings['entity_type'] . '__' . $settings['bundle_name'];
    variable_set('pmpapi_push_' . $uname . '_profile', $settings['target_profile']);
    variable_set('pmpapi_push_mapping_' . $uname . '_' . $settings['target_profile'], $settings['mapping']);
  }

  /**
   * Creates files to be used by tests.
   */
  protected function setUpFiles() {
    $types = array(
      'text',
      'image',
    );
    foreach ($types as $type) {
      foreach ($this
        ->drupalGetTestFiles($type) as $file) {
        $this->files[$type][] = file_save($file);
      }
    }
  }

  /**
   * Pushes a node to the PMP.
   */
  public function testPmpAPIPushPushNode() {
    $this
      ->createContentType();
    $node = $this
      ->createNode();
    $this
      ->assertTrue($node, 'Created test node.');
    $guid = $node->pmpapi_guid;
    sleep(self::SLEEP_TIME);
    $pmp = pmpapi_fetch($guid);
    $this
      ->assertTrue(empty($pmp->errors['query']), 'Pushed node to PMP.');
    sleep(self::SLEEP_TIME);
    node_delete($node->nid);
    sleep(self::SLEEP_TIME);
    $deleted_node = node_load($node->nid);
    $this
      ->assertTrue(!$deleted_node, 'Node deleted from local.');
    sleep(self::SLEEP_TIME);
    $pmp = pmpapi_fetch($guid);
    $this
      ->assertTrue(!empty($pmp->errors['query']), 'Node deleted from PMP.');
  }

  /**
   * Pushes an image to the PMP.
   */
  public function testPmpAPIPushPushImage() {
    $file_entity = $this
      ->createFileEntity(array(
      'type' => 'image',
    ));
    $guid = $file_entity->pmpapi_guid;
    sleep(self::SLEEP_TIME);
    $pmp = pmpapi_fetch($guid);
    $this
      ->assertTrue(empty($pmp->errors['query']), 'Pushed image to PMP.');
    file_delete($file_entity);
    sleep(self::SLEEP_TIME);
    $pmp = pmpapi_fetch($guid);
    $this
      ->assertTrue(!empty($pmp->errors['query']), 'Image deleted from PMP.');
  }

  /**
   * Pushes node (with attached image) to the PMP.
   */
  function testPmpAPIPushNodeWithRelatedImage() {
    $file_entity = $this
      ->createFileEntity();
    $field_image = array(
      'field_name' => 'field_image',
    );
    $this
      ->createContentType(array(
      $field_image,
    ));
    $settings = array(
      'field_image' => array(
        LANGUAGE_NONE => array(
          0 => array(
            'fid' => $file_entity->fid,
            'display' => 1,
          ),
        ),
      ),
    );
    $node = $this
      ->createNode($settings);
    $this
      ->assertTrue($node, 'Node dumped.');
    sleep(self::SLEEP_TIME);
    $this
      ->assertTrue(pmpapi_fetch($file_entity->pmpapi_guid), 'Pushed image to PMP.');
    $pmp = pmpapi_fetch($node->pmpapi_guid);
    $this
      ->assertTrue($pmp, 'Pushed node to PMP.');
  }

  /**
   * Pushes a node, fails to delete, queues doc, succeeds.
   */
  function testPmpAPIPushQueueDelete() {
    $this
      ->createContentType();
    $node = $this
      ->createNode();
    $this
      ->assertTrue($node, 'Created test node.');
    sleep(self::SLEEP_TIME);
    $guid = $node->pmpapi_guid;
    $pmp = pmpapi_fetch($guid);
    $this
      ->assertTrue(empty($pmp->errors['query']), 'Pushed node to PMP.');
    $secret = variable_get('pmpapi_auth_client_secret');
    variable_set('pmpapi_auth_client_secret', '1234');
    DrupalWebTestCase::refreshVariables();
    sleep(self::SLEEP_TIME);
    node_delete($node->nid);
    $this
      ->assertTrue(!node_load($node->nid), 'Node was deleted.');
    sleep(self::SLEEP_TIME);
    variable_set('pmpapi_auth_client_secret', $secret);
    DrupalWebTestCase::refreshVariables();
    $pmp = pmpapi_fetch($guid);
    sleep(self::SLEEP_TIME);
    $this
      ->cronRun();
    sleep(self::SLEEP_TIME);
    $pmp = pmpapi_fetch($guid);
    $this
      ->assertTrue(!empty($pmp->errors), 'Node was deleted on cron run.');
  }

  /**
   * Pushes a node, and then tries to delete three times (with bad API credentials).
   */
  function testPmpAPIPushStopDeleteAttempt() {
    $file_entity = $this
      ->createFileEntity();
    $field_image = array(
      'field_name' => 'field_image',
    );
    $this
      ->createContentType(array(
      $field_image,
    ));
    $settings = array(
      'field_image' => array(
        LANGUAGE_NONE => array(
          0 => array(
            'fid' => $file_entity->fid,
            'display' => 1,
          ),
        ),
      ),
    );
    $node = $this
      ->createNode($settings);
    $this
      ->assertTrue($node, 'Node dumped.');
    sleep(self::SLEEP_TIME);
    $this
      ->assertTrue(pmpapi_fetch($file_entity->pmpapi_guid), 'Pushed image to PMP.');
    $pmp = pmpapi_fetch($node->pmpapi_guid);
    $this
      ->assertTrue($pmp, 'Pushed node to PMP.');
  }

  /**
   * Creates fields.
   *
   * @param $fields
   *   The fields to be created.
   */
  protected function createFields($fields) {
    foreach ($fields as $field) {
      field_create_field($field);
    }
  }

  /**
   * Creates a new content type.
   *
   * @param $instances
   *   Any instances to be added to the content type.
   */
  protected function createContentType($instances = array()) {
    $content_type = $this
      ->drupalCreateContentType(array(
      'type' => $this->nodetype,
    ));
    if (!empty($instances)) {
      foreach ($instances as $instance) {
        $instance['entity_type'] = 'node';
        $instance['bundle'] = $this->nodetype;
        field_create_instance($instance);
      }
    }
    return $content_type;
  }

  /**
   * Creates a node.
   *
   * @param $settings
   *   Any specific settings for the new node.
   */
  protected function createNode($settings = array()) {
    $settings += array(
      'title' => 'PMPAPI Simpletest test: ' . date('G:i:s'),
      'type' => $this->nodetype,
      'body' => array(
        LANGUAGE_NONE => array(
          0 => array(
            'value' => $this
              ->randomName(32),
            'format' => filter_default_format(),
          ),
        ),
      ),
    );
    $node = $this
      ->drupalCreateNode($settings);
    return $node;
  }

  /**
   * Creates a new file entity.
   *
   * Currently can only create an image entity, so the name is deceptive.
   */
  protected function createFileEntity() {
    $file = next($this->files['image']);
    $this
      ->assertTrue(file_save($file), 'Created file.');
    return $file;
  }

}

Classes

Namesort descending Description
PMPAPIPushWebTestCase Tests the functionality of the PMPAPI push module.