SluggedBehaviorTest.php 23 KB

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