You are here

views_limit_grouping.test in Views Grouping Row Limit 7

Contains test for views_limit_grouping module.

File

tests/views_limit_grouping.test
View source
<?php

/**
 * @file
 * Contains test for views_limit_grouping module.
 */

/**
 * Functional test class for views_limit_grouping.
 */
class ViewsLimitGroupingTests extends DrupalWebTestCase {
  const CONTENT_TYPE_AMOUNT = 3;
  const NODES_PER_CONTENT_TYPE = 10;
  private $content_types = array();
  private $nodes = array();

  /**
   * Overrides parent::getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Functional tests',
      'description' => 'Test that views are properly limited in the UI.',
      'group' => 'Views Limit Grouping',
    );
  }

  /**
   * Overrides parent::setUp().
   *
   * Create our initial content types and nodes.
   */
  function setUp() {

    // Enable our test module. It contains all of our default views.
    parent::setUp('views_limit_grouping_test');
    for ($i = 1; $i <= self::CONTENT_TYPE_AMOUNT; $i++) {
      $type = $this
        ->drupalCreateContentType(array(
        'type' => 'content_type_' . $i,
        'name' => 'Content Type ' . $i,
      ));
      $this->content_types[$type->type] = $type;
    }
    $this
      ->assertEqual(count($this->content_types), self::CONTENT_TYPE_AMOUNT, t('Content types created'));

    // Make a bunch of nodes for each content type.
    foreach (array_keys($this->content_types) as $type) {
      for ($i = 0; $i < self::NODES_PER_CONTENT_TYPE; $i++) {
        $this->nodes[$type][] = $this
          ->drupalCreateNode(array(
          'type' => $type,
        ));
      }
      $this
        ->assertEqual(count($this->nodes[$type]), self::NODES_PER_CONTENT_TYPE, t('Nodes for content type %type created', array(
        '%type' => $this->content_types[$type]->name,
      )));
    }
  }

  /**
   * Test a single level of grouping.
   */
  function testSimpleGroupingLimit() {
    $element = $this
      ->getViewContainer('group_by_content_type');

    // Make sure there is a group for each content type.
    $this
      ->assertEqual($element
      ->count(), self::CONTENT_TYPE_AMOUNT, 'There is a grouping for each content type');

    // Make sure each grouping has the correct number of nodes (2, in this
    // case). Each group should also have a header, so we check for a count of
    // 3 instead (nodes + header).
    $limited = TRUE;
    foreach ($element
      ->children() as $grouping) {
      if (count($grouping
        ->children()) !== 3) {
        $limited = FALSE;
        break;
      }
    }
    $this
      ->assertTrue($limited, 'Each of the groupings has been limited to 2 nodes and a header.');
  }

  /**
   * Test a single level of grouping with offset.
   */
  function testSimpleGroupingLimitWithOffset() {
    $this
      ->drupalGet('group-by-content-type-with-offset');

    // Get the nodes manually so that we can tell if the offset happened.
    $nodes = db_query('SELECT type, title FROM {node} ORDER BY created')
      ->fetchAll();
    $groups = array();
    foreach ($nodes as $node) {
      $groups[$node->type][] = $node->title;
    }
    foreach ($groups as $type => $titles) {
      $this
        ->assertNoText($titles[0], 'The first item was not present on the page');
      $this
        ->assertText($titles[1], 'The second item was present on the page');
    }
  }

  /**
   * Helper function to get the HTML container of the view on a certain page.
   *
   * @param string $view_name
   *   The name of the view.
   *
   * @return SimpleXMLElement
   *   A SimpleXMLElement representing the view container div.
   */
  function getViewContainer($view_name) {
    $view_name = str_replace('_', '-', $view_name);

    // The path for all of the views in our test cases is the same as the view
    // name.
    $this
      ->drupalGet($view_name);
    $elements = $this
      ->xpath('//div[contains(string(@class),"view-' . $view_name . '")]/div[@class="view-content"]');
    $this
      ->assertEqual(count($elements), 1, t('The view container div was found.'));
    return $elements[0];
  }

  /**
   * Overrides parent::tearDown().
   */
  function tearDown() {
    parent::tearDown();
    $types = array_keys($this->content_types);

    // Delete the nodes we created.
    $nids = db_query('SELECT nid FROM {node} WHERE type IN (:types)', array(
      ':types' => $types,
    ))
      ->fetchCol();
    node_delete_multiple($nids);

    // Delete the content types.
    foreach ($types as $type) {
      node_type_delete($type);
    }

    // Reset our variables.
    $this->content_types = $this->nodes = array();
  }

}

Classes

Namesort descending Description
ViewsLimitGroupingTests Functional test class for views_limit_grouping.