SluggedBehaviorTest.php 22 KB

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