Skip to content

Commit

Permalink
A bit more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
IsraelOrtuno committed Jun 12, 2018
1 parent dff8f89 commit 4db523a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Permalink.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Builder;
use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\Services\SlugService;
use Illuminate\Database\Eloquent\Relations\Relation;

class Permalink extends Model
{
Expand All @@ -16,7 +17,7 @@ class Permalink extends Model
*
* @var array
*/
public $fillable = ['parent_id', 'slug', 'action', 'seo'];
public $fillable = ['parent_id', 'parent_for', 'slug', 'action', 'seo'];

/**
* Casting attributes.
Expand Down Expand Up @@ -201,6 +202,20 @@ public function getRawActionAttribute()
return $this->attributes['action'];
}

/**
* Set the parent for from the morph map if exists.
*
* @param $value
*/
public function setParentForAttribute($value)
{
if (! Relation::getMorphedModel($value)) {
$value = array_search($value, Relation::morphMap()) ?: $value;
}

$this->attributes['parent_for'] = $value;
}

/**
* Set or get the alias map for aliased actions
*
Expand Down
56 changes: 56 additions & 0 deletions tests/PermalinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

class PermalinkTest extends TestCase
{
public function setUp()
{
parent::setUp();

// Reset maps
Relation::morphMap([], false);
Permalink::actionMap([], false);
}

/** @test */
public function permalink_is_automatically_created_by_default()
Expand Down Expand Up @@ -71,4 +79,52 @@ public function permalink_default_action_can_be_overwritten()

$this->assertEquals('UserController@foo', $user->permalink->action);
}

/** @test */
public function permalink_attributes_can_be_set_when_creating_resource()
{
$user = factory(DummyUser::class)->create(['permalink' => ['slug' => 'foo']]);

$this->assertEquals('foo', $user->permalink->slug);
$this->assertDatabaseHas('permalinks', ['slug' => 'foo']);
}

/** @test */
public function provided_permalink_slug_will_always_be_unique()
{
Permalink::create(['slug' => 'foo', 'action' => 'bar']);
$user = factory(DummyUser::class)->create(['permalink' => ['slug' => 'foo']]);

$this->assertEquals('foo-1', $user->permalink->slug);
$this->assertDatabaseHas('permalinks', ['slug' => 'foo-1']);
}

/** @test */
public function permalink_is_automatically_nested_if_default_parent_is_set()
{
$parent = Permalink::create(['slug' => 'foo', 'parent_for' => DummyUser::class, 'action' => 'bar']);
$user = factory(DummyUser::class)->create(['permalink' => ['slug' => 'foo']]);

$this->assertEquals($parent->id, $user->permalink->parent_id);
}

/** @test */
public function permalink_is_nested_with_morphed_model_name()
{
Relation::morphMap(['user' => DummyUser::class]);
$parent = Permalink::create(['slug' => 'foo', 'parent_for' => 'user', 'action' => 'bar']);
$user = factory(DummyUser::class)->create(['permalink' => ['slug' => 'foo']]);

$this->assertEquals($parent->id, $user->permalink->parent_id);
}

/** @test */
public function permalink_is_nested_with_morphed_model_name_with_full_class_name()
{
Relation::morphMap(['user' => DummyUser::class]);
$parent = Permalink::create(['slug' => 'foo', 'parent_for' => DummyUser::class, 'action' => 'bar']);
$user = factory(DummyUser::class)->create(['permalink' => ['slug' => 'foo']]);

$this->assertEquals($parent->id, $user->permalink->parent_id);
}
}

0 comments on commit 4db523a

Please sign in to comment.