TypographyHelperTest.php 3.8 KB

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