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 $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. * @return void
  316. */
  317. /**
  318. * TestTruncateMultibyte method
  319. *
  320. * Ensure that the first test doesn't cut a multibyte character The test string is:
  321. * 17 chars
  322. * 51 bytes UTF-8 encoded
  323. *
  324. * @return void
  325. */
  326. public function testTruncateMultibyte() {
  327. $this->articles->behaviors()->Slugged->setConfig(['length' => 16]);
  328. $result = $this->articles->generateSlug('モデルのデータベースとデータソース');
  329. $expected = 'モデルのデータベースとデータソー';
  330. $this->assertEquals($expected, $result);
  331. }
  332. /**
  333. * @return void
  334. */
  335. public function testSlugManually() {
  336. $article = new Entity();
  337. $article->title = 'Foo Bar';
  338. $this->articles->slug($article);
  339. $this->assertSame('Foo-Bar', $article->slug);
  340. }
  341. /**
  342. * Test Url method
  343. *
  344. * @return void
  345. */
  346. public function testUrlMode() {
  347. $this->articles->behaviors()->Slugged->setConfig(['mode' => 'url', 'replace' => false]);
  348. $string = 'standard string';
  349. $expected = 'standard-string';
  350. $result = $this->articles->generateSlug($string);
  351. $this->assertEquals($expected, $result);
  352. $string = 'something with a \' in it';
  353. $expected = 'something-with-a-in-it';
  354. $result = $this->articles->generateSlug($string);
  355. $this->assertEquals($expected, $result);
  356. $string = 'something with a " in it';
  357. $expected = 'something-with-a-in-it';
  358. $result = $this->articles->generateSlug($string);
  359. $this->assertEquals($expected, $result);
  360. $string = 'something with a / in it';
  361. $expected = 'something-with-a-in-it';
  362. $result = $this->articles->generateSlug($string);
  363. $this->assertEquals($expected, $result);
  364. $string = 'something with a ? in it';
  365. $expected = 'something-with-a-in-it';
  366. $result = $this->articles->generateSlug($string);
  367. $this->assertEquals($expected, $result);
  368. $string = 'something with a < in it';
  369. $expected = 'something-with-a-in-it';
  370. $result = $this->articles->generateSlug($string);
  371. $this->assertEquals($expected, $result);
  372. $string = 'something with a > in it';
  373. $expected = 'something-with-a-in-it';
  374. $result = $this->articles->generateSlug($string);
  375. $this->assertEquals($expected, $result);
  376. $string = 'something with a . in it';
  377. $expected = 'something-with-a-in-it';
  378. $result = $this->articles->generateSlug($string);
  379. $this->assertEquals($expected, $result);
  380. $string = 'something with a $ in it';
  381. $expected = 'something-with-a-in-it';
  382. $result = $this->articles->generateSlug($string);
  383. $this->assertEquals($expected, $result);
  384. $string = 'something with a / in it';
  385. $expected = 'something-with-a-in-it';
  386. $result = $this->articles->generateSlug($string);
  387. $this->assertEquals($expected, $result);
  388. $string = 'something with a : in it';
  389. $expected = 'something-with-a-in-it';
  390. $result = $this->articles->generateSlug($string);
  391. $this->assertEquals($expected, $result);
  392. $string = 'something with a ; in it';
  393. $expected = 'something-with-a-in-it';
  394. $result = $this->articles->generateSlug($string);
  395. $this->assertEquals($expected, $result);
  396. $string = 'something with a ? in it';
  397. $expected = 'something-with-a-in-it';
  398. $result = $this->articles->generateSlug($string);
  399. $this->assertEquals($expected, $result);
  400. $string = 'something with a @ in it';
  401. $expected = 'something-with-a-in-it';
  402. $result = $this->articles->generateSlug($string);
  403. $this->assertEquals($expected, $result);
  404. $string = 'something with a = in it';
  405. $expected = 'something-with-a-in-it';
  406. $result = $this->articles->generateSlug($string);
  407. $this->assertEquals($expected, $result);
  408. $string = 'something with a + in it';
  409. $expected = 'something-with-a-in-it';
  410. $result = $this->articles->generateSlug($string);
  411. $this->assertEquals($expected, $result);
  412. $string = 'something with a & in it';
  413. $expected = 'something-with-a-in-it';
  414. $result = $this->articles->generateSlug($string);
  415. $this->assertEquals($expected, $result);
  416. $string = 'something with a % in it';
  417. $expected = 'something-with-a-in-it';
  418. $result = $this->articles->generateSlug($string);
  419. $this->assertEquals($expected, $result);
  420. $string = 'something with a \ in it';
  421. $expected = 'something-with-a-in-it';
  422. $result = $this->articles->generateSlug($string);
  423. $this->assertEquals($expected, $result);
  424. $string = 'something with a # in it';
  425. $expected = 'something-with-a-in-it';
  426. $result = $this->articles->generateSlug($string);
  427. $this->assertEquals($expected, $result);
  428. $string = 'something with a , in it';
  429. $expected = 'something-with-a-in-it';
  430. $result = $this->articles->generateSlug($string);
  431. $this->assertEquals($expected, $result);
  432. }
  433. /**
  434. * Test slug with ascii
  435. *
  436. * @return void
  437. */
  438. public function testSlugGenerationModeAscii() {
  439. $this->articles->removeBehavior('Slugged');
  440. $this->articles->addBehavior('Tools.Slugged', [
  441. 'mode' => 'ascii']);
  442. $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
  443. $result = $this->articles->save($article);
  444. $this->assertTrue((bool)$result);
  445. $this->assertEquals('Some-Article-25271', $result['slug']);
  446. }
  447. /**
  448. * Test slug generation/update on beforeSave
  449. *
  450. * @return void
  451. */
  452. public function testSlugGenerationBeforeSave() {
  453. $this->articles->removeBehavior('Slugged');
  454. $this->articles->addBehavior('Tools.Slugged', [
  455. 'on' => 'beforeSave', 'overwrite' => true]);
  456. $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
  457. $result = $this->articles->save($article);
  458. //$result['id'] = $result['id'];
  459. $this->assertEquals('Some-Article-25271', $result['slug']);
  460. }
  461. /**
  462. * Test slug generation with i18n replacement pieces
  463. *
  464. * @return void
  465. */
  466. public function testSlugGenerationI18nReplacementPieces() {
  467. $this->articles->removeBehavior('Slugged');
  468. $this->articles->addBehavior('Tools.Slugged', [
  469. 'overwrite' => true]);
  470. $article = $this->articles->newEntity(['title' => 'Some & More']);
  471. $result = $this->articles->save($article);
  472. $this->assertEquals('Some-' . __d('tools', 'and') . '-More', $result['slug']);
  473. }
  474. /**
  475. * Test dynamic slug overwrite
  476. *
  477. * @return void
  478. */
  479. public function testSlugDynamicOverwrite() {
  480. $this->articles->removeBehavior('Slugged');
  481. $this->articles->addBehavior('Tools.Slugged', [
  482. 'overwrite' => false, 'overwriteField' => 'overwrite_my_slug']);
  483. $article = $this->articles->newEntity(['title' => 'Some Cool String', 'overwrite_my_slug' => false]);
  484. $result = $this->articles->save($article);
  485. $this->assertEquals('Some-Cool-String', $result['slug']);
  486. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => false]);
  487. $result = $this->articles->save($article);
  488. $this->assertEquals('Some-Cool-String', $result['slug']);
  489. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => true]);
  490. $result = $this->articles->save($article);
  491. $this->assertEquals('Some-Cool-Other-String', $result['slug']);
  492. }
  493. /**
  494. * Test slug generation/update based on scope
  495. *
  496. * @return void
  497. */
  498. public function testSlugGenerationWithScope() {
  499. $this->articles->removeBehavior('Slugged');
  500. $this->articles->addBehavior('Tools.Slugged', ['unique' => true]);
  501. $data = ['title' => 'Some Article 12345', 'section' => 0];
  502. $article = $this->articles->newEntity($data);
  503. $result = $this->articles->save($article);
  504. $this->assertTrue((bool)$result);
  505. $this->assertEquals('Some-Article-12345', $result['slug']);
  506. $article = $this->articles->newEntity($data);
  507. $result = $this->articles->save($article);
  508. $this->assertTrue((bool)$result);
  509. $this->assertEquals('Some-Article-12345-1', $result['slug']);
  510. $this->articles->removeBehavior('Slugged');
  511. $this->articles->addBehavior('Tools.Slugged', ['unique' => true, 'scope' => ['section' => 1]]);
  512. $data = ['title' => 'Some Article 12345', 'section' => 1];
  513. $article = $this->articles->newEntity($data);
  514. $result = $this->articles->save($article);
  515. $this->assertTrue((bool)$result);
  516. $this->assertEquals('Some-Article-12345', $result['slug']);
  517. }
  518. /**
  519. * Test slug generation works with virtual fields.
  520. *
  521. * @return void
  522. */
  523. public function testSlugGenerationWithVirtualField() {
  524. $this->articles->removeBehavior('Slugged');
  525. $this->articles->setEntityClass(SluggedArticle::class);
  526. $this->articles->addBehavior('Tools.Slugged', [
  527. 'label' => [
  528. 'title',
  529. 'special',
  530. ],
  531. ]);
  532. $data = ['title' => 'Some Article 12345', 'section' => 0];
  533. $article = $this->articles->newEntity($data);
  534. $result = $this->articles->save($article);
  535. $this->assertTrue((bool)$result);
  536. $this->assertEquals('Some-Article-12345-dereuromark', $result['slug']);
  537. }
  538. /**
  539. * Tests slug generation fails with invalid entity config.
  540. *
  541. * @return void
  542. */
  543. public function testSlugGenerationWithVirtualFieldInvalidField() {
  544. $this->articles->removeBehavior('Slugged');
  545. $this->articles->setEntityClass(SluggedArticle::class);
  546. $this->expectException(RuntimeException::class);
  547. $this->expectExceptionMessage('(SluggedBehavior::setup) model `SluggedArticles` is missing the field `specialNonExistent` (specified in the setup for entity `TestApp\Model\Entity\SluggedArticle`.');
  548. $this->articles->addBehavior('Tools.Slugged', [
  549. 'label' => [
  550. 'specialNonExistent',
  551. ],
  552. ]);
  553. }
  554. /**
  555. * Test slug generation works with new slugger.
  556. *
  557. * @return void
  558. */
  559. public function testSlugGenerationWithNewSlugger() {
  560. $this->articles->removeBehavior('Slugged');
  561. $this->articles->addBehavior('Tools.Slugged', [
  562. 'mode' => [Text::class, 'slug'],
  563. ]);
  564. $data = ['title' => 'Some Article 12345'];
  565. $article = $this->articles->newEntity($data);
  566. $result = $this->articles->save($article);
  567. $this->assertTrue((bool)$result);
  568. $this->assertEquals('Some-Article-12345', $result['slug']);
  569. }
  570. /**
  571. * Test slug generation works with custom slugger.
  572. *
  573. * @return void
  574. */
  575. public function testSlugGenerationWithCustomSlugger() {
  576. $this->articles->removeBehavior('Slugged');
  577. $this->articles->addBehavior('Tools.Slugged', [
  578. 'mode' => [$this, '_customSluggerMethod'],
  579. ]);
  580. $data = ['title' => 'Some Article 12345'];
  581. $article = $this->articles->newEntity($data);
  582. $result = $this->articles->save($article);
  583. $this->assertTrue((bool)$result);
  584. $this->assertEquals('some article 12345', $result['slug']);
  585. }
  586. /**
  587. * @param string $name
  588. *
  589. * @return string
  590. */
  591. public function _customSluggerMethod($name) {
  592. return mb_strtolower($name);
  593. }
  594. /**
  595. * Get a new Entity
  596. *
  597. * @param string|null $title
  598. * @param string|null $field
  599. * @param array $data
  600. * @param array $options
  601. * @return \Cake\ORM\Entity
  602. */
  603. protected function _getEntity($title = null, $field = null, array $data = [], array $options = []) {
  604. $options += ['validate' => false];
  605. if ($title === null) {
  606. $title = 'test 123';
  607. }
  608. if ($field === null) {
  609. $field = 'title';
  610. }
  611. $data = [
  612. $field => $title,
  613. ] + $data;
  614. return new Entity($data, $options);
  615. }
  616. }