TypographicBehaviorTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\ORM\TableRegistry;
  4. use Tools\Model\Behavior\TypographicBehavior;
  5. use Tools\TestSuite\TestCase;
  6. class TypographicBehaviorTest extends TestCase {
  7. /**
  8. * @var \Cake\ORM\Table|\Tools\Model\Behavior\TypographicBehavior
  9. */
  10. public $Model;
  11. /**
  12. * @var array
  13. */
  14. public $fixtures = [
  15. 'core.Articles'
  16. ];
  17. /**
  18. * @return void
  19. */
  20. public function setUp() {
  21. parent::setUp();
  22. $this->Model = TableRegistry::getTableLocator()->get('Articles');
  23. $this->Model->addBehavior('Tools.Typographic', ['fields' => ['body'], 'before' => 'marshal']);
  24. }
  25. public function testObject() {
  26. $this->assertInstanceOf(TypographicBehavior::class, $this->Model->behaviors()->Typographic);
  27. }
  28. /**
  29. * @return void
  30. */
  31. public function testBeforeMarshal() {
  32. $data = [
  33. 'title' => 'some «cool» title',
  34. 'body' => 'A title with normal "qotes" - should be left untouched',
  35. ];
  36. $entity = $this->Model->newEntity($data);
  37. $this->assertEmpty($entity->getErrors());
  38. $result = $entity->toArray();
  39. $this->assertSame($data, $result);
  40. $strings = [
  41. 'some string with ‹single angle quotes›' => 'some string with \'single angle quotes\'',
  42. 'other string with „German‟ quotes' => 'other string with "German" quotes',
  43. 'mixed single ‚one‛ and ‘two’.' => 'mixed single \'one\' and \'two\'.',
  44. 'mixed double “one” and «two».' => 'mixed double "one" and "two".',
  45. ];
  46. foreach ($strings as $was => $expected) {
  47. $data = [
  48. 'title' => 'some «cool» title',
  49. 'body' => $was
  50. ];
  51. $entity = $this->Model->newEntity($data);
  52. $this->assertEmpty($entity->getErrors());
  53. $result = $entity->toArray();
  54. $this->assertSame($data['title'], $result['title']);
  55. $this->assertSame($expected, $result['body']);
  56. }
  57. }
  58. /**
  59. * @return void
  60. */
  61. public function testMergeQuotes() {
  62. $this->Model->removeBehavior('Typographic');
  63. $this->Model->addBehavior('Tools.Typographic', ['before' => 'marshal', 'mergeQuotes' => true]);
  64. $strings = [
  65. 'some string with ‹single angle quotes›' => 'some string with "single angle quotes"',
  66. 'other string with „German‟ quotes' => 'other string with "German" quotes',
  67. 'mixed single ‚one‛ and ‘two’.' => 'mixed single "one" and "two".',
  68. 'mixed double “one” and «two».' => 'mixed double "one" and "two".',
  69. ];
  70. foreach ($strings as $was => $expected) {
  71. $data = [
  72. 'title' => 'some «cool» title',
  73. 'body' => $was
  74. ];
  75. $entity = $this->Model->newEntity($data);
  76. $this->assertEmpty($entity->getErrors());
  77. $result = $entity->toArray();
  78. $this->assertSame('some "cool" title', $result['title']);
  79. $this->assertSame($expected, $result['body']);
  80. }
  81. }
  82. /**
  83. * Test that not defining fields results in all textarea and text fields being processed
  84. *
  85. * @return void
  86. */
  87. public function testAutoFields() {
  88. $this->Model->removeBehavior('Typographic');
  89. $this->Model->addBehavior('Tools.Typographic');
  90. $data = [
  91. 'title' => '„German‟ quotes',
  92. 'body' => 'mixed double “one” and «two»',
  93. ];
  94. $entity = $this->Model->newEntity($data);
  95. $this->assertEmpty($entity->getErrors());
  96. $res = $this->Model->save($entity);
  97. $this->assertTrue((bool)$res);
  98. $expected = [
  99. 'title' => '"German" quotes',
  100. 'body' => 'mixed double "one" and "two"',
  101. ];
  102. $this->assertSame($expected['title'], $res['title']);
  103. $this->assertSame($expected['body'], $res['body']);
  104. }
  105. /**
  106. * @return void
  107. */
  108. public function testUpdateTypography() {
  109. $this->Model->removeBehavior('Typographic');
  110. for ($i = 0; $i < 202; $i++) {
  111. $data = [
  112. 'title' => 'title ' . $i,
  113. 'body' => 'unclean `content` to «correct»',
  114. ];
  115. $entity = $this->Model->newEntity($data);
  116. $result = $this->Model->save($entity);
  117. $this->assertTrue((bool)$result);
  118. }
  119. $this->Model->addBehavior('Tools.Typographic');
  120. $count = $this->Model->updateTypography();
  121. $this->assertTrue($count >= 190, 'Count is only ' . $count);
  122. $record = $this->Model->find()->orderDesc('id')->firstOrFail();
  123. $this->assertSame('unclean `content` to "correct"', $record['body']);
  124. }
  125. }