SluggedBehaviorTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_article',
  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 ', 100));
  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. $config = ConnectionManager::config('test');
  99. $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only works with MySql field lengths.');
  100. $this->articles->addBehavior('Tools.Slugged');
  101. $entity = $this->_getEntity(str_repeat('foo bar ', 100));
  102. $result = $this->articles->save($entity);
  103. $this->assertEquals(255, strlen($result->get('slug')));
  104. }
  105. /**
  106. * Ensure that you can overwrite length.
  107. *
  108. * @return void
  109. */
  110. public function testLengthRestrictionNoLimit() {
  111. $this->articles->addBehavior('Tools.Slugged', ['length' => 0]);
  112. $entity = $this->_getEntity(str_repeat('foo bar ', 100));
  113. debug(strlen($entity->get('slug')));
  114. $result = $this->articles->save($entity);
  115. $this->assertEquals(799, strlen($result->get('slug')));
  116. }
  117. /**
  118. * Get a new Entity
  119. *
  120. * @return Entity
  121. */
  122. protected function _getEntity($title = 'test 123') {
  123. return new Entity([
  124. 'title' => $title
  125. ]);
  126. }
  127. }