PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/core/modules/system/lib/Drupal/system/Tests/Common/CascadingStylesheetsTest.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 191 lines | 108 code | 27 blank | 56 comment | 3 complexity | 94b14040707f5e3d415ba6b45df02d49 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\system\Tests\Common\CascadingStylesheetsTest.
  5. */
  6. namespace Drupal\system\Tests\Common;
  7. use Drupal\Core\Language\Language;
  8. use Drupal\simpletest\DrupalUnitTestBase;
  9. /**
  10. * Tests the Drupal CSS system.
  11. */
  12. class CascadingStylesheetsTest extends DrupalUnitTestBase {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = array('language', 'system');
  19. public static function getInfo() {
  20. return array(
  21. 'name' => 'Cascading stylesheets',
  22. 'description' => 'Tests adding various cascading stylesheets to the page.',
  23. 'group' => 'Common',
  24. );
  25. }
  26. function setUp() {
  27. parent::setUp();
  28. // Reset _drupal_add_css() before each test.
  29. drupal_static_reset('_drupal_add_css');
  30. }
  31. /**
  32. * Checks that default stylesheets are empty.
  33. */
  34. function testDefault() {
  35. $this->assertEqual(array(), _drupal_add_css(), 'Default CSS is empty.');
  36. }
  37. /**
  38. * Tests adding a file stylesheet.
  39. */
  40. function testAddFile() {
  41. $path = drupal_get_path('module', 'simpletest') . '/css/simpletest.module.css';
  42. $css = _drupal_add_css($path);
  43. $this->assertEqual($css['simpletest.module.css']['data'], $path);
  44. }
  45. /**
  46. * Tests adding an external stylesheet.
  47. */
  48. function testAddExternal() {
  49. $path = 'http://example.com/style.css';
  50. $css = _drupal_add_css($path, 'external');
  51. $this->assertEqual($css[$path]['type'], 'external', 'Adding an external CSS file caches it properly.');
  52. }
  53. /**
  54. * Makes sure that resetting the CSS empties the cache.
  55. */
  56. function testReset() {
  57. drupal_static_reset('_drupal_add_css');
  58. $this->assertEqual(array(), _drupal_add_css(), 'Resetting the CSS empties the cache.');
  59. }
  60. /**
  61. * Tests rendering the stylesheets.
  62. */
  63. function testRenderFile() {
  64. $css = drupal_get_path('module', 'simpletest') . '/css/simpletest.module.css';
  65. _drupal_add_css($css);
  66. $styles = drupal_get_css();
  67. $this->assertTrue(strpos($styles, $css) > 0, 'Rendered CSS includes the added stylesheet.');
  68. // Verify that newlines are properly added inside style tags.
  69. $query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
  70. $css_processed = '<link rel="stylesheet" href="' . check_plain(file_create_url($css)) . "?" . $query_string . '" media="all" />';
  71. $this->assertEqual(trim($styles), $css_processed, 'Rendered CSS includes newlines inside style tags for JavaScript use.');
  72. }
  73. /**
  74. * Tests rendering an external stylesheet.
  75. */
  76. function testRenderExternal() {
  77. $css = 'http://example.com/style.css';
  78. _drupal_add_css($css, 'external');
  79. $styles = drupal_get_css();
  80. // Stylesheet URL may be the href of a LINK tag or in an @import statement
  81. // of a STYLE tag.
  82. $this->assertTrue(strpos($styles, 'href="' . $css) > 0 || strpos($styles, '@import url("' . $css . '")') > 0, 'Rendering an external CSS file.');
  83. }
  84. /**
  85. * Tests rendering inline stylesheets with preprocessing on.
  86. */
  87. function testRenderInlinePreprocess() {
  88. // Turn on CSS aggregation to allow for preprocessing.
  89. $config = $this->container->get('config.factory')->get('system.performance');
  90. $config->set('css.preprocess', 1);
  91. $css = 'body { padding: 0px; }';
  92. $css_preprocessed = '<style media="all">' . "\n/* <![CDATA[ */\n" . "body{padding:0px;}\n" . "\n/* ]]> */\n" . '</style>';
  93. _drupal_add_css($css, array('type' => 'inline'));
  94. $styles = drupal_get_css();
  95. $this->assertEqual(trim($styles), $css_preprocessed, 'Rendering preprocessed inline CSS adds it to the page.');
  96. }
  97. /**
  98. * Tests rendering inline stylesheets with preprocessing off.
  99. */
  100. function testRenderInlineNoPreprocess() {
  101. $css = 'body { padding: 0px; }';
  102. _drupal_add_css($css, array('type' => 'inline', 'preprocess' => FALSE));
  103. $styles = drupal_get_css();
  104. $this->assertTrue(strpos($styles, $css) > 0, 'Rendering non-preprocessed inline CSS adds it to the page.');
  105. }
  106. /**
  107. * Tests CSS ordering.
  108. */
  109. function testRenderOrder() {
  110. // Load a module CSS file.
  111. _drupal_add_css(drupal_get_path('module', 'simpletest') . '/css/simpletest.module.css');
  112. // Load a few system CSS files in a custom, early-loading aggregate group.
  113. $test_aggregate_group = -100;
  114. $system_path = drupal_get_path('module', 'system');
  115. _drupal_add_css($system_path . '/css/system.module.css', array('group' => $test_aggregate_group, 'weight' => -10));
  116. _drupal_add_css($system_path . '/css/system.theme.css', array('group' => $test_aggregate_group));
  117. $expected = array(
  118. $system_path . '/css/system.module.css',
  119. $system_path . '/css/system.theme.css',
  120. drupal_get_path('module', 'simpletest') . '/css/simpletest.module.css',
  121. );
  122. $styles = drupal_get_css();
  123. // Stylesheet URL may be the href of a LINK tag or in an @import statement
  124. // of a STYLE tag.
  125. if (preg_match_all('/(href="|url\(")' . preg_quote($GLOBALS['base_url'] . '/', '/') . '([^?]+)\?/', $styles, $matches)) {
  126. $result = $matches[2];
  127. }
  128. else {
  129. $result = array();
  130. }
  131. $this->assertIdentical($result, $expected, 'The CSS files are in the expected order.');
  132. }
  133. /**
  134. * Tests CSS override.
  135. */
  136. function testRenderOverride() {
  137. $system = drupal_get_path('module', 'system');
  138. _drupal_add_css($system . '/css/system.module.css');
  139. _drupal_add_css($system . '/tests/css/system.module.css');
  140. // The dummy stylesheet should be the only one included.
  141. $styles = drupal_get_css();
  142. $this->assert(strpos($styles, $system . '/tests/css/system.module.css') !== FALSE, 'The overriding CSS file is output.');
  143. $this->assert(strpos($styles, $system . '/css/system.module.css') === FALSE, 'The overridden CSS file is not output.');
  144. _drupal_add_css($system . '/tests/css/system.module.css');
  145. _drupal_add_css($system . '/css/system.module.css');
  146. // The standard stylesheet should be the only one included.
  147. $styles = drupal_get_css();
  148. $this->assert(strpos($styles, $system . '/css/system.module.css') !== FALSE, 'The overriding CSS file is output.');
  149. $this->assert(strpos($styles, $system . '/tests/css/system.module.css') === FALSE, 'The overridden CSS file is not output.');
  150. }
  151. /**
  152. * Tests that CSS query string remains intact when added to file.
  153. */
  154. function testAddCssFileWithQueryString() {
  155. $css_without_query_string = drupal_get_path('module', 'node') . '/css/node.admin.css';
  156. $css_with_query_string = '/' . drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&arg2=value2';
  157. _drupal_add_css($css_without_query_string);
  158. _drupal_add_css($css_with_query_string);
  159. $styles = drupal_get_css();
  160. $query_string = $this->container->get('state')->get('system.css_js_query_string') ?: '0';
  161. $this->assertTrue(strpos($styles, $css_without_query_string . '?' . $query_string), 'Query string was appended correctly to css.');
  162. $this->assertTrue(strpos($styles, str_replace('&', '&amp;', $css_with_query_string)), 'Query string not escaped on a URI.');
  163. }
  164. }