SluggedBehaviorTest.php 22 KB

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