SluggedBehaviorTest.php 17 KB

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