SluggedBehaviorTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. $this->articles->addBehavior('Tools.Slugged');
  35. }
  36. /**
  37. * teardown
  38. *
  39. * @return void
  40. */
  41. public function tearDown() {
  42. unset($this->articles);
  43. TableRegistry::clear();
  44. parent::tearDown();
  45. }
  46. /**
  47. * Testing simple slugging when adding a record
  48. *
  49. * @return void
  50. */
  51. public function testAdd() {
  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->behaviors()->Slugged->config(['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. $article = $this->articles->find()->find('slugged', ['slug' => 'foo'])->first();
  78. $this->assertEquals('Foo', $article->get('title'));
  79. }
  80. /**
  81. * Length based on manual config.
  82. *
  83. * @return void
  84. */
  85. public function testLengthRestrictionManual() {
  86. $this->articles->behaviors()->Slugged->config(['length' => 155]);
  87. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  88. $result = $this->articles->save($entity);
  89. $this->assertEquals(155, strlen($result->get('slug')));
  90. $this->articles->behaviors()->Slugged->config(['length' => 10, 'mode' => 'ascii']);
  91. $entity = $this->_getEntity('ä ö ü ä ö ü');
  92. $result = $this->articles->save($entity);
  93. $this->assertEquals('ae-oe-ue-a', $result->get('slug'));
  94. }
  95. /**
  96. * Length based on auto-detect of schema.
  97. *
  98. * @return void
  99. */
  100. public function testLengthRestrictionAutoDetect() {
  101. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  102. $result = $this->articles->save($entity);
  103. $this->assertEquals(245, 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->behaviors()->Slugged->config(['length' => 0, 'label' => 'long_title', 'field' => 'long_slug']);
  112. $entity = $this->_getEntity(str_repeat('foo bar ', 100), 'long_title');
  113. $result = $this->articles->save($entity);
  114. $this->assertEquals(799, strlen($result->get('long_slug')));
  115. }
  116. /**
  117. * SluggedBehaviorTest::testResetSlugs()
  118. *
  119. * @return void
  120. */
  121. public function testResetSlugs() {
  122. $this->articles->removeBehavior('Slugged');
  123. $article = $this->articles->newEntity(array('title' => 'Andy Dawson', 'slug' => 'foo'));
  124. $this->articles->save($article);
  125. $article = $this->articles->newEntity(array('title' => 'Andy Dawsom', 'slug' => 'bar'));
  126. $this->articles->save($article);
  127. $result = $this->articles->find('all', array(
  128. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  129. 'fields' => array('title', 'slug'),
  130. 'order' => 'title'
  131. ))->combine('title', 'slug')->toArray();
  132. $expected = array(
  133. 'Andy Dawsom' => 'bar',
  134. 'Andy Dawson' => 'foo'
  135. );
  136. $this->assertEquals($expected, $result);
  137. $this->articles->addBehavior('Tools.Slugged');
  138. $result = $this->articles->resetSlugs(['limit' => 1]);
  139. $this->assertTrue($result);
  140. $result = $this->articles->find('all', array(
  141. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  142. 'fields' => array('title', 'slug'),
  143. 'order' => 'title'
  144. ))->combine('title', 'slug')->toArray();
  145. $expected = array(
  146. 'Andy Dawsom' => 'Andy-Dawsom',
  147. 'Andy Dawson' => 'Andy-Dawson'
  148. );
  149. $this->assertEquals($expected, $result);
  150. }
  151. /**
  152. * TestDuplicateWithLengthRestriction method
  153. *
  154. * If there's a length restriction - ensure it's respected by the unique slug routine
  155. *
  156. * @return void
  157. */
  158. public function testDuplicateWithLengthRestriction() {
  159. return;
  160. $this->articles->behaviors()->Slugged->config(['length' => 10, 'unique' => true]);
  161. $article = $this->articles->newEntity(array('title' => 'Andy Dawson'));
  162. $this->articles->save($article);
  163. $article = $this->articles->newEntity(array('title' => 'Andy Dawsom'));
  164. $this->articles->save($article);
  165. $article = $this->articles->newEntity(array('title' => 'Andy Dawsoo'));
  166. $this->articles->save($article);
  167. $article = $this->articles->newEntity(array('title' => 'Andy Dawso3'));
  168. $this->articles->save($article);
  169. $article = $this->articles->newEntity(array('title' => 'Andy Dawso4'));
  170. $this->articles->save($article);
  171. $article = $this->articles->newEntity(array('title' => 'Andy Dawso5'));
  172. $this->articles->save($article);
  173. $article = $this->articles->newEntity(array('title' => 'Andy Dawso6'));
  174. $this->articles->save($article);
  175. $article = $this->articles->newEntity(array('title' => 'Andy Dawso7'));
  176. $this->articles->save($article);
  177. $article = $this->articles->newEntity(array('title' => 'Andy Dawso8'));
  178. $this->articles->save($article);
  179. $article = $this->articles->newEntity(array('title' => 'Andy Dawso9'));
  180. $this->articles->save($article);
  181. $article = $this->articles->newEntity(array('title' => 'Andy Dawso0'));
  182. $this->articles->save($article);
  183. $result = $this->articles->find('all', array(
  184. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  185. 'fields' => array('title', 'slug'),
  186. 'order' => 'title'
  187. ))->combine('title', 'slug')->toArray();
  188. $expected = array(
  189. 'Andy Dawson' => 'Andy-Dawso',
  190. 'Andy Dawsom' => 'Andy-Daw-1',
  191. 'Andy Dawsoo' => 'Andy-Daw-2',
  192. 'Andy Dawso3' => 'Andy-Daw-3',
  193. 'Andy Dawso4' => 'Andy-Daw-4',
  194. 'Andy Dawso5' => 'Andy-Daw-5',
  195. 'Andy Dawso6' => 'Andy-Daw-6',
  196. 'Andy Dawso7' => 'Andy-Daw-7',
  197. 'Andy Dawso8' => 'Andy-Daw-8',
  198. 'Andy Dawso9' => 'Andy-Daw-9',
  199. 'Andy Dawso0' => 'Andy-Da-10'
  200. );
  201. $this->assertEquals($expected, $result);
  202. }
  203. /**
  204. * TestTruncateMultibyte method
  205. *
  206. * @return void
  207. */
  208. /**
  209. * TestTruncateMultibyte method
  210. *
  211. * Ensure that the first test doesn't cut a multibyte character The test string is:
  212. * 17 chars
  213. * 51 bytes UTF-8 encoded
  214. *
  215. * @return void
  216. */
  217. public function testTruncateMultibyte() {
  218. $this->articles->behaviors()->Slugged->config(array('length' => 16));
  219. $result = $this->articles->generateSlug('モデルのデータベースとデータソース');
  220. $expected = 'モデルのデータベースとデータソー';
  221. $this->assertEquals($expected, $result);
  222. }
  223. /**
  224. * Test Url method
  225. *
  226. * @return void
  227. */
  228. public function testUrlMode() {
  229. $this->articles->behaviors()->Slugged->config(array('mode' => 'url', 'replace' => false));
  230. $string = 'standard string';
  231. $expected = 'standard-string';
  232. $result = $this->articles->generateSlug($string);
  233. $this->assertEquals($expected, $result);
  234. $string = 'something with a \' in it';
  235. $expected = 'something-with-a-in-it';
  236. $result = $this->articles->generateSlug($string);
  237. $this->assertEquals($expected, $result);
  238. $string = 'something with a " in it';
  239. $expected = 'something-with-a-in-it';
  240. $result = $this->articles->generateSlug($string);
  241. $this->assertEquals($expected, $result);
  242. $string = 'something with a / in it';
  243. $expected = 'something-with-a-in-it';
  244. $result = $this->articles->generateSlug($string);
  245. $this->assertEquals($expected, $result);
  246. $string = 'something with a ? in it';
  247. $expected = 'something-with-a-in-it';
  248. $result = $this->articles->generateSlug($string);
  249. $this->assertEquals($expected, $result);
  250. $string = 'something with a < in it';
  251. $expected = 'something-with-a-in-it';
  252. $result = $this->articles->generateSlug($string);
  253. $this->assertEquals($expected, $result);
  254. $string = 'something with a > in it';
  255. $expected = 'something-with-a-in-it';
  256. $result = $this->articles->generateSlug($string);
  257. $this->assertEquals($expected, $result);
  258. $string = 'something with a . in it';
  259. $expected = 'something-with-a-in-it';
  260. $result = $this->articles->generateSlug($string);
  261. $this->assertEquals($expected, $result);
  262. $string = 'something with a $ in it';
  263. $expected = 'something-with-a-in-it';
  264. $result = $this->articles->generateSlug($string);
  265. $this->assertEquals($expected, $result);
  266. $string = 'something with a / in it';
  267. $expected = 'something-with-a-in-it';
  268. $result = $this->articles->generateSlug($string);
  269. $this->assertEquals($expected, $result);
  270. $string = 'something with a : in it';
  271. $expected = 'something-with-a-in-it';
  272. $result = $this->articles->generateSlug($string);
  273. $this->assertEquals($expected, $result);
  274. $string = 'something with a ; in it';
  275. $expected = 'something-with-a-in-it';
  276. $result = $this->articles->generateSlug($string);
  277. $this->assertEquals($expected, $result);
  278. $string = 'something with a ? in it';
  279. $expected = 'something-with-a-in-it';
  280. $result = $this->articles->generateSlug($string);
  281. $this->assertEquals($expected, $result);
  282. $string = 'something with a @ in it';
  283. $expected = 'something-with-a-in-it';
  284. $result = $this->articles->generateSlug($string);
  285. $this->assertEquals($expected, $result);
  286. $string = 'something with a = in it';
  287. $expected = 'something-with-a-in-it';
  288. $result = $this->articles->generateSlug($string);
  289. $this->assertEquals($expected, $result);
  290. $string = 'something with a + in it';
  291. $expected = 'something-with-a-in-it';
  292. $result = $this->articles->generateSlug($string);
  293. $this->assertEquals($expected, $result);
  294. $string = 'something with a & in it';
  295. $expected = 'something-with-a-in-it';
  296. $result = $this->articles->generateSlug($string);
  297. $this->assertEquals($expected, $result);
  298. $string = 'something with a % in it';
  299. $expected = 'something-with-a-in-it';
  300. $result = $this->articles->generateSlug($string);
  301. $this->assertEquals($expected, $result);
  302. $string = 'something with a \ in it';
  303. $expected = 'something-with-a-in-it';
  304. $result = $this->articles->generateSlug($string);
  305. $this->assertEquals($expected, $result);
  306. $string = 'something with a # in it';
  307. $expected = 'something-with-a-in-it';
  308. $result = $this->articles->generateSlug($string);
  309. $this->assertEquals($expected, $result);
  310. }
  311. /**
  312. * Test slug with ascii
  313. *
  314. * @return void
  315. */
  316. public function testSlugGenerationModeAscii() {
  317. $this->articles->removeBehavior('Slugged');
  318. $this->articles->addBehavior('Tools.Slugged', array(
  319. 'mode' => 'ascii'));
  320. $article = $this->articles->newEntity(array('title' => 'Some Article 25271'));
  321. $result = $this->articles->save($article);
  322. $this->assertTrue((bool)$result);
  323. $this->assertEquals('Some-Article-25271', $result['slug']);
  324. }
  325. /**
  326. * Test slug generation/update on beforeSave
  327. *
  328. * @return void
  329. */
  330. public function testSlugGenerationBeforeSave() {
  331. $this->articles->removeBehavior('Slugged');
  332. $this->articles->addBehavior('Tools.Slugged', array(
  333. 'on' => 'beforeSave', 'overwrite' => true));
  334. $article = $this->articles->newEntity(array('title' => 'Some Article 25271'));
  335. $result = $this->articles->save($article);
  336. //$result['id'] = $result['id'];
  337. $this->assertEquals('Some-Article-25271', $result['slug']);
  338. }
  339. /**
  340. * Test slug generation with i18n replacement pieces
  341. *
  342. * @return void
  343. */
  344. public function testSlugGenerationI18nReplacementPieces() {
  345. $this->articles->removeBehavior('Slugged');
  346. $this->articles->addBehavior('Tools.Slugged', array(
  347. 'overwrite' => true));
  348. $article = $this->articles->newEntity(array('title' => 'Some & More'));
  349. $result = $this->articles->save($article);
  350. $this->assertEquals('Some-' . __d('tools', 'and') . '-More', $result['slug']);
  351. }
  352. /**
  353. * Test dynamic slug overwrite
  354. *
  355. * @return void
  356. */
  357. public function testSlugDynamicOverwrite() {
  358. $this->articles->removeBehavior('Slugged');
  359. $this->articles->addBehavior('Tools.Slugged', array(
  360. 'overwrite' => false, 'overwriteField' => 'overwrite_my_slug'));
  361. $article = $this->articles->newEntity(array('title' => 'Some Cool String', 'overwrite_my_slug' => false));
  362. $result = $this->articles->save($article);
  363. $this->assertEquals('Some-Cool-String', $result['slug']);
  364. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String']);
  365. $result = $this->articles->save($article);
  366. $this->assertEquals('Some-Cool-String', $result['slug']);
  367. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => true]);
  368. $result = $this->articles->save($article);
  369. $this->assertEquals('Some-Cool-Other-String', $result['slug']);
  370. }
  371. /**
  372. * Test slug generation/update based on scope
  373. *
  374. * @return void
  375. */
  376. public function testSlugGenerationWithScope() {
  377. $this->articles->removeBehavior('Slugged');
  378. $this->articles->addBehavior('Tools.Slugged', array('unique' => true));
  379. $data = array('title' => 'Some Article 12345', 'section' => 0);
  380. $article = $this->articles->newEntity($data);
  381. $result = $this->articles->save($article);
  382. $this->assertTrue((bool)$result);
  383. $this->assertEquals('Some-Article-12345', $result['slug']);
  384. $article = $this->articles->newEntity($data);
  385. $result = $this->articles->save($article);
  386. $this->assertTrue((bool)$result);
  387. $this->assertEquals('Some-Article-12345-1', $result['slug']);
  388. $this->articles->removeBehavior('Slugged');
  389. $this->articles->addBehavior('Tools.Slugged', array('unique' => true, 'scope' => array('section' => 1)));
  390. $data = array('title' => 'Some Article 12345', 'section' => 1);
  391. $article = $this->articles->newEntity($data);
  392. $result = $this->articles->save($article);
  393. $this->assertTrue((bool)$result);
  394. $this->assertEquals('Some-Article-12345', $result['slug']);
  395. }
  396. /**
  397. * Get a new Entity
  398. *
  399. * @return Entity
  400. */
  401. protected function _getEntity($title = 'test 123', $field = 'title') {
  402. return new Entity([
  403. $field => $title
  404. ]);
  405. }
  406. }