You are here

views_atom.test in Views Atom 6

Simple tests for views_atom

File

tests/views_atom.test
View source
<?php

/**
 * @file
 * Simple tests for views_atom
 */

/**
 * Tests basic set up for publishing and consuming
 */
class ViewsAtomTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Views Atom',
      'description' => 'Ensure that the views_atom module functions properly.',
      'group' => 'views_atom',
    );
  }
  public function setUp() {
    parent::setUp('autoload', 'content', 'node', 'views', 'views_atom', 'views_atom_test');

    // Build the list of required administration permissions. Additional
    // permissions can be passed as an array into setUp()'s second parameter.
    if (isset($args[1]) && is_array($args[1])) {
      $permissions = $args[1];
    }
    else {
      $permissions = array();
    }
    $permissions[] = 'access content';
    $permissions[] = 'administer site configuration';
    $permissions[] = 'administer content types';
    $permissions[] = 'administer nodes';
    $permissions[] = 'administer taxonomy';
    $permissions[] = 'administer users';

    // Create an admin user and log in.
    $user = $this->admin_user = $this
      ->drupalCreateUser($permissions);
    $this
      ->drupalLogin($this->admin_user);

    // Create a few content types
    $num_node_types = rand(4, 16);
    $i = 0;
    while ($i <= $num_node_types) {
      $i++;
      $node_type = new stdClass();
      $node_type->type = $this
        ->randomName(8);
      $node_type->name = $this
        ->randomName(8);
      $node_type->module = 'views_atom';
      $node_type->description = $this
        ->randomName(140);
      $node_type->has_title = 1;
      $node_type->title_label = 'title';
      $node_type->has_body = 1;
      $node_type->body_label = 'body';
      $node_type->min_word_count = 0;
      $node_type->help = '';
      $node_type->custom = TRUE;
      $node_type->modified = TRUE;
      $node_type->locked = FALSE;
      $node_type->orig_type = $node_type->type;
      node_type_save($node_type);
    }
    $node_types = node_get_types('names');
  }

  /**
   * views_atom tests
   */
  public function test() {

    // Create many dummy nodes
    $nodes = array();
    foreach (range(1, 10) as $count) {
      $nodes[] = $this
        ->vaNewNode();
    }

    // Get feed and ensure the nodes are there
    $feed_path = 'views_atom_test/feed.xml';
    $feed = $this
      ->vaFetchFeed($feed_path);
    foreach ($nodes as $node) {
      $this
        ->assertTrue($this
        ->vaNodeInFeed($node->nid, $feed_path), 'Node exists in feed.');
      foreach ($feed['items'] as $item) {
        if ($item['rdf']['nid'] == $node->nid) {
          $rdf = $item['rdf'];
          $this
            ->assertEqual($rdf['title'], $node->title, 'Title set correctly.');
          $this
            ->assertEqual($rdf['type'], $node->type, 'Node type set correctly.');
          $this
            ->assertEqual($rdf['body'], $node->body, 'Body set correctly.');
        }
      }
    }
  }

  /**
   * Gathers a random number of node types of the given publish status
   *
   * @return array of machine names
   */
  public function vaRandomNodeTypes() {
    $node_types = node_get_types('names');
    $node_types = array_keys($node_types);
    $count = count($node_types) - 1;
    $random_keys = array_rand($node_types, rand(1, $count));
    $random_node_types = array();
    if (is_array($random_keys)) {
      foreach ($random_keys as $key) {
        $random_node_types[] = $node_types[$key];
      }
    }
    else {
      $random_node_types[] = $node_types[$random_keys];
    }
    return $random_node_types;
  }

  /**
   * Picks one random node type of the given type
   *
   * @return machine name of a node type
   */
  public function vaRandomNodeType() {
    $random_node_types = $this
      ->vaRandomNodetypes();
    $random_key = array_rand($random_node_types);
    return $random_node_types[$random_key];
  }

  /**
   * Creates a random new node
   *
   * @param $node_type
   *   Machine name of the type of node to create
   * @param $options
   *   Other options to apply before creating the node
   * @return object of the new node
   */
  public function vaNewNode($node_type = NULL, $options = array()) {
    $node_type = $node_type ? $node_type : $this
      ->vaRandomNodeType();
    $edit = array();
    $edit['type'] = $node_type;
    $edit['title'] = $this
      ->randomName(8);
    $edit['body'] = $this
      ->randomName(300);
    $edit['status'] = 1;
    foreach ($options as $key => $value) {
      $edit[$key] = $value;
    }
    return $this
      ->drupalCreateNode($edit);
  }

  /**
   * Fetch an atom+rdf feed of a given path
   *
   * @param $path
   *   Path of the feed
   * @return array of the feed's values
   */
  public function vaFetchFeed($path) {
    cache_clear_all('*', 'cache_views', TRUE);
    cache_clear_all('*', 'cache_views_data', TRUE);
    module_load_include('inc', 'feeds_atom', 'libraries/atomrdf_parser');
    $feed = $this
      ->drupalGet($path);
    $this
      ->assertResponse(200, 'Feed exists');
    $feed = atomrdf_parser_parse($feed);
    $this
      ->assertEqual($feed['link'], url($path, array(
      'absolute' => TRUE,
    )), 'Feed seems to output correctly.');
    if (is_array($feed) && isset($feed['items'])) {
      return $feed;
    }
    else {
      return array();
    }
  }

  /**
   * Checks to see if a node is in a given feed
   *
   * @param $nid
   *   Node id of the node to check
   * @param $path
   *   Path of the feed
   * @return bool whether the node exists in the feed or not
   */
  public function vaNodeInFeed($nid, $path) {
    $feed = $this
      ->vaFetchFeed($path);
    if (!isset($feed['items'])) {
      return FALSE;
    }
    foreach ($feed['items'] as $item) {
      if (isset($item['rdf']) && isset($item['rdf']['nid']) && $item['rdf']['nid'] == $nid) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Get a random publish status
   *
   * @return publish status value
   */
  public function vaRandomPublishStatus() {
    $statuses = array(
      SYNDICATOR_PUBLISH_NO,
      SYNDICATOR_PUBLISH_YES,
    );
    $random_key = array_rand($statuses);
    return $statuses[$random_key];
  }

}

Classes

Namesort descending Description
ViewsAtomTestCase Tests basic set up for publishing and consuming