function ModuleTest::testLoadFunctions

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/ModuleTest.php \Drupal\Tests\views\Kernel\ModuleTest::testLoadFunctions()
  2. 10 core/modules/views/tests/src/Kernel/ModuleTest.php \Drupal\Tests\views\Kernel\ModuleTest::testLoadFunctions()
  3. 11.x 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->assertIdentical(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->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->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->assertIdentical($expected_opt_groups, $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'all', NULL, TRUE)), 'Expected option array for an option group returned.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.