SluggedBehaviorTest.php 18 KB

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