SluggedBehaviorTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\Database\Query;
  4. use Cake\Datasource\ConnectionManager;
  5. use Cake\Event\Event;
  6. use Cake\ORM\Entity;
  7. use Cake\ORM\Table;
  8. use Cake\ORM\TableRegistry;
  9. use Cake\TestSuite\TestCase;
  10. use Cake\Core\Configure;
  11. /**
  12. * SluggedBehaviorTest
  13. */
  14. class SluggedBehaviorTest extends TestCase {
  15. /**
  16. * Fixture
  17. *
  18. * @var array
  19. */
  20. public $fixtures = [
  21. 'plugin.tools.slugged_articles'
  22. ];
  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->config(['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. * SluggedBehaviorTest::testCustomFinder()
  73. *
  74. * @return void
  75. */
  76. public function testCustomFinder() {
  77. $article = $this->articles->find()->find('slugged', ['slug' => 'foo'])->first();
  78. $this->assertEquals('Foo', $article->get('title'));
  79. }
  80. /**
  81. * Tests that manual slugging works.
  82. *
  83. * @return void
  84. */
  85. public function testSlugManualSave() {
  86. $article = $this->articles->newEntity(array('title' => 'Some Cool String'));
  87. $result = $this->articles->save($article);
  88. $this->assertEquals('Some-Cool-String', $result['slug']);
  89. $article = $this->articles->newEntity(array('title' => 'Some Other String'));
  90. $result = $this->articles->save($article);
  91. $this->assertEquals('Some-Other-String', $result['slug']);
  92. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar']);
  93. $result = $this->articles->save($article);
  94. $this->assertEquals('foo-bar', $result['slug']);
  95. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar-bat']);
  96. $result = $this->articles->save($article);
  97. $this->assertEquals('foo-bar-bat', $result['slug']);
  98. }
  99. /**
  100. * Length based on manual config.
  101. *
  102. * @return void
  103. */
  104. public function testLengthRestrictionManual() {
  105. $this->articles->behaviors()->Slugged->config(['length' => 155]);
  106. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  107. $result = $this->articles->save($entity);
  108. $this->assertEquals(155, strlen($result->get('slug')));
  109. $this->articles->behaviors()->Slugged->config(['length' => 10, 'mode' => 'ascii']);
  110. $entity = $this->_getEntity('ä ö ü ä ö ü');
  111. $result = $this->articles->save($entity);
  112. $this->assertEquals('ae-oe-ue-a', $result->get('slug'));
  113. }
  114. /**
  115. * Test that fieldList doesnt mess with slug storing.
  116. *
  117. * @return void
  118. */
  119. public function testFieldList() {
  120. // field list is only relevant for newEntity(), not for what the behavior does
  121. $entity = $this->articles->newEntity(['title' => 'Some title'], ['fieldList' => ['title']]);
  122. $result = $this->articles->save($entity);
  123. $this->assertEquals('Some-title', $result->get('slug'));
  124. }
  125. /**
  126. * Tests needSlugUpdate()
  127. *
  128. * @return void
  129. */
  130. public function testNeedsSlugUpdate() {
  131. // No title change
  132. $entity = $this->articles->newEntity(['title' => 'Some title'], ['fieldList' => []]);
  133. $result = $this->articles->needsSlugUpdate($entity);
  134. $this->assertFalse($result);
  135. // Title change
  136. $entity = $this->articles->newEntity(['title' => 'Some title']);
  137. $result = $this->articles->needsSlugUpdate($entity);
  138. $this->assertTrue($result);
  139. $result = $this->articles->save($entity);
  140. $this->assertEquals('Some-title', $result->get('slug'));
  141. // No title change
  142. $entity = $this->articles->patchEntity($entity, ['description' => 'Foo bar']);
  143. $result = $this->articles->needsSlugUpdate($entity);
  144. $this->assertFalse($result);
  145. // Needs an update, but overwrite is still false: will not modify the slug
  146. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  147. $result = $this->articles->needsSlugUpdate($entity);
  148. $this->assertTrue($result);
  149. $result = $this->articles->save($entity);
  150. $this->assertEquals('Some-title', $result->get('slug'));
  151. $this->articles->behaviors()->Slugged->config(['overwrite' => true]);
  152. // Now it can modify the slug
  153. $entity = $this->articles->patchEntity($entity, ['title' => 'Some really other title']);
  154. $result = $this->articles->needsSlugUpdate($entity);
  155. $this->assertTrue($result);
  156. $result = $this->articles->save($entity);
  157. $this->assertEquals('Some-really-other-title', $result->get('slug'));
  158. }
  159. /**
  160. * Tests needSlugUpdate() with deep
  161. *
  162. * @return void
  163. */
  164. public function testNeedsSlugUpdateDeep() {
  165. // No title change
  166. $entity = $this->articles->newEntity(['title' => 'Some title']);
  167. $result = $this->articles->needsSlugUpdate($entity);
  168. $this->assertTrue($result);
  169. $result = $this->articles->needsSlugUpdate($entity, true);
  170. $this->assertTrue($result);
  171. $result = $this->articles->save($entity);
  172. $this->assertEquals('Some-title', $result->get('slug'));
  173. // Needs an update, but overwrite is still false: will not modify the slug
  174. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  175. $result = $this->articles->needsSlugUpdate($entity);
  176. $this->assertTrue($result);
  177. $result = $this->articles->needsSlugUpdate($entity, true);
  178. $this->assertTrue($result);
  179. $result = $this->articles->save($entity);
  180. $this->assertEquals('Some-title', $result->get('slug'));
  181. // Here deep would tell the truth
  182. $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
  183. $result = $this->articles->needsSlugUpdate($entity);
  184. $this->assertFalse($result);
  185. $result = $this->articles->needsSlugUpdate($entity, true);
  186. $this->assertTrue($result);
  187. }
  188. /**
  189. * Length based on auto-detect of schema.
  190. *
  191. * @return void
  192. */
  193. public function testLengthRestrictionAutoDetect() {
  194. $entity = $this->_getEntity(str_repeat('foo bar ', 31));
  195. $result = $this->articles->save($entity);
  196. $this->assertEquals(245, strlen($result->get('slug')));
  197. }
  198. /**
  199. * Ensure that you can overwrite length.
  200. *
  201. * @return void
  202. */
  203. public function testLengthRestrictionNoLimit() {
  204. $this->articles->behaviors()->Slugged->config(['length' => 0, 'label' => 'long_title', 'field' => 'long_slug']);
  205. $entity = $this->_getEntity(str_repeat('foo bar ', 100), 'long_title');
  206. $result = $this->articles->save($entity);
  207. $this->assertEquals(799, strlen($result->get('long_slug')));
  208. }
  209. /**
  210. * SluggedBehaviorTest::testResetSlugs()
  211. *
  212. * @return void
  213. */
  214. public function testResetSlugs() {
  215. $this->articles->removeBehavior('Slugged');
  216. $article = $this->articles->newEntity(array('title' => 'Andy Dawson', 'slug' => 'foo'));
  217. $this->articles->save($article);
  218. $article = $this->articles->newEntity(array('title' => 'Andy Dawsom', 'slug' => 'bar'));
  219. $this->articles->save($article);
  220. $result = $this->articles->find('all', array(
  221. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  222. 'fields' => array('title', 'slug'),
  223. 'order' => 'title'
  224. ))->combine('title', 'slug')->toArray();
  225. $expected = array(
  226. 'Andy Dawsom' => 'bar',
  227. 'Andy Dawson' => 'foo'
  228. );
  229. $this->assertEquals($expected, $result);
  230. $this->articles->addBehavior('Tools.Slugged');
  231. $result = $this->articles->resetSlugs(['limit' => 1]);
  232. $this->assertTrue($result);
  233. $result = $this->articles->find('all', array(
  234. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  235. 'fields' => array('title', 'slug'),
  236. 'order' => 'title'
  237. ))->combine('title', 'slug')->toArray();
  238. $expected = array(
  239. 'Andy Dawsom' => 'Andy-Dawsom',
  240. 'Andy Dawson' => 'Andy-Dawson'
  241. );
  242. $this->assertEquals($expected, $result);
  243. }
  244. /**
  245. * TestDuplicateWithLengthRestriction method
  246. *
  247. * If there's a length restriction - ensure it's respected by the unique slug routine
  248. *
  249. * @return void
  250. */
  251. public function testDuplicateWithLengthRestriction() {
  252. return;
  253. $this->articles->behaviors()->Slugged->config(['length' => 10, 'unique' => true]);
  254. $article = $this->articles->newEntity(array('title' => 'Andy Dawson'));
  255. $this->articles->save($article);
  256. $article = $this->articles->newEntity(array('title' => 'Andy Dawsom'));
  257. $this->articles->save($article);
  258. $article = $this->articles->newEntity(array('title' => 'Andy Dawsoo'));
  259. $this->articles->save($article);
  260. $article = $this->articles->newEntity(array('title' => 'Andy Dawso3'));
  261. $this->articles->save($article);
  262. $article = $this->articles->newEntity(array('title' => 'Andy Dawso4'));
  263. $this->articles->save($article);
  264. $article = $this->articles->newEntity(array('title' => 'Andy Dawso5'));
  265. $this->articles->save($article);
  266. $article = $this->articles->newEntity(array('title' => 'Andy Dawso6'));
  267. $this->articles->save($article);
  268. $article = $this->articles->newEntity(array('title' => 'Andy Dawso7'));
  269. $this->articles->save($article);
  270. $article = $this->articles->newEntity(array('title' => 'Andy Dawso8'));
  271. $this->articles->save($article);
  272. $article = $this->articles->newEntity(array('title' => 'Andy Dawso9'));
  273. $this->articles->save($article);
  274. $article = $this->articles->newEntity(array('title' => 'Andy Dawso0'));
  275. $this->articles->save($article);
  276. $result = $this->articles->find('all', array(
  277. 'conditions' => array('title LIKE' => 'Andy Daw%'),
  278. 'fields' => array('title', 'slug'),
  279. 'order' => 'title'
  280. ))->combine('title', 'slug')->toArray();
  281. $expected = array(
  282. 'Andy Dawson' => 'Andy-Dawso',
  283. 'Andy Dawsom' => 'Andy-Daw-1',
  284. 'Andy Dawsoo' => 'Andy-Daw-2',
  285. 'Andy Dawso3' => 'Andy-Daw-3',
  286. 'Andy Dawso4' => 'Andy-Daw-4',
  287. 'Andy Dawso5' => 'Andy-Daw-5',
  288. 'Andy Dawso6' => 'Andy-Daw-6',
  289. 'Andy Dawso7' => 'Andy-Daw-7',
  290. 'Andy Dawso8' => 'Andy-Daw-8',
  291. 'Andy Dawso9' => 'Andy-Daw-9',
  292. 'Andy Dawso0' => 'Andy-Da-10'
  293. );
  294. $this->assertEquals($expected, $result);
  295. }
  296. /**
  297. * TestTruncateMultibyte method
  298. *
  299. * @return void
  300. */
  301. /**
  302. * TestTruncateMultibyte method
  303. *
  304. * Ensure that the first test doesn't cut a multibyte character The test string is:
  305. * 17 chars
  306. * 51 bytes UTF-8 encoded
  307. *
  308. * @return void
  309. */
  310. public function testTruncateMultibyte() {
  311. $this->articles->behaviors()->Slugged->config(array('length' => 16));
  312. $result = $this->articles->generateSlug('モデルのデータベースとデータソース');
  313. $expected = 'モデルのデータベースとデータソー';
  314. $this->assertEquals($expected, $result);
  315. }
  316. /**
  317. * Test Url method
  318. *
  319. * @return void
  320. */
  321. public function testUrlMode() {
  322. $this->articles->behaviors()->Slugged->config(array('mode' => 'url', 'replace' => false));
  323. $string = 'standard string';
  324. $expected = 'standard-string';
  325. $result = $this->articles->generateSlug($string);
  326. $this->assertEquals($expected, $result);
  327. $string = 'something with a \' in it';
  328. $expected = 'something-with-a-in-it';
  329. $result = $this->articles->generateSlug($string);
  330. $this->assertEquals($expected, $result);
  331. $string = 'something with a " in it';
  332. $expected = 'something-with-a-in-it';
  333. $result = $this->articles->generateSlug($string);
  334. $this->assertEquals($expected, $result);
  335. $string = 'something with a / in it';
  336. $expected = 'something-with-a-in-it';
  337. $result = $this->articles->generateSlug($string);
  338. $this->assertEquals($expected, $result);
  339. $string = 'something with a ? in it';
  340. $expected = 'something-with-a-in-it';
  341. $result = $this->articles->generateSlug($string);
  342. $this->assertEquals($expected, $result);
  343. $string = 'something with a < in it';
  344. $expected = 'something-with-a-in-it';
  345. $result = $this->articles->generateSlug($string);
  346. $this->assertEquals($expected, $result);
  347. $string = 'something with a > in it';
  348. $expected = 'something-with-a-in-it';
  349. $result = $this->articles->generateSlug($string);
  350. $this->assertEquals($expected, $result);
  351. $string = 'something with a . in it';
  352. $expected = 'something-with-a-in-it';
  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. }
  404. /**
  405. * Test slug with ascii
  406. *
  407. * @return void
  408. */
  409. public function testSlugGenerationModeAscii() {
  410. $this->articles->removeBehavior('Slugged');
  411. $this->articles->addBehavior('Tools.Slugged', array(
  412. 'mode' => 'ascii'));
  413. $article = $this->articles->newEntity(array('title' => 'Some Article 25271'));
  414. $result = $this->articles->save($article);
  415. $this->assertTrue((bool)$result);
  416. $this->assertEquals('Some-Article-25271', $result['slug']);
  417. }
  418. /**
  419. * Test slug generation/update on beforeSave
  420. *
  421. * @return void
  422. */
  423. public function testSlugGenerationBeforeSave() {
  424. $this->articles->removeBehavior('Slugged');
  425. $this->articles->addBehavior('Tools.Slugged', array(
  426. 'on' => 'beforeSave', 'overwrite' => true));
  427. $article = $this->articles->newEntity(array('title' => 'Some Article 25271'));
  428. $result = $this->articles->save($article);
  429. //$result['id'] = $result['id'];
  430. $this->assertEquals('Some-Article-25271', $result['slug']);
  431. }
  432. /**
  433. * Test slug generation with i18n replacement pieces
  434. *
  435. * @return void
  436. */
  437. public function testSlugGenerationI18nReplacementPieces() {
  438. $this->articles->removeBehavior('Slugged');
  439. $this->articles->addBehavior('Tools.Slugged', array(
  440. 'overwrite' => true));
  441. $article = $this->articles->newEntity(array('title' => 'Some & More'));
  442. $result = $this->articles->save($article);
  443. $this->assertEquals('Some-' . __d('tools', 'and') . '-More', $result['slug']);
  444. }
  445. /**
  446. * Test dynamic slug overwrite
  447. *
  448. * @return void
  449. */
  450. public function testSlugDynamicOverwrite() {
  451. $this->articles->removeBehavior('Slugged');
  452. $this->articles->addBehavior('Tools.Slugged', array(
  453. 'overwrite' => false, 'overwriteField' => 'overwrite_my_slug'));
  454. $article = $this->articles->newEntity(array('title' => 'Some Cool String', 'overwrite_my_slug' => false));
  455. $result = $this->articles->save($article);
  456. $this->assertEquals('Some-Cool-String', $result['slug']);
  457. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => false]);
  458. $result = $this->articles->save($article);
  459. $this->assertEquals('Some-Cool-String', $result['slug']);
  460. $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => true]);
  461. $result = $this->articles->save($article);
  462. $this->assertEquals('Some-Cool-Other-String', $result['slug']);
  463. }
  464. /**
  465. * Test slug generation/update based on scope
  466. *
  467. * @return void
  468. */
  469. public function testSlugGenerationWithScope() {
  470. $this->articles->removeBehavior('Slugged');
  471. $this->articles->addBehavior('Tools.Slugged', array('unique' => true));
  472. $data = array('title' => 'Some Article 12345', 'section' => 0);
  473. $article = $this->articles->newEntity($data);
  474. $result = $this->articles->save($article);
  475. $this->assertTrue((bool)$result);
  476. $this->assertEquals('Some-Article-12345', $result['slug']);
  477. $article = $this->articles->newEntity($data);
  478. $result = $this->articles->save($article);
  479. $this->assertTrue((bool)$result);
  480. $this->assertEquals('Some-Article-12345-1', $result['slug']);
  481. $this->articles->removeBehavior('Slugged');
  482. $this->articles->addBehavior('Tools.Slugged', array('unique' => true, 'scope' => array('section' => 1)));
  483. $data = array('title' => 'Some Article 12345', 'section' => 1);
  484. $article = $this->articles->newEntity($data);
  485. $result = $this->articles->save($article);
  486. $this->assertTrue((bool)$result);
  487. $this->assertEquals('Some-Article-12345', $result['slug']);
  488. }
  489. /**
  490. * Get a new Entity
  491. *
  492. * @return Entity
  493. */
  494. protected function _getEntity($title = 'test 123', $field = 'title', array $options = array()) {
  495. return new Entity([
  496. $field => $title
  497. ], $options);
  498. }
  499. }