You are here

public function ViewEditTest::testOtherOptions in Drupal 8

Same name and namespace in other branches
  1. 9 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
    ->drupalPostForm(NULL, [], '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
    ->drupalPostForm('admin/structure/views/nojs/display/test_view/attachment_1/display_comment', $edit, 'Apply');
  $this
    ->assertText('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
    ->drupalPostForm('admin/structure/views/nojs/display/test_view/attachment_1/display_id', $edit, 'Apply');
  $this
    ->assertSession()
    ->linkExists(t('test_1'));

  // Save the view, and test the new ID has been saved.
  $this
    ->drupalPostForm(NULL, [], '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
    ->assertIdentical($displays['test_1']['id'], 'test_1', '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
    ->drupalPostForm('admin/structure/views/nojs/display/test_view/test_1/display_id', $edit, 'Apply');
  $this
    ->drupalPostForm(NULL, [], 'Save');
  $this
    ->assertSession()
    ->linkExists(t('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 = t('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
    ->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
  $this
    ->assertText($error_text);
  $edit = [
    'display_id' => 'test_1#',
  ];
  $this
    ->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
  $this
    ->assertText($error_text);

  // Test using an existing display ID.
  $edit = [
    'display_id' => 'default',
  ];
  $this
    ->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
  $this
    ->assertText(t('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(t('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
    ->drupalPostForm('admin/structure/views/nojs/rearrange/test_view/default/field', $fields, t('Apply'));
  $this
    ->drupalPostForm(NULL, [], 'Save');
  $this
    ->drupalPostForm(NULL, [], t('Cancel'));
  $this
    ->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');
  $this
    ->assertUrl('admin/structure/views', [], 'Redirected back to the view listing page..');
}