You are here

InitialStateTest.php in Style Switcher 3.0.x

Same filename and directory in other branches
  1. 8.2 tests/src/Functional/InitialStateTest.php

File

tests/src/Functional/InitialStateTest.php
View source
<?php

namespace Drupal\Tests\styleswitcher\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Tests the module in its initial state.
 *
 * @group styleswitcher
 */
class InitialStateTest extends BrowserTestBase {
  use HelperTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'styleswitcher',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

  /**
   * Tests the dynamic link tag exists and it's the last of stylesheets.
   */
  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'));

    // The href may contain path prefix and query params.
    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'));
    }
  }

  /**
   * Tests access to admin routes.
   */
  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',
    ];

    /** @var string[]|null $permissions */

    /** @var int $code */
    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();
      }
    }
  }

  /**
   * Data provider for testAdminAccess().
   *
   * @return array[]
   *   The data sets to test.
   */
  public function providerAdminAccess() : array {
    return [
      [
        NULL,
        403,
      ],
      [
        [],
        403,
      ],
      [
        [
          'administer styleswitcher',
        ],
        200,
      ],
    ];
  }

  /**
   * Tests the Configure link for the Style Switcher exists.
   */
  public function testModuleConfigureLink() {
    $assert = $this
      ->assertSession();

    /** @var string[] $permissions */

    /** @var string $path */
    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();
    }
  }

  /**
   * Data provider for testModuleConfigureLink().
   *
   * @return array[]
   *   The data sets to test.
   */
  public function providerModuleConfigureLink() : array {
    return [
      [
        [
          'administer styleswitcher',
          'administer modules',
        ],
        'admin/modules',
      ],
      [
        [
          'administer styleswitcher',
          'access administration pages',
        ],
        'admin/config/user-interface',
      ],
    ];
  }

}

Classes

Namesort descending Description
InitialStateTest Tests the module in its initial state.