SluggedBehaviorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 Tools\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. * Tests that manual slugging works.
  82. *
  83. * @return void
  84. */
  85. public function testSlugManualSave() {
  86. $article = $this->articles->newEntity(array('title' => 'Some Cool String'));
  87. $result = $this->articles->save($article);
  88. $this->assertEquals('Some-Cool-String', $result['slug']);
  89. $article = $this->articles->newEntity(array('title' => 'Some Other String'));
  90. $result = $this->articles->save($article);
  91. $this->assertEquals('Some-Other-String', $result['slug']);
  92. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar']);
  93. $result = $this->articles->save($article);
  94. $this->assertEquals('foo-bar', $result['slug']);
  95. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar-bat']);
  96. $result = $this->articles->save($article);
  97. $this->assertEquals('foo-bar-bat', $result['slug']);
  98. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => '']);
  99. $result = $this->articles->save($article);
  100. $this->assertEquals('Some-Cool-Other-String', $result['slug']);
  101. }
  102. /**
  103. * Length based on manual config.
  104. *
  105. * @return void
  106. */
  107. public function testLengthRestrictionManual() {
  108. $this->articles->behaviors()->Slugged->config(['length' => 155]);
  109. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  110. $result = $this->articles->save($entity);
  111. $this->assertEquals(155, strlen($result->get('slug')));
  112. $this->articles->behaviors()->Slugged->config(['length' => 10, 'mode' => 'ascii']);
  113. $entity = $this->_getEntity('ä ö ü ä ö ü');
  114. $result = $this->articles->save($entity);
  115. $this->assertEquals('ae-oe-ue-a', $result->get('slug'));
  116. }
  117. /**
  118. * Test that fieldList doesnt mess with slug storing.
  119. *
  120. * @return void
  121. */
  122. public function testFieldList() {
  123. // field list is only relevant for newEntity(), not for what the behavior does
  124. $entity = $this->articles->newEntity(['title' => 'Some title'], ['fieldList' => ['title']]);
  125. $result = $this->articles->save($entity);
  126. $this->assertEquals('Some-title', $result->get('slug'));
  127. }
  128. /**
  129. * Tests needSlugUpdate()
  130. *
  131. * @return void
  132. */
  133. public function testNeedsSlugUpdate() {
  134. // No title change
  135. $entity = $this->articles->newEntity(['title' => 'Some title'], ['fieldList' => []]);
  136. $result = $this->articles->needsSlugUpdate($entity);
  137. $this->assertFalse($result);
  138. // Title change
  139. $entity = $this->articles->newEntity(['title' => 'Some title']);
  140. $result = $this->articles->needsSlugUpdate($entity);
  141. $this->assertTrue($result);
  142. $result = $this->articles->save($entity);
  143. $this->assertEquals('Some-title', $result->get('slug'));
  144. // No title change
  145. $entity = $this->articles->patchEntity($entity, ['description' => 'Foo bar']);
  146. $result = $this->articles->needsSlugUpdate($entity);
  147. $this->assertFalse($result);
  148. // Needs an update, but overwrite is still false: will not modify the slug
  149. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  150. $result = $this->articles->needsSlugUpdate($entity);
  151. $this->assertTrue($result);
  152. $result = $this->articles->save($entity);
  153. $this->assertEquals('Some-title', $result->get('slug'));
  154. $this->articles->behaviors()->Slugged->config(['overwrite' => true]);
  155. // Now it can modify the slug
  156. $entity = $this->articles->patchEntity($entity, ['title' => 'Some really other title']);
  157. $result = $this->articles->needsSlugUpdate($entity);
  158. $this->assertTrue($result);
  159. $result = $this->articles->save($entity);
  160. $this->assertEquals('Some-really-other-title', $result->get('slug'));
  161. }
  162. /**
  163. * Tests needSlugUpdate() with deep
  164. *
  165. * @return void
  166. */
  167. public function testNeedsSlugUpdateDeep() {
  168. // No title change
  169. $entity = $this->articles->newEntity(['title' => 'Some title']);
  170. $result = $this->articles->needsSlugUpdate($entity);
  171. $this->assertTrue($result);
  172. $result = $this->articles->needsSlugUpdate($entity, true);
  173. $this->assertTrue($result);
  174. $result = $this->articles->save($entity);
  175. $this->assertEquals('Some-title', $result->get('slug'));
  176. // Needs an update, but overwrite is still false: will not modify the slug
  177. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  178. $result = $this->articles->needsSlugUpdate($entity);
  179. $this->assertTrue($result);
  180. $result = $this->articles->needsSlugUpdate($entity, true);
  181. $this->assertTrue($result);
  182. $result = $this->articles->save($entity);
  183. $this->assertEquals('Some-title', $result->get('slug'));
  184. // Here deep would tell the truth
  185. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  186. $result = $this->articles->needsSlugUpdate($entity);
  187. $this->assertFalse($result);
  188. $result = $this->articles->needsSlugUpdate($entity, true);
  189. $this->assertTrue($result);
  190. }
  191. /**
  192. * Length based on auto-detect of schema.
  193. *
  194. * @return void
  195. */
  196. public function testLengthRestrictionAutoDetect() {
  197. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  198. $result = $this->articles->save($entity);
  199. $this->assertEquals(245, strlen($result->get('slug')));
  200. }
  201. /**
  202. * Ensure that you can overwrite length.
  203. *
  204. * @return void
  205. */
  206. public function testLengthRestrictionNoLimit() {
  207. $this->articles->behaviors()->Slugged->config(['length' => 0, 'label' => 'long_title', 'field' => 'long_slug']);
  208. $entity = $this->_getEntity(str_repeat('foo bar ', 100), 'long_title');
  209. $result = $this->articles->save($entity);
  210. $this->assertEquals(799, strlen($result->get('long_slug')));
  211. }
  212. /**
  213. * SluggedBehaviorTest::testResetSlugs()
  214. *
  215. * @return void
  216. */
  217. public function testResetSlugs() {
  218. $this->articles->removeBehavior('Slugged');
  219. $article = $this->articles->newEntity(array('title' => 'Andy Dawson', 'slug' => 'foo'));
  220. $this->articles->save($article);
  221. $article = $this->articles->newEntity(array('title' => 'Andy Dawsom', 'slug' => 'bar'));
  222. $this->articles->save($article);
  223. $result = $this->articles->find('all', array(
  224. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  225. 'fields' => array('title', 'slug'),
  226. 'order' => 'title'
  227. ))->combine('title', 'slug')->toArray();
  228. $expected = array(
  229. 'Andy Dawsom' => 'bar',
  230. 'Andy Dawson' => 'foo'
  231. );
  232. $this->assertEquals($expected, $result);
  233. $this->articles->addBehavior('Tools.Slugged');
  234. $result = $this->articles->resetSlugs(['limit' => 1]);
  235. $this->assertTrue($result);
  236. $result = $this->articles->find('all', array(
  237. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  238. 'fields' => array('title', 'slug'),
  239. 'order' => 'title'
  240. ))->combine('title', 'slug')->toArray();
  241. $expected = array(
  242. 'Andy Dawsom' => 'Andy-Dawsom',
  243. 'Andy Dawson' => 'Andy-Dawson'
  244. );
  245. $this->assertEquals($expected, $result);
  246. }
  247. /**
  248. * TestDuplicateWithLengthRestriction method
  249. *
  250. * If there's a length restriction - ensure it's respected by the unique slug routine
  251. *
  252. * @return void
  253. */
  254. public function testDuplicateWithLengthRestriction() {
  255. return;
  256. $this->articles->behaviors()->Slugged->config(['length' => 10, 'unique' => true]);
  257. $article = $this->articles->newEntity(array('title' => 'Andy Dawson'));
  258. $this->articles->save($article);
  259. $article = $this->articles->newEntity(array('title' => 'Andy Dawsom'));
  260. $this->articles->save($article);
  261. $article = $this->articles->newEntity(array('title' => 'Andy Dawsoo'));
  262. $this->articles->save($article);
  263. $article = $this->articles->newEntity(array('title' => 'Andy Dawso3'));
  264. $this->articles->save($article);
  265. $article = $this->articles->newEntity(array('title' => 'Andy Dawso4'));
  266. $this->articles->save($article);
  267. $article = $this->articles->newEntity(array('title' => 'Andy Dawso5'));
  268. $this->articles->save($article);
  269. $article = $this->articles->newEntity(array('title' => 'Andy Dawso6'));
  270. $this->articles->save($article);
  271. $article = $this->articles->newEntity(array('title' => 'Andy Dawso7'));
  272. $this->articles->save($article);
  273. $article = $this->articles->newEntity(array('title' => 'Andy Dawso8'));
  274. $this->articles->save($article);
  275. $article = $this->articles->newEntity(array('title' => 'Andy Dawso9'));
  276. $this->articles->save($article);
  277. $article = $this->articles->newEntity(array('title' => 'Andy Dawso0'));
  278. $this->articles->save($article);
  279. $result = $this->articles->find('all', array(
  280. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  281. 'fields' => array('title', 'slug'),
  282. 'order' => 'title'
  283. ))->combine('title', 'slug')->toArray();
  284. $expected = array(
  285. 'Andy Dawson' => 'Andy-Dawso',
  286. 'Andy Dawsom' => 'Andy-Daw-1',
  287. 'Andy Dawsoo' => 'Andy-Daw-2',
  288. 'Andy Dawso3' => 'Andy-Daw-3',
  289. 'Andy Dawso4' => 'Andy-Daw-4',
  290. 'Andy Dawso5' => 'Andy-Daw-5',
  291. 'Andy Dawso6' => 'Andy-Daw-6',
  292. 'Andy Dawso7' => 'Andy-Daw-7',
  293. 'Andy Dawso8' => 'Andy-Daw-8',
  294. 'Andy Dawso9' => 'Andy-Daw-9',
  295. 'Andy Dawso0' => 'Andy-Da-10'
  296. );
  297. $this->assertEquals($expected, $result);
  298. }
  299. /**
  300. * TestTruncateMultibyte method
  301. *
  302. * @return void
  303. */
  304. /**
  305. * TestTruncateMultibyte method
  306. *
  307. * Ensure that the first test doesn't cut a multibyte character The test string is:
  308. * 17 chars
  309. * 51 bytes UTF-8 encoded
  310. *
  311. * @return void
  312. */
  313. public function testTruncateMultibyte() {
  314. $this->articles->behaviors()->Slugged->config(array('length' => 16));
  315. $result = $this->articles->generateSlug('モデルのデータベースとデータソース');
  316. $expected = 'モデルのデータベースとデータソー';
  317. $this->assertEquals($expected, $result);
  318. }
  319. /**
  320. * Test Url method
  321. *
  322. * @return void
  323. */
  324. public function testUrlMode() {
  325. $this->articles->behaviors()->Slugged->config(array('mode' => 'url', 'replace' => false));
  326. $string = 'standard string';
  327. $expected = 'standard-string';
  328. $result = $this->articles->generateSlug($string);
  329. $this->assertEquals($expected, $result);
  330. $string = 'something with a \' in it';
  331. $expected = 'something-with-a-in-it';
  332. $result = $this->articles->generateSlug($string);
  333. $this->assertEquals($expected, $result);
  334. $string = 'something with a " in it';
  335. $expected = 'something-with-a-in-it';
  336. $result = $this->articles->generateSlug($string);
  337. $this->assertEquals($expected, $result);
  338. $string = 'something with a / in it';
  339. $expected = 'something-with-a-in-it';
  340. $result = $this->articles->generateSlug($string);
  341. $this->assertEquals($expected, $result);
  342. $string = 'something with a ? in it';
  343. $expected = 'something-with-a-in-it';
  344. $result = $this->articles->generateSlug($string);
  345. $this->assertEquals($expected, $result);
  346. $string = 'something with a < in it';
  347. $expected = 'something-with-a-in-it';
  348. $result = $this->articles->generateSlug($string);
  349. $this->assertEquals($expected, $result);
  350. $string = 'something with a > in it';
  351. $expected = 'something-with-a-in-it';
  352. $result = $this->articles->generateSlug($string);
  353. $this->assertEquals($expected, $result);
  354. $string = 'something with a . in it';
  355. $expected = 'something-with-a-in-it';
  356. $result = $this->articles->generateSlug($string);
  357. $this->assertEquals($expected, $result);
  358. $string = 'something with a $ in it';
  359. $expected = 'something-with-a-in-it';
  360. $result = $this->articles->generateSlug($string);
  361. $this->assertEquals($expected, $result);
  362. $string = 'something with a / in it';
  363. $expected = 'something-with-a-in-it';
  364. $result = $this->articles->generateSlug($string);
  365. $this->assertEquals($expected, $result);
  366. $string = 'something with a : in it';
  367. $expected = 'something-with-a-in-it';
  368. $result = $this->articles->generateSlug($string);
  369. $this->assertEquals($expected, $result);
  370. $string = 'something with a ; in it';
  371. $expected = 'something-with-a-in-it';
  372. $result = $this->articles->generateSlug($string);
  373. $this->assertEquals($expected, $result);
  374. $string = 'something with a ? in it';
  375. $expected = 'something-with-a-in-it';
  376. $result = $this->articles->generateSlug($string);
  377. $this->assertEquals($expected, $result);
  378. $string = 'something with a @ in it';
  379. $expected = 'something-with-a-in-it';
  380. $result = $this->articles->generateSlug($string);
  381. $this->assertEquals($expected, $result);
  382. $string = 'something with a = in it';
  383. $expected = 'something-with-a-in-it';
  384. $result = $this->articles->generateSlug($string);
  385. $this->assertEquals($expected, $result);
  386. $string = 'something with a + in it';
  387. $expected = 'something-with-a-in-it';
  388. $result = $this->articles->generateSlug($string);
  389. $this->assertEquals($expected, $result);
  390. $string = 'something with a & in it';
  391. $expected = 'something-with-a-in-it';
  392. $result = $this->articles->generateSlug($string);
  393. $this->assertEquals($expected, $result);
  394. $string = 'something with a % in it';
  395. $expected = 'something-with-a-in-it';
  396. $result = $this->articles->generateSlug($string);
  397. $this->assertEquals($expected, $result);
  398. $string = 'something with a \ in it';
  399. $expected = 'something-with-a-in-it';
  400. $result = $this->articles->generateSlug($string);
  401. $this->assertEquals($expected, $result);
  402. $string = 'something with a # in it';
  403. $expected = 'something-with-a-in-it';
  404. $result = $this->articles->generateSlug($string);
  405. $this->assertEquals($expected, $result);
  406. }
  407. /**
  408. * Test slug with ascii
  409. *
  410. * @return void
  411. */
  412. public function testSlugGenerationModeAscii() {
  413. $this->articles->removeBehavior('Slugged');
  414. $this->articles->addBehavior('Tools.Slugged', array(
  415. 'mode' => 'ascii'));
  416. $article = $this->articles->newEntity(array('title' => 'Some Article 25271'));
  417. $result = $this->articles->save($article);
  418. $this->assertTrue((bool)$result);
  419. $this->assertEquals('Some-Article-25271', $result['slug']);
  420. }
  421. /**
  422. * Test slug generation/update on beforeSave
  423. *
  424. * @return void
  425. */
  426. public function testSlugGenerationBeforeSave() {
  427. $this->articles->removeBehavior('Slugged');
  428. $this->articles->addBehavior('Tools.Slugged', array(
  429. 'on' => 'beforeSave', 'overwrite' => true));
  430. $article = $this->articles->newEntity(array('title' => 'Some Article 25271'));
  431. $result = $this->articles->save($article);
  432. //$result['id'] = $result['id'];
  433. $this->assertEquals('Some-Article-25271', $result['slug']);
  434. }
  435. /**
  436. * Test slug generation with i18n replacement pieces
  437. *
  438. * @return void
  439. */
  440. public function testSlugGenerationI18nReplacementPieces() {
  441. $this->articles->removeBehavior('Slugged');
  442. $this->articles->addBehavior('Tools.Slugged', array(
  443. 'overwrite' => true));
  444. $article = $this->articles->newEntity(array('title' => 'Some & More'));
  445. $result = $this->articles->save($article);
  446. $this->assertEquals('Some-' . __d('tools', 'and') . '-More', $result['slug']);
  447. }
  448. /**
  449. * Test dynamic slug overwrite
  450. *
  451. * @return void
  452. */
  453. public function testSlugDynamicOverwrite() {
  454. $this->articles->removeBehavior('Slugged');
  455. $this->articles->addBehavior('Tools.Slugged', array(
  456. 'overwrite' => false, 'overwriteField' => 'overwrite_my_slug'));
  457. $article = $this->articles->newEntity(array('title' => 'Some Cool String', 'overwrite_my_slug' => false));
  458. $result = $this->articles->save($article);
  459. $this->assertEquals('Some-Cool-String', $result['slug']);
  460. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => false]);
  461. $result = $this->articles->save($article);
  462. $this->assertEquals('Some-Cool-String', $result['slug']);
  463. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => true]);
  464. $result = $this->articles->save($article);
  465. $this->assertEquals('Some-Cool-Other-String', $result['slug']);
  466. }
  467. /**
  468. * Test slug generation/update based on scope
  469. *
  470. * @return void
  471. */
  472. public function testSlugGenerationWithScope() {
  473. $this->articles->removeBehavior('Slugged');
  474. $this->articles->addBehavior('Tools.Slugged', array('unique' => true));
  475. $data = array('title' => 'Some Article 12345', 'section' => 0);
  476. $article = $this->articles->newEntity($data);
  477. $result = $this->articles->save($article);
  478. $this->assertTrue((bool)$result);
  479. $this->assertEquals('Some-Article-12345', $result['slug']);
  480. $article = $this->articles->newEntity($data);
  481. $result = $this->articles->save($article);
  482. $this->assertTrue((bool)$result);
  483. $this->assertEquals('Some-Article-12345-1', $result['slug']);
  484. $this->articles->removeBehavior('Slugged');
  485. $this->articles->addBehavior('Tools.Slugged', array('unique' => true, 'scope' => array('section' => 1)));
  486. $data = array('title' => 'Some Article 12345', 'section' => 1);
  487. $article = $this->articles->newEntity($data);
  488. $result = $this->articles->save($article);
  489. $this->assertTrue((bool)$result);
  490. $this->assertEquals('Some-Article-12345', $result['slug']);
  491. }
  492. /**
  493. * Get a new Entity
  494. *
  495. * @return Entity
  496. */
  497. protected function _getEntity($title = 'test 123', $field = 'title', array $options = array()) {
  498. return new Entity([
  499. $field => $title
  500. ], $options);
  501. }
  502. }