You are here

public function FilterWidgetKernelTest::testSortFilterOptions in Better Exposed Filters 8.4

Same name and namespace in other branches
  1. 8.5 tests/src/Kernel/Plugin/filter/FilterWidgetKernelTest.php \Drupal\Tests\better_exposed_filters\Kernel\Plugin\filter\FilterWidgetKernelTest::testSortFilterOptions()

Tests sorting filter options alphabetically.

File

tests/src/Kernel/Plugin/filter/FilterWidgetKernelTest.php, line 79

Class

FilterWidgetKernelTest
Tests the advanced options of a filter widget.

Namespace

Drupal\Tests\better_exposed_filters\Kernel\Plugin\filter

Code

public function testSortFilterOptions() {
  $view = Views::getView('bef_test');
  $display =& $view->storage
    ->getDisplay('default');

  // Get the exposed form render array.
  $output = $this
    ->getExposedFormRenderArray($view);

  // Assert our "field_bef_integer" filter options are not sorted
  // alphabetically, but by key.
  $sorted_options = $options = $output['field_bef_integer_value']['#options'];
  asort($sorted_options);
  $this
    ->assertNotEqual(array_keys($options), array_keys($sorted_options), '"Field BEF integer" options are not sorted alphabetically.');
  $view
    ->destroy();

  // Enable sort for filter options.
  $this
    ->setBetterExposedOptions($view, [
    'filter' => [
      'field_bef_integer_value' => [
        'plugin_id' => 'default',
        'advanced' => [
          'sort_options' => TRUE,
        ],
      ],
    ],
  ]);

  // Get the exposed form render array.
  $output = $this
    ->getExposedFormRenderArray($view);

  // Assert our "field_bef_integer" filter options are sorted alphabetically.
  $sorted_options = $options = $output['field_bef_integer_value']['#options'];
  asort($sorted_options);

  // Assert our "collapsible" options detail is visible.
  $this
    ->assertEqual(array_keys($options), array_keys($sorted_options), '"Field BEF integer" options are sorted alphabetically.');
  $view
    ->destroy();
}