css.test 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Test menu links depending on user permissions.
  4. */
  5. class CtoolsCssTestCase extends DrupalWebTestCase {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'CSS Tools tests',
  12. 'description' => 'Confirm the custom CSS handling works.',
  13. 'group' => 'ctools',
  14. 'dependencies' => array('ctools'),
  15. );
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function setUp(array $modules = array()) {
  21. $modules[] = 'ctools';
  22. parent::setUp($modules);
  23. ctools_include('css');
  24. }
  25. /**
  26. * Test that Stored CSS snippets can be retrieved, filtered or otherwise.
  27. */
  28. public function testCssStoreFilterRetrieve() {
  29. $css = "#some-id .some-class {\n color: black;\n illegal-key: foo;\n}";
  30. $filtered_css = '#some-id .some-class{color:black;}';
  31. $this->assertNull(ctools_css_retrieve('missing-css-test'), 'Missing css snippet is not found');
  32. $filename1 = ctools_css_store('unfiltered-css-test', $css, FALSE);
  33. $filename2 = ctools_css_store('filtered-css-test', $css, TRUE);
  34. $file_contents = file_get_contents($filename1);
  35. $this->assertEqual($css, $file_contents, 'Unfiltered css file contents are correct');
  36. $this->assertEqual($filename1, ctools_css_retrieve('unfiltered-css-test'), 'Unfiltered css file successfully fetched');
  37. $file_contents = file_get_contents($filename1);
  38. $this->assertEqual($css, $file_contents, 'Unfiltered css file contents are correct');
  39. $this->assertEqual($filename2, ctools_css_retrieve('filtered-css-test'), 'Filtered css file succcesfully fetched');
  40. $file_contents = file_get_contents($filename2);
  41. $this->assertEqual($filtered_css, $file_contents, 'Filtered css file contents are correct');
  42. }
  43. /**
  44. * Test that Stored CSS snippets can be correctly overwritten.
  45. */
  46. public function testCssStoreOverwrite() {
  47. $css1 = "#some-id .some-class {\n color: black;\n illegal-key: foo;\n}";
  48. $css2 = "#other-id .other-class {\n color: blue;\n illegal-key: foo;\n}";
  49. $filtered_css2 = '#other-id .other-class{color:blue;}';
  50. ctools_css_store('unfiltered-css-test', $css1, FALSE);
  51. ctools_css_store('filtered-css-test', $css1, TRUE);
  52. // Now overwrite the first css with the second version.
  53. $filename3 = ctools_css_store('unfiltered-css-test', $css2, FALSE);
  54. $filename4 = ctools_css_store('filtered-css-test', $css2, TRUE);
  55. $file_contents3 = file_get_contents($filename3);
  56. $file_contents4 = file_get_contents($filename4);
  57. $this->assertEqual($css2, $file_contents3, 'Unfiltered CSS has overwritten earlier contents.');
  58. $this->assertEqual($filtered_css2, $file_contents4, 'Filtered CSS has overwritten earlier contents.');
  59. }
  60. /**
  61. * Test that in case that url is used, the colons survives filtering.
  62. */
  63. public function testCssFilterURLHandling() {
  64. $css = "#some-id {\n background-image: url(http://example.com/example.gif);\n}";
  65. $css_data = ctools_css_disassemble($css);
  66. $empty_array = array();
  67. $disallowed_values_regex = '/(expression)/';
  68. $intermediate = ctools_css_filter_css_data($css_data, $empty_array, $empty_array, '', $disallowed_values_regex);
  69. $filtered = ctools_css_assemble($intermediate);
  70. $url = (strpos($filtered, 'http://example.com/example.gif') !== FALSE);
  71. $this->assertTrue($url, 'CSS with multiple colons can survive.');
  72. }
  73. /**
  74. * Test that when the CSS has two properties defined they are merged.
  75. */
  76. public function testCssFilterMergeProperties() {
  77. $css = "#some-id {\n font-size: 12px;\n}\n#some-id {\n color: blue;\n}";
  78. $filtered = ctools_css_filter($css);
  79. $font_size = (strpos($filtered, 'font-size:12px;') !== FALSE);
  80. $color = (strpos($filtered, 'color:blue') !== FALSE);
  81. $this->assertTrue($font_size && $color, 'Multiple properties are merged.');
  82. $css = '@import url("other.css");p {color: red;}';
  83. $filtered = ctools_css_filter($css);
  84. $other_css = (strpos($filtered, 'other.css') === FALSE);
  85. $color = (strpos($filtered, 'color:red') !== FALSE);
  86. $this->assertTrue($other_css && $color, 'CSS is properly sanitized.');
  87. $css = ';p {color: red; font-size: 12px;}';
  88. $filtered = ctools_css_filter($css);
  89. $font_size = (strpos($filtered, 'font-size:12px;') !== FALSE);
  90. $color = (strpos($filtered, 'color:red') !== FALSE);
  91. $this->assertTrue($font_size && $color, 'Multiple properties are retained.');
  92. }
  93. }