| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657 |
- <?php
- namespace Tools\Test\TestCase\Model\Behavior;
- use Cake\Core\Configure;
- use Cake\ORM\Entity;
- use Cake\ORM\TableRegistry;
- use Tools\TestSuite\TestCase;
- /**
- * SluggedBehaviorTest
- */
- class SluggedBehaviorTest extends TestCase {
- /**
- * Fixture
- *
- * @var array
- */
- public $fixtures = [
- 'plugin.tools.slugged_articles'
- ];
- /**
- * @var \Cake\ORM\Table|\Tools\Model\Behavior\SluggedBehavior
- */
- protected $articles;
- /**
- * setup
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- //$this->connection = ConnectionManager::get('test');
- $options = ['alias' => 'Articles'];
- $this->articles = TableRegistry::get('SluggedArticles', $options);
- Configure::delete('Slugged');
- $this->articles->addBehavior('Tools.Slugged');
- }
- /**
- * teardown
- *
- * @return void
- */
- public function tearDown() {
- unset($this->articles);
- TableRegistry::clear();
- parent::tearDown();
- }
- /**
- * Testing simple slugging when adding a record
- *
- * @return void
- */
- public function testAdd() {
- $entity = $this->_getEntity();
- $result = $this->articles->save($entity);
- $this->assertEquals('test-123', $result->get('slug'));
- }
- /**
- * Testing simple slugging when adding a record
- *
- * @return void
- */
- public function testAddUnique() {
- $this->articles->behaviors()->Slugged->setConfig(['unique' => true]);
- $entity = $this->_getEntity();
- $result = $this->articles->save($entity);
- $this->assertEquals('test-123', $result->get('slug'));
- //$entity = $this->_getEntity();
- //$result = $this->articles->save($entity);
- //$this->assertEquals('test-123', $result->get('slug'));
- //debug($result);
- }
- /**
- * @return void
- */
- public function testAddUniqueMultipleLabels() {
- /** @var \Tools\Model\Behavior\SluggedBehavior $sluggedBehavior */
- $sluggedBehavior = $this->articles->behaviors()->Slugged;
- //$this->articles->behaviors()->Slugged->setConfig('label', ''); // Hack necessary right now to avoid title showing up twice
- $sluggedBehavior->configShallow(['mode' => 'ascii', 'unique' => true, 'label' => ['title', 'long_title']]);
- $entity = $this->_getEntity(null, null, ['long_title' => 'blae']);
- $result = $this->articles->save($entity);
- $this->assertEquals('test-123-blae', $result->get('slug'));
- $entity = $this->_getEntity(null, null, ['long_title' => 'blä']);
- $result = $this->articles->save($entity);
- $this->assertEquals('test-123-blae-1', $result->get('slug'));
- }
- /**
- * SluggedBehaviorTest::testCustomFinder()
- *
- * @return void
- */
- public function testCustomFinder() {
- $article = $this->articles->find()->find('slugged', ['slug' => 'foo'])->first();
- $this->assertEquals('Foo', $article->get('title'));
- }
- /**
- * Tests that manual slugging works.
- *
- * @return void
- */
- public function testSlugManualSave() {
- $article = $this->articles->newEntity(['title' => 'Some Cool String']);
- $result = $this->articles->save($article);
- $this->assertEquals('Some-Cool-String', $result['slug']);
- $article = $this->articles->newEntity(['title' => 'Some Other String']);
- $result = $this->articles->save($article);
- $this->assertEquals('Some-Other-String', $result['slug']);
- $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar']);
- $result = $this->articles->save($article);
- $this->assertEquals('foo-bar', $result['slug']);
- $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar-bat']);
- $result = $this->articles->save($article);
- $this->assertEquals('foo-bar-bat', $result['slug']);
- $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => '']);
- $result = $this->articles->save($article);
- $this->assertEquals('Some-Cool-Other-String', $result['slug']);
- }
- /**
- * Length based on manual config.
- *
- * @return void
- */
- public function testLengthRestrictionManual() {
- $this->articles->behaviors()->Slugged->setConfig(['length' => 155]);
- $entity = $this->_getEntity(str_repeat('foo bar ', 31));
- $result = $this->articles->save($entity);
- $this->assertEquals(155, strlen($result->get('slug')));
- $this->articles->behaviors()->Slugged->setConfig(['length' => 10, 'mode' => 'ascii']);
- $entity = $this->_getEntity('ä ö ü ä ö ü');
- $result = $this->articles->save($entity);
- $this->assertEquals('ae-oe-ue-a', $result->get('slug'));
- }
- /**
- * Test that fields doesnt mess with slug storing.
- *
- * @return void
- */
- public function testFields() {
- // field list is only relevant for newEntity(), not for what the behavior does
- $entity = $this->articles->newEntity(['title' => 'Some title'], ['fields' => ['title']]);
- $result = $this->articles->save($entity);
- $this->assertEquals('Some-title', $result->get('slug'));
- }
- /**
- * Tests needSlugUpdate()
- *
- * @return void
- */
- public function testNeedsSlugUpdate() {
- // No title change
- $entity = $this->articles->newEntity(['title' => 'Some title'], ['fields' => []]);
- $result = $this->articles->needsSlugUpdate($entity);
- $this->assertFalse($result);
- // Title change
- $entity = $this->articles->newEntity(['title' => 'Some title']);
- $result = $this->articles->needsSlugUpdate($entity);
- $this->assertTrue($result);
- $result = $this->articles->save($entity);
- $this->assertEquals('Some-title', $result->get('slug'));
- // No title change
- $entity = $this->articles->patchEntity($entity, ['description' => 'Foo bar']);
- $result = $this->articles->needsSlugUpdate($entity);
- $this->assertFalse($result);
- // Needs an update, but overwrite is still false: will not modify the slug
- $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
- $result = $this->articles->needsSlugUpdate($entity);
- $this->assertTrue($result);
- $result = $this->articles->save($entity);
- $this->assertEquals('Some-title', $result->get('slug'));
- $this->articles->behaviors()->Slugged->setConfig(['overwrite' => true]);
- // Now it can modify the slug
- $entity = $this->articles->patchEntity($entity, ['title' => 'Some really other title']);
- $result = $this->articles->needsSlugUpdate($entity);
- $this->assertTrue($result);
- $result = $this->articles->save($entity);
- $this->assertEquals('Some-really-other-title', $result->get('slug'));
- }
- /**
- * Tests needSlugUpdate() with deep
- *
- * @return void
- */
- public function testNeedsSlugUpdateDeep() {
- // No title change
- $entity = $this->articles->newEntity(['title' => 'Some title']);
- $result = $this->articles->needsSlugUpdate($entity);
- $this->assertTrue($result);
- $result = $this->articles->needsSlugUpdate($entity, true);
- $this->assertTrue($result);
- $result = $this->articles->save($entity);
- $this->assertEquals('Some-title', $result->get('slug'));
- // Needs an update, but overwrite is still false: will not modify the slug
- $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
- $result = $this->articles->needsSlugUpdate($entity);
- $this->assertTrue($result);
- $result = $this->articles->needsSlugUpdate($entity, true);
- $this->assertTrue($result);
- $result = $this->articles->save($entity);
- $this->assertEquals('Some-title', $result->get('slug'));
- // Here deep would tell the truth
- $entity = $this->articles->patchEntity($entity, ['title' => 'Some other title']);
- $result = $this->articles->needsSlugUpdate($entity);
- $this->assertFalse($result);
- $result = $this->articles->needsSlugUpdate($entity, true);
- $this->assertTrue($result);
- }
- /**
- * Length based on auto-detect of schema.
- *
- * @return void
- */
- public function testLengthRestrictionAutoDetect() {
- $entity = $this->_getEntity(str_repeat('foo bar ', 31));
- $result = $this->articles->save($entity);
- $this->assertEquals(245, strlen($result->get('slug')));
- }
- /**
- * Ensure that you can overwrite length.
- *
- * @return void
- */
- public function testLengthRestrictionNoLimit() {
- $this->articles->behaviors()->Slugged->setConfig(['length' => 0, 'label' => 'long_title', 'field' => 'long_slug']);
- $entity = $this->_getEntity(str_repeat('foo bar ', 100), 'long_title');
- $result = $this->articles->save($entity);
- $this->assertEquals(799, strlen($result->get('long_slug')));
- }
- /**
- * SluggedBehaviorTest::testResetSlugs()
- *
- * @return void
- */
- public function testResetSlugs() {
- $this->articles->removeBehavior('Slugged');
- $article = $this->articles->newEntity(['title' => 'Andy Dawson', 'slug' => 'foo']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawsom', 'slug' => 'bar']);
- $this->articles->save($article);
- $result = $this->articles->find('all', [
- 'conditions' => ['title LIKE' => 'Andy Daw%'],
- 'fields' => ['title', 'slug'],
- 'order' => 'title'
- ])->combine('title', 'slug')->toArray();
- $expected = [
- 'Andy Dawsom' => 'bar',
- 'Andy Dawson' => 'foo'
- ];
- $this->assertEquals($expected, $result);
- $this->articles->addBehavior('Tools.Slugged');
- $result = $this->articles->resetSlugs(['limit' => 1]);
- $this->assertTrue($result);
- $result = $this->articles->find('all', [
- 'conditions' => ['title LIKE' => 'Andy Daw%'],
- 'fields' => ['title', 'slug'],
- 'order' => 'title'
- ])->combine('title', 'slug')->toArray();
- $expected = [
- 'Andy Dawsom' => 'Andy-Dawsom',
- 'Andy Dawson' => 'Andy-Dawson'
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * TestDuplicateWithLengthRestriction method
- *
- * If there's a length restriction - ensure it's respected by the unique slug routine
- *
- * @return void
- */
- public function testDuplicateWithLengthRestriction() {
- $this->skipIf(true);
- $this->articles->behaviors()->Slugged->setConfig(['length' => 10, 'unique' => true]);
- $article = $this->articles->newEntity(['title' => 'Andy Dawson']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawsom']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawsoo']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawso3']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawso4']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawso5']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawso6']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawso7']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawso8']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawso9']);
- $this->articles->save($article);
- $article = $this->articles->newEntity(['title' => 'Andy Dawso0']);
- $this->articles->save($article);
- $result = $this->articles->find('all', [
- 'conditions' => ['title LIKE' => 'Andy Daw%'],
- 'fields' => ['title', 'slug'],
- 'order' => 'title'
- ])->combine('title', 'slug')->toArray();
- $expected = [
- 'Andy Dawson' => 'Andy-Dawso',
- 'Andy Dawsom' => 'Andy-Daw-1',
- 'Andy Dawsoo' => 'Andy-Daw-2',
- 'Andy Dawso3' => 'Andy-Daw-3',
- 'Andy Dawso4' => 'Andy-Daw-4',
- 'Andy Dawso5' => 'Andy-Daw-5',
- 'Andy Dawso6' => 'Andy-Daw-6',
- 'Andy Dawso7' => 'Andy-Daw-7',
- 'Andy Dawso8' => 'Andy-Daw-8',
- 'Andy Dawso9' => 'Andy-Daw-9',
- 'Andy Dawso0' => 'Andy-Da-10'
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * TestTruncateMultibyte method
- *
- * @return void
- */
- /**
- * TestTruncateMultibyte method
- *
- * Ensure that the first test doesn't cut a multibyte character The test string is:
- * 17 chars
- * 51 bytes UTF-8 encoded
- *
- * @return void
- */
- public function testTruncateMultibyte() {
- $this->articles->behaviors()->Slugged->setConfig(['length' => 16]);
- $result = $this->articles->generateSlug('モデルのデータベースとデータソース');
- $expected = 'モデルのデータベースとデータソー';
- $this->assertEquals($expected, $result);
- }
- /**
- * Test Url method
- *
- * @return void
- */
- public function testUrlMode() {
- $this->articles->behaviors()->Slugged->setConfig(['mode' => 'url', 'replace' => false]);
- $string = 'standard string';
- $expected = 'standard-string';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a \' in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a " in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a / in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a ? in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a < in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a > in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a . in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a $ in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a / in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a : in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a ; in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a ? in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a @ in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a = in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a + in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a & in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a % in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a \ in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a # in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- $string = 'something with a , in it';
- $expected = 'something-with-a-in-it';
- $result = $this->articles->generateSlug($string);
- $this->assertEquals($expected, $result);
- }
- /**
- * Test slug with ascii
- *
- * @return void
- */
- public function testSlugGenerationModeAscii() {
- $this->articles->removeBehavior('Slugged');
- $this->articles->addBehavior('Tools.Slugged', [
- 'mode' => 'ascii']);
- $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
- $result = $this->articles->save($article);
- $this->assertTrue((bool)$result);
- $this->assertEquals('Some-Article-25271', $result['slug']);
- }
- /**
- * Test slug generation/update on beforeSave
- *
- * @return void
- */
- public function testSlugGenerationBeforeSave() {
- $this->articles->removeBehavior('Slugged');
- $this->articles->addBehavior('Tools.Slugged', [
- 'on' => 'beforeSave', 'overwrite' => true]);
- $article = $this->articles->newEntity(['title' => 'Some Article 25271']);
- $result = $this->articles->save($article);
- //$result['id'] = $result['id'];
- $this->assertEquals('Some-Article-25271', $result['slug']);
- }
- /**
- * Test slug generation with i18n replacement pieces
- *
- * @return void
- */
- public function testSlugGenerationI18nReplacementPieces() {
- $this->articles->removeBehavior('Slugged');
- $this->articles->addBehavior('Tools.Slugged', [
- 'overwrite' => true]);
- $article = $this->articles->newEntity(['title' => 'Some & More']);
- $result = $this->articles->save($article);
- $this->assertEquals('Some-' . __d('tools', 'and') . '-More', $result['slug']);
- }
- /**
- * Test dynamic slug overwrite
- *
- * @return void
- */
- public function testSlugDynamicOverwrite() {
- $this->articles->removeBehavior('Slugged');
- $this->articles->addBehavior('Tools.Slugged', [
- 'overwrite' => false, 'overwriteField' => 'overwrite_my_slug']);
- $article = $this->articles->newEntity(['title' => 'Some Cool String', 'overwrite_my_slug' => false]);
- $result = $this->articles->save($article);
- $this->assertEquals('Some-Cool-String', $result['slug']);
- $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => false]);
- $result = $this->articles->save($article);
- $this->assertEquals('Some-Cool-String', $result['slug']);
- $this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'overwrite_my_slug' => true]);
- $result = $this->articles->save($article);
- $this->assertEquals('Some-Cool-Other-String', $result['slug']);
- }
- /**
- * Test slug generation/update based on scope
- *
- * @return void
- */
- public function testSlugGenerationWithScope() {
- $this->articles->removeBehavior('Slugged');
- $this->articles->addBehavior('Tools.Slugged', ['unique' => true]);
- $data = ['title' => 'Some Article 12345', 'section' => 0];
- $article = $this->articles->newEntity($data);
- $result = $this->articles->save($article);
- $this->assertTrue((bool)$result);
- $this->assertEquals('Some-Article-12345', $result['slug']);
- $article = $this->articles->newEntity($data);
- $result = $this->articles->save($article);
- $this->assertTrue((bool)$result);
- $this->assertEquals('Some-Article-12345-1', $result['slug']);
- $this->articles->removeBehavior('Slugged');
- $this->articles->addBehavior('Tools.Slugged', ['unique' => true, 'scope' => ['section' => 1]]);
- $data = ['title' => 'Some Article 12345', 'section' => 1];
- $article = $this->articles->newEntity($data);
- $result = $this->articles->save($article);
- $this->assertTrue((bool)$result);
- $this->assertEquals('Some-Article-12345', $result['slug']);
- }
- /**
- * Test slug generation works with virtual fields.
- *
- * @return void
- */
- public function testSlugGenerationWithVirualField() {
- $this->articles->removeBehavior('Slugged');
- $this->articles->setEntityClass('\App\Model\Entity\SluggedArticle');
- $this->articles->addBehavior('Tools.Slugged', [
- 'label' => [
- 'title',
- 'special'
- ],
- ]);
- $data = ['title' => 'Some Article 12345', 'section' => 0];
- $article = $this->articles->newEntity($data);
- $result = $this->articles->save($article);
- $this->assertTrue((bool)$result);
- $this->assertEquals('Some-Article-12345-dereuromark', $result['slug']);
- }
- /**
- * Get a new Entity
- *
- * @param string|null $title
- * @param string|null $field
- * @param array $data
- * @param array $options
- * @return \Cake\ORM\Entity
- */
- protected function _getEntity($title = null, $field = null, array $data = [], array $options = []) {
- $options += ['validate' => false];
- if ($title === null) {
- $title = 'test 123';
- }
- if ($field === null) {
- $field = 'title';
- }
- $data = [
- $field => $title
- ] + $data;
- return new Entity($data, $options);
- }
- }
|