You are here

public function FormTest::testConfigThemeForm 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::testConfigThemeForm()

Tests the config theme form exists and functions properly.

File

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

Class

FormTest
Tests Style Switcher forms.

Namespace

Drupal\Tests\styleswitcher\Functional

Code

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

  // Mock custom styles.
  $styles = $this
    ->composeStylesMockingPaths(range('A', 'J'));
  $default = array_rand($styles);
  $styles[$default]['path'] = NULL;
  $this
    ->config('styleswitcher.custom_styles')
    ->set('styles', $styles)
    ->save();

  // Test the form exists.
  $this
    ->drupalGet($this->adminPath . "/settings/{$this->defaultTheme}");

  // Test the config theme form's field values correspond to the styles config
  // without the style settings config.
  foreach (array_keys($styles) as $x) {
    $assert
      ->elementTextContains('css', '#styleswitcher-styles-table tbody', 'Machine name: ' . $x);
    $assert
      ->fieldValueEquals('settings[weight][' . $x . ']', 0);
    if ($x !== $default) {
      $assert
        ->checkboxChecked('settings[enabled][' . $x . ']');
    }
  }
  $assert
    ->fieldDisabled('settings[enabled][' . $default . ']');
  $assert
    ->fieldValueEquals('settings[default]', $default);

  // Change field values.
  $edit = [];
  $expected = $this
    ->composeStylesSettings(array_keys($styles));

  // Set random weights.
  foreach (array_keys($styles) as $x) {
    $weight = rand(-5, 5);
    $edit['settings[weight][' . $x . ']'] = $expected[$x]['weight'] = $weight;
  }

  // Disable 5 random styles, except the current default.
  unset($styles[$default]);
  $disabled = array_rand($styles, 5);
  foreach ($disabled as $x) {
    $edit['settings[enabled][' . $x . ']'] = $expected[$x]['status'] = FALSE;
  }

  // Set a random disabled style as default.
  $default = $disabled[array_rand($disabled)];
  $edit['settings[default]'] = $default;
  $expected[$default]['is_default'] = $expected[$default]['status'] = TRUE;
  $this
    ->submitForm($edit, 'Save configuration');

  // Test config is saved correctly.
  $assert
    ->pageTextContains('The configuration options have been saved.');
  $this
    ->assertSame($expected, $this
    ->config('styleswitcher.styles_settings')
    ->get("settings.{$this->defaultTheme}"));
}