Skip to content

Commit

Permalink
Fix slug generation using associated table field.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jul 10, 2017
1 parent c44c6eb commit aaa2054
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"require-dev": {
"cakephp/cakephp": "^3.2.7",
"phpunit/phpunit": "4.1.*",
"phpunit/phpunit": "<6.0",
"cocur/slugify": "^1.2"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Behavior/SlugBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public function slug($entity, $string = null, $separator = '-')
if ($entity->errors($field)) {
throw new InvalidArgumentException();
}
$string[] = $entity->get($field);
$string[] = $value = Hash::get($entity, $field);
}
$string = implode($separator, $string);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Model/Behavior/SlugBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ public function testSlug()
$this->assertEquals($expected, $result);
}

public function testSlugWithAssociatedTableField()
{
$Articles = TableRegistry::get('Muffin/Slug.Articles', ['table' => 'slug_articles']);
$Authors = TableRegistry::get('Muffin/Slug.Authors', ['table' => 'slug_authors']);

$Articles->belongsTo('Authors', ['className' => 'Muffin/Slug.Authors']);
$Articles->addBehavior('Muffin/Slug.Slug', ['displayField' => ['author.name', 'title']]);

$data = ['title' => 'foo', 'sub_title' => 'unused'];
$article = $Articles->newEntity($data);
$article->author = $Authors->get(1);

$result = $Articles->slug($article);
$expected = 'admad-foo';
$this->assertEquals($expected, $result);
}

public function testBeforeSaveMultiField()
{
$Articles = TableRegistry::get('Muffin/Slug.Articles', ['table' => 'slug_articles']);
Expand Down

0 comments on commit aaa2054

Please sign in to comment.