You are here

public function ColorTest::_testColor in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/color/tests/src/Functional/ColorTest.php \Drupal\Tests\color\Functional\ColorTest::_testColor()

Tests the Color module functionality using the given theme.

Parameters

string $theme: The machine name of the theme being tested.

array $test_values: An associative array of test settings (i.e. 'Main background', 'Text color', 'Color set', etc) for the theme which being tested.

1 call to ColorTest::_testColor()
ColorTest::testColor in core/modules/color/tests/src/Functional/ColorTest.php
Tests the Color module functionality.

File

core/modules/color/tests/src/Functional/ColorTest.php, line 107

Class

ColorTest
Modify the Bartik theme colors and make sure the changes are reflected on the frontend.

Namespace

Drupal\Tests\color\Functional

Code

public function _testColor($theme, $test_values) {
  $this
    ->config('system.theme')
    ->set('default', $theme)
    ->save();
  $settings_path = 'admin/appearance/settings/' . $theme;
  $this
    ->drupalLogin($this->bigUser);
  $this
    ->drupalGet($settings_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContainsOnce('Color set');
  $edit['scheme'] = '';
  $edit[$test_values['palette_input']] = '#123456';
  $this
    ->drupalGet($settings_path);
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->drupalGet('<front>');
  $stylesheets = $this
    ->config('color.theme.' . $theme)
    ->get('stylesheets');

  /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
  $file_url_generator = \Drupal::service('file_url_generator');

  // Make sure the color stylesheet is included in the content.
  foreach ($stylesheets as $stylesheet) {
    $this
      ->assertSession()
      ->responseMatches('|' . $file_url_generator
      ->generateString($stylesheet) . '|');
    $stylesheet_content = implode("\n", file($stylesheet));
    $this
      ->assertStringContainsString('color: #123456', $stylesheet_content, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
  }
  $this
    ->drupalGet($settings_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $edit['scheme'] = $test_values['scheme'];
  $this
    ->drupalGet($settings_path);
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->drupalGet('<front>');
  $stylesheets = $this
    ->config('color.theme.' . $theme)
    ->get('stylesheets');
  foreach ($stylesheets as $stylesheet) {
    $stylesheet_content = implode("\n", file($stylesheet));
    $this
      ->assertStringContainsString('color: ' . $test_values['scheme_color'], $stylesheet_content, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
  }

  // Test with aggregated CSS turned on.
  $config = $this
    ->config('system.performance');
  $config
    ->set('css.preprocess', 1);
  $config
    ->save();
  $this
    ->drupalGet('<front>');
  $stylesheets = \Drupal::state()
    ->get('drupal_css_cache_files', []);
  $stylesheet_content = '';
  foreach ($stylesheets as $uri) {
    $stylesheet_content .= implode("\n", file(\Drupal::service('file_system')
      ->realpath($uri)));
  }
  $this
    ->assertStringNotContainsString('public://', $stylesheet_content, 'Make sure the color paths have been translated to local paths. (' . $theme . ')');
  $config
    ->set('css.preprocess', 0);
  $config
    ->save();
}