TypographicBehaviorTest.php 4.1 KB

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