TypographicBehaviorTest.php 4.1 KB

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