You are here

public function HandlerTest::testUICRUD in Drupal 8

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. 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 98

Class

HandlerTest
Tests handler UI for views.

Namespace

Drupal\Tests\views_ui\Functional

Code

public function testUICRUD() {
  $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
        ->drupalPostForm($add_handler_url, [
        'name[views.area]' => TRUE,
      ], t('Add and configure @handler', [
        '@handler' => $type_info['ltitle'],
      ]));
      $id = 'area';
      $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/{$type}/{$id}";
    }
    elseif ($type == 'relationship') {
      $this
        ->drupalPostForm($add_handler_url, [
        'name[views_test_data.uid]' => TRUE,
      ], t('Add and configure @handler', [
        '@handler' => $type_info['ltitle'],
      ]));
      $id = 'uid';
      $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/{$type}/{$id}";
    }
    else {
      $this
        ->drupalPostForm($add_handler_url, [
        'name[views_test_data.job]' => TRUE,
      ], t('Add and configure @handler', [
        '@handler' => $type_info['ltitle'],
      ]));
      $id = 'job';
      $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/{$type}/{$id}";
    }
    $this
      ->assertUrl($edit_handler_url, [], 'The user got redirected to the handler edit form.');
    $random_label = $this
      ->randomMachineName();
    $this
      ->drupalPostForm(NULL, [
      'options[admin_label]' => $random_label,
    ], t('Apply'));
    $this
      ->assertUrl('admin/structure/views/view/test_view_empty/edit/default', [], 'The user got redirected to the views edit form.');
    $this
      ->assertLinkByHref($edit_handler_url, 0, 'The handler edit link appears in the UI.');
    $links = $this
      ->xpath('//a[starts-with(normalize-space(text()), :label)]', [
      ':label' => $random_label,
    ]);
    $this
      ->assertTrue(isset($links[0]), 'The handler edit link has the right label');

    // Save the view and have a look whether the handler was added as expected.
    $this
      ->drupalPostForm(NULL, [], t('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
      ->drupalPostForm($edit_handler_url, [], t('Remove'));
    $this
      ->assertNoLinkByHref($edit_handler_url, 0, 'The handler edit link does not appears in the UI after removing.');
    $this
      ->drupalPostForm(NULL, [], t('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
    ->drupalPostForm($add_handler_url, [
    'name[views_test_data.uid]' => TRUE,
  ], t('Add and configure @handler', [
    '@handler' => $type_info['ltitle'],
  ]));
  $add_handler_url = "admin/structure/views/nojs/add-handler/test_view_empty/default/field";
  $type_info = $handler_types['field'];
  $this
    ->drupalPostForm($add_handler_url, [
    'name[users_field_data.name]' => TRUE,
  ], t('Add and configure @handler', [
    '@handler' => $type_info['ltitle'],
  ]));
  $id = 'name';
  $edit_handler_url = "admin/structure/views/nojs/handler/test_view_empty/default/field/{$id}";
  $this
    ->assertUrl($edit_handler_url, [], 'The user got redirected to the handler edit form.');
  $this
    ->assertFieldByName('options[relationship]', 'uid', 'Ensure the relationship select is filled with the UID relationship.');
  $this
    ->drupalPostForm(NULL, [], t('Apply'));
  $this
    ->drupalPostForm(NULL, [], t('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.');
}