TypographicBehaviorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. App::import('Behavior', 'Tools.Typographic');
  3. App::uses('AppModel', 'Model');
  4. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  5. class TypographicBehaviorTest extends MyCakeTestCase {
  6. public $Model;
  7. public $fixtures = ['core.article'];
  8. public function setUp() {
  9. parent::setUp();
  10. $this->Model = ClassRegistry::init('Article');
  11. $this->Model->Behaviors->load('Tools.Typographic', ['fields' => ['body'], 'before' => 'validate']);
  12. }
  13. public function testObject() {
  14. $this->assertInstanceOf('TypographicBehavior', $this->Model->Behaviors->Typographic);
  15. }
  16. public function testBeforeValidate() {
  17. $this->out($this->_header(__FUNCTION__), false);
  18. $data = [
  19. 'title' => 'some «cool» title',
  20. 'body' => 'A title with normal "qotes" - should be left untouched',
  21. ];
  22. $this->Model->set($data);
  23. $res = $this->Model->validates();
  24. $this->assertTrue($res);
  25. $res = $this->Model->data;
  26. $this->assertSame($data, $res['Article']);
  27. $strings = [
  28. 'some string with ‹single angle quotes›' => 'some string with \'single angle quotes\'',
  29. 'other string with „German‟ quotes' => 'other string with "German" quotes',
  30. 'mixed single ‚one‛ and ‘two’.' => 'mixed single \'one\' and \'two\'.',
  31. 'mixed double “one” and «two».' => 'mixed double "one" and "two".',
  32. ];
  33. foreach ($strings as $was => $expected) {
  34. $data = [
  35. 'title' => 'some «cool» title',
  36. 'body' => $was
  37. ];
  38. $this->Model->set($data);
  39. $res = $this->Model->validates();
  40. $this->assertTrue($res);
  41. $res = $this->Model->data;
  42. $this->assertSame($data['title'], $res['Article']['title']);
  43. $this->assertSame($expected, $res['Article']['body']);
  44. }
  45. }
  46. public function testMergeQuotes() {
  47. $this->Model->Behaviors->unload('Typographic');
  48. $this->Model->Behaviors->load('Tools.Typographic', ['before' => 'validate', 'mergeQuotes' => true]);
  49. $strings = [
  50. 'some string with ‹single angle quotes›' => 'some string with "single angle quotes"',
  51. 'other string with „German‟ quotes' => 'other string with "German" quotes',
  52. 'mixed single ‚one‛ and ‘two’.' => 'mixed single "one" and "two".',
  53. 'mixed double “one” and «two».' => 'mixed double "one" and "two".',
  54. ];
  55. foreach ($strings as $was => $expected) {
  56. $data = [
  57. 'title' => 'some «cool» title',
  58. 'body' => $was
  59. ];
  60. $this->Model->set($data);
  61. $res = $this->Model->validates();
  62. $this->assertTrue($res);
  63. $res = $this->Model->data;
  64. $this->assertSame('some "cool" title', $res['Article']['title']);
  65. $this->assertSame($expected, $res['Article']['body']);
  66. }
  67. }
  68. /**
  69. * Test that not defining fields results in all textarea and text fields being processed
  70. */
  71. public function testAutoFields() {
  72. $this->Model->Behaviors->unload('Typographic');
  73. $this->Model->Behaviors->load('Tools.Typographic');
  74. $data = [
  75. 'title' => '„German‟ quotes',
  76. 'body' => 'mixed double “one” and «two»',
  77. ];
  78. $this->Model->set($data);
  79. $res = $this->Model->save();
  80. $this->assertTrue((bool)$res);
  81. $expected = [
  82. 'title' => '"German" quotes',
  83. 'body' => 'mixed double "one" and "two"',
  84. ];
  85. $this->assertSame($expected['title'], $res['Article']['title']);
  86. $this->assertSame($expected['body'], $res['Article']['body']);
  87. }
  88. public function testUpdateTypography() {
  89. $this->Model->Behaviors->unload('Typographic');
  90. for ($i = 0; $i < 202; $i++) {
  91. $data = [
  92. 'title' => 'title ' . $i,
  93. 'body' => 'unclean `content` to «correct»',
  94. ];
  95. $this->Model->create();
  96. $this->Model->save($data);
  97. }
  98. $this->Model->Behaviors->load('Tools.Typographic');
  99. $count = $this->Model->updateTypography();
  100. $this->assertTrue($count >= 200);
  101. $record = $this->Model->find('first', ['order' => ['id' => 'DESC']]);
  102. $this->assertSame('unclean `content` to "correct"', $record['Article']['body']);
  103. }
  104. }