You are here

pmpapi_pull.test in Public Media Platform API Integration 7

File

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

/**
 * Tests the functionality of the PMPAPI pull module.
 */
class PMPAPIPullWebTestCase 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 Pull Tests',
      'description' => 'Ensure that the basic PMPAPI pull functionality functions properly.',
      'group' => 'PMPAPI',
    );
  }

  /**
   * Sets up the test environment.
   *
   * @see DrupalWebTestCase::setUp()
   */
  public function setUp() {

    // Enable any modules required for the test.
    parent::setUp(array(
      'pmpapi_pull',
      '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
    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']);
    $this->story_guid = $vars['story_guid'];
    $this->story_with_image_guid = $vars['story_with_image_guid'];
    $this->image_guid = $vars['image_guid'];

    // Turn off caching
    variable_set('pmpapi_cache', FALSE);
    $this
      ->assertTrue(variable_get('pmpapi_pull_pull_active'), 'Pull is active.');

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

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

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

  /**
   * Pulls a story doc from the PMP and maps it to a node.
   */
  public function testPmpAPIPullPullStory() {
    $this
      ->createContentType();
    $node = pmpapi_pull_pull_doc($this->story_guid);
    $this
      ->assertTrue($node->nid, 'Pulled story and created node.');
  }

  /**
   * Pulls an image doc from the PMP and maps it to an image entity.
   */
  public function testPmpAPIPullPullImage() {
    $image = pmpapi_pull_pull_doc($this->image_guid);
    $this
      ->assertTrue($image->fid, 'Pulled image and create image file entity.');
  }

  /**
   * Pulls a story doc (with attached image) from the PMP and maps it to a node.
   */
  public function testPmpAPIPullPullStoryWithImage() {
    $field_image = array(
      'field_name' => 'field_image',
    );
    $this
      ->createContentType(array(
      $field_image,
    ));
    $node = pmpapi_pull_pull_doc($this->story_with_image_guid);
    $this
      ->assertTrue($node->nid, 'Pulled story with image and created node.');
    $this
      ->assertTrue(!empty($node->field_image['und'][0]['fid']), 'Image is attached to node.');
  }

  /**
   * Attempts to pull a story doc with a bad GUID.
   */
  public function testPmpAPIPullTryBadDoc() {
    $this
      ->createContentType();
    $node = pmpapi_pull_pull_doc('1234');
    $this
      ->assertFalse($node, 'Failed to pull bogus GUID.');
  }

  /**
   * Checks for status = 0 on pulled embargoed story.
   */
  public function testPmpAPIPullEmbargoedStory() {
    $this
      ->createContentType();
    $valid = new stdClass();
    $valid->from = pmpapi_convert_timestamp(strtotime('+ 100 days', time()));
    $values = array(
      'profile' => 'story',
      'attributes' => array(
        'valid' => $valid,
        'title' => $this
          ->randomName(20),
      ),
    );
    $doc = pmpapi_send($values);
    $this
      ->assertTrue(is_object($doc), 'Successfully pushed a story to the PMP.');
    sleep(self::SLEEP_TIME);
    $guid = $doc->attributes->guid;
    pmpapi_pull_pull_doc($guid);
    $node = node_load(1);
    $this
      ->assertTrue(is_object($node), 'Pulled story and created node.');
    $this
      ->assertFalse($node->status, 'Embargoed story is unpublished.');
    pmpapi_remove($doc->attributes->guid);
  }

  /**
   * Checks for status = 0 on pulled expired story.
   */
  public function testPmpAPIPullExpiredStory() {
    $this
      ->createContentType();
    $valid = new stdClass();
    $valid->from = pmpapi_convert_timestamp(strtotime('200 days ago', REQUEST_TIME));
    $valid->to = pmpapi_convert_timestamp(strtotime('100 days ago', REQUEST_TIME));
    $values = array(
      'profile' => 'story',
      'attributes' => array(
        'valid' => $valid,
        'title' => $this
          ->randomName(20),
      ),
    );
    $doc = pmpapi_send($values);
    $this
      ->assertTrue(is_object($doc), 'Successfully pushed a story to the PMP.');
    sleep(self::SLEEP_TIME);
    $guid = $doc->attributes->guid;
    pmpapi_pull_pull_doc($guid);
    $node = node_load(1);
    $this
      ->assertTrue(is_object($node), 'Pulled story and created node.');
    $this
      ->assertFalse($node->status, 'Expired story is unpublished.');
    pmpapi_remove($doc->attributes->guid);
  }

  /**
   * Checks that node is published when it becomes valid.
   */
  public function testPmpAPIEmbargoedThenValid() {
    variable_set('pmpapi_pull_last_validity_check', time() - 100);
    $this
      ->createContentType();

    // Send up a doc (but not a node)
    $valid = new stdClass();
    $now = time();
    $valid->from = pmpapi_convert_timestamp(strtotime('+30 seconds', $now), $now);
    $values = array(
      'profile' => 'story',
      'attributes' => array(
        'valid' => $valid,
        'title' => $this
          ->randomName(20),
      ),
    );
    $doc = pmpapi_send($values);
    $this
      ->assertTrue(is_object($doc), 'Successfully pushed a doc to the PMP.');
    sleep(self::SLEEP_TIME);

    // Pull that (embargoed) doc
    $guid = $doc->attributes->guid;
    pmpapi_pull_pull_doc($guid);
    $node = node_load(1);
    $this
      ->assertTrue(is_object($node), 'Pulled doc and created node.');
    $this
      ->assertFalse($node->status, 'Embargoed node is unpublished.');

    // Wait until doc is valid, and run check function
    sleep(45);
    pmpapi_pull_check_valid_dates();
    $valid_node = node_load(1, NULL, TRUE);
    $this
      ->assertTrue($valid_node->status, 'Now-valid node is published.');
    pmpapi_remove($doc->attributes->guid);
  }

  /**
   * Checks that node is unpublished when it becomes expired.
   */
  public function testPmpAPIValidThenExpired() {
    variable_set('pmpapi_pull_last_validity_check', time() - 100);
    $this
      ->createContentType();

    // Send up a doc (but don't create a node)
    $valid = new stdClass();
    $now = time();
    $valid->from = pmpapi_convert_timestamp(strtotime('-1 day', $now), $now);
    $valid->to = pmpapi_convert_timestamp(strtotime('+30 seconds', $now), $now);
    $values = array(
      'profile' => 'story',
      'attributes' => array(
        'valid' => $valid,
        'title' => $this
          ->randomName(20),
      ),
    );
    $doc = pmpapi_send($values);
    $this
      ->assertTrue(is_object($doc), 'Successfully pushed a doc to the PMP.');
    sleep(self::SLEEP_TIME);

    // Pull that (valid) doc
    $guid = $doc->attributes->guid;
    pmpapi_pull_pull_doc($guid);
    $node = node_load(1);
    $this
      ->assertTrue(is_object($node), 'Pulled doc and created node.');
    $this
      ->assertTrue($node->status, 'Valid node is published.');

    // Wait until doc is expired, and run check function
    sleep(45);
    pmpapi_pull_check_valid_dates();
    $valid_node = node_load(1, NULL, TRUE);
    $this
      ->assertFalse($valid_node->status, 'Now-expired node is unpublished.');
    pmpapi_remove($doc->attributes->guid);
  }

  /**
   * 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;
  }

}

Classes

Namesort descending Description
PMPAPIPullWebTestCase Tests the functionality of the PMPAPI pull module.