ElggCoreOutputAutoPTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Test case for \ElggAutoP functionality.
  4. */
  5. class ElggCoreOutputAutoPTest extends \ElggCoreUnitTest {
  6. /**
  7. * @var \ElggAutoP
  8. */
  9. protected $_autop;
  10. public function setUp() {
  11. $this->_autop = new \ElggAutoP();
  12. }
  13. public function testDomRoundtrip() {
  14. $d = dir(dirname(__FILE__) . '/test_files/output/autop');
  15. $in = file_get_contents($d->path . "/domdoc_in.html");
  16. $exp = file_get_contents($d->path . "/domdoc_exp.html");
  17. $exp = $this->flattenString($exp);
  18. $doc = new DOMDocument();
  19. libxml_use_internal_errors(true);
  20. $doc->loadHTML("<html><meta http-equiv='content-type' content='text/html; charset=utf-8'><body>"
  21. . $in . '</body></html>');
  22. $serialized = $doc->saveHTML();
  23. list(,$out) = explode('<body>', $serialized, 2);
  24. list($out) = explode('</body>', $out, 2);
  25. $out = $this->flattenString($out);
  26. $this->assertEqual($exp, $out, "DOMDocument's parsing/serialization roundtrip");
  27. }
  28. public function testProcess() {
  29. $data = $this->provider();
  30. foreach ($data as $row) {
  31. list($test, $in, $exp) = $row;
  32. $exp = $this->flattenString($exp);
  33. $out = $this->_autop->process($in);
  34. $out = $this->flattenString($out);
  35. $this->assertEqual($exp, $out, "Equality case {$test}");
  36. }
  37. }
  38. public function provider() {
  39. $d = dir(dirname(__FILE__) . '/test_files/output/autop');
  40. $tests = array();
  41. while (false !== ($entry = $d->read())) {
  42. if (preg_match('/^([a-z\\-]+)\.in\.html$/i', $entry, $m)) {
  43. $tests[] = $m[1];
  44. }
  45. }
  46. $data = array();
  47. foreach ($tests as $test) {
  48. $data[] = array(
  49. $test,
  50. file_get_contents($d->path . '/' . "{$test}.in.html"),
  51. file_get_contents($d->path . '/' . "{$test}.exp.html"),
  52. );
  53. }
  54. return $data;
  55. }
  56. /**
  57. * Different versions of PHP return different whitespace between tags.
  58. * Removing all line breaks normalizes that.
  59. */
  60. public function flattenString($string) {
  61. $r = preg_replace('/[\n\r]+/', '', $string);
  62. return $r;
  63. }
  64. }