SluggedBehaviorTest.php 20 KB

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