SluggedBehaviorTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. * @return void
  151. */
  152. public function testNeedsSlugUpdate() {
  153. // No title change
  154. $entity = $this->articles->newEntity(['title' => 'Some title'], ['fields' => []]);
  155. $result = $this->articles->needsSlugUpdate($entity);
  156. $this->assertFalse($result);
  157. // Title change
  158. $entity = $this->articles->newEntity(['title' => 'Some title']);
  159. $result = $this->articles->needsSlugUpdate($entity);
  160. $this->assertTrue($result);
  161. $result = $this->articles->save($entity);
  162. $this->assertEquals('Some-title', $result->get('slug'));
  163. // No title change
  164. $entity = $this->articles->patchEntity($entity, ['description' => 'Foo bar']);
  165. $result = $this->articles->needsSlugUpdate($entity);
  166. $this->assertFalse($result);
  167. // Needs an update, but overwrite is still false: will not modify the slug
  168. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  169. $result = $this->articles->needsSlugUpdate($entity);
  170. $this->assertTrue($result);
  171. $result = $this->articles->save($entity);
  172. $this->assertEquals('Some-title', $result->get('slug'));
  173. $this->articles->behaviors()->Slugged->setConfig(['overwrite' => true]);
  174. // Now it can modify the slug
  175. $entity = $this->articles->patchEntity($entity, ['title' => 'Some really other title']);
  176. $result = $this->articles->needsSlugUpdate($entity);
  177. $this->assertTrue($result);
  178. $result = $this->articles->save($entity);
  179. $this->assertEquals('Some-really-other-title', $result->get('slug'));
  180. $this->articles->behaviors()->Slugged->setConfig(['overwrite' => true]);
  181. // Without title present it should not modify the slug
  182. $entity = $this->articles->patchEntity($entity, ['foo' => 'bar']);
  183. $result = $this->articles->needsSlugUpdate($entity);
  184. $this->assertFalse($result);
  185. $result = $this->articles->save($entity);
  186. $this->assertEquals('Some-really-other-title', $result->get('slug'));
  187. }
  188. /**
  189. * @return void
  190. */
  191. public function testNeedsNoSlugUpdate() {
  192. $entity = $this->articles->newEntity(['title' => 'Some title']);
  193. $result = $this->articles->save($entity);
  194. $this->assertEquals('Some-title', $result->get('slug'));
  195. $entity = $this->articles->get($entity->id, ['fields' => ['id', 'slug']]);
  196. $this->articles->behaviors()->Slugged->setConfig(['overwrite' => true]);
  197. // Without title present it should not modify the slug
  198. $entity = $this->articles->patchEntity($entity, ['foo' => 'bar']);
  199. $result = $this->articles->needsSlugUpdate($entity);
  200. $this->assertFalse($result);
  201. $this->articles->saveOrFail($entity);
  202. $entity = $this->articles->get($entity->id);
  203. $this->assertEquals('Some-title', $entity->get('slug'));
  204. }
  205. /**
  206. * Tests needSlugUpdate() with deep
  207. *
  208. * @return void
  209. */
  210. public function testNeedsSlugUpdateDeep() {
  211. // No title change
  212. $entity = $this->articles->newEntity(['title' => 'Some title']);
  213. $result = $this->articles->needsSlugUpdate($entity);
  214. $this->assertTrue($result);
  215. $result = $this->articles->needsSlugUpdate($entity, true);
  216. $this->assertTrue($result);
  217. $result = $this->articles->save($entity);
  218. $this->assertEquals('Some-title', $result->get('slug'));
  219. // Needs an update, but overwrite is still false: will not modify the slug
  220. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  221. $result = $this->articles->needsSlugUpdate($entity);
  222. $this->assertTrue($result);
  223. $result = $this->articles->needsSlugUpdate($entity, true);
  224. $this->assertTrue($result);
  225. $result = $this->articles->save($entity);
  226. $this->assertEquals('Some-title', $result->get('slug'));
  227. // Here deep would tell the truth
  228. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  229. $result = $this->articles->needsSlugUpdate($entity);
  230. $this->assertFalse($result);
  231. $result = $this->articles->needsSlugUpdate($entity, true);
  232. $this->assertTrue($result);
  233. }
  234. /**
  235. * Length based on auto-detect of schema.
  236. *
  237. * @return void
  238. */
  239. public function testLengthRestrictionAutoDetect() {
  240. $entity = $this->_getEntity(str_repeat('foo bar', 36));
  241. $result = $this->articles->save($entity);
  242. $this->assertEquals(252, strlen($result->get('slug')));
  243. }
  244. /**
  245. * Ensure that you can overwrite length.
  246. *
  247. * @return void
  248. */
  249. public function testLengthRestrictionNoLimit() {
  250. $this->articles->behaviors()->Slugged->setConfig(['length' => 0, 'label' => 'long_title', 'field' => 'long_slug']);
  251. $entity = $this->_getEntity(str_repeat('foo bar', 35), 'long_title');
  252. $result = $this->articles->save($entity);
  253. $this->assertEquals(245, strlen($result->get('long_slug')));
  254. }
  255. /**
  256. * @return void
  257. */
  258. public function testResetSlugs() {
  259. $this->articles->removeBehavior('Slugged');
  260. $article = $this->articles->newEntity(['title' => 'Andy Dawson', 'slug' => 'foo']);
  261. $this->articles->save($article);
  262. $article = $this->articles->newEntity(['title' => 'Andy Dawsom', 'slug' => 'bar']);
  263. $this->articles->save($article);
  264. $result = $this->articles->find('all', [
  265. 'conditions' => ['title LIKE' => 'Andy Daw%'],
  266. 'fields' => ['title', 'slug'],
  267. 'order' => 'title',
  268. ])->all()->combine('title', 'slug')->toArray();
  269. $expected = [
  270. 'Andy Dawsom' => 'bar',
  271. 'Andy Dawson' => 'foo',
  272. ];
  273. $this->assertEquals($expected, $result);
  274. $this->articles->addBehavior('Tools.Slugged');
  275. $result = $this->articles->resetSlugs(['limit' => 1]);
  276. $this->assertTrue($result);
  277. $result = $this->articles->find('all', [
  278. 'conditions' => ['title LIKE' => 'Andy Daw%'],
  279. 'fields' => ['title', 'slug'],
  280. 'order' => 'title',
  281. ])->all()->combine('title', 'slug')->toArray();
  282. $expected = [
  283. 'Andy Dawsom' => 'Andy-Dawsom',
  284. 'Andy Dawson' => 'Andy-Dawson',
  285. ];
  286. $this->assertEquals($expected, $result);
  287. }
  288. /**
  289. * If there's a length restriction - ensure it's respected by the unique slug routine
  290. *
  291. * @return void
  292. */
  293. public function testDuplicateWithLengthRestriction() {
  294. $this->skipIf(true);
  295. $this->articles->behaviors()->Slugged->setConfig(['length' => 10, 'unique' => true]);
  296. $article = $this->articles->newEntity(['title' => 'Andy Dawson']);
  297. $this->articles->save($article);
  298. $article = $this->articles->newEntity(['title' => 'Andy Dawsom']);
  299. $this->articles->save($article);
  300. $article = $this->articles->newEntity(['title' => 'Andy Dawsoo']);
  301. $this->articles->save($article);
  302. $article = $this->articles->newEntity(['title' => 'Andy Dawso3']);
  303. $this->articles->save($article);
  304. $article = $this->articles->newEntity(['title' => 'Andy Dawso4']);
  305. $this->articles->save($article);
  306. $article = $this->articles->newEntity(['title' => 'Andy Dawso5']);
  307. $this->articles->save($article);
  308. $article = $this->articles->newEntity(['title' => 'Andy Dawso6']);
  309. $this->articles->save($article);
  310. $article = $this->articles->newEntity(['title' => 'Andy Dawso7']);
  311. $this->articles->save($article);
  312. $article = $this->articles->newEntity(['title' => 'Andy Dawso8']);
  313. $this->articles->save($article);
  314. $article = $this->articles->newEntity(['title' => 'Andy Dawso9']);
  315. $this->articles->save($article);
  316. $article = $this->articles->newEntity(['title' => 'Andy Dawso0']);
  317. $this->articles->save($article);
  318. $result = $this->articles->find('all', [
  319. 'conditions' => ['title LIKE' => 'Andy Daw%'],
  320. 'fields' => ['title', 'slug'],
  321. 'order' => 'title',
  322. ])->all()->combine('title', 'slug')->toArray();
  323. $expected = [
  324. 'Andy Dawson' => 'Andy-Dawso',
  325. 'Andy Dawsom' => 'Andy-Daw-1',
  326. 'Andy Dawsoo' => 'Andy-Daw-2',
  327. 'Andy Dawso3' => 'Andy-Daw-3',
  328. 'Andy Dawso4' => 'Andy-Daw-4',
  329. 'Andy Dawso5' => 'Andy-Daw-5',
  330. 'Andy Dawso6' => 'Andy-Daw-6',
  331. 'Andy Dawso7' => 'Andy-Daw-7',
  332. 'Andy Dawso8' => 'Andy-Daw-8',
  333. 'Andy Dawso9' => 'Andy-Daw-9',
  334. 'Andy Dawso0' => 'Andy-Da-10',
  335. ];
  336. $this->assertEquals($expected, $result);
  337. }
  338. /**
  339. * TestTruncateMultibyte method
  340. *
  341. * Ensure that the first test doesn't cut a multibyte character The test string is:
  342. * 17 chars
  343. * 51 bytes UTF-8 encoded
  344. *
  345. * @return void
  346. */
  347. public function testTruncateMultibyte() {
  348. $this->articles->behaviors()->Slugged->setConfig(['length' => 16]);
  349. $result = $this->articles->generateSlug('モデルのデータベースとデータソース');
  350. $expected = 'モデルのデータベースとデータソー';
  351. $this->assertEquals($expected, $result);
  352. }
  353. /**
  354. * @return void
  355. */
  356. public function testSlugManually() {
  357. $article = new Entity();
  358. $article->title = 'Foo Bar';
  359. $this->articles->slug($article);
  360. $this->assertSame('Foo-Bar', $article->slug);
  361. }
  362. /**
  363. * Test Url method
  364. *
  365. * @return void
  366. */
  367. public function testUrlMode() {
  368. $this->articles->behaviors()->Slugged->setConfig(['mode' => 'url', 'replace' => false]);
  369. $string = 'standard string';
  370. $expected = 'standard-string';
  371. $result = $this->articles->generateSlug($string);
  372. $this->assertEquals($expected, $result);
  373. $string = 'something with a \' in it';
  374. $expected = 'something-with-a-in-it';
  375. $result = $this->articles->generateSlug($string);
  376. $this->assertEquals($expected, $result);
  377. $string = 'something with a " in it';
  378. $expected = 'something-with-a-in-it';
  379. $result = $this->articles->generateSlug($string);
  380. $this->assertEquals($expected, $result);
  381. $string = 'something with a / in it';
  382. $expected = 'something-with-a-in-it';
  383. $result = $this->articles->generateSlug($string);
  384. $this->assertEquals($expected, $result);
  385. $string = 'something with a ? in it';
  386. $expected = 'something-with-a-in-it';
  387. $result = $this->articles->generateSlug($string);
  388. $this->assertEquals($expected, $result);
  389. $string = 'something with a < in it';
  390. $expected = 'something-with-a-in-it';
  391. $result = $this->articles->generateSlug($string);
  392. $this->assertEquals($expected, $result);
  393. $string = 'something with a > in it';
  394. $expected = 'something-with-a-in-it';
  395. $result = $this->articles->generateSlug($string);
  396. $this->assertEquals($expected, $result);
  397. $string = 'something with a . in it';
  398. $expected = 'something-with-a-in-it';
  399. $result = $this->articles->generateSlug($string);
  400. $this->assertEquals($expected, $result);
  401. $string = 'something with a $ in it';
  402. $expected = 'something-with-a-in-it';
  403. $result = $this->articles->generateSlug($string);
  404. $this->assertEquals($expected, $result);
  405. $string = 'something with a / in it';
  406. $expected = 'something-with-a-in-it';
  407. $result = $this->articles->generateSlug($string);
  408. $this->assertEquals($expected, $result);
  409. $string = 'something with a : in it';
  410. $expected = 'something-with-a-in-it';
  411. $result = $this->articles->generateSlug($string);
  412. $this->assertEquals($expected, $result);
  413. $string = 'something with a ; in it';
  414. $expected = 'something-with-a-in-it';
  415. $result = $this->articles->generateSlug($string);
  416. $this->assertEquals($expected, $result);
  417. $string = 'something with a ? in it';
  418. $expected = 'something-with-a-in-it';
  419. $result = $this->articles->generateSlug($string);
  420. $this->assertEquals($expected, $result);
  421. $string = 'something with a @ in it';
  422. $expected = 'something-with-a-in-it';
  423. $result = $this->articles->generateSlug($string);
  424. $this->assertEquals($expected, $result);
  425. $string = 'something with a = in it';
  426. $expected = 'something-with-a-in-it';
  427. $result = $this->articles->generateSlug($string);
  428. $this->assertEquals($expected, $result);
  429. $string = 'something with a + in it';
  430. $expected = 'something-with-a-in-it';
  431. $result = $this->articles->generateSlug($string);
  432. $this->assertEquals($expected, $result);
  433. $string = 'something with a & in it';
  434. $expected = 'something-with-a-in-it';
  435. $result = $this->articles->generateSlug($string);
  436. $this->assertEquals($expected, $result);
  437. $string = 'something with a % in it';
  438. $expected = 'something-with-a-in-it';
  439. $result = $this->articles->generateSlug($string);
  440. $this->assertEquals($expected, $result);
  441. $string = 'something with a \ in it';
  442. $expected = 'something-with-a-in-it';
  443. $result = $this->articles->generateSlug($string);
  444. $this->assertEquals($expected, $result);
  445. $string = 'something with a # in it';
  446. $expected = 'something-with-a-in-it';
  447. $result = $this->articles->generateSlug($string);
  448. $this->assertEquals($expected, $result);
  449. $string = 'something with a , in it';
  450. $expected = 'something-with-a-in-it';
  451. $result = $this->articles->generateSlug($string);
  452. $this->assertEquals($expected, $result);
  453. }
  454. /**
  455. * Test slug with ascii
  456. *
  457. * @return void
  458. */
  459. public function testSlugGenerationModeAscii() {
  460. $this->articles->removeBehavior('Slugged');
  461. $this->articles->addBehavior('Tools.Slugged', [
  462. 'mode' => 'ascii']);
  463. $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
  464. $result = $this->articles->save($article);
  465. $this->assertTrue((bool)$result);
  466. $this->assertEquals('Some-Article-25271', $result['slug']);
  467. }
  468. /**
  469. * Test slug generation/update on beforeSave
  470. *
  471. * @return void
  472. */
  473. public function testSlugGenerationBeforeSave() {
  474. $this->articles->removeBehavior('Slugged');
  475. $this->articles->addBehavior('Tools.Slugged', [
  476. 'on' => 'beforeSave', 'overwrite' => true]);
  477. $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
  478. $result = $this->articles->save($article);
  479. //$result['id'] = $result['id'];
  480. $this->assertEquals('Some-Article-25271', $result['slug']);
  481. }
  482. /**
  483. * Test slug generation with i18n replacement pieces
  484. *
  485. * @return void
  486. */
  487. public function testSlugGenerationI18nReplacementPieces() {
  488. $this->articles->removeBehavior('Slugged');
  489. $this->articles->addBehavior('Tools.Slugged', [
  490. 'overwrite' => true]);
  491. $article = $this->articles->newEntity(['title' => 'Some & More']);
  492. $result = $this->articles->save($article);
  493. $this->assertEquals('Some-' . __d('tools', 'and') . '-More', $result['slug']);
  494. }
  495. /**
  496. * Test dynamic slug overwrite
  497. *
  498. * @return void
  499. */
  500. public function testSlugDynamicOverwrite() {
  501. $this->articles->removeBehavior('Slugged');
  502. $this->articles->addBehavior('Tools.Slugged', [
  503. 'overwrite' => false, 'overwriteField' => 'overwrite_my_slug']);
  504. $article = $this->articles->newEntity(['title' => 'Some Cool String', 'overwrite_my_slug' => false]);
  505. $result = $this->articles->save($article);
  506. $this->assertEquals('Some-Cool-String', $result['slug']);
  507. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => false]);
  508. $result = $this->articles->save($article);
  509. $this->assertEquals('Some-Cool-String', $result['slug']);
  510. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => true]);
  511. $result = $this->articles->save($article);
  512. $this->assertEquals('Some-Cool-Other-String', $result['slug']);
  513. }
  514. /**
  515. * Test slug generation/update based on scope
  516. *
  517. * @return void
  518. */
  519. public function testSlugGenerationWithScope() {
  520. $this->articles->removeBehavior('Slugged');
  521. $this->articles->addBehavior('Tools.Slugged', ['unique' => true]);
  522. $data = ['title' => 'Some Article 12345', 'section' => 0];
  523. $article = $this->articles->newEntity($data);
  524. $result = $this->articles->save($article);
  525. $this->assertTrue((bool)$result);
  526. $this->assertEquals('Some-Article-12345', $result['slug']);
  527. $article = $this->articles->newEntity($data);
  528. $result = $this->articles->save($article);
  529. $this->assertTrue((bool)$result);
  530. $this->assertEquals('Some-Article-12345-1', $result['slug']);
  531. $this->articles->removeBehavior('Slugged');
  532. $this->articles->addBehavior('Tools.Slugged', ['unique' => true, 'scope' => ['section' => 1]]);
  533. $data = ['title' => 'Some Article 12345', 'section' => 1];
  534. $article = $this->articles->newEntity($data);
  535. $result = $this->articles->save($article);
  536. $this->assertTrue((bool)$result);
  537. $this->assertEquals('Some-Article-12345', $result['slug']);
  538. }
  539. /**
  540. * Test slug generation works with virtual fields.
  541. *
  542. * @return void
  543. */
  544. public function testSlugGenerationWithVirtualField() {
  545. $this->articles->removeBehavior('Slugged');
  546. $this->articles->setEntityClass(SluggedArticle::class);
  547. $this->articles->addBehavior('Tools.Slugged', [
  548. 'label' => [
  549. 'title',
  550. 'special',
  551. ],
  552. ]);
  553. $data = ['title' => 'Some Article 12345', 'section' => 0];
  554. $article = $this->articles->newEntity($data);
  555. $result = $this->articles->save($article);
  556. $this->assertTrue((bool)$result);
  557. $this->assertEquals('Some-Article-12345-dereuromark', $result['slug']);
  558. }
  559. /**
  560. * Tests slug generation fails with invalid entity config.
  561. *
  562. * @return void
  563. */
  564. public function testSlugGenerationWithVirtualFieldInvalidField() {
  565. $this->articles->removeBehavior('Slugged');
  566. $this->articles->setEntityClass(SluggedArticle::class);
  567. $this->expectException(RuntimeException::class);
  568. $this->expectExceptionMessage('(SluggedBehavior::setup) model `SluggedArticles` is missing the field `specialNonExistent` (specified in the setup for entity `TestApp\Model\Entity\SluggedArticle`.');
  569. $this->articles->addBehavior('Tools.Slugged', [
  570. 'label' => [
  571. 'specialNonExistent',
  572. ],
  573. ]);
  574. }
  575. /**
  576. * Test slug generation works with new slugger.
  577. *
  578. * @return void
  579. */
  580. public function testSlugGenerationWithNewSlugger() {
  581. $this->articles->removeBehavior('Slugged');
  582. $this->articles->addBehavior('Tools.Slugged', [
  583. 'mode' => [Text::class, 'slug'],
  584. ]);
  585. $data = ['title' => 'Some Article 12345'];
  586. $article = $this->articles->newEntity($data);
  587. $result = $this->articles->save($article);
  588. $this->assertTrue((bool)$result);
  589. $this->assertEquals('Some-Article-12345', $result['slug']);
  590. }
  591. /**
  592. * Test slug generation works with custom slugger.
  593. *
  594. * @return void
  595. */
  596. public function testSlugGenerationWithCustomSlugger() {
  597. $this->articles->removeBehavior('Slugged');
  598. $this->articles->addBehavior('Tools.Slugged', [
  599. 'mode' => [$this, '_customSluggerMethod'],
  600. ]);
  601. $data = ['title' => 'Some Article 12345'];
  602. $article = $this->articles->newEntity($data);
  603. $result = $this->articles->save($article);
  604. $this->assertTrue((bool)$result);
  605. $this->assertEquals('some article 12345', $result['slug']);
  606. }
  607. /**
  608. * @param string $name
  609. *
  610. * @return string
  611. */
  612. public function _customSluggerMethod($name) {
  613. return mb_strtolower($name);
  614. }
  615. /**
  616. * Get a new Entity
  617. *
  618. * @param string|null $title
  619. * @param string|null $field
  620. * @param array $data
  621. * @param array<string, mixed> $options
  622. * @return \Cake\ORM\Entity
  623. */
  624. protected function _getEntity($title = null, $field = null, array $data = [], array $options = []) {
  625. $options += ['validate' => false];
  626. if ($title === null) {
  627. $title = 'test 123';
  628. }
  629. if ($field === null) {
  630. $field = 'title';
  631. }
  632. $data = [
  633. $field => $title,
  634. ] + $data;
  635. return new Entity($data, $options);
  636. }
  637. }