TypographicBehaviorTest.php 3.9 KB

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