View source
<?php
namespace Drupal\Tests\styleswitcher\Functional;
use Drupal\Tests\BrowserTestBase;
class InitialStateTest extends BrowserTestBase {
use HelperTrait;
protected static $modules = [
'styleswitcher',
];
protected $defaultTheme = 'classy';
public function testDynamicLink() {
$assert = $this
->assertSession();
$this
->drupalGet('');
$link = $assert
->elementExists('css', 'head > link:last-child[rel="stylesheet"]');
$this
->assertSame('styleswitcher-css', $link
->getAttribute('id'));
$this
->assertSame('all', $link
->getAttribute('media'));
if (floatval(\Drupal::VERSION) >= 8.800000000000001) {
$this
->assertStringContainsString("/styleswitcher/css/{$this->defaultTheme}", $link
->getAttribute('href'));
}
else {
$this
->assertContains("/styleswitcher/css/{$this->defaultTheme}", $link
->getAttribute('href'));
}
}
public function testAdminAccess() {
$assert = $this
->assertSession();
$paths = [
'admin/config/user-interface/styleswitcher',
"admin/config/user-interface/styleswitcher/settings/{$this->defaultTheme}",
'admin/config/user-interface/styleswitcher/add',
'admin/config/user-interface/styleswitcher/custom/default',
'admin/config/user-interface/styleswitcher/custom/default/delete',
];
foreach ($this
->providerAdminAccess() as [
$permissions,
$code,
]) {
if (isset($permissions)) {
$user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($user);
}
foreach ($paths as $path) {
$this
->drupalGet($path);
$assert
->statusCodeEquals($code);
}
if (isset($permissions)) {
$this
->drupalLogout();
}
}
}
public function providerAdminAccess() : array {
return [
[
NULL,
403,
],
[
[],
403,
],
[
[
'administer styleswitcher',
],
200,
],
];
}
public function testModuleConfigureLink() {
$assert = $this
->assertSession();
foreach ($this
->providerModuleConfigureLink() as [
$permissions,
$path,
]) {
$user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($user);
$this
->drupalGet($path);
$assert
->linkByHrefExists('/admin/config/user-interface/styleswitcher');
$this
->drupalLogout();
}
}
public function providerModuleConfigureLink() : array {
return [
[
[
'administer styleswitcher',
'administer modules',
],
'admin/modules',
],
[
[
'administer styleswitcher',
'access administration pages',
],
'admin/config/user-interface',
],
];
}
}