You are here

views_block_filter_block.test in Views Block Exposed Filter Blocks 7

Tests for the Views Block Exposed Filter Block module.

File

tests/views_block_filter_block.test
View source
<?php

/**
 * @file
 * Tests for the Views Block Exposed Filter Block module.
 */

/**
 * Base/helper class for Views Block Filter Block tests.
 */
class ViewsBlockFilterBlockBase extends DrupalWebTestCase {
  function setUp() {
    $modules = func_get_args();
    if (isset($modules[0]) && is_array($modules[0])) {
      $modules = $modules[0];
    }

    // Enable views_ui.
    parent::setUp($modules + array(
      'block',
      'node',
      'views',
      'views_block_filter_block',
    ));

    // Create and log in a user with administer views permission.
    $this->adminUser = $this
      ->drupalCreateUser(array(
      'administer modules',
      'administer views',
      'administer blocks',
      'bypass node access',
      'access user profiles',
      'view revisions',
    ));
    $this
      ->drupalLogin($this->adminUser);
    variable_set('clean_url', TRUE);
  }

  /**
   * Click a link to perform an operation on a view.
   *
   * In general, we expect lots of links titled "enable" or "disable" on the
   * various views listing pages, and they might have tokens in them. So we
   * need special code to find the correct one to click.
   *
   * @param $label
   *   Text between the anchor tags of the desired link.
   * @param $unique_href_part
   *   A unique string that is expected to occur within the href of the desired
   *   link. For example, if the link URL is expected to look like
   *   "admin/structure/views/view/frontpage/...", then "/frontpage/" could be
   *   passed as the expected unique string.
   *
   * @return
   *   The page content that results from clicking on the link, or FALSE on
   *   failure. Failure also results in a failed assertion.
   */
  protected function clickViewsOperationLink($label, $unique_href_part) {
    $links = $this
      ->xpath('//a[normalize-space(text())=:label]', array(
      ':label' => $label,
    ));
    foreach ($links as $link_index => $link) {
      $position = strpos($link['href'], $unique_href_part);
      if ($position !== FALSE) {
        $index = $link_index;
        break;
      }
    }
    $this
      ->assertTrue(isset($index), t('Link to "@label" containing @part found.', array(
      '@label' => $label,
      '@part' => $unique_href_part,
    )));
    if (isset($index)) {
      return $this
        ->clickLink($label, $index);
    }
    else {
      return FALSE;
    }
  }

}

/**
 * Tests that block views can be created with exposed filter blocks via the
 * Views UI.
 */
class ViewsBlockFilterBlockBasicUICase extends ViewsBlockFilterBlockBase {
  public static function getInfo() {
    return array(
      'name' => 'Views Block Filter Block UI Tests',
      'description' => 'Test creating a block view and ensure the exposed filter block option exists and is saved.',
      'group' => 'VBFB',
    );
  }
  function setUp() {
    parent::setUp(array(
      'views_ui',
    ));
  }
  public function testCreateBlockViewAndSave() {

    // Create a simple and not at all useful view.
    $view = array();
    $view['human_name'] = $this
      ->randomName(16);
    $view['name'] = strtolower($this
      ->randomName(16));
    $view['description'] = $this
      ->randomName(16);
    $view['page[create]'] = FALSE;
    $view['block[create]'] = TRUE;
    $this
      ->drupalPost('admin/structure/views/add', $view, t('Continue & edit'));

    // Assert that the "exposed form in block" text exists; click and submit it.
    $this
      ->assertText('Exposed form in block', 'Exposed form in block text exists.');
    $this
      ->clickViewsOperationLink('No', '/exposed_block');
    $this
      ->drupalPost(NULL, array(
      'exposed_block' => TRUE,
    ), 'Apply');

    // Save the view.
    $this
      ->drupalPost(NULL, array(), 'Save');

    // Ensure the view exists on the Views overview page.
    $this
      ->drupalGet('admin/structure/views');
    $this
      ->assertText($view['human_name'], 'View saved successfully.');

    // Ensure the exposed block edit value was saved.
    $this
      ->clickViewsOperationLink('Edit', $view['name'] . '/edit');
    $this
      ->clickViewsOperationLink('Yes', '/exposed_block');

    // Ensure the exposed form block exists.
    $this
      ->drupalGet('/admin/structure/block');
    $this
      ->assertText('Exposed form: ' . $view['name'] . '-block', 'Exposed form block created successfully.');
  }

}

/**
 * Tests exposed block placement visibility and basic functionality.
 */
class ViewsBlockFilterBlockExposedBlockCase extends ViewsBlockFilterBlockBase {
  public static function getInfo() {
    return array(
      'name' => 'Views Block Filter Exposed Block Tests',
      'description' => 'Test creating placing and using an exposed filter block for a block display View.',
      'group' => 'VBFB',
    );
  }
  function setUp() {
    parent::setUp(array(
      'views_block_filter_block_test',
    ));

    // Clear all caches to ensure Views/blocks have been loaded.
    drupal_flush_all_caches();

    // Move View blocks into visible regions.
    db_update('block')
      ->fields(array(
      'region' => 'content',
      'status' => 1,
    ))
      ->condition('module', 'views')
      ->execute();
  }
  public function testPlacedBlock() {

    // Visit the front page, where the filter block should appear.
    $this
      ->drupalGet('node');

    // Ensure the configured filters are exposed.
    $this
      ->assertFieldByName('status', 1, 'Found the exposed filter block.');

    // Ensure the form action points to the absolute URL of the current page.
    $expected_url = $this
      ->getAbsoluteUrl('node');
    $form = $this
      ->xpath($this
      ->buildXPathQuery('//form[@id=:id]', array(
      ':id' => 'views-exposed-form-test-view-block',
    )));
    $action = (string) $form[0]['action'];
    $this
      ->assertEqual($action, $expected_url, 'The exposed filter block action is set correctly.');

    // Try using the exposed filter form.
    $this
      ->drupalGet($action . '?status=0');

    // Ensure the value was changed.
    $this
      ->assertFieldByName('status', 0, 'The exposed filter form worked as expected.');
  }
  public function testModuleCleanup() {

    // For sanity, ensure we have a block to begin with.
    $select = db_select('block')
      ->fields('block')
      ->condition('delta', '-exp-test_view-block')
      ->execute();
    $rows = $select
      ->fetchAllAssoc('delta');
    $this
      ->assertIdentical(count($rows), 1, t('Sanity check: exposed filter block exists in DB.'));

    // Disable the module.
    $edit = array(
      'modules[Views][views_block_filter_block][enable]' => FALSE,
    );
    $this
      ->drupalPost('admin/modules', $edit, t('Save configuration'));

    // Ensure the exposed filter block was removed on module disable.
    $select = db_select('block')
      ->fields('block')
      ->condition('delta', '-exp-test_view-block')
      ->execute();
    $rows = $select
      ->fetchAllAssoc('delta');
    $this
      ->assertIdentical(count($rows), 0, t('Module disable removed exposed filter block.'));
  }

}

Classes

Namesort descending Description
ViewsBlockFilterBlockBase Base/helper class for Views Block Filter Block tests.
ViewsBlockFilterBlockBasicUICase Tests that block views can be created with exposed filter blocks via the Views UI.
ViewsBlockFilterBlockExposedBlockCase Tests exposed block placement visibility and basic functionality.