You are here

views_navigation.test in Views navigation 7

Test class for Views Navigation.

File

tests/views_navigation.test
View source
<?php

/**
 * @file
 * Test class for Views Navigation.
 */

/**
 * Class ViewsNavigationTestCase
 */
class ViewsNavigationTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return [
      'name' => 'Views navigation tests',
      'description' => 'Some tests for the Views navigation module.',
      'group' => 'Views navigation',
    ];
  }

  // Based on ViewsUIWizardHelper.
  public function setUp() {
    parent::setUp([
      'views_ui',
      'views_navigation',
    ]);
    $views_admin = $this
      ->drupalCreateUser([
      'administer views',
      'administer blocks',
      'bypass node access',
      'access user profiles',
      'view revisions',
    ]);
    $this
      ->drupalLogin($views_admin);

    // Create some nodes.
    $this->nodes = [];
    for ($i = 0; $i < 11; $i++) {
      $node = $this
        ->drupalCreateNode([
        'promote' => 1,
      ]);
      $this->nodes[$node->nid] = $node;
    }

    // Enable the frontpage view.
    $this
      ->drupalGet('admin/structure/views');
    $enable_link = reset($this
      ->xpath('//a[starts-with(@href,:href)]', [
      ':href' => '/admin/structure/views/view/frontpage/enable',
    ]));
    $enable_url = $this
      ->getAbsoluteUrl($enable_link['href']);
    $this
      ->drupalGet($enable_url);

    // Display the title field only.
    $edit = [];
    $edit['row_plugin'] = 'fields';
    $this
      ->drupalPost('admin/structure/views/nojs/display/frontpage/page/row_plugin', $edit, 'Apply');
    $this
      ->drupalPost($this
      ->getUrl(), [], 'Apply');
    $edit = [];
    $edit['name[node.title]'] = TRUE;
    $this
      ->drupalPost('admin/structure/views/nojs/add-item/frontpage/page/field', $edit, 'Add and configure fields');
    $this
      ->drupalPost($this
      ->getUrl(), [], 'Apply');
    $this
      ->drupalPost('admin/structure/views/view/frontpage/edit', [], 'Save');
  }
  public function testViewsNavigation() {

    // Enable simple views navigation.
    $view_edit_url = 'admin/structure/views/view/frontpage/edit';
    $this
      ->drupalGet($view_edit_url);
    $settings_link = reset($this
      ->xpath('//a[@id=:id]', [
      ':id' => 'views-page-views-navigation',
    ]));
    $settings_url = $this
      ->getAbsoluteUrl($settings_link['href']);
    $edit = [];
    $edit['views_navigation'] = TRUE;
    $this
      ->drupalPost($settings_url, $edit, 'Apply');
    $this
      ->drupalPost($view_edit_url, [], 'Save');

    // Check simple views navigation.
    $this
      ->drupalGet('frontpage');
    $node_links = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links));
    $this
      ->clickLink('Next');
    $this
      ->assertTitle($node_links[1] . ' | Drupal', 'Next from first item leads to second item.');
    $this
      ->clickLink('Next');
    $this
      ->assertTitle($node_links[2] . ' | Drupal', 'Next from second item leads to third item.');
    $this
      ->clickLink('Previous');
    $this
      ->assertTitle($node_links[1] . ' | Drupal', 'Previous from third item leads to second item.');
    $this
      ->drupalGet('frontpage');
    $this
      ->clickLink((string) end($node_links));
    $this
      ->clickLink('Next');
    $last_title = (string) current($this
      ->xpath('//title'));
    $this
      ->drupalGet($this
      ->getAbsoluteUrl('frontpage?page=1'));
    $node_links1 = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->assertEqual($last_title, reset($node_links1) . ' | Drupal', 'Next from last item leads to first item of next page.');
    $this
      ->clickLink((string) reset($node_links1));
    $this
      ->clickLink('Previous');
    $this
      ->assertTitle(end($node_links) . ' | Drupal', 'Previous from first item of second page leads to last item of first page.');

    // Check several field handlers.
    $edit = [];

    // Simple link to the node.
    $edit['name[views_entity_node.view_node]'] = TRUE;

    // Node path (can be useful as a token).
    $edit['name[node.path]'] = TRUE;

    // We won't check image fields for now, it's much work.
    $this
      ->drupalPost('admin/structure/views/nojs/add-item/frontpage/page/field', $edit, 'Add and configure fields');
    $this
      ->drupalPost($this
      ->getUrl(), [], 'Apply');
    $this
      ->drupalPost($this
      ->getUrl(), [], 'Apply');
    $this
      ->drupalPost($view_edit_url, [], 'Save');

    // Check they work.
    $this
      ->drupalGet('frontpage');
    $node_paths = $this
      ->xpath('//div[@class=:class1]/span[@class=:class2]', [
      ':class1' => 'views-field views-field-path',
      ':class2' => 'field-content',
    ]);
    $this
      ->verbose(print_r($node_paths, 1));
    $this
      ->clickLink('view');
    $this
      ->assertLink('Next', 0, 'The simple node view link field is correctly handled.');
    $this
      ->drupalGet($this
      ->getAbsoluteUrl(reset($node_paths)));
    $this
      ->assertLink('Next', 0, 'The node path field is correctly handled.');

    // Check back link with and without view's title.
    $edit = [];
    $edit['views_navigation_back'] = TRUE;
    $this
      ->drupalPost($settings_url, $edit, 'Apply');
    $this
      ->drupalPost($view_edit_url, [], 'Save');
    $this
      ->drupalGet('frontpage');
    $node_links = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links));
    $this
      ->clickLink('Back');
    $this
      ->assertUrl('frontpage', [], "Back link from a first page's result leads back to the first view page.");
    $this
      ->drupalGet($this
      ->getAbsoluteUrl('frontpage?page=1'));
    $node_links1 = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links1));
    $this
      ->clickLink('Back');
    $this
      ->assertUrl('frontpage', [], "Back link from a second page's result leads back to the first view page.");
    $edit = [];
    $edit['views_navigation_title'] = TRUE;
    $this
      ->drupalPost($settings_url, $edit, 'Apply');
    $edit = [];
    $edit['title'] = 'My beautiful view';
    $this
      ->drupalPost('admin/structure/views/nojs/display/frontpage/page/title', $edit, 'Apply');
    $this
      ->drupalPost($view_edit_url, [], 'Save');
    $this
      ->drupalGet('frontpage');
    $node_links = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links));
    $back_links = $this
      ->xpath('//a[normalize-space(string())=:text]', [
      ':text' => 'Back to My beautiful view',
    ]);
    $url_target = $this
      ->getAbsoluteUrl($back_links[0]['href']);
    $this
      ->drupalGet($url_target);
    $this
      ->assertUrl('frontpage', [], "Back link with view's title works too.");

    // Check cycling navigation.
    $edit = [];
    $edit['views_navigation_cycle'] = TRUE;
    $this
      ->drupalPost($settings_url, $edit, 'Apply');
    $this
      ->drupalPost($view_edit_url, [], 'Save');
    $this
      ->drupalGet('frontpage');
    $node_links = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links));
    $first_title = (string) current($this
      ->xpath('//title'));
    $this
      ->drupalGet($this
      ->getAbsoluteUrl('frontpage?page=1'));
    $node_links = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) end($node_links));
    $this
      ->clickLink('Next');
    $this
      ->assertEqual($first_title, (string) current($this
      ->xpath('//title')), 'Next from last item of last page leads to first item of first page when cycling is on.');

    // Same checks in SEO mode. Reset the view.
    $edit = [];
    $edit['views_navigation_seo_first'] = TRUE;
    $edit['views_navigation_back'] = FALSE;
    $edit['views_navigation_title'] = FALSE;
    $edit['views_navigation_cycle'] = FALSE;
    $this
      ->drupalPost($settings_url, $edit, 'Apply');
    $this
      ->drupalPost('admin/structure/views/nojs/config-item/frontpage/page/field/view_node', [], 'Remove');
    $this
      ->drupalPost('admin/structure/views/nojs/config-item/frontpage/page/field/path', [], 'Remove');
    $this
      ->drupalPost($view_edit_url, [], 'Save');
    $this
      ->drupalGet('frontpage');
    $node_links = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links));
    $this
      ->clickLink((string) $node_links[1]);
    $this
      ->assertTitle($node_links[1] . ' | Drupal', "Second item's title as a link from first item leads to second item.");
    $this
      ->clickLink((string) $node_links[2]);
    $this
      ->assertTitle($node_links[2] . ' | Drupal', "Third item's title as a link from second item leads to third item.");
    $this
      ->clickLink((string) $node_links[1]);
    $this
      ->assertTitle($node_links[1] . ' | Drupal', "Second item's title as a link from third item leads to second item.");
    $this
      ->drupalGet($this
      ->getAbsoluteUrl('frontpage?page=1'));
    $node_links1 = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $last_title = (string) reset($node_links1);
    $this
      ->drupalGet('frontpage');
    $this
      ->clickLink((string) end($node_links));
    $this
      ->clickLink($last_title);
    $this
      ->assertEqual((string) current($this
      ->xpath('//title')), $last_title . ' | Drupal', 'A link from last item leads to first item of next page.');
    $this
      ->drupalGet($this
      ->getAbsoluteUrl('frontpage?page=1'));
    $this
      ->clickLink((string) reset($node_links1));
    $this
      ->clickLink((string) end($node_links));
    $this
      ->assertTitle(end($node_links) . ' | Drupal', 'A link from first item of second page leads to last item of first page.');

    // Check several field handlers.
    $edit = [];

    // Simple link to the node.
    $edit['name[views_entity_node.view_node]'] = TRUE;

    // Node path (can be useful as a token).
    $edit['name[node.path]'] = TRUE;

    // We won't check image fields for now, it's much work.
    $this
      ->drupalPost('admin/structure/views/nojs/add-item/frontpage/page/field', $edit, 'Add and configure fields');
    $this
      ->drupalPost($this
      ->getUrl(), [], 'Apply');
    $this
      ->drupalPost($this
      ->getUrl(), [], 'Apply');
    $this
      ->drupalPost($view_edit_url, [], 'Save');

    // Check they work.
    $this
      ->drupalGet('frontpage');
    $node_paths = $this
      ->xpath('//div[@class=:class1]/span[@class=:class2]', [
      ':class1' => 'views-field views-field-path',
      ':class2' => 'field-content',
    ]);
    $this
      ->verbose(print_r($node_paths, 1));
    $this
      ->clickLink('view');
    $this
      ->assertLink((string) $node_links[1], 0, 'The simple node view link field is correctly handled.');
    $this
      ->drupalGet($this
      ->getAbsoluteUrl(reset($node_paths)));
    $this
      ->assertLink((string) $node_links[1], 0, 'The node path field is correctly handled.');

    // Check back link with and without view's title.
    $edit = [];
    $edit['views_navigation_back'] = TRUE;
    $this
      ->drupalPost($settings_url, $edit, 'Apply');
    $this
      ->drupalPost($view_edit_url, [], 'Save');
    $this
      ->drupalGet('frontpage');
    $node_links = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links));
    $this
      ->clickLink('Back');
    $this
      ->assertUrl('frontpage', [], "Back link from a first page's result leads back to the first view page.");
    $this
      ->drupalGet($this
      ->getAbsoluteUrl('frontpage?page=1'));
    $node_links1 = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links1));
    $this
      ->clickLink('Back');
    $this
      ->assertUrl('frontpage', [], "Back link from a second page's result leads back to the first view page.");
    $edit = [];
    $edit['views_navigation_title'] = TRUE;
    $this
      ->drupalPost($settings_url, $edit, 'Apply');
    $edit = [];
    $edit['title'] = 'My beautiful view';
    $this
      ->drupalPost('admin/structure/views/nojs/display/frontpage/page/title', $edit, 'Apply');
    $this
      ->drupalPost($view_edit_url, [], 'Save');
    $this
      ->drupalGet('frontpage');
    $node_links = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links));
    $back_links = $this
      ->xpath('//a[normalize-space(string())=:text]', [
      ':text' => 'Back to My beautiful view',
    ]);
    $url_target = $this
      ->getAbsoluteUrl($back_links[0]['href']);
    $this
      ->drupalGet($url_target);
    $this
      ->assertUrl('frontpage', [], "Back link with view's title works too.");

    // Check cycling navigation.
    $edit = [];
    $edit['views_navigation_cycle'] = TRUE;
    $this
      ->drupalPost($settings_url, $edit, 'Apply');
    $this
      ->drupalPost($view_edit_url, [], 'Save');
    $this
      ->drupalGet('frontpage');
    $node_links = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) reset($node_links));
    $first_title = (string) current($this
      ->xpath('//title'));
    $this
      ->drupalGet($this
      ->getAbsoluteUrl('frontpage?page=1'));
    $node_links1 = $this
      ->xpath('//span[@class=:class]//a', [
      ':class' => 'field-content',
    ]);
    $this
      ->clickLink((string) end($node_links1));
    $this
      ->clickLink((string) reset($node_links));
    $this
      ->assertEqual($first_title, (string) current($this
      ->xpath('//title')), 'A link from last item of last page leads to first item of first page when cycling is on.');
  }

}

Classes

Namesort descending Description
ViewsNavigationTestCase Class ViewsNavigationTestCase