SluggedBehaviorTest.php 21 KB

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