You are here

class ViewsLazyLoadTest in Views Lazy Load 8

Same name and namespace in other branches
  1. 7 views_lazy_load.test \ViewsLazyLoadTest

Test the basic lazy loading of views.

Hierarchy

Expanded class hierarchy of ViewsLazyLoadTest

File

./views_lazy_load.test, line 11
Contains ViewsLazyLoadTest

View source
class ViewsLazyLoadTest extends DrupalWebTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Views Lazy Load',
      'group' => 'Views Lazy Load',
      'description' => 'Tests the lazy loading of views displays.',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp(array(
      'views_lazy_load',
    ));
  }

  /**
   * Tests the basics of lazy loading.
   */
  public function testLazyLoading() {

    // Create a views using a page.
    $view = $this
      ->getTestView();

    // Render the view, everything should be normal, no lazy loading.
    $output = $view
      ->preview();
    $this
      ->assertNotContains('Loading...', $output, 'The view output does not have the lazy loading text.');

    // Update the view to be lazy, re-render and expect the exact loading
    // indicator, using theme_views_lazy_load_throbber to ensure no incorrectly
    // escaped HTML.
    $view = $this
      ->getTestView(TRUE);
    $output = $view
      ->preview('page');
    $this
      ->assertContains(theme('views_lazy_load_throbber'), $output, 'The view output has the lazy loading text.');

    // Assert the view display to make sure we're getting the right display.
    $this
      ->assertContains('view-display-id-page', $output, 'Correct display title');

    // Make sure AJAX is forced on with lazy loading.
    $view = $this
      ->getTestView(TRUE);
    $view
      ->preview();
    $this
      ->assertTrue($view->use_ajax, 'View uses AJAX whenever Views Lazy Load is enabled.');

    // We can use a GET parameter to disable the lazy loading which is used on
    // the second pass.
    $_GET['views_lazy_load_disabled'] = TRUE;
    $view = $this
      ->getTestView(TRUE);
    $output = $view
      ->preview();
    $this
      ->assertNotContains('Loading...', $output, 'The view output does not have the lazy loading text.');

    // Make sure that our view won't be lazy under a crawler robot visit
    // Set our agent to some robot's agent:
    unset($_GET['views_lazy_load_disabled']);
    $_SERVER['HTTP_USER_AGENT'] = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
    $view = $this
      ->getTestView(TRUE);
    $output = $view
      ->preview();
    $this
      ->assertNotContains('Loading...', $output, 'The view output does not have the lazy loading text because of the webcrawler\'s agent.');
  }

  /**
   * Gets a test view.
   *
   * @param bool $lazy
   *   TRUE if we want our lazy loading enabled.
   * @param bool $initialized
   *   TRUE if we want the view setup otherwise FALSE.
   *
   * @return \view
   *   The view object.
   */
  protected function getTestView($lazy = FALSE, $initialized = TRUE) {
    views_include('view');
    $view = new view();
    $view->name = 'test_view';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'node';
    $view->human_name = 'Test View';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE;

    /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view
      ->new_display('default', 'Master', 'default');
    $handler->display->display_options['title'] = 'Test View';
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['pager']['options']['items_per_page'] = '10';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'node';

    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    $handler->display->display_options['fields']['title']['label'] = '';
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;

    /* Sort criterion: Content: Post date */
    $handler->display->display_options['sorts']['created']['id'] = 'created';
    $handler->display->display_options['sorts']['created']['table'] = 'node';
    $handler->display->display_options['sorts']['created']['field'] = 'created';
    $handler->display->display_options['sorts']['created']['order'] = 'DESC';

    /* Filter criterion: Content: Published */
    $handler->display->display_options['filters']['status']['id'] = 'status';
    $handler->display->display_options['filters']['status']['table'] = 'node';
    $handler->display->display_options['filters']['status']['field'] = 'status';
    $handler->display->display_options['filters']['status']['value'] = 1;
    $handler->display->display_options['filters']['status']['group'] = 1;
    $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;

    /* Filter criterion: Content: Type */
    $handler->display->display_options['filters']['type']['id'] = 'type';
    $handler->display->display_options['filters']['type']['table'] = 'node';
    $handler->display->display_options['filters']['type']['field'] = 'type';
    $handler->display->display_options['filters']['type']['value'] = array(
      'page' => 'page',
    );

    /* Display: Page */
    $handler = $view
      ->new_display('page', 'Page', 'page');
    $handler->display->display_options['path'] = 'test-view';

    // If we've enabled lazy loading, set the appropriate settings.
    if ($lazy) {
      $handler->display->display_options['views_lazy_load_enabled'] = 1;
    }

    // Setup the view object ready to go.
    if ($initialized) {
      $view
        ->set_display($handler->display->id);
      $view
        ->init_handlers();
    }
    return $view;
  }

  /**
   * Asserts a string does exist in the haystack.
   *
   * @param string $needle
   *   The string to search for.
   * @param string $haystack
   *   The string to search within.
   * @param string $message
   *   The message to log.
   *
   * @return bool
   *   TRUE if it was found otherwise FALSE.
   */
  protected function assertContains($needle, $haystack, $message = '') {
    return $this
      ->assertTrue(stripos($haystack, $needle) !== FALSE, $message);
  }

  /**
   * Asserts a string does not exist in the haystack.
   *
   * @param string $needle
   *   The string to search for.
   * @param string $haystack
   *   The string to search within.
   * @param string $message
   *   The message to log.
   *
   * @return bool
   *   TRUE if it was not found otherwise FALSE.
   */
  protected function assertNotContains($needle, $haystack, $message = '') {
    return $this
      ->assertTrue(stripos($haystack, $needle) === FALSE, $message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ViewsLazyLoadTest::assertContains protected function Asserts a string does exist in the haystack.
ViewsLazyLoadTest::assertNotContains protected function Asserts a string does not exist in the haystack.
ViewsLazyLoadTest::getInfo public static function
ViewsLazyLoadTest::getTestView protected function Gets a test view.
ViewsLazyLoadTest::setUp public function
ViewsLazyLoadTest::testLazyLoading public function Tests the basics of lazy loading.