ViewsSqlExceptionTest.php

Same filename in other branches
  1. 8.9.x core/modules/views/tests/src/Kernel/Plugin/ViewsSqlExceptionTest.php
  2. 10 core/modules/views/tests/src/Kernel/Plugin/ViewsSqlExceptionTest.php
  3. 11.x core/modules/views/tests/src/Kernel/Plugin/ViewsSqlExceptionTest.php

Namespace

Drupal\Tests\views\Kernel\Plugin

File

core/modules/views/tests/src/Kernel/Plugin/ViewsSqlExceptionTest.php

View source
<?php

namespace Drupal\Tests\views\Kernel\Plugin;

use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
use Drupal\Core\Database\DatabaseExceptionWrapper;

/**
 * Tests the views exception handling.
 *
 * @group views
 */
class ViewsSqlExceptionTest extends ViewsKernelTestBase {
    
    /**
     * {@inheritdoc}
     */
    public static $testViews = [
        'test_filter',
    ];
    
    /**
     * {@inheritdoc}
     */
    protected function viewsData() {
        $data = parent::viewsData();
        $data['views_test_data']['name']['filter']['id'] = 'test_exception_filter';
        return $data;
    }
    
    /**
     * Tests for the SQL exception.
     */
    public function testSqlException() {
        $view = Views::getView('test_filter');
        $view->initDisplay();
        // Adding a filter that will result in an invalid query.
        $view->displayHandlers
            ->get('default')
            ->overrideOption('filters', [
            'test_filter' => [
                'id' => 'test_exception_filter',
                'table' => 'views_test_data',
                'field' => 'name',
                'operator' => '=',
                'value' => 'John',
                'group' => 0,
            ],
        ]);
        $this->expectException(DatabaseExceptionWrapper::class);
        $this->expectExceptionMessageMatches('/^Exception in Test filters\\[test_filter\\]:/');
        $this->executeView($view);
    }

}

Classes

Title Deprecated Summary
ViewsSqlExceptionTest Tests the views exception handling.

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