TypographicBehaviorTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. App::import('Behavior', 'Tools.Typographic');
  3. App::uses('AppModel', 'Model');
  4. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  5. class TypographicBehaviorTest extends MyCakeTestCase {
  6. public $Model;
  7. public $fixtures = array('core.article');
  8. public function setUp() {
  9. parent::setUp();
  10. $this->Model = ClassRegistry::init('Article');
  11. $this->Model->Behaviors->load('Tools.Typographic', array('fields'=>array('body'), 'before'=>'validate'));
  12. }
  13. public function testObject() {
  14. $this->assertTrue(is_a($this->Model->Behaviors->Typographic, 'TypographicBehavior'));
  15. }
  16. public function testBeforeValidate() {
  17. $this->out($this->_header(__FUNCTION__), false);
  18. $data = array(
  19. 'title' => 'some «cool» title',
  20. 'body' => 'A title with normal "qotes" - should be left untouched',
  21. );
  22. $this->Model->set($data);
  23. $res = $this->Model->validates();
  24. $this->assertTrue($res);
  25. $res = $this->Model->data;
  26. $this->assertSame($data, $res['Article']);
  27. $strings = array(
  28. 'some string with ‹single angle quotes›' => 'some string with \'single angle quotes\'',
  29. 'other string with „German‟ quotes' => 'other string with "German" quotes',
  30. 'mixed single ‚one‛ and ‘two’.' => 'mixed single \'one\' and \'two\'.',
  31. 'mixed double “one” and «two».' => 'mixed double "one" and "two".',
  32. );
  33. foreach ($strings as $was => $expected) {
  34. $data = array(
  35. 'title' => 'some «cool» title',
  36. 'body' => $was
  37. );
  38. $this->Model->set($data);
  39. $res = $this->Model->validates();
  40. $this->assertTrue($res);
  41. $res = $this->Model->data;
  42. $this->assertSame($data['title'], $res['Article']['title']);
  43. $this->assertSame($expected, $res['Article']['body']);
  44. }
  45. }
  46. public function testMergeQuotes() {
  47. $this->Model->Behaviors->unload('Typographic');
  48. $this->Model->Behaviors->load('Tools.Typographic', array('before' => 'validate', 'mergeQuotes' => true));
  49. $strings = array(
  50. 'some string with ‹single angle quotes›' => 'some string with "single angle quotes"',
  51. 'other string with „German‟ quotes' => 'other string with "German" quotes',
  52. 'mixed single ‚one‛ and ‘two’.' => 'mixed single "one" and "two".',
  53. 'mixed double “one” and «two».' => 'mixed double "one" and "two".',
  54. );
  55. foreach ($strings as $was => $expected) {
  56. $data = array(
  57. 'title' => 'some «cool» title',
  58. 'body' => $was
  59. );
  60. $this->Model->set($data);
  61. $res = $this->Model->validates();
  62. $this->assertTrue($res);
  63. $res = $this->Model->data;
  64. $this->assertSame('some "cool" title', $res['Article']['title']);
  65. $this->assertSame($expected, $res['Article']['body']);
  66. }
  67. }
  68. /**
  69. * test that not defining fields results in all textarea and text fields being processed
  70. * 2012-08-07 ms
  71. */
  72. public function testAutoFields() {
  73. $this->Model->Behaviors->unload('Typographic');
  74. $this->Model->Behaviors->load('Tools.Typographic');
  75. $data = array(
  76. 'title' => '„German‟ quotes',
  77. 'body' => 'mixed double “one” and «two»',
  78. );
  79. $this->Model->set($data);
  80. $res = $this->Model->save();
  81. $this->assertTrue((bool)$res);
  82. $expected = array(
  83. 'title' => '"German" quotes',
  84. 'body' => 'mixed double "one" and "two"',
  85. );
  86. $this->assertSame($expected['title'], $res['Article']['title']);
  87. $this->assertSame($expected['body'], $res['Article']['body']);
  88. }
  89. public function testUpdateTypography() {
  90. $this->Model->Behaviors->unload('Typographic');
  91. for ($i = 0; $i < 202; $i++) {
  92. $data = array(
  93. 'title' => 'title '.$i,
  94. 'body' => 'unclean `content` to «correct»',
  95. );
  96. $this->Model->create();
  97. $this->Model->save($data);
  98. }
  99. $this->Model->Behaviors->load('Tools.Typographic');
  100. $count = $this->Model->updateTypography();
  101. $this->assertTrue($count >= 200);
  102. $record = $this->Model->find('first', array('order' => array('id' => 'DESC')));
  103. $this->assertSame('unclean `content` to "correct"', $record['Article']['body']);
  104. }
  105. }