SluggedBehaviorTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\Database\Query;
  4. use Cake\Datasource\ConnectionManager;
  5. use Cake\Event\Event;
  6. use Cake\ORM\Entity;
  7. use Cake\ORM\Table;
  8. use Cake\ORM\TableRegistry;
  9. use Cake\TestSuite\TestCase;
  10. use Cake\Core\Configure;
  11. /**
  12. * SluggedBehaviorTest
  13. */
  14. class SluggedBehaviorTest extends TestCase {
  15. /**
  16. * Fixture
  17. *
  18. * @var array
  19. */
  20. public $fixtures = [
  21. 'plugin.tools.slugged_articles'
  22. ];
  23. /**
  24. * setup
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. //$this->connection = ConnectionManager::get('test');
  31. $options = ['alias' => 'Articles'];
  32. $this->articles = TableRegistry::get('SluggedArticles', $options);
  33. Configure::delete('Slugged');
  34. }
  35. /**
  36. * teardown
  37. *
  38. * @return void
  39. */
  40. public function tearDown() {
  41. unset($this->articles);
  42. TableRegistry::clear();
  43. parent::tearDown();
  44. }
  45. /**
  46. * Testing simple slugging when adding a record
  47. *
  48. * @return void
  49. */
  50. public function testAdd() {
  51. $this->articles->addBehavior('Tools.Slugged');
  52. $entity = $this->_getEntity();
  53. $result = $this->articles->save($entity);
  54. $this->assertEquals('test-123', $result->get('slug'));
  55. }
  56. /**
  57. * Testing simple slugging when adding a record
  58. *
  59. * @return void
  60. */
  61. public function testAddUnique() {
  62. $this->articles->addBehavior('Tools.Slugged', ['unique' => true]);
  63. $entity = $this->_getEntity();
  64. $result = $this->articles->save($entity);
  65. $this->assertEquals('test-123', $result->get('slug'));
  66. //$entity = $this->_getEntity();
  67. //$result = $this->articles->save($entity);
  68. //$this->assertEquals('test-123', $result->get('slug'));
  69. //debug($result);
  70. }
  71. /**
  72. * SluggedBehaviorTest::testCustomFinder()
  73. *
  74. * @return void
  75. */
  76. public function testCustomFinder() {
  77. $this->articles->addBehavior('Tools.Slugged');
  78. $article = $this->articles->find()->find('slugged', ['slug' => 'foo'])->first();
  79. $this->assertEquals('Foo', $article->get('title'));
  80. }
  81. /**
  82. * Length based on manual config.
  83. *
  84. * @return void
  85. */
  86. public function testLengthRestrictionManual() {
  87. $this->articles->addBehavior('Tools.Slugged', ['length' => 155]);
  88. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  89. $result = $this->articles->save($entity);
  90. $this->assertEquals(155, strlen($result->get('slug')));
  91. }
  92. /**
  93. * Length based on auto-detect of schema.
  94. *
  95. * @return void
  96. */
  97. public function testLengthRestrictionAutoDetect() {
  98. $this->articles->addBehavior('Tools.Slugged');
  99. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  100. $result = $this->articles->save($entity);
  101. $this->assertEquals(245, strlen($result->get('slug')));
  102. }
  103. /**
  104. * Ensure that you can overwrite length.
  105. *
  106. * @return void
  107. */
  108. public function testLengthRestrictionNoLimit() {
  109. $this->articles->addBehavior('Tools.Slugged', ['length' => 0, 'label' => 'long_title', 'field' => 'long_slug']);
  110. $entity = $this->_getEntity(str_repeat('foo bar ', 100), 'long_title');
  111. $result = $this->articles->save($entity);
  112. $this->assertEquals(799, strlen($result->get('long_slug')));
  113. }
  114. /**
  115. * Get a new Entity
  116. *
  117. * @return Entity
  118. */
  119. protected function _getEntity($title = 'test 123', $field = 'title') {
  120. return new Entity([
  121. $field => $title
  122. ]);
  123. }
  124. }