Skip to content

Exceptions if slug field not exists or its length is NULL #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 1.next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ env:
global:
- DEFAULT=1

services:
- postgresql
- mysql

matrix:
fast_finish: true

Expand Down
41 changes: 35 additions & 6 deletions src/Model/Behavior/SlugBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cake\Validation\Validator;
use InvalidArgumentException;
use Muffin\Slug\SluggerInterface;
use RuntimeException;

/**
* Slug behavior.
Expand All @@ -34,6 +35,8 @@ class SlugBehavior extends Behavior
* to `Muffin\Slug\Slugger\CakeSlugger`.
* - unique: Tells if slugs should be unique. Set this to a callable if you
* want to customize how unique slugs are generated. Defaults to `true`.
* - virtual: Tells if slugs are a virtual property of the entity or not to skip
* validating the column's existence and length.
* - scope: Extra conditions or a callable `$callable($entity)` used when
* checking a slug for uniqueness.
* - implementedEvents: Events this behavior listens to. Defaults to
Expand Down Expand Up @@ -64,6 +67,7 @@ class SlugBehavior extends Behavior
'maxLength' => null,
'slugger' => 'Muffin\Slug\Slugger\CakeSlugger',
'unique' => true,
'virtual' => false,
'scope' => [],
'implementedEvents' => [
'Model.buildValidator' => 'buildValidator',
Expand Down Expand Up @@ -113,16 +117,40 @@ public function initialize(array $config)
$this->setConfig('displayField', $this->_table->getDisplayField());
}

if ($this->getConfig('unique') === true) {
$this->setConfig('unique', [$this, '_uniqueSlug']);
}

if ($this->getConfig('virtual')) {
return;
}

$field = $this->getConfig('field');

if (!$this->getTable()->hasField($field)) {
throw new RuntimeException(sprintf(
'SlugBehavior: Table `%s` is missing field `%s`',
$this->getTable()->getTable(),
$field
));
}

$fieldSchema = $this->_table->getSchema()->getColumn($field);

if ($this->getConfig('maxLength') === null) {
if ($fieldSchema['length'] === null) {
throw new RuntimeException(sprintf(
'SlugBehavior: The schema for field `%s.%s` has no length defined',
$this->getTable()->getTable(),
$field
));
}

$this->setConfig(
'maxLength',
$this->_table->getSchema()->getColumn($this->getConfig('field'))['length']
$fieldSchema['length']
);
}

if ($this->getConfig('unique') === true) {
$this->setConfig('unique', [$this, '_uniqueSlug']);
}
}

/**
Expand Down Expand Up @@ -204,7 +232,8 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options)

$onDirty = $this->getConfig('onDirty');
$field = $this->getConfig('field');
if (!$onDirty
if (
!$onDirty
&& $entity->isDirty($field)
&& (!$entity->isNew() || (!empty($entity->{$field})))
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Slugger/CakeSlugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CakeSlugger implements SluggerInterface
* @var array
*/
public $config = [
'lowercase' => true
'lowercase' => true,
];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Slugger/CocurSlugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CocurSlugger implements SluggerInterface
*/
public $config = [
'regex' => null,
'lowercase' => true
'lowercase' => true,
];

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixture/ArticlesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class ArticlesFixture extends TestFixture
'id' => ['type' => 'integer'],
'author_id' => ['type' => 'integer'],
'title' => ['type' => 'string', 'null' => false],
'sub_title' => ['type' => 'string', 'null' => false],
'slug' => ['type' => 'string', 'null' => false],
'sub_title' => ['type' => 'string', 'null' => false, 'length' => 255],
'slug' => ['type' => 'string', 'null' => false, 'length' => 255],
'created' => ['type' => 'datetime', 'null' => true],
'modified' => ['type' => 'datetime', 'null' => true],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
];

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/ArticlesTagsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ArticlesTagsFixture extends TestFixture
public $fields = [
'id' => ['type' => 'integer'],
'article_id' => ['type' => 'integer'],
'slug_tag_id' => ['type' => 'integer']
'slug_tag_id' => ['type' => 'integer'],
];

public $records = [
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/AuthorsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AuthorsFixture extends TestFixture
public $fields = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'null' => false],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
];

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/Fixture/RecipesFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Muffin\Slug\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class RecipesFixture extends TestFixture
{
public $table = 'slug_recipes';

public $fields = [
'id' => ['type' => 'integer'],
'slug' => ['type' => 'string'],
'name' => ['type' => 'string', 'length' => 320],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']],
],
];

public $records = [
[
'slug' => 'cake',
'name' => 'Cake',
],
[
'slug' => 'muffin',
'name' => 'Muffin',
],
];
}
28 changes: 28 additions & 0 deletions tests/TestCase/Model/Behavior/SlugBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SlugBehaviorTest extends TestCase
'plugin.Muffin/Slug.Articles',
'plugin.Muffin/Slug.ArticlesTags',
'plugin.Muffin/Slug.Authors',
'plugin.Muffin/Slug.Recipes',
];

public function setUp()
Expand Down Expand Up @@ -440,4 +441,31 @@ public function testCallableForUnique()

$this->assertEquals('color', $this->Tags->slug($newEntity));
}

public function testSlugMissingFieldLengthThrowsException()
{
$table = TableRegistry::get('Muffin/Slug.Recipes', ['table' => 'slug_recipes']);

$this->skipIf(get_class($table->getConnection()->getDriver()) !== 'postgres');

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('SlugBehavior: The schema for field `slug_recipes.slug` has no length defined');

$table->addBehavior('Muffin/Slug.Slug');
}

public function testSlugMissingFieldThrowsException()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('SlugBehavior: Table `slug_recipes` is missing field `real_slug`');
$table = TableRegistry::get('Muffin/Slug.Recipes', ['table' => 'slug_recipes']);
$table->addBehavior('Muffin/Slug.Slug', ['field' => 'real_slug']);
}

public function testVirtualSlugDoesNotThrowExceptions()
{
$table = TableRegistry::get('Muffin/Slug.Recipes', ['table' => 'slug_recipes']);
$table->addBehavior('Muffin/Slug.Slug', ['field' => 'real_slug', 'virtual' => true]);
$this->assertTrue($table->hasBehavior('Slug'));
}
}