SluggedBehaviorTest.php 22 KB

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