You are here

pmpapi_permissions.test in Public Media Platform API Integration 7

File

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

/**
 * Tests the functionality of the PMPAPI permissions module.
 */
class PMPAPIPermissionsWebTestCase extends DrupalWebTestCase {
  protected $privileged_user;
  protected $testOrgGUID;
  protected $testGroupGUID;
  protected $files = array();
  protected $nodetype = 'testnode';
  const SLEEP_TIME = 1;

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

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

    // 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']);
    variable_set('pmpapi_user_id', $vars['user_id']);
    $this
      ->createTestOrg();
    $this
      ->createTestGroup();

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

    // Set up node mapping
    $node_settings = array(
      'entity_type' => 'node',
      'bundle_name' => $this->nodetype,
      'target_profile' => 'story',
      'mapping' => array(
        'title' => 'title',
        'body' => 'contentencoded',
      ),
    );
    $this
      ->setUpEntityMapping($node_settings);
  }
  public function tearDown() {
    pmpapi_remove($this->testOrgGUID);
    pmpapi_remove($this->testGroupGUID);
    parent::tearDown();
  }

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

  /**
   * Pushes a node to the PMP.
   */
  public function testPmpAPIPermissionsPushNodeWhitelistAll() {
    $this
      ->createContentType();
    $node = $this
      ->createNodeWithPermissions();
    $this
      ->assertTrue($node, 'Created test node.');
    $guid = $node->pmpapi_guid;
    sleep(self::SLEEP_TIME);
    $pmp = pmpapi_fetch($guid);
    $doc = $pmp->query->results->json;
    $this
      ->assertTrue(empty($pmp->errors['query']), 'Pushed node to PMP.');
    $this
      ->assertTrue(empty($doc->links->permission), 'No permissions attached to node (== `whitelist, ALL`)');
    node_delete($node->nid);
  }

  /**
   * Pushes a node to the PMP.
   */
  public function testPmpAPIPermissionsPushNodeWhitelistGroup() {
    $this
      ->createContentType();
    $node = $this
      ->createNodeWithPermissions($this->testGroupGUID);
    $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.');
    $doc = $pmp->query->results->json;
    $perm_href = variable_get('pmpapi_base_url') . '/docs/' . $this->testGroupGUID;
    $perm_ok = !empty($doc->links->permission) && !empty($doc->links->permission[0]->href) && $doc->links->permission[0]->href == $perm_href && empty($doc->links->permission[0]->blacklist);
    $this
      ->assertTrue($perm_ok, '`Whitelist group` permission attached to node.');
    node_delete($node->nid);
  }

  /**
   * Pushes a node to the PMP.
   */
  public function testPmpAPIPermissionsPushNodeWhitelistOnlyUser() {
    $this
      ->createContentType();
    $user_id = variable_get('pmpapi_user_id');
    $node = $this
      ->createNodeWithPermissions($user_id);
    $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.');
    $doc = $pmp->query->results->json;
    $perm_href = variable_get('pmpapi_base_url') . '/docs/' . $user_id;
    $perm_ok = !empty($doc->links->permission) && !empty($doc->links->permission[0]->href) && $doc->links->permission[0]->href == $perm_href && empty($doc->links->permission[0]->blacklist);
    $this
      ->assertTrue($perm_ok, '`Whitelist Only Current User` permission attached to node.');
    node_delete($node->nid);
  }

  /**
   * 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;
  }
  protected function createNodeWithPermissions($group = NULL) {
    $settings = array();
    $permission = pmp_permissions_create_permission($group);
    if ($permission !== NULL) {
      $settings['pmpapi_permissions'] = array(
        $permission,
      );
    }
    return $this
      ->createNode($settings);
  }
  protected function createTestOrg() {
    $values = array(
      'profile' => 'organization',
      'attributes' => array(
        'title' => variable_get('site_name') . ' SimpleTest test org: ' . date('G:i:s'),
      ),
    );
    $org = pmpapi_send($values);
    $this
      ->assertTrue(is_object($org), 'Test group successfully created.');
    $this->testOrgGUID = !empty($org->attributes->guid) ? $org->attributes->guid : '';
  }
  protected function createTestGroup() {
    $values = array(
      'profile' => 'group',
      'attributes' => array(
        'title' => variable_get('site_name') . ' SimpleTest test group: ' . date('G:i:s'),
      ),
    );
    $item = new stdClass();
    $item->href = variable_get('pmpapi_base_url') . '/docs/' . $this->testOrgGUID;
    $values['items'][] = $item;
    $group = pmpapi_send($values);
    $this
      ->assertTrue(is_object($group), 'Test group successfully created.');
    $this->testGroupGUID = !empty($group->attributes->guid) ? $group->attributes->guid : '';
  }

}

Classes

Namesort descending Description
PMPAPIPermissionsWebTestCase Tests the functionality of the PMPAPI permissions module.