You are here

public function ViewsExposedGroupsUiTest::testViewsUi in Views exposed groups 7.2

Asserts that the a user can group exposed forms.

File

tests/views_exposed_groups_ui.test, line 27
Views Exposed Groups UI Test.

Class

ViewsExposedGroupsUiTest
Tests the views user interface when using views_exposed_groups.

Code

public function testViewsUi() {
  $view_path = $this->defaultView
    ->get_path();
  $views_ui_path = '/admin/structure/views/view/' . $this->defaultView->name;
  $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
    ->drupalPost(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
    ->drupalPost(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
    ->drupalPost($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
    ->drupalPost($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
    ->drupalPost($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');
}