function ModuleTest::testViewsGetHandler

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/ModuleTest.php \Drupal\Tests\views\Kernel\ModuleTest::testViewsGetHandler()
  2. 10 core/modules/views/tests/src/Kernel/ModuleTest.php \Drupal\Tests\views\Kernel\ModuleTest::testViewsGetHandler()
  3. 11.x core/modules/views/tests/src/Kernel/ModuleTest.php \Drupal\Tests\views\Kernel\ModuleTest::testViewsGetHandler()

Tests the ViewsHandlerManager::getHandler() method.

See also

\Drupal\views\Plugin\ViewsHandlerManager::getHandler()

File

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

Class

ModuleTest
Tests basic functions from the Views module.

Namespace

Drupal\Tests\views\Kernel

Code

public function testViewsGetHandler() {
    $types = [
        'field' => BrokenField::class,
        'area' => BrokenArea::class,
        'filter' => BrokenFilter::class,
    ];
    // Test non-existent tables/fields.
    $items = [
        [
            'table' => 'table_invalid',
            'field' => 'id',
        ],
        [
            'table' => 'views_test_data',
            'field' => 'field_invalid',
        ],
    ];
    $form_state = new FormState();
    $description_top = '<p>' . t('The handler for this item is broken or missing. The following details are available:') . '</p>';
    $description_bottom = '<p>' . t('Enabling the appropriate module may solve this issue. Otherwise, check to see if there is a module update available.') . '</p>';
    foreach ($types as $type => $class) {
        foreach ($items as $item) {
            $handler = $this->container
                ->get('plugin.manager.views.' . $type)
                ->getHandler($item);
            $this->assertTrue($handler instanceof $class);
            // Make sure details available at edit form.
            $form = [];
            $handler->buildOptionsForm($form, $form_state);
            $this->assertEquals($description_top, $form['description']['description_top']['#markup']);
            $this->assertEquals($description_bottom, $form['description']['description_bottom']['#markup']);
            $details = [];
            foreach ($item as $key => $value) {
                $details[] = new FormattableMarkup('@key: @value', [
                    '@key' => $key,
                    '@value' => $value,
                ]);
            }
            $this->assertEquals($details, $form['description']['detail_list']['#items']);
        }
    }
    $views_data = $this->viewsData();
    $test_tables = [
        'views_test_data' => [
            'id',
            'name',
        ],
    ];
    foreach ($test_tables as $table => $fields) {
        foreach ($fields as $field) {
            $data = $views_data[$table][$field];
            $item = [
                'table' => $table,
                'field' => $field,
            ];
            foreach ($data as $id => $field_data) {
                if (!in_array($id, [
                    'title',
                    'help',
                ])) {
                    $handler = $this->container
                        ->get('plugin.manager.views.' . $id)
                        ->getHandler($item);
                    $this->assertInstanceHandler($handler, $table, $field, $id);
                }
            }
        }
    }
    // Test the override handler feature.
    $item = [
        'table' => 'views_test_data',
        'field' => 'job',
    ];
    $handler = $this->container
        ->get('plugin.manager.views.filter')
        ->getHandler($item, 'standard');
    $this->assertInstanceOf(Standard::class, $handler);
}

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