You are here

public function CriticalCssHeadTest::testCriticalCssInHead in Critical CSS 8

Test critical CSS inside <head> element.

File

tests/src/Functional/CriticalCssHeadTest.php, line 38

Class

CriticalCssHeadTest
Tests Critical CSS functionality on HTML HEAD.

Namespace

Drupal\Tests\critical_css\Functional

Code

public function testCriticalCssInHead() {

  // No critical CSS should be found when module is disabled.
  $this
    ->config('critical_css.settings')
    ->set("enabled", FALSE)
    ->save();
  $html = $this
    ->drupalGet("<front>");
  $originalCssFiles = $this
    ->getCssFiles($html);
  $this
    ->assertSession()
    ->responseNotContains('<style id="critical-css">');

  // Critical CSS should be found when module is enabled.
  $this
    ->config('critical_css.settings')
    ->set("enabled", TRUE)
    ->save();
  \Drupal::service("critical_css")
    ->reset();
  $this
    ->drupalGet("/user/login");
  $this
    ->assertSession()
    ->responseContains('<style id="critical-css">/* default-critical.css MOCK */</style>');

  // Homepage's critical CSS should be found when accessing homepage.
  \Drupal::service("critical_css")
    ->reset();
  drupal_flush_all_caches();
  $this
    ->drupalGet("<front>");
  $this
    ->assertSession()
    ->responseContains('<style id="critical-css">/* front.css MOCK */</style>');
  NodeType::create([
    'type' => 'article',
  ])
    ->save();
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'title' => 'this is an article',
    'status' => 1,
  ]);
  $node
    ->save();

  // Node 1's critical CSS should be found when accessing /node/1.
  \Drupal::service("critical_css")
    ->reset();
  $this
    ->drupalGet("/node/1");
  $this
    ->assertSession()
    ->responseContains('<style id="critical-css">/* node-1.css MOCK */</style>');

  // Node 1's critical CSS should be not found when accessing /node/1 and
  // excluded_ids options is empty.
  \Drupal::service("critical_css")
    ->reset();
  drupal_flush_all_caches();
  $this
    ->config('critical_css.settings')
    ->set("excluded_ids", "1\n2")
    ->save();
  $this
    ->drupalGet("/node/1");
  $this
    ->assertSession()
    ->responseNotContains('<style id="critical-css">');

  // No critical CSS should be found when user is logged.
  \Drupal::service("critical_css")
    ->reset();
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->drupalGet("/user/1");
  $this
    ->assertSession()
    ->responseNotContains('<style id="critical-css">');

  // Critical CSS should be found when user is logged and
  // enabled_for_logged_in_users option is enabled.
  \Drupal::service("critical_css")
    ->reset();
  $this
    ->config('critical_css.settings')
    ->set("enabled_for_logged_in_users", TRUE)
    ->save();
  drupal_flush_all_caches();
  $this
    ->drupalGet("/user/1");
  $this
    ->assertSession()
    ->responseContains('<style id="critical-css">/* default-critical.css MOCK */</style>');

  // No non-critical CSS should be preloaded.
  \Drupal::service("critical_css")
    ->reset();
  $html = $this
    ->drupalGet("/user/1");
  $preloadedCssFiles = $this
    ->getCssFiles($html, '//link[@rel="preload" and @as="style"]');
  $this
    ->assertEmpty(array_intersect($originalCssFiles, $preloadedCssFiles), "No non-critical CSS should be preloaded.");

  // Non-critical CSS should be preloaded when preload_non_critical_css
  // options is enabled.
  \Drupal::service("critical_css")
    ->reset();
  $this
    ->config('critical_css.settings')
    ->set("preload_non_critical_css", TRUE)
    ->save();
  $html = $this
    ->drupalGet("/user/1");
  $preloadedCssFiles = $this
    ->getCssFiles($html, '//link[@rel="preload" and @as="style"]');
  $this
    ->assertEqualsArrays($originalCssFiles, $preloadedCssFiles, "Non-critical CSS should be preloaded when preload_non_critical_css options is enabled.");
}