You are here

public function ModuleTest::testLoadFunctions in Zircon Profile 8.0

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

Tests the load wrapper/helper functions.

File

core/modules/views/src/Tests/ModuleTest.php, line 144
Contains \Drupal\views\Tests\ModuleTest.

Class

ModuleTest

Namespace

Drupal\views\Tests

Code

public function testLoadFunctions() {
  $this
    ->enableModules(array(
    'field',
    'text',
    'node',
  ));
  $this
    ->installConfig(array(
    'node',
  ));
  $storage = $this->container
    ->get('entity.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().
  $this
    ->assertIdentical(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
    ->assertIdentical(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
    ->assertIdentical(array_keys($expected_disabled), array_keys(Views::getDisabledViews()), 'Expected disabled views returned.');

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

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

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

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

  // Test the sort parameter.
  $all_views_sorted = $all_views;
  ksort($all_views_sorted);
  $this
    ->assertIdentical(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
    ->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive')), 'View excluded from options based on name');
  $this
    ->assertFalse(array_key_exists('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default')), 'View display excluded from options based on name');
  $this
    ->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', $archive
    ->getExecutable())), 'View excluded from options based on object');

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