You are here

public function FormTest::testCustomStyleOps in Style Switcher 3.0.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/FormTest.php \Drupal\Tests\styleswitcher\Functional\FormTest::testCustomStyleOps()

Tests add/edit and delete operations with a custom style.

File

tests/src/Functional/FormTest.php, line 66

Class

FormTest
Tests Style Switcher forms.

Namespace

Drupal\Tests\styleswitcher\Functional

Code

public function testCustomStyleOps() {
  $assert = $this
    ->assertSession();

  // Test the form exists.
  $this
    ->drupalGet($this->adminPath . '/add');
  $assert
    ->elementExists('css', 'form#styleswitcher-style-form');

  // Create a style.
  $path = 'public://styleswitcher_style.css';
  file_put_contents($path, '');
  $label = $this
    ->randomString();
  $name = strtolower($this
    ->randomMachineName(8));
  $edit = [
    'label' => $label,
    'name' => $name,
    'path' => $path,
  ];
  $this
    ->submitForm($edit, 'Save');
  $assert
    ->pageTextContains('has been saved.');

  // Test new style is in the table and config.
  $assert
    ->elementTextContains('css', '#edit-styleswitcher-custom-styles tbody', $label);
  $this
    ->assertArrayHasKey('custom/' . $name, $this
    ->config('styleswitcher.custom_styles')
    ->get('styles'));

  // Test the style is editable.
  $this
    ->drupalGet($this->adminPath . '/custom/' . $name);
  $assert
    ->elementExists('css', 'form#styleswitcher-style-form');
  $name = strtolower($this
    ->randomMachineName(10));
  $edit = [
    'name' => $name,
  ];
  $this
    ->submitForm($edit, 'Save');
  $assert
    ->pageTextContains('has been saved.');
  $this
    ->assertArrayHasKey('custom/' . $name, $this
    ->config('styleswitcher.custom_styles')
    ->get('styles'));

  // Test the style is deletable.
  $assert
    ->linkByHrefExists($this->adminPath . '/custom/' . $name . '/delete');
  $this
    ->drupalGet($this->adminPath . '/custom/' . $name);
  $assert
    ->linkByHrefExists($this->adminPath . '/custom/' . $name . '/delete');
  $this
    ->drupalGet($this->adminPath . '/custom/' . $name . '/delete');
  $assert
    ->elementExists('css', 'form#styleswitcher-style-delete-form');
  $this
    ->submitForm([], 'Confirm');
  $assert
    ->pageTextContains('has been deleted.');
  $assert
    ->elementTextNotContains('css', '#edit-styleswitcher-custom-styles tbody', $label);
  $this
    ->assertArrayNotHasKey('custom/' . $name, $this
    ->config('styleswitcher.custom_styles')
    ->get('styles'));
}