SluggedBehaviorTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\Core\Configure;
  4. use Cake\ORM\Entity;
  5. use Cake\ORM\TableRegistry;
  6. use Tools\TestSuite\TestCase;
  7. use Tools\Utility\Text;
  8. /**
  9. * SluggedBehaviorTest
  10. */
  11. class SluggedBehaviorTest extends TestCase {
  12. /**
  13. * Fixture
  14. *
  15. * @var array
  16. */
  17. public $fixtures = [
  18. 'plugin.tools.slugged_articles'
  19. ];
  20. /**
  21. * @var \Cake\ORM\Table|\Tools\Model\Behavior\SluggedBehavior
  22. */
  23. protected $articles;
  24. /**
  25. * setup
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. //$this->connection = ConnectionManager::get('test');
  32. $options = ['alias' => 'Articles'];
  33. $this->articles = TableRegistry::get('SluggedArticles', $options);
  34. Configure::delete('Slugged');
  35. $this->articles->addBehavior('Tools.Slugged');
  36. }
  37. /**
  38. * teardown
  39. *
  40. * @return void
  41. */
  42. public function tearDown() {
  43. unset($this->articles);
  44. TableRegistry::clear();
  45. parent::tearDown();
  46. }
  47. /**
  48. * Testing simple slugging when adding a record
  49. *
  50. * @return void
  51. */
  52. public function testAdd() {
  53. $entity = $this->_getEntity();
  54. $result = $this->articles->save($entity);
  55. $this->assertEquals('test-123', $result->get('slug'));
  56. }
  57. /**
  58. * Testing simple slugging when adding a record
  59. *
  60. * @return void
  61. */
  62. public function testAddUnique() {
  63. $this->articles->behaviors()->Slugged->setConfig(['unique' => true]);
  64. $entity = $this->_getEntity();
  65. $result = $this->articles->save($entity);
  66. $this->assertEquals('test-123', $result->get('slug'));
  67. //$entity = $this->_getEntity();
  68. //$result = $this->articles->save($entity);
  69. //$this->assertEquals('test-123', $result->get('slug'));
  70. //debug($result);
  71. }
  72. /**
  73. * @return void
  74. */
  75. public function testAddUniqueMultipleLabels() {
  76. /** @var \Tools\Model\Behavior\SluggedBehavior $sluggedBehavior */
  77. $sluggedBehavior = $this->articles->behaviors()->Slugged;
  78. //$this->articles->behaviors()->Slugged->setConfig('label', ''); // Hack necessary right now to avoid title showing up twice
  79. $sluggedBehavior->configShallow(['mode' => 'ascii', 'unique' => true, 'label' => ['title', 'long_title']]);
  80. $entity = $this->_getEntity(null, null, ['long_title' => 'blae']);
  81. $result = $this->articles->save($entity);
  82. $this->assertEquals('test-123-blae', $result->get('slug'));
  83. $entity = $this->_getEntity(null, null, ['long_title' => 'blä']);
  84. $result = $this->articles->save($entity);
  85. $this->assertEquals('test-123-blae-1', $result->get('slug'));
  86. }
  87. /**
  88. * SluggedBehaviorTest::testCustomFinder()
  89. *
  90. * @return void
  91. */
  92. public function testCustomFinder() {
  93. $article = $this->articles->find()->find('slugged', ['slug' => 'foo'])->first();
  94. $this->assertEquals('Foo', $article->get('title'));
  95. }
  96. /**
  97. * Tests that manual slugging works.
  98. *
  99. * @return void
  100. */
  101. public function testSlugManualSave() {
  102. $article = $this->articles->newEntity(['title' => 'Some Cool String']);
  103. $result = $this->articles->save($article);
  104. $this->assertEquals('Some-Cool-String', $result['slug']);
  105. $article = $this->articles->newEntity(['title' => 'Some Other String']);
  106. $result = $this->articles->save($article);
  107. $this->assertEquals('Some-Other-String', $result['slug']);
  108. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar']);
  109. $result = $this->articles->save($article);
  110. $this->assertEquals('foo-bar', $result['slug']);
  111. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar-bat']);
  112. $result = $this->articles->save($article);
  113. $this->assertEquals('foo-bar-bat', $result['slug']);
  114. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => '']);
  115. $result = $this->articles->save($article);
  116. $this->assertEquals('Some-Cool-Other-String', $result['slug']);
  117. }
  118. /**
  119. * Length based on manual config.
  120. *
  121. * @return void
  122. */
  123. public function testLengthRestrictionManual() {
  124. $this->articles->behaviors()->Slugged->setConfig(['length' => 155]);
  125. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  126. $result = $this->articles->save($entity);
  127. $this->assertEquals(155, strlen($result->get('slug')));
  128. $this->articles->behaviors()->Slugged->setConfig(['length' => 10, 'mode' => 'ascii']);
  129. $entity = $this->_getEntity('ä ö ü ä ö ü');
  130. $result = $this->articles->save($entity);
  131. $this->assertEquals('ae-oe-ue-a', $result->get('slug'));
  132. }
  133. /**
  134. * Test that fields doesnt mess with slug storing.
  135. *
  136. * @return void
  137. */
  138. public function testFields() {
  139. // field list is only relevant for newEntity(), not for what the behavior does
  140. $entity = $this->articles->newEntity(['title' => 'Some title'], ['fields' => ['title']]);
  141. $result = $this->articles->save($entity);
  142. $this->assertEquals('Some-title', $result->get('slug'));
  143. }
  144. /**
  145. * Tests needSlugUpdate()
  146. *
  147. * @return void
  148. */
  149. public function testNeedsSlugUpdate() {
  150. // No title change
  151. $entity = $this->articles->newEntity(['title' => 'Some title'], ['fields' => []]);
  152. $result = $this->articles->needsSlugUpdate($entity);
  153. $this->assertFalse($result);
  154. // Title change
  155. $entity = $this->articles->newEntity(['title' => 'Some title']);
  156. $result = $this->articles->needsSlugUpdate($entity);
  157. $this->assertTrue($result);
  158. $result = $this->articles->save($entity);
  159. $this->assertEquals('Some-title', $result->get('slug'));
  160. // No title change
  161. $entity = $this->articles->patchEntity($entity, ['description' => 'Foo bar']);
  162. $result = $this->articles->needsSlugUpdate($entity);
  163. $this->assertFalse($result);
  164. // Needs an update, but overwrite is still false: will not modify the slug
  165. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  166. $result = $this->articles->needsSlugUpdate($entity);
  167. $this->assertTrue($result);
  168. $result = $this->articles->save($entity);
  169. $this->assertEquals('Some-title', $result->get('slug'));
  170. $this->articles->behaviors()->Slugged->setConfig(['overwrite' => true]);
  171. // Now it can modify the slug
  172. $entity = $this->articles->patchEntity($entity, ['title' => 'Some really other title']);
  173. $result = $this->articles->needsSlugUpdate($entity);
  174. $this->assertTrue($result);
  175. $result = $this->articles->save($entity);
  176. $this->assertEquals('Some-really-other-title', $result->get('slug'));
  177. }
  178. /**
  179. * Tests needSlugUpdate() with deep
  180. *
  181. * @return void
  182. */
  183. public function testNeedsSlugUpdateDeep() {
  184. // No title change
  185. $entity = $this->articles->newEntity(['title' => 'Some title']);
  186. $result = $this->articles->needsSlugUpdate($entity);
  187. $this->assertTrue($result);
  188. $result = $this->articles->needsSlugUpdate($entity, true);
  189. $this->assertTrue($result);
  190. $result = $this->articles->save($entity);
  191. $this->assertEquals('Some-title', $result->get('slug'));
  192. // Needs an update, but overwrite is still false: will not modify the slug
  193. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  194. $result = $this->articles->needsSlugUpdate($entity);
  195. $this->assertTrue($result);
  196. $result = $this->articles->needsSlugUpdate($entity, true);
  197. $this->assertTrue($result);
  198. $result = $this->articles->save($entity);
  199. $this->assertEquals('Some-title', $result->get('slug'));
  200. // Here deep would tell the truth
  201. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  202. $result = $this->articles->needsSlugUpdate($entity);
  203. $this->assertFalse($result);
  204. $result = $this->articles->needsSlugUpdate($entity, true);
  205. $this->assertTrue($result);
  206. }
  207. /**
  208. * Length based on auto-detect of schema.
  209. *
  210. * @return void
  211. */
  212. public function testLengthRestrictionAutoDetect() {
  213. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  214. $result = $this->articles->save($entity);
  215. $this->assertEquals(245, strlen($result->get('slug')));
  216. }
  217. /**
  218. * Ensure that you can overwrite length.
  219. *
  220. * @return void
  221. */
  222. public function testLengthRestrictionNoLimit() {
  223. $this->articles->behaviors()->Slugged->setConfig(['length' => 0, 'label' => 'long_title', 'field' => 'long_slug']);
  224. $entity = $this->_getEntity(str_repeat('foo bar ', 100), 'long_title');
  225. $result = $this->articles->save($entity);
  226. $this->assertEquals(799, strlen($result->get('long_slug')));
  227. }
  228. /**
  229. * SluggedBehaviorTest::testResetSlugs()
  230. *
  231. * @return void
  232. */
  233. public function testResetSlugs() {
  234. $this->articles->removeBehavior('Slugged');
  235. $article = $this->articles->newEntity(['title' => 'Andy Dawson', 'slug' => 'foo']);
  236. $this->articles->save($article);
  237. $article = $this->articles->newEntity(['title' => 'Andy Dawsom', 'slug' => 'bar']);
  238. $this->articles->save($article);
  239. $result = $this->articles->find('all', [
  240. 'conditions' => ['title LIKE' => 'Andy Daw%'],
  241. 'fields' => ['title', 'slug'],
  242. 'order' => 'title'
  243. ])->combine('title', 'slug')->toArray();
  244. $expected = [
  245. 'Andy Dawsom' => 'bar',
  246. 'Andy Dawson' => 'foo'
  247. ];
  248. $this->assertEquals($expected, $result);
  249. $this->articles->addBehavior('Tools.Slugged');
  250. $result = $this->articles->resetSlugs(['limit' => 1]);
  251. $this->assertTrue($result);
  252. $result = $this->articles->find('all', [
  253. 'conditions' => ['title LIKE' => 'Andy Daw%'],
  254. 'fields' => ['title', 'slug'],
  255. 'order' => 'title'
  256. ])->combine('title', 'slug')->toArray();
  257. $expected = [
  258. 'Andy Dawsom' => 'Andy-Dawsom',
  259. 'Andy Dawson' => 'Andy-Dawson'
  260. ];
  261. $this->assertEquals($expected, $result);
  262. }
  263. /**
  264. * TestDuplicateWithLengthRestriction method
  265. *
  266. * If there's a length restriction - ensure it's respected by the unique slug routine
  267. *
  268. * @return void
  269. */
  270. public function testDuplicateWithLengthRestriction() {
  271. $this->skipIf(true);
  272. $this->articles->behaviors()->Slugged->setConfig(['length' => 10, 'unique' => true]);
  273. $article = $this->articles->newEntity(['title' => 'Andy Dawson']);
  274. $this->articles->save($article);
  275. $article = $this->articles->newEntity(['title' => 'Andy Dawsom']);
  276. $this->articles->save($article);
  277. $article = $this->articles->newEntity(['title' => 'Andy Dawsoo']);
  278. $this->articles->save($article);
  279. $article = $this->articles->newEntity(['title' => 'Andy Dawso3']);
  280. $this->articles->save($article);
  281. $article = $this->articles->newEntity(['title' => 'Andy Dawso4']);
  282. $this->articles->save($article);
  283. $article = $this->articles->newEntity(['title' => 'Andy Dawso5']);
  284. $this->articles->save($article);
  285. $article = $this->articles->newEntity(['title' => 'Andy Dawso6']);
  286. $this->articles->save($article);
  287. $article = $this->articles->newEntity(['title' => 'Andy Dawso7']);
  288. $this->articles->save($article);
  289. $article = $this->articles->newEntity(['title' => 'Andy Dawso8']);
  290. $this->articles->save($article);
  291. $article = $this->articles->newEntity(['title' => 'Andy Dawso9']);
  292. $this->articles->save($article);
  293. $article = $this->articles->newEntity(['title' => 'Andy Dawso0']);
  294. $this->articles->save($article);
  295. $result = $this->articles->find('all', [
  296. 'conditions' => ['title LIKE' => 'Andy Daw%'],
  297. 'fields' => ['title', 'slug'],
  298. 'order' => 'title'
  299. ])->combine('title', 'slug')->toArray();
  300. $expected = [
  301. 'Andy Dawson' => 'Andy-Dawso',
  302. 'Andy Dawsom' => 'Andy-Daw-1',
  303. 'Andy Dawsoo' => 'Andy-Daw-2',
  304. 'Andy Dawso3' => 'Andy-Daw-3',
  305. 'Andy Dawso4' => 'Andy-Daw-4',
  306. 'Andy Dawso5' => 'Andy-Daw-5',
  307. 'Andy Dawso6' => 'Andy-Daw-6',
  308. 'Andy Dawso7' => 'Andy-Daw-7',
  309. 'Andy Dawso8' => 'Andy-Daw-8',
  310. 'Andy Dawso9' => 'Andy-Daw-9',
  311. 'Andy Dawso0' => 'Andy-Da-10'
  312. ];
  313. $this->assertEquals($expected, $result);
  314. }
  315. /**
  316. * TestTruncateMultibyte method
  317. *
  318. * @return void
  319. */
  320. /**
  321. * TestTruncateMultibyte method
  322. *
  323. * Ensure that the first test doesn't cut a multibyte character The test string is:
  324. * 17 chars
  325. * 51 bytes UTF-8 encoded
  326. *
  327. * @return void
  328. */
  329. public function testTruncateMultibyte() {
  330. $this->articles->behaviors()->Slugged->setConfig(['length' => 16]);
  331. $result = $this->articles->generateSlug('モデルのデータベースとデータソース');
  332. $expected = 'モデルのデータベースとデータソー';
  333. $this->assertEquals($expected, $result);
  334. }
  335. /**
  336. * Test Url method
  337. *
  338. * @return void
  339. */
  340. public function testUrlMode() {
  341. $this->articles->behaviors()->Slugged->setConfig(['mode' => 'url', 'replace' => false]);
  342. $string = 'standard string';
  343. $expected = 'standard-string';
  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. $string = 'something with a & in it';
  407. $expected = 'something-with-a-in-it';
  408. $result = $this->articles->generateSlug($string);
  409. $this->assertEquals($expected, $result);
  410. $string = 'something with a % in it';
  411. $expected = 'something-with-a-in-it';
  412. $result = $this->articles->generateSlug($string);
  413. $this->assertEquals($expected, $result);
  414. $string = 'something with a \ in it';
  415. $expected = 'something-with-a-in-it';
  416. $result = $this->articles->generateSlug($string);
  417. $this->assertEquals($expected, $result);
  418. $string = 'something with a # in it';
  419. $expected = 'something-with-a-in-it';
  420. $result = $this->articles->generateSlug($string);
  421. $this->assertEquals($expected, $result);
  422. $string = 'something with a , in it';
  423. $expected = 'something-with-a-in-it';
  424. $result = $this->articles->generateSlug($string);
  425. $this->assertEquals($expected, $result);
  426. }
  427. /**
  428. * Test slug with ascii
  429. *
  430. * @return void
  431. */
  432. public function testSlugGenerationModeAscii() {
  433. $this->articles->removeBehavior('Slugged');
  434. $this->articles->addBehavior('Tools.Slugged', [
  435. 'mode' => 'ascii']);
  436. $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
  437. $result = $this->articles->save($article);
  438. $this->assertTrue((bool)$result);
  439. $this->assertEquals('Some-Article-25271', $result['slug']);
  440. }
  441. /**
  442. * Test slug generation/update on beforeSave
  443. *
  444. * @return void
  445. */
  446. public function testSlugGenerationBeforeSave() {
  447. $this->articles->removeBehavior('Slugged');
  448. $this->articles->addBehavior('Tools.Slugged', [
  449. 'on' => 'beforeSave', 'overwrite' => true]);
  450. $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
  451. $result = $this->articles->save($article);
  452. //$result['id'] = $result['id'];
  453. $this->assertEquals('Some-Article-25271', $result['slug']);
  454. }
  455. /**
  456. * Test slug generation with i18n replacement pieces
  457. *
  458. * @return void
  459. */
  460. public function testSlugGenerationI18nReplacementPieces() {
  461. $this->articles->removeBehavior('Slugged');
  462. $this->articles->addBehavior('Tools.Slugged', [
  463. 'overwrite' => true]);
  464. $article = $this->articles->newEntity(['title' => 'Some & More']);
  465. $result = $this->articles->save($article);
  466. $this->assertEquals('Some-' . __d('tools', 'and') . '-More', $result['slug']);
  467. }
  468. /**
  469. * Test dynamic slug overwrite
  470. *
  471. * @return void
  472. */
  473. public function testSlugDynamicOverwrite() {
  474. $this->articles->removeBehavior('Slugged');
  475. $this->articles->addBehavior('Tools.Slugged', [
  476. 'overwrite' => false, 'overwriteField' => 'overwrite_my_slug']);
  477. $article = $this->articles->newEntity(['title' => 'Some Cool String', 'overwrite_my_slug' => false]);
  478. $result = $this->articles->save($article);
  479. $this->assertEquals('Some-Cool-String', $result['slug']);
  480. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => false]);
  481. $result = $this->articles->save($article);
  482. $this->assertEquals('Some-Cool-String', $result['slug']);
  483. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => true]);
  484. $result = $this->articles->save($article);
  485. $this->assertEquals('Some-Cool-Other-String', $result['slug']);
  486. }
  487. /**
  488. * Test slug generation/update based on scope
  489. *
  490. * @return void
  491. */
  492. public function testSlugGenerationWithScope() {
  493. $this->articles->removeBehavior('Slugged');
  494. $this->articles->addBehavior('Tools.Slugged', ['unique' => true]);
  495. $data = ['title' => 'Some Article 12345', 'section' => 0];
  496. $article = $this->articles->newEntity($data);
  497. $result = $this->articles->save($article);
  498. $this->assertTrue((bool)$result);
  499. $this->assertEquals('Some-Article-12345', $result['slug']);
  500. $article = $this->articles->newEntity($data);
  501. $result = $this->articles->save($article);
  502. $this->assertTrue((bool)$result);
  503. $this->assertEquals('Some-Article-12345-1', $result['slug']);
  504. $this->articles->removeBehavior('Slugged');
  505. $this->articles->addBehavior('Tools.Slugged', ['unique' => true, 'scope' => ['section' => 1]]);
  506. $data = ['title' => 'Some Article 12345', 'section' => 1];
  507. $article = $this->articles->newEntity($data);
  508. $result = $this->articles->save($article);
  509. $this->assertTrue((bool)$result);
  510. $this->assertEquals('Some-Article-12345', $result['slug']);
  511. }
  512. /**
  513. * Test slug generation works with virtual fields.
  514. *
  515. * @return void
  516. */
  517. public function testSlugGenerationWithVirtualField() {
  518. $this->articles->removeBehavior('Slugged');
  519. $this->articles->setEntityClass('\App\Model\Entity\SluggedArticle');
  520. $this->articles->addBehavior('Tools.Slugged', [
  521. 'label' => [
  522. 'title',
  523. 'special'
  524. ],
  525. ]);
  526. $data = ['title' => 'Some Article 12345', 'section' => 0];
  527. $article = $this->articles->newEntity($data);
  528. $result = $this->articles->save($article);
  529. $this->assertTrue((bool)$result);
  530. $this->assertEquals('Some-Article-12345-dereuromark', $result['slug']);
  531. }
  532. /**
  533. * Test slug generation works with new slugger.
  534. *
  535. * @return void
  536. */
  537. public function testSlugGenerationWithNewSlugger() {
  538. $this->articles->removeBehavior('Slugged');
  539. $this->articles->addBehavior('Tools.Slugged', [
  540. 'mode' => [Text::class, 'slug'],
  541. ]);
  542. $data = ['title' => 'Some Article 12345'];
  543. $article = $this->articles->newEntity($data);
  544. $result = $this->articles->save($article);
  545. $this->assertTrue((bool)$result);
  546. $this->assertEquals('Some-Article-12345', $result['slug']);
  547. }
  548. /**
  549. * Test slug generation works with custom slugger.
  550. *
  551. * @return void
  552. */
  553. public function testSlugGenerationWithCustomSlugger() {
  554. $this->articles->removeBehavior('Slugged');
  555. $this->articles->addBehavior('Tools.Slugged', [
  556. 'mode' => [$this, '_customSluggerMethod'],
  557. ]);
  558. $data = ['title' => 'Some Article 12345'];
  559. $article = $this->articles->newEntity($data);
  560. $result = $this->articles->save($article);
  561. $this->assertTrue((bool)$result);
  562. $this->assertEquals('some article 12345', $result['slug']);
  563. }
  564. /**
  565. * @param string $name
  566. *
  567. * @return string
  568. */
  569. public function _customSluggerMethod($name) {
  570. return mb_strtolower($name);
  571. }
  572. /**
  573. * Get a new Entity
  574. *
  575. * @param string|null $title
  576. * @param string|null $field
  577. * @param array $data
  578. * @param array $options
  579. * @return \Cake\ORM\Entity
  580. */
  581. protected function _getEntity($title = null, $field = null, array $data = [], array $options = []) {
  582. $options += ['validate' => false];
  583. if ($title === null) {
  584. $title = 'test 123';
  585. }
  586. if ($field === null) {
  587. $field = 'title';
  588. }
  589. $data = [
  590. $field => $title
  591. ] + $data;
  592. return new Entity($data, $options);
  593. }
  594. }