function HandlerTest::testUiCrud

Same name and namespace in other branches
  1. 9 core/modules/views_ui/tests/src/Functional/HandlerTest.php \Drupal\Tests\views_ui\Functional\HandlerTest::testUICRUD()
  2. 8.9.x core/modules/views_ui/tests/src/Functional/HandlerTest.php \Drupal\Tests\views_ui\Functional\HandlerTest::testUICRUD()
  3. 10 core/modules/views_ui/tests/src/Functional/HandlerTest.php \Drupal\Tests\views_ui\Functional\HandlerTest::testUiCrud()

Tests UI CRUD.

File

core/modules/views_ui/tests/src/Functional/HandlerTest.php, line 100

Class

HandlerTest
Tests handler UI for views.

Namespace

Drupal\Tests\views_ui\Functional

Code

public function testUiCrud() : void {
    $handler_types = ViewExecutable::getHandlerTypes();
    foreach ($handler_types as $type => $type_info) {
        // Test adding handlers.
        $add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/{$type}";
        // Area handler types need to use a different handler.
        if (in_array($type, [
            'header',
            'footer',
            'empty',
        ])) {
            $this->drupalGet($add_handler_url);
            $this->submitForm([
                'name[views.area]' => TRUE,
            ], 'Add and configure ' . $type_info['ltitle']);
            $id = 'area';
            $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/{$type}/{$id}";
        }
        elseif ($type == 'relationship') {
            $this->drupalGet($add_handler_url);
            $this->submitForm([
                'name[views_test_data.uid]' => TRUE,
            ], 'Add and configure ' . $type_info['ltitle']);
            $id = 'uid';
            $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/{$type}/{$id}";
        }
        else {
            $this->drupalGet($add_handler_url);
            $this->submitForm([
                'name[views_test_data.job]' => TRUE,
            ], 'Add and configure ' . $type_info['ltitle']);
            $id = 'job';
            $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/{$type}/{$id}";
        }
        // Verify that the user got redirected to the handler edit form.
        $this->assertSession()
            ->addressEquals($edit_handler_url);
        $random_label = $this->randomMachineName();
        $this->submitForm([
            'options[admin_label]' => $random_label,
        ], 'Apply');
        // Verify that the user got redirected to the views edit form.
        $this->assertSession()
            ->addressEquals('admin/structure/views/view/test_view_empty/edit/default');
        $this->assertSession()
            ->linkByHrefExists($edit_handler_url, 0, 'The handler edit link appears in the UI.');
        // Test that the  handler edit link has the right label.
        $this->assertSession()
            ->elementExists('xpath', "//a[starts-with(normalize-space(text()), '{$random_label}')]");
        // Save the view and have a look whether the handler was added as expected.
        $this->submitForm([], 'Save');
        $view = $this->container
            ->get('entity_type.manager')
            ->getStorage('view')
            ->load('test_view_empty');
        $display = $view->getDisplay('default');
        $this->assertTrue(isset($display['display_options'][$type_info['plural']][$id]), 'Ensure the field was added to the view itself.');
        // Remove the item and check that it's removed
        $this->drupalGet($edit_handler_url);
        $this->submitForm([], 'Remove');
        $this->assertSession()
            ->linkByHrefNotExists($edit_handler_url, 0, 'The handler edit link does not appears in the UI after removing.');
        $this->submitForm([], 'Save');
        $view = $this->container
            ->get('entity_type.manager')
            ->getStorage('view')
            ->load('test_view_empty');
        $display = $view->getDisplay('default');
        $this->assertFalse(isset($display['display_options'][$type_info['plural']][$id]), 'Ensure the field was removed from the view itself.');
    }
    // Test adding a field of the user table using the uid relationship.
    $type_info = $handler_types['relationship'];
    $add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/relationship";
    $this->drupalGet($add_handler_url);
    $this->submitForm([
        'name[views_test_data.uid]' => TRUE,
    ], 'Add and configure ' . $type_info['ltitle']);
    $add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/field";
    $type_info = $handler_types['field'];
    $this->drupalGet($add_handler_url);
    $this->submitForm([
        'name[users_field_data.name]' => TRUE,
    ], 'Add and configure ' . $type_info['ltitle']);
    $id = 'name';
    $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/field/{$id}";
    // Verify that the user got redirected to the handler edit form.
    $this->assertSession()
        ->addressEquals($edit_handler_url);
    $this->assertSession()
        ->fieldValueEquals('options[relationship]', 'uid');
    $this->submitForm([], 'Apply');
    $this->submitForm([], 'Save');
    $view = $this->container
        ->get('entity_type.manager')
        ->getStorage('view')
        ->load('test_view_empty');
    $display = $view->getDisplay('default');
    $this->assertTrue(isset($display['display_options'][$type_info['plural']][$id]), 'Ensure the field was added to the view itself.');
}

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