You are here

CookieUpdateTest.php in Style Switcher 8.2

Same filename and directory in other branches
  1. 3.0.x tests/src/Functional/CookieUpdateTest.php

File

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

namespace Drupal\Tests\styleswitcher\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;

/**
 * Tests Style Switcher cookie updates.
 *
 * @group styleswitcher
 */
class CookieUpdateTest extends BrowserTestBase {
  use AssertPageCacheContextsAndTagsTrait;
  use AssertTrait;
  use HelperTrait;

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

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->enablePageCaching();
  }

  /**
   * Tests cookie updates.
   *
   * @param int $version
   *   Version of a Style Switcher cookie to test.
   * @param string $cookie_name
   *   Old cookie name.
   *
   * @dataProvider providerCookieUpdates
   */
  public function testCookieUpdates(int $version, string $cookie_name) {
    $styles['custom/default'] = $this
      ->composeStyleMockingPath('Default');

    // Mock a theme style by saving it to the custom styles config.
    $styles['theme/active'] = $this
      ->composeStyleMockingPath('Active', 'theme');
    $this
      ->config('styleswitcher.custom_styles')
      ->set('styles', $styles)
      ->save();
    $settings = $this
      ->composeStylesSettings([
      'custom/default',
      'theme/active',
    ]);
    $settings['custom/default']['is_default'] = TRUE;
    $this
      ->config('styleswitcher.styles_settings')
      ->set("settings.{$this->defaultTheme}", $settings)
      ->save();
    $this
      ->assertActiveStylePath('custom/default');
    $this
      ->drupalGet('styleswitcher-test-legacy-cookies/' . $version);
    $this
      ->assertActiveStylePath('theme/active');
    $this
      ->assertSame('theme/active', $this
      ->getSession()
      ->getCookie("styleswitcher[{$this->defaultTheme}]"));
    $this
      ->assertNull($this
      ->getSession()
      ->getCookie($cookie_name));
  }

  /**
   * Data provider for testCookieUpdates().
   *
   * @return array[]
   *   The data sets to test.
   */
  public function providerCookieUpdates() : array {
    return [
      [
        1,
        'styleSwitcher',
      ],
      [
        2,
        'styleswitcher',
      ],
      [
        3,
        'styleswitcher',
      ],
    ];
  }

}

Classes

Namesort descending Description
CookieUpdateTest Tests Style Switcher cookie updates.