You are here

public function ModuleTest::testLoadFunctions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/ModuleTest.php \Drupal\Tests\views\Kernel\ModuleTest::testLoadFunctions()

Tests the load wrapper/helper functions.

File

core/modules/views/tests/src/Kernel/ModuleTest.php, line 116

Class

ModuleTest
Tests basic functions from the Views module.

Namespace

Drupal\Tests\views\Kernel

Code

public function testLoadFunctions() {
  $this
    ->enableModules([
    'text',
    'node',
  ]);
  $this
    ->installEntitySchema('node');
  $this
    ->installConfig([
    'node',
  ]);
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('view');

  // Test views_view_is_enabled/disabled.
  $archive = $storage
    ->load('archive');
  $this
    ->assertTrue(views_view_is_disabled($archive), 'views_view_is_disabled works as expected.');

  // Enable the view and check this.
  $archive
    ->enable();
  $this
    ->assertTrue(views_view_is_enabled($archive), ' views_view_is_enabled works as expected.');

  // We can store this now, as we have enabled/disabled above.
  $all_views = $storage
    ->loadMultiple();

  // Test Views::getAllViews().
  ksort($all_views);
  $this
    ->assertEquals(array_keys($all_views), array_keys(Views::getAllViews()), 'Views::getAllViews works as expected.');

  // Test Views::getEnabledViews().
  $expected_enabled = array_filter($all_views, function ($view) {
    return views_view_is_enabled($view);
  });
  $this
    ->assertEquals(array_keys($expected_enabled), array_keys(Views::getEnabledViews()), 'Expected enabled views returned.');

  // Test Views::getDisabledViews().
  $expected_disabled = array_filter($all_views, function ($view) {
    return views_view_is_disabled($view);
  });
  $this
    ->assertEquals(array_keys($expected_disabled), array_keys(Views::getDisabledViews()), 'Expected disabled views returned.');

  // Test Views::getViewsAsOptions().
  // Test the $views_only parameter.
  $this
    ->assertSame(array_keys($all_views), array_keys(Views::getViewsAsOptions(TRUE)), 'Expected option keys for all views were returned.');
  $expected_options = [];
  foreach ($all_views as $id => $view) {
    $expected_options[$id] = $view
      ->label();
  }
  $this
    ->assertSame($expected_options, Views::getViewsAsOptions(TRUE), 'Expected options array was returned.');

  // Test the default.
  $this
    ->assertEquals($this
    ->formatViewOptions($all_views), Views::getViewsAsOptions(), 'Expected options array for all views was returned.');

  // Test enabled views.
  $this
    ->assertEquals($this
    ->formatViewOptions($expected_enabled), Views::getViewsAsOptions(FALSE, 'enabled'), 'Expected enabled options array was returned.');

  // Test disabled views.
  $this
    ->assertEquals($this
    ->formatViewOptions($expected_disabled), Views::getViewsAsOptions(FALSE, 'disabled'), 'Expected disabled options array was returned.');

  // Test the sort parameter.
  $all_views_sorted = $all_views;
  ksort($all_views_sorted);
  $this
    ->assertSame(array_keys($all_views_sorted), array_keys(Views::getViewsAsOptions(TRUE, 'all', NULL, FALSE, TRUE)), 'All view id keys returned in expected sort order');

  // Test $exclude_view parameter.
  $this
    ->assertArrayNotHasKey('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive'));
  $this
    ->assertArrayNotHasKey('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default'));
  $this
    ->assertArrayNotHasKey('archive', Views::getViewsAsOptions(TRUE, 'all', $archive
    ->getExecutable()));

  // Test the $opt_group parameter.
  $expected_opt_groups = [];
  foreach ($all_views as $view) {
    foreach ($view
      ->get('display') as $display) {
      $expected_opt_groups[$view
        ->id()][$view
        ->id() . ':' . $display['id']] = (string) t('@view : @display', [
        '@view' => $view
          ->id(),
        '@display' => $display['id'],
      ]);
    }
  }
  $this
    ->assertEquals($expected_opt_groups, Views::getViewsAsOptions(FALSE, 'all', NULL, TRUE), 'Expected option array for an option group returned.');
}