SluggedBehaviorTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use App\Model\Entity\SluggedArticle;
  4. use Cake\Core\Configure;
  5. use Cake\ORM\Entity;
  6. use Cake\ORM\TableRegistry;
  7. use Tools\TestSuite\TestCase;
  8. use Tools\Utility\Text;
  9. /**
  10. * SluggedBehaviorTest
  11. */
  12. class SluggedBehaviorTest extends TestCase {
  13. /**
  14. * Fixture
  15. *
  16. * @var array
  17. */
  18. public $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() {
  31. parent::setUp();
  32. //$this->connection = ConnectionManager::get('test');
  33. $options = ['alias' => 'Articles'];
  34. $this->articles = TableRegistry::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() {
  44. unset($this->articles);
  45. TableRegistry::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 ', 31));
  215. $result = $this->articles->save($entity);
  216. $this->assertEquals(245, 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 ', 100), 'long_title');
  226. $result = $this->articles->save($entity);
  227. $this->assertEquals(799, strlen($result->get('long_slug')));
  228. }
  229. /**
  230. * SluggedBehaviorTest::testResetSlugs()
  231. *
  232. * @return void
  233. */
  234. public function testResetSlugs() {
  235. $this->articles->removeBehavior('Slugged');
  236. $article = $this->articles->newEntity(['title' => 'Andy Dawson', 'slug' => 'foo']);
  237. $this->articles->save($article);
  238. $article = $this->articles->newEntity(['title' => 'Andy Dawsom', 'slug' => 'bar']);
  239. $this->articles->save($article);
  240. $result = $this->articles->find('all', [
  241. 'conditions' => ['title LIKE' => 'Andy Daw%'],
  242. 'fields' => ['title', 'slug'],
  243. 'order' => 'title'
  244. ])->combine('title', 'slug')->toArray();
  245. $expected = [
  246. 'Andy Dawsom' => 'bar',
  247. 'Andy Dawson' => 'foo'
  248. ];
  249. $this->assertEquals($expected, $result);
  250. $this->articles->addBehavior('Tools.Slugged');
  251. $result = $this->articles->resetSlugs(['limit' => 1]);
  252. $this->assertTrue($result);
  253. $result = $this->articles->find('all', [
  254. 'conditions' => ['title LIKE' => 'Andy Daw%'],
  255. 'fields' => ['title', 'slug'],
  256. 'order' => 'title'
  257. ])->combine('title', 'slug')->toArray();
  258. $expected = [
  259. 'Andy Dawsom' => 'Andy-Dawsom',
  260. 'Andy Dawson' => 'Andy-Dawson'
  261. ];
  262. $this->assertEquals($expected, $result);
  263. }
  264. /**
  265. * TestDuplicateWithLengthRestriction method
  266. *
  267. * If there's a length restriction - ensure it's respected by the unique slug routine
  268. *
  269. * @return void
  270. */
  271. public function testDuplicateWithLengthRestriction() {
  272. $this->skipIf(true);
  273. $this->articles->behaviors()->Slugged->setConfig(['length' => 10, 'unique' => true]);
  274. $article = $this->articles->newEntity(['title' => 'Andy Dawson']);
  275. $this->articles->save($article);
  276. $article = $this->articles->newEntity(['title' => 'Andy Dawsom']);
  277. $this->articles->save($article);
  278. $article = $this->articles->newEntity(['title' => 'Andy Dawsoo']);
  279. $this->articles->save($article);
  280. $article = $this->articles->newEntity(['title' => 'Andy Dawso3']);
  281. $this->articles->save($article);
  282. $article = $this->articles->newEntity(['title' => 'Andy Dawso4']);
  283. $this->articles->save($article);
  284. $article = $this->articles->newEntity(['title' => 'Andy Dawso5']);
  285. $this->articles->save($article);
  286. $article = $this->articles->newEntity(['title' => 'Andy Dawso6']);
  287. $this->articles->save($article);
  288. $article = $this->articles->newEntity(['title' => 'Andy Dawso7']);
  289. $this->articles->save($article);
  290. $article = $this->articles->newEntity(['title' => 'Andy Dawso8']);
  291. $this->articles->save($article);
  292. $article = $this->articles->newEntity(['title' => 'Andy Dawso9']);
  293. $this->articles->save($article);
  294. $article = $this->articles->newEntity(['title' => 'Andy Dawso0']);
  295. $this->articles->save($article);
  296. $result = $this->articles->find('all', [
  297. 'conditions' => ['title LIKE' => 'Andy Daw%'],
  298. 'fields' => ['title', 'slug'],
  299. 'order' => 'title'
  300. ])->combine('title', 'slug')->toArray();
  301. $expected = [
  302. 'Andy Dawson' => 'Andy-Dawso',
  303. 'Andy Dawsom' => 'Andy-Daw-1',
  304. 'Andy Dawsoo' => 'Andy-Daw-2',
  305. 'Andy Dawso3' => 'Andy-Daw-3',
  306. 'Andy Dawso4' => 'Andy-Daw-4',
  307. 'Andy Dawso5' => 'Andy-Daw-5',
  308. 'Andy Dawso6' => 'Andy-Daw-6',
  309. 'Andy Dawso7' => 'Andy-Daw-7',
  310. 'Andy Dawso8' => 'Andy-Daw-8',
  311. 'Andy Dawso9' => 'Andy-Daw-9',
  312. 'Andy Dawso0' => 'Andy-Da-10'
  313. ];
  314. $this->assertEquals($expected, $result);
  315. }
  316. /**
  317. * TestTruncateMultibyte method
  318. *
  319. * @return void
  320. */
  321. /**
  322. * TestTruncateMultibyte method
  323. *
  324. * Ensure that the first test doesn't cut a multibyte character The test string is:
  325. * 17 chars
  326. * 51 bytes UTF-8 encoded
  327. *
  328. * @return void
  329. */
  330. public function testTruncateMultibyte() {
  331. $this->articles->behaviors()->Slugged->setConfig(['length' => 16]);
  332. $result = $this->articles->generateSlug('モデルのデータベースとデータソース');
  333. $expected = 'モデルのデータベースとデータソー';
  334. $this->assertEquals($expected, $result);
  335. }
  336. /**
  337. * @return void
  338. */
  339. public function testSlugManually() {
  340. $article = new Entity();
  341. $article->title = 'Foo Bar';
  342. $this->articles->slug($article);
  343. $this->assertSame('Foo-Bar', $article->slug);
  344. }
  345. /**
  346. * Test Url method
  347. *
  348. * @return void
  349. */
  350. public function testUrlMode() {
  351. $this->articles->behaviors()->Slugged->setConfig(['mode' => 'url', 'replace' => false]);
  352. $string = 'standard string';
  353. $expected = 'standard-string';
  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. $string = 'something with a , in it';
  433. $expected = 'something-with-a-in-it';
  434. $result = $this->articles->generateSlug($string);
  435. $this->assertEquals($expected, $result);
  436. }
  437. /**
  438. * Test slug with ascii
  439. *
  440. * @return void
  441. */
  442. public function testSlugGenerationModeAscii() {
  443. $this->articles->removeBehavior('Slugged');
  444. $this->articles->addBehavior('Tools.Slugged', [
  445. 'mode' => 'ascii']);
  446. $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
  447. $result = $this->articles->save($article);
  448. $this->assertTrue((bool)$result);
  449. $this->assertEquals('Some-Article-25271', $result['slug']);
  450. }
  451. /**
  452. * Test slug generation/update on beforeSave
  453. *
  454. * @return void
  455. */
  456. public function testSlugGenerationBeforeSave() {
  457. $this->articles->removeBehavior('Slugged');
  458. $this->articles->addBehavior('Tools.Slugged', [
  459. 'on' => 'beforeSave', 'overwrite' => true]);
  460. $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
  461. $result = $this->articles->save($article);
  462. //$result['id'] = $result['id'];
  463. $this->assertEquals('Some-Article-25271', $result['slug']);
  464. }
  465. /**
  466. * Test slug generation with i18n replacement pieces
  467. *
  468. * @return void
  469. */
  470. public function testSlugGenerationI18nReplacementPieces() {
  471. $this->articles->removeBehavior('Slugged');
  472. $this->articles->addBehavior('Tools.Slugged', [
  473. 'overwrite' => true]);
  474. $article = $this->articles->newEntity(['title' => 'Some & More']);
  475. $result = $this->articles->save($article);
  476. $this->assertEquals('Some-' . __d('tools', 'and') . '-More', $result['slug']);
  477. }
  478. /**
  479. * Test dynamic slug overwrite
  480. *
  481. * @return void
  482. */
  483. public function testSlugDynamicOverwrite() {
  484. $this->articles->removeBehavior('Slugged');
  485. $this->articles->addBehavior('Tools.Slugged', [
  486. 'overwrite' => false, 'overwriteField' => 'overwrite_my_slug']);
  487. $article = $this->articles->newEntity(['title' => 'Some Cool 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' => false]);
  491. $result = $this->articles->save($article);
  492. $this->assertEquals('Some-Cool-String', $result['slug']);
  493. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => true]);
  494. $result = $this->articles->save($article);
  495. $this->assertEquals('Some-Cool-Other-String', $result['slug']);
  496. }
  497. /**
  498. * Test slug generation/update based on scope
  499. *
  500. * @return void
  501. */
  502. public function testSlugGenerationWithScope() {
  503. $this->articles->removeBehavior('Slugged');
  504. $this->articles->addBehavior('Tools.Slugged', ['unique' => true]);
  505. $data = ['title' => 'Some Article 12345', 'section' => 0];
  506. $article = $this->articles->newEntity($data);
  507. $result = $this->articles->save($article);
  508. $this->assertTrue((bool)$result);
  509. $this->assertEquals('Some-Article-12345', $result['slug']);
  510. $article = $this->articles->newEntity($data);
  511. $result = $this->articles->save($article);
  512. $this->assertTrue((bool)$result);
  513. $this->assertEquals('Some-Article-12345-1', $result['slug']);
  514. $this->articles->removeBehavior('Slugged');
  515. $this->articles->addBehavior('Tools.Slugged', ['unique' => true, 'scope' => ['section' => 1]]);
  516. $data = ['title' => 'Some Article 12345', 'section' => 1];
  517. $article = $this->articles->newEntity($data);
  518. $result = $this->articles->save($article);
  519. $this->assertTrue((bool)$result);
  520. $this->assertEquals('Some-Article-12345', $result['slug']);
  521. }
  522. /**
  523. * Test slug generation works with virtual fields.
  524. *
  525. * @return void
  526. */
  527. public function testSlugGenerationWithVirtualField() {
  528. $this->articles->removeBehavior('Slugged');
  529. $this->articles->setEntityClass(SluggedArticle::class);
  530. $this->articles->addBehavior('Tools.Slugged', [
  531. 'label' => [
  532. 'title',
  533. 'special'
  534. ],
  535. ]);
  536. $data = ['title' => 'Some Article 12345', 'section' => 0];
  537. $article = $this->articles->newEntity($data);
  538. $result = $this->articles->save($article);
  539. $this->assertTrue((bool)$result);
  540. $this->assertEquals('Some-Article-12345-dereuromark', $result['slug']);
  541. }
  542. /**
  543. * Tests slug generation fails with invalid entity config.
  544. *
  545. * @expectedException \RuntimeException
  546. * @expectedExceptionMessage (SluggedBehavior::setup) model `SluggedArticles` is missing the field `specialNonExistent` (specified in the setup for entity `App\Model\Entity\SluggedArticle`.
  547. *
  548. * @return void
  549. */
  550. public function testSlugGenerationWithVirtualFieldInvalidField() {
  551. $this->articles->removeBehavior('Slugged');
  552. $this->articles->setEntityClass(SluggedArticle::class);
  553. $this->articles->addBehavior('Tools.Slugged', [
  554. 'label' => [
  555. 'specialNonExistent',
  556. ],
  557. ]);
  558. $data = [
  559. 'title' => 'Some Article 12345',
  560. ];
  561. $article = $this->articles->newEntity($data);
  562. $this->articles->save($article);
  563. }
  564. /**
  565. * Test slug generation works with new slugger.
  566. *
  567. * @return void
  568. */
  569. public function testSlugGenerationWithNewSlugger() {
  570. $this->articles->removeBehavior('Slugged');
  571. $this->articles->addBehavior('Tools.Slugged', [
  572. 'mode' => [Text::class, 'slug'],
  573. ]);
  574. $data = ['title' => 'Some Article 12345'];
  575. $article = $this->articles->newEntity($data);
  576. $result = $this->articles->save($article);
  577. $this->assertTrue((bool)$result);
  578. $this->assertEquals('Some-Article-12345', $result['slug']);
  579. }
  580. /**
  581. * Test slug generation works with custom slugger.
  582. *
  583. * @return void
  584. */
  585. public function testSlugGenerationWithCustomSlugger() {
  586. $this->articles->removeBehavior('Slugged');
  587. $this->articles->addBehavior('Tools.Slugged', [
  588. 'mode' => [$this, '_customSluggerMethod'],
  589. ]);
  590. $data = ['title' => 'Some Article 12345'];
  591. $article = $this->articles->newEntity($data);
  592. $result = $this->articles->save($article);
  593. $this->assertTrue((bool)$result);
  594. $this->assertEquals('some article 12345', $result['slug']);
  595. }
  596. /**
  597. * @param string $name
  598. *
  599. * @return string
  600. */
  601. public function _customSluggerMethod($name) {
  602. return mb_strtolower($name);
  603. }
  604. /**
  605. * Get a new Entity
  606. *
  607. * @param string|null $title
  608. * @param string|null $field
  609. * @param array $data
  610. * @param array $options
  611. * @return \Cake\ORM\Entity
  612. */
  613. protected function _getEntity($title = null, $field = null, array $data = [], array $options = []) {
  614. $options += ['validate' => false];
  615. if ($title === null) {
  616. $title = 'test 123';
  617. }
  618. if ($field === null) {
  619. $field = 'title';
  620. }
  621. $data = [
  622. $field => $title
  623. ] + $data;
  624. return new Entity($data, $options);
  625. }
  626. }