You are here

public function AdvAggCascadingStylesheetsTestCase::testRenderFile in Advanced CSS/JS Aggregation 7.2

Tests rendering the stylesheets.

File

tests/advagg.test, line 189
Tests for advagg.module.

Class

AdvAggCascadingStylesheetsTestCase
Test the Drupal CSS system.

Code

public function testRenderFile() {
  foreach ($this->themes as $theme => $test_values) {
    variable_set('theme_default', $theme);
    $settings_path = 'admin/appearance/settings/' . $theme;
    $this
      ->drupalLogin($this->bigUser);
    $this
      ->drupalGet($settings_path);
    $this
      ->assertResponse(200);
    $edit['scheme'] = '';
    $edit[$test_values['palette_input']] = '#123456';
    $this
      ->drupalPost($settings_path, $edit, t('Save configuration'));

    // Reset drupal_add_css() before each test.
    $GLOBALS['conf']['advagg_convert_absolute_to_relative_path'] = FALSE;
    $GLOBALS['conf']['advagg_convert_absolute_to_protocol_relative_path'] = FALSE;
    advagg_test_reset_statics();

    // Add the css file.
    $stylesheets = variable_get('color_' . $theme . '_stylesheets', array());
    drupal_add_css($stylesheets[0]);
    $css = file_create_url($stylesheets[0]);

    // Get the render array.
    $full_css = advagg_get_css();
    $styles = drupal_render($full_css);
    $this
      ->assertTrue(strpos($styles, $css) !== FALSE, "Rendered CSS includes the added stylesheet ({$css}).");
  }

  // Reset drupal_add_css() before each test.
  advagg_test_reset_statics();

  // Add the css file.
  $css = drupal_get_path('module', 'simpletest') . '/simpletest.css';
  drupal_add_css($css);

  // Get the render array.
  $full_css = advagg_get_css();

  // Render the CSS.
  $styles = drupal_render($full_css);
  $this
    ->assertTrue(strpos($styles, $css) > 0, "Rendered CSS includes the added stylesheet ({$css}).");

  // Verify that newlines are properly added inside style tags.
  $query_string = variable_get('css_js_query_string', '0');
  $css_processed = "<style type=\"text/css\" media=\"all\">\n@import url(\"" . check_plain(file_create_url($css)) . "?" . $query_string . "\");\n</style>";
  $this
    ->assertEqual(trim($styles), $css_processed, 'Rendered CSS includes newlines inside style tags for JavaScript use.');

  //
  // Tests rendering an external stylesheet.
  advagg_test_reset_statics();

  // Add the css file.
  $css = 'http://example.com/style.css';
  drupal_add_css($css, 'external');

  // Get the render array.
  $full_css = advagg_get_css();

  // Render the CSS.
  $styles = drupal_render($full_css);

  // Stylesheet URL may be the href of a LINK tag or in an @import statement
  // of a STYLE tag.
  $this
    ->assertTrue(strpos($styles, 'href="' . $css) > 0 || strpos($styles, '@import url("' . $css . '")') > 0, 'Rendering an external CSS file.');

  //
  // Tests rendering inline stylesheets with preprocessing on.
  advagg_test_reset_statics();

  // Add the inline css.
  $css = 'body { padding: 0px; }';
  list($embed_prefix, $embed_suffix) = advagg_get_css_prefix_suffix();
  $css_preprocessed = '<style type="text/css" media="all">' . $embed_prefix . advagg_load_stylesheet_content($css, TRUE) . $embed_suffix . '</style>';
  drupal_add_css($css, array(
    'type' => 'inline',
  ));

  // Get the render array.
  $full_css = advagg_get_css();

  // Render the CSS.
  $styles = drupal_render($full_css);
  $this
    ->assertEqual(trim($styles), $css_preprocessed, 'Rendering preprocessed inline CSS adds it to the page.');

  //
  // Tests removing charset when rendering stylesheets with preprocessing on.
  advagg_test_reset_statics();
  $cases = array(
    array(
      'asset' => '@charset "UTF-8";html{font-family:"sans-serif";}',
      'expected' => 'html{font-family:"sans-serif";}',
    ),
    // This asset contains extra \n character.
    array(
      'asset' => "@charset 'UTF-8';\nhtml{font-family:'sans-serif';}",
      'expected' => "\nhtml{font-family:'sans-serif';}",
    ),
  );
  foreach ($cases as $case) {
    $this
      ->assertEqual($case['expected'], advagg_load_stylesheet_content($case['asset']), 'CSS optimizing correctly removes the charset declaration.');
  }

  //
  // Tests rendering inline stylesheets with preprocessing off.
  advagg_test_reset_statics();

  // Add the inline css.
  $css = 'body { padding: 0px; }';
  drupal_add_css($css, array(
    'type' => 'inline',
    'preprocess' => FALSE,
  ));

  // Get the render array.
  $full_css = advagg_get_css();

  // Render the CSS.
  $styles = drupal_render($full_css);
  $this
    ->assertTrue(strpos($styles, $css) > 0, 'Rendering non-preprocessed inline CSS adds it to the page.');

  //
  // Test CSS ordering.
  advagg_test_reset_statics();

  // A module CSS file.
  drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');

  // A few system CSS files, ordered in a strange way.
  $system_path = drupal_get_path('module', 'system');
  drupal_add_css($system_path . '/system.menus.css', array(
    'group' => CSS_SYSTEM,
  ));
  drupal_add_css($system_path . '/system.base.css', array(
    'group' => CSS_SYSTEM,
    'weight' => -10,
  ));
  drupal_add_css($system_path . '/system.theme.css', array(
    'group' => CSS_SYSTEM,
  ));
  $expected = array(
    $system_path . '/system.base.css',
    $system_path . '/system.menus.css',
    $system_path . '/system.theme.css',
    drupal_get_path('module', 'simpletest') . '/simpletest.css',
  );

  // Get the render array.
  $full_css = advagg_get_css();

  // Render the CSS.
  $styles = drupal_render($full_css);

  // Stylesheet URL may be the href of a LINK tag or in an @import statement
  // of a STYLE tag.
  if (preg_match_all('/(href="|url\\(")' . preg_quote($GLOBALS['base_url'] . '/', '/') . '([^?]+)\\?/', $styles, $matches)) {
    $result = $matches[2];
  }
  else {
    $result = array();
  }
  $this
    ->assertIdentical($result, $expected, 'The CSS files are in the expected order.');

  //
  // Test CSS override.
  advagg_test_reset_statics();
  $system = drupal_get_path('module', 'system');
  $simpletest = drupal_get_path('module', 'simpletest');
  drupal_add_css($system . '/system.base.css');
  drupal_add_css($simpletest . '/tests/system.base.css');

  // The dummy stylesheet should be the only one included.
  // Get the render array.
  $full_css = advagg_get_css();

  // Render the CSS.
  $styles = drupal_render($full_css);
  $this
    ->assert(strpos($styles, $simpletest . '/tests/system.base.css') !== FALSE, 'The overriding CSS file is output.');
  $this
    ->assert(strpos($styles, $system . '/system.base.css') === FALSE, 'The overridden CSS file is not output.');

  // The reset is needed here until this is fixed
  // https://www.drupal.org/node/1388546
  advagg_test_reset_statics();
  drupal_add_css($simpletest . '/tests/system.base.css');
  drupal_add_css($system . '/system.base.css');

  // Get the render array.
  $full_css = advagg_get_css();

  // Render the CSS.
  $styles = drupal_render($full_css);

  // The standard stylesheet should be the only one included.
  $this
    ->assert(strpos($styles, $system . '/system.base.css') !== FALSE, 'The overriding CSS file is output.');
  $this
    ->assert(strpos($styles, $simpletest . '/tests/system.base.css') === FALSE, 'The overridden CSS file is not output.');

  //
  //  Tests Locale module's CSS Alter to include RTL overrides.
  advagg_test_reset_statics();

  // Switch the language to a right to left language and add system.base.css.
  global $language;
  $language->direction = LANGUAGE_RTL;
  $path = drupal_get_path('module', 'system');
  drupal_add_css($path . '/system.base.css', array(
    'group' => CSS_SYSTEM,
  ));
  drupal_add_css($path . '/system.menus.css', array(
    'group' => CSS_SYSTEM,
  ));
  drupal_add_css($path . '/system.theme.css', array(
    'group' => CSS_SYSTEM,
  ));

  // Get the render array.
  $full_css = advagg_get_css();

  // Render the CSS.
  $styles = drupal_render($full_css);

  // Check to see if system.base-rtl.css was also added.
  $base_pos = strpos($styles, $path . '/system.base.css');
  $base_rtl_pos = strpos($styles, $path . '/system.base-rtl.css');
  $this
    ->assert($base_rtl_pos !== FALSE, 'CSS is alterable as right to left overrides are added.');
  $this
    ->assert($base_pos < $base_rtl_pos, 'system.base-rtl.css is added after system.base.css.');

  // Check to see if system.menus-rtl.css was also added.
  $menus_pos = strpos($styles, $path . '/system.menus.css');
  $menus_rtl_pos = strpos($styles, $path . '/system.menus-rtl.css');
  $this
    ->assert($menus_rtl_pos !== FALSE, 'CSS is alterable as right to left overrides are added.');
  $this
    ->assert($menus_pos < $menus_rtl_pos, 'system.menus-rtl.css is added after system.menus.css.');

  // Check to see if system.theme-rtl.css was also added.
  $theme_pos = strpos($styles, $path . '/system.theme.css');
  $theme_rtl_pos = strpos($styles, $path . '/system.theme-rtl.css');
  $this
    ->assert($theme_rtl_pos !== FALSE, 'CSS is alterable as right to left overrides are added.');
  $this
    ->assert($theme_pos < $theme_rtl_pos, 'system.theme-rtl.css is added after system.theme.css.');

  // Change the language back to left to right.
  $language->direction = LANGUAGE_LTR;

  //
  // Tests rendering inline stylesheets through a full page request.
  advagg_test_reset_statics();
  $css = 'body { font-size: 254px; }';

  // Inline CSS is minified unless 'preprocess' => FALSE is passed as a
  // drupal_add_css() option.
  $expected = 'body{font-size:254px;}';

  // Create a node, using the PHP filter that tests drupal_add_css().
  $php_format_id = 'php_code';
  $settings = array(
    'type' => 'page',
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => t('This tests the inline CSS!') . "<?php drupal_add_css('{$css}', 'inline'); ?>",
          'format' => $php_format_id,
        ),
      ),
    ),
    'promote' => 1,
  );
  $node = $this
    ->drupalCreateNode($settings);

  // Fetch the page.
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertRaw($expected, 'Inline stylesheets appear in the full page rendering.');

  //
  // Tests that the query string remains intact when adding CSS files that
  // have query string parameters.
  advagg_test_reset_statics();
  $this
    ->drupalGet('common-test/query-string');
  $query_string = variable_get('css_js_query_string', '0');
  $this
    ->assertRaw(drupal_get_path('module', 'node') . '/node.css?' . $query_string, 'Query string was appended correctly to css.');
  $this
    ->assertRaw(drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&amp;arg2=value2', 'Query string not escaped on a URI.');

  //
  // Make the tests below more robust by explicitly setting the default theme
  // and administrative theme that they expect.
  theme_enable(array(
    'bartik',
  ));
  variable_set('theme_default', 'bartik');
  variable_set('admin_theme', 'seven');

  // Test the theme callback when it is set to use an administrative theme.
  advagg_test_reset_statics();
  $this
    ->drupalGet('menu-test/theme-callback/use-admin-theme');
  $this
    ->assertText('Custom theme: seven. Actual theme: seven.', 'The administrative theme can be correctly set in a theme callback.');
  $this
    ->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page.");

  //
  // Test that the theme callback is properly inherited.
  advagg_test_reset_statics();
  $this
    ->drupalGet('menu-test/theme-callback/use-admin-theme/inheritance');
  $this
    ->assertText('Custom theme: seven. Actual theme: seven. Theme callback inheritance is being tested.', 'Theme callback inheritance correctly uses the administrative theme.');
  $this
    ->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page.");

  //
  // Test the theme callback when the site is in maintenance mode.
  advagg_test_reset_statics();
  variable_set('maintenance_mode', TRUE);

  // For a regular user, the fact that the site is in maintenance mode means
  // we expect the theme callback system to be bypassed entirely.
  $this
    ->drupalGet('menu-test/theme-callback/use-admin-theme');
  $this
    ->assertRaw('bartik/css/style.css', "The maintenance theme's CSS appears on the page.");

  // An administrator, however, should continue to see the requested theme.
  $admin_user = $this
    ->drupalCreateUser(array(
    'access site in maintenance mode',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('menu-test/theme-callback/use-admin-theme');
  $this
    ->assertText('Custom theme: seven. Actual theme: seven.', 'The theme callback system is correctly triggered for an administrator when the site is in maintenance mode.');
  $this
    ->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page.");
  variable_set('maintenance_mode', FALSE);

  //
  // Test the theme callback when it is set to use an optional theme.
  advagg_test_reset_statics();

  // Request a theme that is not enabled.
  $this
    ->drupalGet('menu-test/theme-callback/use-stark-theme');
  $this
    ->assertText('Custom theme: NONE. Actual theme: bartik.', 'The theme callback system falls back on the default theme when a theme that is not enabled is requested.');
  $this
    ->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page.");

  // Now enable the theme and request it again.
  theme_enable(array(
    'stark',
  ));
  $this
    ->drupalGet('menu-test/theme-callback/use-stark-theme');
  $this
    ->assertText('Custom theme: stark. Actual theme: stark.', 'The theme callback system uses an optional theme once it has been enabled.');
  $this
    ->assertRaw('stark/layout.css', "The optional theme's CSS appears on the page.");

  // Test the theme callback when it is set to use a theme that does not
  // exist.
  $this
    ->drupalGet('menu-test/theme-callback/use-fake-theme');
  $this
    ->assertText('Custom theme: NONE. Actual theme: bartik.', 'The theme callback system falls back on the default theme when a theme that does not exist is requested.');
  $this
    ->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page.");

  //
  // Test the theme callback when no theme is requested.
  advagg_test_reset_statics();
  $this
    ->drupalGet('menu-test/theme-callback/no-theme-requested');
  $this
    ->assertText('Custom theme: NONE. Actual theme: bartik.', 'The theme callback system falls back on the default theme when no theme is requested.');
  $this
    ->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page.");

  //
  // Test that hook_custom_theme() can control the theme of a page.
  advagg_test_reset_statics();

  // Trigger hook_custom_theme() to dynamically request the Stark theme for
  // the requested page.
  variable_set('menu_test_hook_custom_theme_name', 'stark');
  theme_enable(array(
    'stark',
  ));

  // Visit a page that does not implement a theme callback. The above request
  // should be honored.
  $this
    ->drupalGet('menu-test/no-theme-callback');
  $this
    ->assertText('Custom theme: stark. Actual theme: stark.', 'The result of hook_custom_theme() is used as the theme for the current page.');
  $this
    ->assertRaw('stark/layout.css', "The Stark theme's CSS appears on the page.");

  //
  // Test that the theme callback wins out over hook_custom_theme().
  advagg_test_reset_statics();

  // Trigger hook_custom_theme() to dynamically request the Stark theme for
  // the requested page.
  variable_set('menu_test_hook_custom_theme_name', 'stark');
  theme_enable(array(
    'stark',
  ));

  // The menu "theme callback" should take precedence over a value set in
  // hook_custom_theme().
  $this
    ->drupalGet('menu-test/theme-callback/use-admin-theme');
  $this
    ->assertText('Custom theme: seven. Actual theme: seven.', 'The result of hook_custom_theme() does not override what was set in a theme callback.');
  $this
    ->assertRaw('seven/style.css', "The Seven theme's CSS appears on the page.");

  //
  // Test css split file processing.
  // Generate a massive css file.
  $css_string = advagg_test_generate_selector_css(1000);
  $css_string .= '@media print {' . advagg_test_generate_selector_css(1000) . '}';
  $css_string .= advagg_test_generate_selector_css(1000);
  $css_string .= '@media screen {' . advagg_test_generate_selector_css(1000) . '}';
  $css_string .= advagg_test_generate_selector_css(1000);
  $css_string .= '@media print {' . advagg_test_generate_selector_css(1000) . '}';
  $css_string .= advagg_test_generate_selector_css(9000);
  $css_string .= '@media print {' . advagg_test_generate_selector_css(9000) . '}';
  $css_string .= advagg_test_generate_selector_css(9000);
  $css_string .= '@media screen {' . advagg_test_generate_selector_css(9000) . '}';
  $css_string .= '@media print {' . advagg_test_generate_selector_css(50) . '}';
  $css_string .= '@media screen {' . advagg_test_generate_selector_css(50) . '}';
  $css_string .= advagg_test_generate_selector_css(10);
  $css_string .= '@media print {' . advagg_test_generate_selector_css(50) . '}';
  $css_string .= '@media screen {' . advagg_test_generate_selector_css(50) . '}';
  $css_string .= '@media print {' . advagg_test_generate_selector_css(50) . '}';
  $css_string .= '@media screen {' . advagg_test_generate_selector_css(50) . '}';
  $css_string .= advagg_test_generate_selector_css(10);
  $css_string .= advagg_test_generate_selector_css(10);
  $css_string .= advagg_test_generate_selector_css(10);
  $css_string .= advagg_test_generate_selector_css(10);
  $css_string .= advagg_test_generate_selector_css(15000);
  $css_string .= '@media print {' . advagg_test_generate_selector_css(15000) . '}';
  $css_string .= advagg_test_generate_selector_css(10);
  $css_string .= advagg_test_generate_selector_css(10);
  $css_string .= advagg_test_generate_selector_css(10);
  $css_string .= advagg_test_generate_selector_css(10);
  $css_string .= advagg_test_generate_selector_css(10);
  $file_info = array(
    // Use a file that exists but isn't actually being used here.
    'data' => drupal_get_path('module', 'advagg') . '/tests/css_test_files/advagg.css',
  );
  $before_selector_count = advagg_count_css_selectors($css_string);

  // Split the huge css file.
  $parts = advagg_split_css_file($file_info, $css_string);
  $after = '';
  foreach ($parts as $part) {

    // Get written file.
    $after .= "\n" . (string) @advagg_file_get_contents($part['data']);

    // Cleanup.
    unlink($part['data']);
  }

  // Note that a diff of the text can not be used for this test. Counting
  // selectors is close enough for now.
  $after_selector_count = advagg_count_css_selectors($after);
  $this
    ->assertEqual($before_selector_count, $after_selector_count, t('Expected %before selectors, got %after.', array(
    '%before' => $before_selector_count,
    '%after' => $after_selector_count,
  )));

  //
  // Test css file processing.
  advagg_test_reset_statics();

  // Array of files to test living in 'advagg/tests/css_test_files/'.
  // - Original: name.css
  // - Unoptimized expected content: name.css.unoptimized.css
  // - Optimized expected content: name.css.optimized.css
  //
  // File. Tests: css_input_without_import.css.
  // - Stripped comments and white-space.
  // - Retain white-space in selectors. (http://drupal.org/node/472820)
  // - Retain pseudo-selectors. (http://drupal.org/node/460448)
  //
  // File. Tests: css_input_with_import.css.
  // - Proper URLs in imported files. (http://drupal.org/node/265719)
  // - A background image with relative paths, which must be rewritten.
  // - The rewritten background image path must also be passed through
  //   file_create_url(). (https://drupal.org/node/1961340)
  // - Imported files that are external (protocol-relative URL or not)
  //   should not be expanded. (https://drupal.org/node/2014851)
  //
  // File in sub-folder. Tests: css_subfolder/css_input_with_import.css.
  // - CSS import path interpreted. (https://drupal.org/node/1198904)
  // - Don't adjust data URIs (https://drupal.org/node/2142441)
  //
  // File. Tests: comment_hacks.css.
  // - Retain comment hacks.
  $testfiles = array(
    'css_input_without_import.css',
    'css_input_with_import.css',
    'css_subfolder/css_input_with_import.css',
    'comment_hacks.css',
  );
  $path = drupal_get_path('module', 'advagg') . '/tests/css_test_files';
  foreach ($testfiles as $file) {
    $file_path = $path . '/' . $file;
    $file_url = $GLOBALS['base_url'] . '/' . $file_path;
    $expected = advagg_file_get_contents($file_path . '.unoptimized.css');
    $unoptimized_output = advagg_load_stylesheet($file_path, FALSE);
    $this
      ->assertEqual($unoptimized_output, $expected, format_string('Unoptimized CSS file has expected contents (@file)', array(
      '@file' => $file,
    )));
    $expected = advagg_file_get_contents($file_path . '.optimized.css');
    $expected = advagg_test_remove_sniffer_comments($expected);
    $optimized_output = advagg_load_stylesheet($file_path, TRUE);
    $this
      ->assertEqual($optimized_output, $expected, format_string('Optimized CSS file has expected contents (@file)', array(
      '@file' => $file,
    )));

    // Repeat the tests by accessing the stylesheets by URL.
    $expected = advagg_file_get_contents($file_path . '.unoptimized.css');
    $unoptimized_output_url = advagg_load_stylesheet($file_url, FALSE);
    $this
      ->assertEqual($unoptimized_output_url, $expected, format_string('Unoptimized CSS file (loaded from an URL) has expected contents (@file)', array(
      '@file' => $file,
    )));
    $expected = advagg_file_get_contents($file_path . '.optimized.css');
    $expected = advagg_test_remove_sniffer_comments($expected);
    $optimized_output_url = advagg_load_stylesheet($file_url, TRUE);
    $this
      ->assertEqual($optimized_output_url, $expected, format_string('Optimized CSS file (loaded from an URL) has expected contents (@file)', array(
      '@file' => $file,
    )));
  }

  // File. Tests: charset*.css
  // - Any @charaset declaration at the beginning of a file should be
  //   removed without breaking subsequent CSS.
  $testfiles = array(
    'charset.css',
    'charset_newline.css',
    'charset_sameline.css',
  );
  $path = drupal_get_path('module', 'advagg') . '/tests/css_test_files';
  foreach ($testfiles as $file) {
    $file_path = $path . '/' . $file;
    $file_url = $GLOBALS['base_url'] . '/' . $file_path;
    $expected = advagg_file_get_contents($file_path . '.optimized.css');
    $expected = advagg_test_remove_sniffer_comments($expected);
    $optimized_output = advagg_load_stylesheet($file_path, TRUE);
    $this
      ->assertEqual($optimized_output, $expected, format_string('Optimized CSS file has expected contents (@file)', array(
      '@file' => $file,
    )));
    $expected = advagg_file_get_contents($file_path . '.optimized.css');
    $expected = advagg_test_remove_sniffer_comments($expected);
    $optimized_output_url = advagg_load_stylesheet($file_url, TRUE);
    $this
      ->assertEqual($optimized_output_url, $expected, format_string('Optimized CSS file (loaded from an URL) has expected contents (@file)', array(
      '@file' => $file,
    )));
  }

  // Set all to FALSE.
  $GLOBALS['conf']['advagg_convert_absolute_to_relative_path'] = FALSE;
  $GLOBALS['conf']['advagg_convert_absolute_to_protocol_relative_path'] = FALSE;
  $GLOBALS['conf']['advagg_force_https_path'] = FALSE;
  $GLOBALS['conf']['advagg_skip_file_create_url_inside_css'] = FALSE;
  $settings_to_change = array(
    '' => '',
    'advagg_skip_file_create_url_inside_css' => TRUE,
    'advagg_convert_absolute_to_relative_path' => TRUE,
    'advagg_convert_absolute_to_protocol_relative_path' => TRUE,
    'advagg_force_https_path' => TRUE,
  );
  $advagg_path = drupal_get_path('module', 'advagg');
  $path = $advagg_path . '/tests/css_test_files';
  foreach ($settings_to_change as $name => $value) {
    $before = '';
    if (!empty($name)) {
      $before = variable_get($name, defined(strtoupper($name)) ? constant(strtoupper($name)) : NULL);
      $GLOBALS['conf'][$name] = $value;
    }

    // File. Tests: advagg.css
    // - Various url() tests.
    //   https://www.drupal.org/node/1514182
    //   https://www.drupal.org/node/1961340
    //   https://www.drupal.org/node/2362643
    //   https://www.drupal.org/node/2112067
    $testfiles = array(
      'advagg.css',
    );
    foreach ($testfiles as $testfile) {
      $base_url_before = $GLOBALS['base_url'];
      $GLOBALS['base_url'] = advagg_force_http_path($GLOBALS['base_url']);
      $aggregate_settings = array(
        'variables' => array(
          'is_https' => FALSE,
          'base_path' => $GLOBALS['base_path'] === '/checkout/' ? $GLOBALS['base_path'] : $GLOBALS['base_path'] . 'advagg_base_path_test/',
          'advagg_convert_absolute_to_relative_path' => $GLOBALS['conf']['advagg_convert_absolute_to_relative_path'],
          'advagg_convert_absolute_to_protocol_relative_path' => $GLOBALS['conf']['advagg_convert_absolute_to_protocol_relative_path'],
          'advagg_force_https_path' => $GLOBALS['conf']['advagg_force_https_path'],
          'advagg_skip_file_create_url_inside_css' => $GLOBALS['conf']['advagg_skip_file_create_url_inside_css'],
        ),
      );
      if (module_exists('cdn')) {
        $aggregate_settings['variables'][CDN_MODE_VARIABLE] = CDN_DISABLED;
        $aggregate_settings['variables'][CDN_STATUS_VARIABLE] = CDN_DISABLED;
      }
      $file_path = $path . '/' . $testfile;
      $files = array(
        'file' => $file_path,
        'external' => $GLOBALS['base_url'] . '/' . $file_path,
      );
      $expected = advagg_test_remove_sniffer_comments(advagg_file_get_contents($file_path . '.optimized.css'));
      foreach ($files as $type => $file) {
        $optimized_output = advagg_load_css_stylesheet($file, TRUE, $aggregate_settings);
        $mode = 0;
        if ($aggregate_settings['variables']['advagg_skip_file_create_url_inside_css'] && $type !== 'external') {
          $mode = "01";
          $optimized_output = str_replace($aggregate_settings['variables']['base_path'] . $advagg_path . '/', '', $optimized_output);
        }
        elseif (!$aggregate_settings['variables']['advagg_convert_absolute_to_relative_path'] && !$aggregate_settings['variables']['advagg_convert_absolute_to_protocol_relative_path'] && !$aggregate_settings['variables']['advagg_force_https_path']) {
          $mode = 4;
          $optimized_output = str_replace('http://' . $_SERVER['HTTP_HOST'] . $aggregate_settings['variables']['base_path'] . $advagg_path . '/', '', $optimized_output);
        }
        elseif ($aggregate_settings['variables']['advagg_convert_absolute_to_protocol_relative_path']) {
          $mode = 2;
          $optimized_output = str_replace('//' . $_SERVER['HTTP_HOST'] . $aggregate_settings['variables']['base_path'] . $advagg_path . '/', '', $optimized_output);
        }
        elseif ($aggregate_settings['variables']['advagg_force_https_path']) {
          $mode = 3;
          $optimized_output = str_replace('https://' . $_SERVER['HTTP_HOST'] . $aggregate_settings['variables']['base_path'] . $advagg_path . '/', '', $optimized_output);
        }
        else {
          $mode = 1;
          $optimized_output = str_replace($aggregate_settings['variables']['base_path'] . $advagg_path . '/', '', $optimized_output);
        }
        $this
          ->assertEqual($optimized_output, $expected, format_string("Optimized CSS file has expected contents (@file). Setting tested: @name; value before: @before, value after: @after.<br>mode: @mode. <p>!replacements</p> <p><code>!debug</code></p>", array(
          '@file' => $file,
          '@name' => $name,
          '@before' => is_bool($before) || strlen((string) $before) == 0 ? strtoupper(var_export($before, TRUE)) : $before,
          '@after' => is_bool($value) || strlen((string) $value) == 0 ? strtoupper(var_export($value, TRUE)) : $value,
          '@mode' => $mode,
          '!replacements' => "1: {$aggregate_settings['variables']['base_path']}{$advagg_path}/ <br> 2: //{$_SERVER['HTTP_HOST']}{$aggregate_settings['variables']['base_path']}{$advagg_path}/ <br> 3: https://{$_SERVER['HTTP_HOST']}{$aggregate_settings['variables']['base_path']}{$advagg_path}/ <br> 4: http://{$_SERVER['HTTP_HOST']}{$aggregate_settings['variables']['base_path']}{$advagg_path}/",
          '!debug' => nl2br(str_replace(' ', '&nbsp;', $optimized_output)),
        )));
        $GLOBALS['base_url'] = $base_url_before;
      }
    }
    if (!empty($name)) {
      $GLOBALS['conf'][$name] = $before;
    }
  }
}