TypographyHelperTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. App::uses('TypographyHelper', 'Tools.View/Helper');
  3. App::uses('View', 'View');
  4. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  5. /**
  6. * TypographyHelper Test Case
  7. *
  8. */
  9. class TypographyHelperTest extends MyCakeTestCase {
  10. public $Typography;
  11. /**
  12. * SetUp method
  13. *
  14. * @return void
  15. */
  16. public function setUp() {
  17. parent::setUp();
  18. Configure::delete('Typography.locale');
  19. Configure::write('App.language', 'eng');
  20. $this->Typography = new TypographyHelper(new View(null));
  21. }
  22. /**
  23. * TearDown method
  24. *
  25. * @return void
  26. */
  27. public function tearDown() {
  28. unset($this->Typography);
  29. parent::tearDown();
  30. }
  31. /**
  32. * TestFormatCharacter method
  33. *
  34. * @return void
  35. */
  36. public function testFormatCharacter() {
  37. $strs = [
  38. '"double quotes"' => '&#8220;double quotes&#8221;',
  39. '"testing" in "theory" that is' => '&#8220;testing&#8221; in &#8220;theory&#8221; that is',
  40. "Here's what I'm" => 'Here&rsquo;s what I&rsquo;m',
  41. '&' => '&amp;',
  42. '&amp;' => '&amp;',
  43. '&nbsp;' => '&nbsp;',
  44. '--' => '&#8212;',
  45. 'foo...' => 'foo&#8230;',
  46. 'foo..' => 'foo..',
  47. 'foo...bar.' => 'foo&#8230;bar.',
  48. 'test. new' => 'test.&nbsp; new',
  49. ];
  50. foreach ($strs as $str => $expected) {
  51. $result = $this->Typography->formatCharacters($str);
  52. //debug($result);
  53. $this->assertEquals($expected, $result);
  54. }
  55. Configure::write('Typography.locale', 'low');
  56. $strs = [
  57. '"double quotes"' => '&bdquo;double quotes&#8223;',
  58. '"testing" in "theory" that is' => '&bdquo;testing&#8223; in &bdquo;theory&#8223; that is',
  59. "Here's what I'm" => 'Here&rsquo;s what I&rsquo;m',
  60. ];
  61. foreach ($strs as $str => $expected) {
  62. $result = $this->Typography->formatCharacters($str);
  63. //echo pre($result);
  64. $this->assertEquals($expected, $result);
  65. }
  66. Configure::write('Typography.locale', 'angle');
  67. $strs = [
  68. '"double quotes"' => '&#171;double quotes&#187;',
  69. '"testing" in "theory" that is' => '&#171;testing&#187; in &#171;theory&#187; that is',
  70. "Here's what I'm" => 'Here&rsquo;s what I&rsquo;m',
  71. ];
  72. foreach ($strs as $str => $expected) {
  73. $result = $this->Typography->formatCharacters($str);
  74. //echo debug($result);
  75. $this->assertEquals($expected, $result);
  76. }
  77. }
  78. /**
  79. * TestAutoTypography method
  80. *
  81. * @return void
  82. */
  83. public function testAutoTypography() {
  84. $str = 'Some \'funny\' and "funky" test';
  85. $result = $this->Typography->autoTypography($str);
  86. //echo pre($result);
  87. $expected = '<p>Some &#8216;funny&#8217; and &#8220;funky&#8221; test</p>';
  88. $this->assertEquals($expected, $result);
  89. Configure::write('App.language', 'deu');
  90. $result = $this->Typography->autoTypography($str);
  91. //echo pre($result);
  92. $expected = '<p>Some &sbquo;funny&#8219; and &bdquo;funky&#8223; test</p>';
  93. $this->assertEquals($expected, $result);
  94. Configure::write('App.language', 'fra');
  95. $result = $this->Typography->autoTypography($str);
  96. //echo debug($result);
  97. $expected = '<p>Some &lsaquo;funny&rsaquo; and &#171;funky&#187; test</p>';
  98. $this->assertEquals($expected, $result);
  99. }
  100. /**
  101. * TestNl2brExceptPre method
  102. *
  103. * @return void
  104. */
  105. public function testNl2brExceptPre() {
  106. $str = <<<EOH
  107. Hello, I'm a happy string with some new lines.
  108. I like to skip.
  109. Jump
  110. and sing.
  111. <pre>
  112. I am inside a pre tag. Please don't mess with me.
  113. k?
  114. </pre>
  115. That's my story and I'm sticking to it.
  116. The End.
  117. EOH;
  118. $expected = <<<EOH
  119. Hello, I'm a happy string with some new lines.<br />
  120. <br />
  121. I like to skip.<br />
  122. <br />
  123. Jump<br />
  124. <br />
  125. and sing.<br />
  126. <br />
  127. <pre>
  128. I am inside a pre tag. Please don't mess with me.
  129. k?
  130. </pre><br />
  131. <br />
  132. That's my story and I'm sticking to it.<br />
  133. <br />
  134. The End.
  135. EOH;
  136. $result = $this->Typography->nl2brExceptPre($str);
  137. $this->assertEquals($expected, $result);
  138. }
  139. }