TypographicBehaviorTest.php 4.0 KB

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