/tests/Kwc/Mail/HtmlParser/Test.php

https://github.com/koala-framework/koala-framework · PHP · 523 lines · 456 code · 59 blank · 8 comment · 0 complexity · 315ba7aa79a49a131bf25f65178fb858 MD5 · raw file

  1. <?php
  2. /**
  3. * @group MailHtmlParser
  4. */
  5. class Kwc_Mail_HtmlParser_Test extends Kwf_Test_TestCase
  6. {
  7. private function _assertHtmlEquals($expected, $html)
  8. {
  9. $html = preg_replace('#^ +#m', '', $html);
  10. $html = str_replace("\n", '', $html);
  11. $this->assertEquals($expected, $html);
  12. }
  13. public function testIt()
  14. {
  15. $styles = array(
  16. array(
  17. 'tag' => 'p',
  18. 'styles' => array(
  19. 'font-family' => 'Verdana',
  20. 'font-size' => '18px'
  21. )
  22. ),
  23. array(
  24. 'tag' => 'p',
  25. 'class' => 'red',
  26. 'styles' => array(
  27. 'font-family' => 'Verdana',
  28. 'font-size' => '18px',
  29. 'color' => 'red'
  30. )
  31. ),
  32. array(
  33. 'tag' => 'h1',
  34. 'styles' => array(
  35. 'font-family' => 'Verdana',
  36. 'font-size' => '24px',
  37. 'font-weight' => 'bold'
  38. )
  39. ),
  40. array(
  41. 'tag' => 'strong',
  42. 'replaceTag' => 'b'
  43. ),
  44. );
  45. $p = new Kwc_Mail_HtmlParser($styles);
  46. $html = '<p>Lorem Ipsum</p>';
  47. $html = $p->parse($html);
  48. $this->_assertHtmlEquals('<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><p style="font-size: 18px; "><font face="Verdana">Lorem Ipsum</font></p></html>', $html);
  49. $html = '<p class="red">Lorem Ipsum</p>';
  50. $html = $p->parse($html);
  51. $this->_assertHtmlEquals('<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><p class="red" style="font-size: 18px; "><font face="Verdana" color="red">Lorem Ipsum</font></p></html>', $html);
  52. $html = '<h1>Lorem Ipsum</h1>';
  53. $html = $p->parse($html);
  54. $this->_assertHtmlEquals('<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><h1 style="font-size: 24px; "><font face="Verdana"><b>Lorem Ipsum</b></font></h1></html>', $html);
  55. $html = '<div>Lorem</div><div>Ipsum</div>';
  56. $html = $p->parse($html);
  57. $this->_assertHtmlEquals('<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><div>Lorem</div><div>Ipsum</div></html>', $html);
  58. $html = '<strong foo="bar">Lorem Ipsum</strong>';
  59. $html = $p->parse($html);
  60. $this->_assertHtmlEquals('<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><p style="font-size: 18px; "><font face="Verdana"><b>Lorem Ipsum</b></font></p></html>', $html);
  61. $html = 'Lorem<br />Ipsum';
  62. $html = $p->parse($html);
  63. $this->_assertHtmlEquals('<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><p style="font-size: 18px; "><font face="Verdana">Lorem<br />Ipsum</font></p></html>', $html);
  64. // wenn man ein nicht geschlossenes <br> rein gibt, macht das xml einen fehler,
  65. // geht aber normal weiter, deshalb dieser teil auskommentiert. siehe auch den kommentar beim HtmlParser
  66. /* $html = 'Lorem<br>Ipsum';
  67. $html = $p->parse($html);
  68. $this->_assertHtmlEquals('Lorem<br />Ipsum', $html);*/
  69. }
  70. public function testMore()
  71. {
  72. $styles = array(
  73. array(
  74. 'tag' => 'td',
  75. 'styles' => array(
  76. 'font-family' => 'Verdana',
  77. 'font-size' => '12px'
  78. ),
  79. )
  80. );
  81. $html = '<table><tr><td>Guten Tag, &NBSP; <strong>Frau Staterau!</strong></td><td>&nbsp;</td><td>Testtext</td></tr></table>';
  82. $expected= '<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><table><tr><td style="font-size: 12px; "><font face="Verdana">Guten Tag, &NBSP; <strong>Frau Staterau!</strong></font></td><td style="font-size: 12px; "><font face="Verdana">&nbsp;</font></td><td style="font-size: 12px; "><font face="Verdana">Testtext</font></td></tr></table></html>';
  83. $p = new Kwc_Mail_HtmlParser($styles);
  84. $html = $p->parse($html);
  85. $this->_assertHtmlEquals($expected, $html);
  86. }
  87. public function testMoreAddAttributesToExistingTag()
  88. {
  89. $styles = array(
  90. array(
  91. 'tag' => 'td',
  92. 'styles' => array(
  93. 'font-family' => 'Verdana',
  94. ),
  95. 'appendTags' => array(
  96. 'font' => array(
  97. 'color' => 'red'
  98. )
  99. )
  100. )
  101. );
  102. $html = '<table><tr><td>Testtext</td></tr></table>';
  103. $expected= '<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><table><tr><td><font color="red" face="Verdana">Testtext</font></td></tr></table></html>';
  104. $p = new Kwc_Mail_HtmlParser($styles);
  105. $html = $p->parse($html);
  106. $this->_assertHtmlEquals($expected, $html);
  107. }
  108. public function testSelector()
  109. {
  110. $styles = array(
  111. array(
  112. 'tag' => 'p',
  113. 'styles' => array(
  114. 'font-size' => '18px'
  115. ),
  116. ),
  117. array(
  118. 'selector' => 'table.foo p',
  119. 'styles' => array(
  120. 'font-size' => '12px'
  121. ),
  122. )
  123. );
  124. $html = '<table><tr><td><p>Blu bla Bli</p></td></tr></table>';
  125. $html .= '<table class="foo"><tr><td><p>Blu bla Bli</p></td></tr></table>';
  126. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  127. $expected .= '<head><title></title></head><table><tr><td><p style="font-size: 18px; ">Blu bla Bli</p></td></tr></table>';
  128. $expected .= '<table class="foo"><tr><td><p style="font-size: 12px; ">Blu bla Bli</p></td></tr></table>';
  129. $expected .= '</html>';
  130. $p = new Kwc_Mail_HtmlParser($styles);
  131. $html = $p->parse($html);
  132. $this->_assertHtmlEquals($expected, $html);
  133. }
  134. public function testAppendTagsKeyAsTag()
  135. {
  136. $styles = array(
  137. array(
  138. 'tag' => 'li',
  139. 'appendTags' => array(
  140. 'img' => array(
  141. 'src' => 'image1.png',
  142. ),
  143. )
  144. )
  145. );
  146. $html = '<table><tr><td><ul><li>Here</li></ul></td></tr></table>';
  147. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  148. $expected .= '<head><title></title></head><table><tr><td><ul><li><img src="image1.png"/>Here</li></ul></td></tr></table>';
  149. $expected .= '</html>';
  150. $p = new Kwc_Mail_HtmlParser($styles);
  151. $html = $p->parse($html);
  152. $this->_assertHtmlEquals($expected, $html);
  153. }
  154. public function testSelectorMultipleClasses1()
  155. {
  156. $styles = array(
  157. array(
  158. 'selector' => 'p.foo',
  159. 'styles' => array(
  160. 'font-size' => '12px'
  161. ),
  162. ),
  163. array(
  164. 'selector' => '.bar',
  165. 'styles' => array(
  166. 'line-height' => '100%'
  167. ),
  168. )
  169. );
  170. $html = '<p class="foo bar">Lorem Ipsum</p>';
  171. $html .= '<p class="foo">Lorem Ipsum</p>';
  172. $html .= '<p class="bar">Lorem Ipsum</p>';
  173. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  174. $expected .= '<head><title></title></head>';
  175. $expected .= '<p class="foo bar" style="font-size: 12px; line-height: 100%; ">Lorem Ipsum</p>';
  176. $expected .= '<p class="foo" style="font-size: 12px; ">Lorem Ipsum</p>';
  177. $expected .= '<p class="bar" style="line-height: 100%; ">Lorem Ipsum</p>';
  178. $expected .= '</html>';
  179. $p = new Kwc_Mail_HtmlParser($styles);
  180. $html = $p->parse($html);
  181. $this->_assertHtmlEquals($expected, $html);
  182. }
  183. public function testSelectorMultipleClasses2()
  184. {
  185. $styles = array(
  186. array(
  187. 'selector' => 'p.foo',
  188. 'styles' => array(
  189. 'font-size' => '12px'
  190. ),
  191. ),
  192. array(
  193. 'selector' => 'div.bar',
  194. 'styles' => array(
  195. 'line-height' => '100%'
  196. ),
  197. )
  198. );
  199. $html = '<p class="foo bar">Lorem Ipsum</p>';
  200. $html .= '<p class="foo">Lorem Ipsum</p>';
  201. $html .= '<p class="bar">Lorem Ipsum</p>';
  202. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  203. $expected .= '<head><title></title></head>';
  204. $expected .= '<p class="foo bar" style="font-size: 12px; ">Lorem Ipsum</p>';
  205. $expected .= '<p class="foo" style="font-size: 12px; ">Lorem Ipsum</p>';
  206. $expected .= '<p class="bar">Lorem Ipsum</p>';
  207. $expected .= '</html>';
  208. $p = new Kwc_Mail_HtmlParser($styles);
  209. $html = $p->parse($html);
  210. $this->_assertHtmlEquals($expected, $html);
  211. }
  212. public function testSelectorMultipleClasses3()
  213. {
  214. $styles = array(
  215. array(
  216. 'selector' => 'p',
  217. 'styles' => array(
  218. 'font-size' => '12px'
  219. ),
  220. ),
  221. array(
  222. 'selector' => '.bar',
  223. 'styles' => array(
  224. 'line-height' => '100%'
  225. ),
  226. )
  227. );
  228. $html = '<p class="foo bar">Lorem Ipsum</p>';
  229. $html .= '<p class="foo">Lorem Ipsum</p>';
  230. $html .= '<p class="bar">Lorem Ipsum</p>';
  231. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  232. $expected .= '<head><title></title></head>';
  233. $expected .= '<p class="foo bar" style="font-size: 12px; line-height: 100%; ">Lorem Ipsum</p>';
  234. $expected .= '<p class="foo" style="font-size: 12px; ">Lorem Ipsum</p>';
  235. $expected .= '<p class="bar" style="font-size: 12px; line-height: 100%; ">Lorem Ipsum</p>';
  236. $expected .= '</html>';
  237. $p = new Kwc_Mail_HtmlParser($styles);
  238. $html = $p->parse($html);
  239. $this->_assertHtmlEquals($expected, $html);
  240. }
  241. public function testAppendTagsTagInArray()
  242. {
  243. $styles = array(
  244. array(
  245. 'tag' => 'li',
  246. 'appendTags' => array(
  247. array(
  248. 'tag' => 'img',
  249. 'src' => 'image1.png',
  250. )
  251. )
  252. )
  253. );
  254. $html = '<table><tr><td><ul><li>Here</li></ul></td></tr></table>';
  255. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  256. $expected .= '<head><title></title></head><table><tr><td><ul><li><img src="image1.png"/>Here</li></ul></td></tr></table>';
  257. $expected .= '</html>';
  258. $p = new Kwc_Mail_HtmlParser($styles);
  259. $html = $p->parse($html);
  260. $this->_assertHtmlEquals($expected, $html);
  261. }
  262. public function testSelectorMultipleClasses4()
  263. {
  264. $styles = array(
  265. array(
  266. 'selector' => 'div',
  267. 'styles' => array(
  268. 'font-size' => '12px'
  269. ),
  270. ),
  271. array(
  272. 'selector' => '.bar',
  273. 'styles' => array(
  274. 'line-height' => '100%'
  275. ),
  276. )
  277. );
  278. $html = '<p class="foo bar">Lorem Ipsum</p>';
  279. $html .= '<p class="foo">Lorem Ipsum</p>';
  280. $html .= '<p class="bar">Lorem Ipsum</p>';
  281. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  282. $expected .= '<head><title></title></head>';
  283. $expected .= '<p class="foo bar" style="line-height: 100%; ">Lorem Ipsum</p>';
  284. $expected .= '<p class="foo">Lorem Ipsum</p>';
  285. $expected .= '<p class="bar" style="line-height: 100%; ">Lorem Ipsum</p>';
  286. $expected .= '</html>';
  287. $p = new Kwc_Mail_HtmlParser($styles);
  288. $html = $p->parse($html);
  289. $this->_assertHtmlEquals($expected, $html);
  290. }
  291. public function testAppendTagsMoreTagsImg()
  292. {
  293. $styles = array(
  294. array(
  295. 'tag' => 'li',
  296. 'appendTags' => array(
  297. array(
  298. 'tag' => 'img',
  299. 'src' => 'image1.png',
  300. ),
  301. array(
  302. 'tag' => 'img',
  303. 'src' => 'image2.png'
  304. )
  305. )
  306. )
  307. );
  308. $html = '<table><tr><td><ul><li>Here</li></ul></td></tr></table>';
  309. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  310. $expected .= '<head><title></title></head><table><tr><td><ul><li><img src="image1.png"/><img src="image2.png"/>Here</li></ul></td></tr></table>';
  311. $expected .= '</html>';
  312. $p = new Kwc_Mail_HtmlParser($styles);
  313. $html = $p->parse($html);
  314. $this->_assertHtmlEquals($expected, $html);
  315. }
  316. public function testSelectorMultipleClasses5()
  317. {
  318. $styles = array(
  319. array(
  320. 'selector' => '.foo.bar',
  321. 'styles' => array(
  322. 'font-size' => '12px'
  323. ),
  324. )
  325. );
  326. $html = '<p class="foo bar">Lorem Ipsum</p>';
  327. $html .= '<p class="foo">Lorem Ipsum</p>';
  328. $html .= '<p class="bar">Lorem Ipsum</p>';
  329. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  330. $expected .= '<head><title></title></head>';
  331. $expected .= '<p class="foo bar" style="font-size: 12px; ">Lorem Ipsum</p>';
  332. $expected .= '<p class="foo">Lorem Ipsum</p>';
  333. $expected .= '<p class="bar">Lorem Ipsum</p>';
  334. $expected .= '</html>';
  335. $p = new Kwc_Mail_HtmlParser($styles);
  336. $html = $p->parse($html);
  337. $this->_assertHtmlEquals($expected, $html);
  338. }
  339. public function testAppendTagsMoreTagsSpan()
  340. {
  341. $styles = array(
  342. array(
  343. 'tag' => 'li',
  344. 'appendTags' => array(
  345. array(
  346. 'tag' => 'span',
  347. 'style' => 'margin-left:10px',
  348. ),
  349. array(
  350. 'tag' => 'span',
  351. 'style' => 'margin-right:10px',
  352. )
  353. )
  354. )
  355. );
  356. $html = '<table><tr><td><ul><li>Here</li></ul></td></tr></table>';
  357. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  358. $expected .= '<head><title></title></head><table><tr><td><ul><li><span style="margin-left:10px"><span style="margin-right:10px">Here</span></span></li></ul></td></tr></table>';
  359. $expected .= '</html>';
  360. $p = new Kwc_Mail_HtmlParser($styles);
  361. $html = $p->parse($html);
  362. $this->_assertHtmlEquals($expected, $html);
  363. }
  364. public function testSelectorMultipleClasses6()
  365. {
  366. $styles = array(
  367. array(
  368. 'selector' => 'p.foo.bar',
  369. 'styles' => array(
  370. 'font-size' => '12px'
  371. ),
  372. )
  373. );
  374. $html = '<p class="foo bar">Lorem Ipsum</p>';
  375. $html .= '<p class="foo">Lorem Ipsum</p>';
  376. $html .= '<p class="bar">Lorem Ipsum</p>';
  377. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  378. $expected .= '<head><title></title></head>';
  379. $expected .= '<p class="foo bar" style="font-size: 12px; ">Lorem Ipsum</p>';
  380. $expected .= '<p class="foo">Lorem Ipsum</p>';
  381. $expected .= '<p class="bar">Lorem Ipsum</p>';
  382. $expected .= '</html>';
  383. $p = new Kwc_Mail_HtmlParser($styles);
  384. $html = $p->parse($html);
  385. $this->_assertHtmlEquals($expected, $html);
  386. }
  387. public function testSelectorMultipleClasses7()
  388. {
  389. $styles = array(
  390. array(
  391. 'selector' => 'div.foo.bar',
  392. 'styles' => array(
  393. 'font-size' => '12px'
  394. ),
  395. )
  396. );
  397. $html = '<p class="foo bar">Lorem Ipsum</p>';
  398. $html .= '<div class="foo bar">Lorem Ipsum</div>';
  399. $html .= '<p class="foo">Lorem Ipsum</p>';
  400. $html .= '<p class="bar">Lorem Ipsum</p>';
  401. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  402. $expected .= '<head><title></title></head>';
  403. $expected .= '<p class="foo bar">Lorem Ipsum</p>';
  404. $expected .= '<div class="foo bar" style="font-size: 12px; ">Lorem Ipsum</div>';
  405. $expected .= '<p class="foo">Lorem Ipsum</p>';
  406. $expected .= '<p class="bar">Lorem Ipsum</p>';
  407. $expected .= '</html>';
  408. $p = new Kwc_Mail_HtmlParser($styles);
  409. $html = $p->parse($html);
  410. $this->_assertHtmlEquals($expected, $html);
  411. }
  412. public function testSelectorMultipleClasses8()
  413. {
  414. $styles = array(
  415. array(
  416. 'selector' => '.foo.bar.koala',
  417. 'styles' => array(
  418. 'font-size' => '12px'
  419. ),
  420. ),
  421. array(
  422. 'selector' => 'div.foo.bar.koala',
  423. 'styles' => array(
  424. 'line-height' => '100%'
  425. ),
  426. )
  427. );
  428. $html = '<p class="foo koala bar">Lorem Ipsum</p>';
  429. $html .= '<div class="foo bar koala">Lorem Ipsum</div>';
  430. $html .= '<p class="foo bar">Lorem Ipsum</p>';
  431. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  432. $expected .= '<head><title></title></head>';
  433. $expected .= '<p class="foo koala bar" style="font-size: 12px; ">Lorem Ipsum</p>';
  434. $expected .= '<div class="foo bar koala" style="font-size: 12px; line-height: 100%; ">Lorem Ipsum</div>';
  435. $expected .= '<p class="foo bar">Lorem Ipsum</p>';
  436. $expected .= '</html>';
  437. $p = new Kwc_Mail_HtmlParser($styles);
  438. $html = $p->parse($html);
  439. $this->_assertHtmlEquals($expected, $html);
  440. }
  441. public function testSelectorMultipleClasses9()
  442. {
  443. $styles = array(
  444. array(
  445. 'selector' => '.foo .bar',
  446. 'styles' => array(
  447. 'font-size' => '12px'
  448. ),
  449. )
  450. );
  451. $html = '<p class="foo bar">Lorem Ipsum</p>';
  452. $html .= '<p class="foo"><span class="bar">Lorem Ipsum</span></p>';
  453. $html .= '<p class="bar">Lorem Ipsum</p>';
  454. $expected = '<html xmlns="http://www.w3.org/1999/xhtml">';
  455. $expected .= '<head><title></title></head>';
  456. $expected .= '<p class="foo bar">Lorem Ipsum</p>';
  457. $expected .= '<p class="foo"><span class="bar" style="font-size: 12px; ">Lorem Ipsum</span></p>';
  458. $expected .= '<p class="bar">Lorem Ipsum</p>';
  459. $expected .= '</html>';
  460. $p = new Kwc_Mail_HtmlParser($styles);
  461. $html = $p->parse($html);
  462. $this->_assertHtmlEquals($expected, $html);
  463. }
  464. }