You are here

ViewsExposedGroupsUiTest.php in Views exposed groups 3.0.x

File

tests/src/Functional/ViewsExposedGroupsUiTest.php
View source
<?php

namespace Drupal\Tests\views_exposed_groups\Functional;


/**
 * Tests the views user interface when using views_exposed_groups.
 *
 * @group views_exposed_groups
 */
class ViewsExposedGroupsUiTest extends ViewsExposedGroupsTestBase {

  /**
   * Asserts that the a user can group exposed forms.
   */
  public function testViewsUi() {
    $view_path = $this->defaultView
      ->getDisplay('page_1')
      ->getOption('path');
    $views_ui_path = '/admin/structure/views/view/' . $this->defaultView
      ->id();
    $this
      ->drupalLogin($this->privilegedUser);

    // Asserts that the view does not have an exposed form.
    $this
      ->drupalGet($view_path);
    $fieldset_links = $this
      ->xpath('//fieldset/legend/span');
    $this
      ->assertEqual(0, count($fieldset_links), 'Found no grouped exposed filters.');

    // Goes to view edit and changes to use exposed groups.
    $this
      ->drupalGet($views_ui_path . '/edit');
    $this
      ->clickLink('Basic');
    $this
      ->drupalPostForm(NULL, [
      'exposed_form[type]' => 'views_exposed_groups',
    ], 'Apply');

    // Adds some groups.
    // @todo Asserts that the groups default to - No group -.
    $edit_form = [
      'exposed_form_options[views_exposed_groups][format_groups]' => 'fieldsets',
      'exposed_form_options[views_exposed_groups][groups]' => "First\nSecond",
    ];
    $this
      ->drupalPostForm(NULL, $edit_form, 'Apply');
    $this
      ->assertLink('Grouped filters');

    // Configures the groups.
    $edit_form = [
      'exposed_form_options[views_exposed_groups][group-nid][group]' => '0',
      'exposed_form_options[views_exposed_groups][group-title][group]' => '1',
      'exposed_form_options[views_exposed_groups][group-uid][group]' => 'no-group',
    ];
    $exposed_options_path = 'admin/structure/views/nojs/display/' . $this->defaultView->name . '/default/exposed_form_options';
    $this
      ->drupalPostForm($exposed_options_path, $edit_form, 'Apply');

    // Confirms that the table has the correct rows.
    $this
      ->drupalGet($exposed_options_path);
    $rows = $this
      ->xpath('//table[@id="reorder-group-filters"]/tbody/tr');
    $this
      ->assertEqual(6, count($rows), 'Found six rows in filter group table.');
    $this
      ->assertFieldByName('exposed_form_options[views_exposed_groups][group-nid][group]', '0');
    $this
      ->assertFieldByName('exposed_form_options[views_exposed_groups][group-title][group]', '1');
    $this
      ->assertFieldByName('exposed_form_options[views_exposed_groups][group-uid][group]', 'no-group');

    // Saves the view.
    $this
      ->drupalPostForm($views_ui_path . '/edit', [], 'Save');

    // Checks to make sure that the view now has fieldsets.
    $this
      ->drupalGet($view_path);

    // Note that even though the actual HTML has an anchor element, SimpleTest
    // hides this and so a real xpath will not work.
    $fieldset_links = $this
      ->xpath('//fieldset/legend/span/text()');
    $this
      ->assertEqual(2, count($fieldset_links), 'Found the two fieldset links for grouped filters.');
    $this
      ->assertEqual('First', (string) $fieldset_links[0]);
    $this
      ->assertEqual('Second', (string) $fieldset_links[1]);

    // Asserts that the filters work.
    $options = [
      'query' => [
        'uid' => '',
        'nid' => '1',
        'title' => '',
      ],
    ];
    $this
      ->drupalGet($view_path, $options);
    $this
      ->assertViewsTableResultCount(1);
    $options['query']['title'] = substr($this->testNodes[3]->title, 0, 3);
    $this
      ->drupalGet($view_path, $options);
    $this
      ->assertViewsTableResultCount(1);
    $this
      ->assertText('There are no results for your search.');
    $options['query']['nid'] = '';
    $this
      ->drupalGet($view_path, $options);
    $this
      ->assertViewsTableResultCount(1);

    // Goes back to view edit and makes sure that can gracefully remove groups.
    $edit_form = [
      'exposed_form_options[views_exposed_groups][format_groups]' => 'fieldsets',
      'exposed_form_options[views_exposed_groups][groups]' => "",
    ];
    $this
      ->drupalPostForm($exposed_options_path, $edit_form, 'Apply');
    $this
      ->drupalGet($exposed_options_path);
    $this
      ->assertFieldByName('exposed_form_options[views_exposed_groups][group-nid][group]', 'no-group');
    $this
      ->assertFieldByName('exposed_form_options[views_exposed_groups][group-title][group]', 'no-group');
    $this
      ->assertFieldByName('exposed_form_options[views_exposed_groups][group-uid][group]', 'no-group');
  }

}

Classes

Namesort descending Description
ViewsExposedGroupsUiTest Tests the views user interface when using views_exposed_groups.