You are here

public function ViewEditTest::testOtherOptions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views_ui/tests/src/Functional/ViewEditTest.php \Drupal\Tests\views_ui\Functional\ViewEditTest::testOtherOptions()
  2. 10 core/modules/views_ui/tests/src/Functional/ViewEditTest.php \Drupal\Tests\views_ui\Functional\ViewEditTest::testOtherOptions()

Tests the machine name and administrative comment forms.

File

core/modules/views_ui/tests/src/Functional/ViewEditTest.php, line 49

Class

ViewEditTest
Tests some general functionality of editing views, like deleting a view.

Namespace

Drupal\Tests\views_ui\Functional

Code

public function testOtherOptions() {
  $this
    ->drupalGet('admin/structure/views/view/test_view');

  // Add a new attachment display.
  $this
    ->submitForm([], 'Add Attachment');

  // Test that a long administrative comment is truncated.
  $edit = [
    'display_comment' => 'one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen',
  ];
  $this
    ->drupalGet('admin/structure/views/nojs/display/test_view/attachment_1/display_comment');
  $this
    ->submitForm($edit, 'Apply');
  $this
    ->assertSession()
    ->pageTextContains('one two three four five six seven eight nine ten eleven twelve thirteen fourteen...');

  // Change the machine name for the display from page_1 to test_1.
  $edit = [
    'display_id' => 'test_1',
  ];
  $this
    ->drupalGet('admin/structure/views/nojs/display/test_view/attachment_1/display_id');
  $this
    ->submitForm($edit, 'Apply');
  $this
    ->assertSession()
    ->linkExists('test_1');

  // Save the view, and test the new ID has been saved.
  $this
    ->submitForm([], 'Save');
  $view = \Drupal::entityTypeManager()
    ->getStorage('view')
    ->load('test_view');
  $displays = $view
    ->get('display');
  $this
    ->assertTrue(!empty($displays['test_1']), 'Display data found for new display ID key.');
  $this
    ->assertSame('test_1', $displays['test_1']['id'], 'New display ID matches the display ID key.');
  $this
    ->assertArrayNotHasKey('attachment_1', $displays);

  // Set to the same machine name and save the View.
  $edit = [
    'display_id' => 'test_1',
  ];
  $this
    ->drupalGet('admin/structure/views/nojs/display/test_view/test_1/display_id');
  $this
    ->submitForm($edit, 'Apply');
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->linkExists('test_1');

  // Test the form validation with invalid IDs.
  $machine_name_edit_url = 'admin/structure/views/nojs/display/test_view/test_1/display_id';
  $error_text = 'Display machine name must contain only lowercase letters, numbers, or underscores.';

  // Test that potential invalid display ID requests are detected
  try {
    $this
      ->drupalGet('admin/structure/views/ajax/handler/test_view/fake_display_name/filter/title');
    $this
      ->fail('Expected error, when setDisplay() called with invalid display ID');
  } catch (\Exception $e) {
    $this
      ->assertStringContainsString('setDisplay() called with invalid display ID "fake_display_name".', $e
      ->getMessage());
  }
  $edit = [
    'display_id' => 'test 1',
  ];
  $this
    ->drupalGet($machine_name_edit_url);
  $this
    ->submitForm($edit, 'Apply');
  $this
    ->assertSession()
    ->pageTextContains($error_text);
  $edit = [
    'display_id' => 'test_1#',
  ];
  $this
    ->drupalGet($machine_name_edit_url);
  $this
    ->submitForm($edit, 'Apply');
  $this
    ->assertSession()
    ->pageTextContains($error_text);

  // Test using an existing display ID.
  $edit = [
    'display_id' => 'default',
  ];
  $this
    ->drupalGet($machine_name_edit_url);
  $this
    ->submitForm($edit, 'Apply');
  $this
    ->assertSession()
    ->pageTextContains('Display id should be unique.');

  // Test that the display ID has not been changed.
  $this
    ->drupalGet('admin/structure/views/view/test_view/edit/test_1');
  $this
    ->assertSession()
    ->linkExists('test_1');

  // Test that validation does not run on cancel.
  $this
    ->drupalGet('admin/structure/views/view/test_view');

  // Delete the field to cause an error on save.
  $fields = [];
  $fields['fields[age][removed]'] = 1;
  $fields['fields[id][removed]'] = 1;
  $fields['fields[name][removed]'] = 1;
  $this
    ->drupalGet('admin/structure/views/nojs/rearrange/test_view/default/field');
  $this
    ->submitForm($fields, 'Apply');
  $this
    ->submitForm([], 'Save');
  $this
    ->submitForm([], 'Cancel');

  // Verify that no error message is displayed.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//div[contains(@class, "error")]');

  // Verify page was redirected to the view listing.
  $this
    ->assertSession()
    ->addressEquals('admin/structure/views');
}