View source
<?php
namespace Drupal\Tests\styleswitcher\Functional;
use Drupal\Tests\BrowserTestBase;
class FormTest extends BrowserTestBase {
use HelperTrait;
protected static $modules = [
'styleswitcher',
];
protected $defaultTheme = 'classy';
protected $adminPath = 'admin/config/user-interface/styleswitcher';
protected function setUp() {
parent::setUp();
$user = $this
->drupalCreateUser([
'administer styleswitcher',
]);
$this
->drupalLogin($user);
}
public function testMainAdminForm() {
$assert = $this
->assertSession();
$this
->drupalGet($this->adminPath);
$assert
->elementExists('css', 'form#styleswitcher-admin');
$assert
->checkboxChecked('enable_overlay');
$edit = [
'enable_overlay' => '0',
];
$this
->submitForm($edit, 'Save configuration');
$assert
->pageTextContains('The configuration options have been saved.');
$assert
->checkboxNotChecked('enable_overlay');
}
public function testCustomStyleOps() {
$assert = $this
->assertSession();
$this
->drupalGet($this->adminPath . '/add');
$assert
->elementExists('css', 'form#styleswitcher-style-form');
$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.');
$assert
->elementTextContains('css', '#edit-styleswitcher-custom-styles tbody', $label);
$this
->assertArrayHasKey('custom/' . $name, $this
->config('styleswitcher.custom_styles')
->get('styles'));
$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'));
$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'));
}
public function testEmptyStyleOps() {
$assert = $this
->assertSession();
$this
->drupalGet($this->adminPath);
$assert
->elementTextContains('css', '#edit-styleswitcher-custom-styles tbody', 'Default');
$this
->drupalGet($this->adminPath . "/settings/{$this->defaultTheme}");
$assert
->fieldValueEquals('settings[default]', 'custom/default');
$assert
->fieldDisabled('settings[enabled][custom/default]');
$this
->drupalGet($this->adminPath . '/custom/default');
$assert
->elementExists('css', 'form#styleswitcher-style-form');
$label = $this
->randomString();
$name = strtolower($this
->randomMachineName(10));
$edit = [
'label' => $label,
'name' => $name,
];
$this
->submitForm($edit, 'Save');
$assert
->pageTextContains('has been saved.');
$styles = [
'custom/' . $name => [
'label' => $label,
'name' => 'custom/' . $name,
'path' => NULL,
],
];
$this
->assertSame($styles, $this
->config('styleswitcher.custom_styles')
->get('styles'));
$this
->assertArrayNotHasKey('custom/default', $this
->config('styleswitcher.custom_styles')
->get('styles'));
$assert
->linkByHrefNotExists($this->adminPath . '/custom/' . $name . '/delete');
$this
->drupalGet($this->adminPath . '/custom/' . $name);
$assert
->linkByHrefNotExists($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('The blank style cannot be deleted.');
$assert
->elementTextContains('css', '#edit-styleswitcher-custom-styles tbody', $label);
$this
->assertArrayHasKey('custom/' . $name, $this
->config('styleswitcher.custom_styles')
->get('styles'));
}
public function testAddStyleExistingName() {
$assert = $this
->assertSession();
$this
->config('styleswitcher.custom_styles')
->set('styles', $this
->composeStyles([
'A' => NULL,
]))
->save();
$this
->drupalGet($this->adminPath . '/add');
$path = 'public://styleswitcher_style.css';
file_put_contents($path, '');
$edit = [
'label' => 'B',
'name' => 'a',
'path' => $path,
];
$this
->submitForm($edit, 'Save');
$assert
->pageTextContains('The machine-readable name is already in use. It must be unique.');
$assert
->elementExists('css', 'form#styleswitcher-style-form');
$this
->assertSame('A', $this
->config('styleswitcher.custom_styles')
->get('styles.custom/a.label'));
}
public function testEditStyleExistingName() {
$assert = $this
->assertSession();
$path = 'public://styleswitcher_style.css';
file_put_contents($path, '');
$this
->config('styleswitcher.custom_styles')
->set('styles', $this
->composeStyles([
'A' => $path,
'B' => NULL,
]))
->save();
$this
->drupalGet($this->adminPath . '/custom/b');
$this
->submitForm([
'name' => 'a',
], 'Save');
$assert
->pageTextContains('The machine-readable name is already in use. It must be unique.');
$assert
->elementExists('css', 'form#styleswitcher-style-form');
$this
->assertSame('A', $this
->config('styleswitcher.custom_styles')
->get('styles.custom/a.label'));
}
public function testStylesSettingsOnStyleRename() {
$this
->config('styleswitcher.custom_styles')
->set('styles', $this
->composeStyles([
'A' => NULL,
]))
->save();
$this
->config('styleswitcher.styles_settings')
->set('settings.theme_z', [
'custom/a' => [],
])
->save();
$this
->drupalGet($this->adminPath . '/custom/a');
$this
->submitForm([
'name' => 'b',
], 'Save');
$this
->assertArrayHasKey('custom/b', $this
->config('styleswitcher.styles_settings')
->get('settings.theme_z'));
$this
->assertArrayNotHasKey('custom/a', $this
->config('styleswitcher.styles_settings')
->get('settings.theme_z'));
}
public function testConfigThemeForm() {
$assert = $this
->assertSession();
$styles = $this
->composeStylesMockingPaths(range('A', 'J'));
$default = array_rand($styles);
$styles[$default]['path'] = NULL;
$this
->config('styleswitcher.custom_styles')
->set('styles', $styles)
->save();
$this
->drupalGet($this->adminPath . "/settings/{$this->defaultTheme}");
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);
$edit = [];
$expected = $this
->composeStylesSettings(array_keys($styles));
foreach (array_keys($styles) as $x) {
$weight = rand(-5, 5);
$edit['settings[weight][' . $x . ']'] = $expected[$x]['weight'] = $weight;
}
unset($styles[$default]);
$disabled = array_rand($styles, 5);
foreach ($disabled as $x) {
$edit['settings[enabled][' . $x . ']'] = $expected[$x]['status'] = FALSE;
}
$default = $disabled[array_rand($disabled)];
$edit['settings[default]'] = $default;
$expected[$default]['is_default'] = $expected[$default]['status'] = TRUE;
$this
->submitForm($edit, 'Save configuration');
$assert
->pageTextContains('The configuration options have been saved.');
$this
->assertSame($expected, $this
->config('styleswitcher.styles_settings')
->get("settings.{$this->defaultTheme}"));
}
}