InlineCssLibTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. App::uses('InlineCssLib', 'Tools.Lib');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class InlineCssLibTest extends MyCakeTestCase {
  5. public function setUp() {
  6. $this->InlineCss = new InlineCssLib(['engine' => InlineCssLib::ENGINE_CSS_TO_INLINE]);
  7. $result = App::import('Vendor', 'Tools.CssToInlineStyles', ['file' => 'CssToInlineStyles' . DS . 'CssToInlineStyles.php']);
  8. $this->skipIf(!$result);
  9. parent::setUp();
  10. }
  11. /**
  12. * InlineCssLibTest::testProcess()
  13. *
  14. * @return void
  15. */
  16. public function testProcess() {
  17. $result = $this->InlineCss->process($this->testHtml);
  18. $this->debug($this->testHtml);
  19. $this->debug($result);
  20. }
  21. /**
  22. * InlineCssLibTest::testProcessPlainPiece()
  23. *
  24. * @return void
  25. */
  26. public function testProcessPlainPiece() {
  27. $html = 'blabla
  28. <style>
  29. div#container { margin: 1em auto; }
  30. h1 { font-weight: bold; font-size: 2em; }
  31. p { margin-bottom: 1em; font-family: sans-serif; text-align: justify; }
  32. p.small { font-size: 70%; }
  33. </style>
  34. <div id="container">
  35. <h1>Sample Page Title</h1>
  36. <p>Bacon ipsum dolor sit amet in cow elit, in t-bone qui meatloaf corned beef aute ullamco minim. Consequat swine short ribs pastrami jerky.</p>
  37. <p class="small">Some small note!</p>
  38. </div>
  39. bla';
  40. $result = $this->InlineCss->process($html);
  41. $this->debug($html);
  42. $this->debug($result);
  43. }
  44. public $testHtml = '<!doctype html>
  45. <html lang="en">
  46. <head>
  47. <style>
  48. body { font: 11px/20px Georgia, "Times New Roman", Times, serif; }
  49. div#container { margin: 1em auto; }
  50. h1 { font-weight: bold; font-size: 2em; }
  51. p { margin-bottom: 1em; font-family: sans-serif; text-align: justify; }
  52. p.small { font-size: 70%; }
  53. </style>
  54. <title>Example</title>
  55. </head>
  56. <body>
  57. <div id="container">
  58. <h1>Sample Page Title</h1>
  59. <p>Bacon ipsum dolor sit amet in cow elit, in t-bone qui meatloaf corned beef aute ullamco minim. Consequat swine short ribs pastrami jerky.</p>
  60. <p class="small">Some small note!</p>
  61. </div>
  62. </body>
  63. </html>';
  64. /**
  65. * InlineCssLibTest::testProcessUtf8()
  66. *
  67. * @return void
  68. */
  69. public function testProcessUtf8() {
  70. $this->skipIf(version_compare(PHP_VERSION, '5.4.0') < 0, 'UTF8 only works with PHP5.4 and above');
  71. $html = 'チェック
  72. <style>
  73. div#container { margin: 1em auto; }
  74. h1 { font-weight: bold; font-size: 2em; }
  75. p { margin-bottom: 1em; font-family: sans-serif; text-align: justify; }
  76. p.small { font-size: 70%; }
  77. </style>
  78. <div id="container">
  79. <h1>チェック</h1>
  80. <p>降です。アーリーチェックインはリクエ</p>
  81. <p class="small">チェック\'foo\'</p>
  82. </div>
  83. bla';
  84. $result = $this->InlineCss->process($html);
  85. $this->debug($html);
  86. $this->debug($result);
  87. $this->assertTextStartsWith('チェック', $result);
  88. $this->assertTextContains('<h1 style="font-size: 2em; font-weight: bold;">チェック</h1>', $result);
  89. }
  90. /**
  91. * InlineCssLibTest::testProcessSpecialChars()
  92. *
  93. * @return void
  94. */
  95. public function testProcessSpecialChars() {
  96. $this->skipIf(version_compare(PHP_VERSION, '5.4.0') < 0, 'UTF8 only works with PHP5.4 and above');
  97. $html = '<style>
  98. div#container { margin: 1em auto; }
  99. h1 { font-weight: bold; font-size: 2em; }
  100. table tr td { margin-bottom: 1em; font-family: sans-serif; text-align: justify; }
  101. </style>
  102. <div id="container">
  103. <h1>&laquo;X&raquo; &amp; Y &hellip;</h1>
  104. <table>
  105. <tr>
  106. <td style="font-size: 0; line-height: 0;" height="20" colspan="2">&nbsp;</td>
  107. </tr>
  108. </table>
  109. </div>
  110. bla';
  111. $result = $this->InlineCss->process($html);
  112. $expected = '<td style="font-family: sans-serif; margin-bottom: 1em; text-align: justify; font-size: 0; line-height: 0;" height="20" colspan="2">&nbsp;</td>';
  113. $this->debug($result);
  114. $this->assertTextContains($expected, $result);
  115. }
  116. /**
  117. * InlineCssLibTest::testProcessAlternativeEngine()
  118. *
  119. * @return void
  120. */
  121. public function testProcessAlternativeEngine() {
  122. $this->out('Emogrifier');
  123. $this->InlineCss = new InlineCssLib(['engine' => InlineCssLib::ENGINE_EMOGRIFIER]);
  124. $html = '<b>blabla</b><style>
  125. div#container { margin: 1em auto; }
  126. h1 { font-weight: bold; font-size: 2em; }
  127. p { margin-bottom: 1em; font-family: sans-serif; text-align: justify; }
  128. p.small { font-size: 70%; }
  129. </style>
  130. <div id="container">
  131. <h1>Sample Page Title</h1>
  132. <p>Bacon ipsum dolor sit amet in cow elit, in t-bone qui meatloaf corned beef aute ullamco minim. Consequat swine short ribs pastrami jerky.</p>
  133. <p class="small">Some small note!</p>
  134. </div>
  135. bla';
  136. $result = $this->InlineCss->process($html);
  137. $this->debug($html);
  138. $this->debug($result);
  139. }
  140. /**
  141. * InlineCssLibTest::testProcessUtf8()
  142. *
  143. * @return void
  144. */
  145. public function testProcessUtf8AlternativeEngine() {
  146. $this->InlineCss = new InlineCssLib(['engine' => InlineCssLib::ENGINE_EMOGRIFIER]);
  147. $html = 'チェック
  148. <style>
  149. div#container { margin: 1em auto; }
  150. h1 { font-weight: bold; font-size: 2em; }
  151. p { margin-bottom: 1em; font-family: sans-serif; text-align: justify; }
  152. p.small { font-size: 70%; }
  153. </style>
  154. <div id="container">
  155. <h1>チェック</h1>
  156. <p>降です。アーリーチェックインはリクエ</p>
  157. <p class="small">チェック\'foo\'</p>
  158. </div>
  159. bla';
  160. $result = $this->InlineCss->process($html);
  161. $this->debug($html);
  162. $this->debug($result);
  163. //$this->assertTextStartsWith('チェック', $result);
  164. //$this->assertTextContains('<h1 style="font-size: 2em; font-weight: bold;">チェック</h1>', $result);
  165. }
  166. /**
  167. * InlineCssLibTest::testProcessSpecialChars()
  168. *
  169. * @return void
  170. */
  171. public function testProcessSpecialCharsAlternativeEngine() {
  172. $this->InlineCss = new InlineCssLib(['engine' => InlineCssLib::ENGINE_EMOGRIFIER]);
  173. $html = '<style>
  174. div#container { margin: 1em auto; }
  175. h1 { font-weight: bold; font-size: 2em; }
  176. table tr td { margin-bottom: 1em; font-family: sans-serif; text-align: justify; }
  177. </style>
  178. <div id="container">
  179. <h1>&laquo;X&raquo; &amp; Y &hellip;</h1>
  180. <table>
  181. <tr>
  182. <td style="font-size: 0; line-height: 0;" height="20" colspan="2">&nbsp;</td>
  183. </tr>
  184. </table>
  185. </div>
  186. bla';
  187. $result = $this->InlineCss->process($html);
  188. $this->debug($result);
  189. }
  190. /**
  191. * InlineCssLibTest::testProcessSpecialChars()
  192. *
  193. * @return void
  194. */
  195. public function testProcessCompleteTemplateAlternativeEngine() {
  196. $path = CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'html' . DS;
  197. $this->InlineCss = new InlineCssLib(['engine' => InlineCssLib::ENGINE_EMOGRIFIER]);
  198. $html = file_get_contents($path . 'email_template.html');
  199. $result = $this->InlineCss->process($html);
  200. $this->debug($result);
  201. $expected = '<td style="vertical-align:';
  202. $this->assertTextContains($expected, $result);
  203. $this->InlineCss = new InlineCssLib(['engine' => InlineCssLib::ENGINE_EMOGRIFIER]);
  204. $html = file_get_contents($path . 'email_template_utf8.html');
  205. $result = $this->InlineCss->process($html);
  206. $this->debug($result);
  207. $expected = '<table cellspacing="0" cellpadding="0" style=\'font-family:';
  208. $this->assertTextContains($expected, $result);
  209. $this->assertTextContains('香港酒店', $result);
  210. }
  211. }