Commit b062a660 by Klimov Paul

Default event for `yii\behaviors\Sluggable` with `unique` enabled changed to 'insert'.

parent 327914e4
......@@ -118,7 +118,11 @@ class SluggableBehavior extends AttributeBehavior
parent::init();
if (empty($this->attributes)) {
$this->attributes = [BaseActiveRecord::EVENT_BEFORE_VALIDATE => $this->slugAttribute];
if ($this->unique) {
$this->attributes = [BaseActiveRecord::EVENT_BEFORE_INSERT => $this->slugAttribute];
} else {
$this->attributes = [BaseActiveRecord::EVENT_BEFORE_VALIDATE => $this->slugAttribute];
}
}
if ($this->attribute === null && $this->value === null) {
......@@ -135,9 +139,9 @@ class SluggableBehavior extends AttributeBehavior
if (is_array($this->attribute)) {
$slugParts = [];
foreach ($this->attribute as $attribute) {
$slugParts[] = Inflector::slug($this->owner->{$attribute});
$slugParts[] = $this->owner->{$attribute};
}
$this->value = implode('-', $slugParts);
$this->value = Inflector::slug(implode('-', $slugParts));
} else {
$this->value = Inflector::slug($this->owner->{$this->attribute});
}
......
......@@ -87,12 +87,12 @@ class SluggableBehaviorTest extends TestCase
{
$name = 'test name';
$model = new ActiveRecordSluggable();
$model = new ActiveRecordSluggableUnique();
$model->name = $name;
$model->save();
$model = new ActiveRecordSluggable();
$model->sluggable->unique = true;
$model = new ActiveRecordSluggableUnique();
$model->sluggable->uniqueSlugGenerator = 'increment';
$model->name = $name;
$model->save();
......@@ -106,12 +106,11 @@ class SluggableBehaviorTest extends TestCase
{
$name = 'test name';
$model = new ActiveRecordSluggable();
$model = new ActiveRecordSluggableUnique();
$model->name = $name;
$model->save();
$model = new ActiveRecordSluggable();
$model->sluggable->unique = true;
$model = new ActiveRecordSluggableUnique();
$model->sluggable->uniqueSlugGenerator = function($baseSlug, $iteration) {return $baseSlug . '-callback';};
$model->name = $name;
$model->save();
......@@ -126,12 +125,11 @@ class SluggableBehaviorTest extends TestCase
{
$name = 'test name';
$model1 = new ActiveRecordSluggable();
$model1 = new ActiveRecordSluggableUnique();
$model1->name = $name;
$model1->save();
$model2 = new ActiveRecordSluggable();
$model2->sluggable->unique = true;
$model2 = new ActiveRecordSluggableUnique();
$model2->sluggable->uniqueSlugGenerator = 'uniqueid';
$model2->name = $name;
$model2->save();
......@@ -146,18 +144,34 @@ class SluggableBehaviorTest extends TestCase
{
$name = 'test name';
$model1 = new ActiveRecordSluggable();
$model1 = new ActiveRecordSluggableUnique();
$model1->name = $name;
$model1->save();
$model2 = new ActiveRecordSluggable();
$model2->sluggable->unique = true;
$model2 = new ActiveRecordSluggableUnique();
$model2->sluggable->uniqueSlugGenerator = 'timestamp';
$model2->name = $name;
$model2->save();
$this->assertNotEquals($model2->slug, $model1->slug);
}
/**
* @depends testSlug
*/
public function testUpdateUnique()
{
$name = 'test name';
$model = new ActiveRecordSluggableUnique();
$model->name = $name;
$model->save();
$model = ActiveRecordSluggableUnique::find()->one();
$model->save();
$this->assertEquals('test-name', $model->slug);
}
}
/**
......@@ -194,4 +208,18 @@ class ActiveRecordSluggable extends ActiveRecord
{
return $this->getBehavior('sluggable');
}
}
class ActiveRecordSluggableUnique extends ActiveRecordSluggable
{
public function behaviors()
{
return [
'sluggable' => [
'class' => SluggableBehavior::className(),
'attribute' => 'name',
'unique' => true,
],
];
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment