SluggedBehaviorTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // For non ascii chars it might be longer, though...
  92. $this->articles->behaviors()->Slugged->config(['length' => 10, 'mode' => 'ascii']);
  93. $entity = $this->_getEntity('ä ö ü ä ö ü');
  94. $result = $this->articles->save($entity);
  95. $this->assertEquals('ae-oe-ue-ae-oe-ue', $result->get('slug'));
  96. }
  97. /**
  98. * Length based on auto-detect of schema.
  99. *
  100. * @return void
  101. */
  102. public function testLengthRestrictionAutoDetect() {
  103. $this->articles->addBehavior('Tools.Slugged');
  104. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  105. $result = $this->articles->save($entity);
  106. $this->assertEquals(245, strlen($result->get('slug')));
  107. }
  108. /**
  109. * Ensure that you can overwrite length.
  110. *
  111. * @return void
  112. */
  113. public function testLengthRestrictionNoLimit() {
  114. $this->articles->addBehavior('Tools.Slugged', ['length' => 0, 'label' => 'long_title', 'field' => 'long_slug']);
  115. $entity = $this->_getEntity(str_repeat('foo bar ', 100), 'long_title');
  116. $result = $this->articles->save($entity);
  117. $this->assertEquals(799, strlen($result->get('long_slug')));
  118. }
  119. /**
  120. * SluggedBehaviorTest::testResetSlugs()
  121. *
  122. * @return void
  123. */
  124. public function testResetSlugs() {
  125. $article = $this->articles->newEntity(array('title' => 'Andy Dawson', 'slug' => 'foo'));
  126. $this->articles->save($article);
  127. $article = $this->articles->newEntity(array('title' => 'Andy Dawsom', 'slug' => 'bar'));
  128. $this->articles->save($article);
  129. $result = $this->articles->find('all', array(
  130. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  131. 'fields' => array('title', 'slug'),
  132. 'order' => 'title'
  133. ))->combine('title', 'slug')->toArray();
  134. $expected = array(
  135. 'Andy Dawsom' => 'bar',
  136. 'Andy Dawson' => 'foo'
  137. );
  138. $this->assertEquals($expected, $result);
  139. $this->articles->addBehavior('Tools.Slugged');
  140. $result = $this->articles->resetSlugs(['limit' => 1]);
  141. $this->assertTrue($result);
  142. $result = $this->articles->find('all', array(
  143. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  144. 'fields' => array('title', 'slug'),
  145. 'order' => 'title'
  146. ))->combine('title', 'slug')->toArray();
  147. $expected = array(
  148. 'Andy Dawsom' => 'Andy-Dawsom',
  149. 'Andy Dawson' => 'Andy-Dawson'
  150. );
  151. $this->assertEquals($expected, $result);
  152. }
  153. /**
  154. * TestDuplicateWithLengthRestriction method
  155. *
  156. * If there's a length restriction - ensure it's respected by the unique slug routine
  157. *
  158. * @return void
  159. */
  160. public function testDuplicateWithLengthRestriction() {
  161. return;
  162. $this->articles->addBehavior('Tools.Slugged', ['length' => 10, 'unique' => true]);
  163. $article = $this->articles->newEntity(array('title' => 'Andy Dawson'));
  164. $this->articles->save($article);
  165. $article = $this->articles->newEntity(array('title' => 'Andy Dawsom'));
  166. $this->articles->save($article);
  167. $article = $this->articles->newEntity(array('title' => 'Andy Dawsoo'));
  168. $this->articles->save($article);
  169. $article = $this->articles->newEntity(array('title' => 'Andy Dawso3'));
  170. $this->articles->save($article);
  171. $article = $this->articles->newEntity(array('title' => 'Andy Dawso4'));
  172. $this->articles->save($article);
  173. $article = $this->articles->newEntity(array('title' => 'Andy Dawso5'));
  174. $this->articles->save($article);
  175. $article = $this->articles->newEntity(array('title' => 'Andy Dawso6'));
  176. $this->articles->save($article);
  177. $article = $this->articles->newEntity(array('title' => 'Andy Dawso7'));
  178. $this->articles->save($article);
  179. $article = $this->articles->newEntity(array('title' => 'Andy Dawso8'));
  180. $this->articles->save($article);
  181. $article = $this->articles->newEntity(array('title' => 'Andy Dawso9'));
  182. $this->articles->save($article);
  183. $article = $this->articles->newEntity(array('title' => 'Andy Dawso0'));
  184. $this->articles->save($article);
  185. $result = $this->articles->find('all', array(
  186. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  187. 'fields' => array('title', 'slug'),
  188. 'order' => 'title'
  189. ))->combine('title', 'slug')->toArray();
  190. $expects = array(
  191. 'Andy Dawson' => 'Andy-Dawso',
  192. 'Andy Dawsom' => 'Andy-Daw-1',
  193. 'Andy Dawsoo' => 'Andy-Daw-2',
  194. 'Andy Dawso3' => 'Andy-Daw-3',
  195. 'Andy Dawso4' => 'Andy-Daw-4',
  196. 'Andy Dawso5' => 'Andy-Daw-5',
  197. 'Andy Dawso6' => 'Andy-Daw-6',
  198. 'Andy Dawso7' => 'Andy-Daw-7',
  199. 'Andy Dawso8' => 'Andy-Daw-8',
  200. 'Andy Dawso9' => 'Andy-Daw-9',
  201. 'Andy Dawso0' => 'Andy-Da-10'
  202. );
  203. $this->assertEquals($expects, $result);
  204. }
  205. /**
  206. * Get a new Entity
  207. *
  208. * @return Entity
  209. */
  210. protected function _getEntity($title = 'test 123', $field = 'title') {
  211. return new Entity([
  212. $field => $title
  213. ]);
  214. }
  215. }