-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from kontenta/linkauth
Serialization of AuthorizesWithAbility
- Loading branch information
Showing
3 changed files
with
92 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Kontenta\Kontour\Tests\Feature; | ||
|
||
use Illuminate\Support\Facades\Gate; | ||
use Kontenta\Kontour\AdminLink; | ||
use Kontenta\Kontour\Tests\Feature\Fakes\User; | ||
use Kontenta\Kontour\Tests\IntegrationTest; | ||
|
||
class AuthorizesWithAbilityTest extends IntegrationTest | ||
{ | ||
/** | ||
* @var User | ||
*/ | ||
private $user; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->prepareDatabase(); | ||
$this->user = factory(User::class)->create(); | ||
|
||
Gate::define('testGate', function ($user, $argument) { | ||
return $user->id == $argument->id; | ||
}); | ||
} | ||
|
||
public function test_authorizing_with_gate() | ||
{ | ||
$link = AdminLink::create('Reset password', 'http://test.com'); | ||
$link->registerAbilityForAuthorization('testGate', $this->user); | ||
|
||
$this->assertTrue($link->isAuthorized($this->user)); | ||
} | ||
|
||
public function test_not_authorized_without_user() | ||
{ | ||
$link = AdminLink::create('Reset password', 'http://test.com'); | ||
$link->registerAbilityForAuthorization('testGate', $this->user); | ||
|
||
$this->assertFalse($link->isAuthorized()); | ||
} | ||
|
||
public function test_not_authorized_with_non_existing_guard() | ||
{ | ||
$link = AdminLink::create('Reset password', 'http://test.com'); | ||
$link->registerAbilityForAuthorization('testGate', $this->user); | ||
$link->registerGuardForAuthorization('non.existing.guard'); | ||
|
||
$this->assertFalse($link->isAuthorized($this->user)); | ||
} | ||
|
||
public function test_can_be_serialized_and_deserialized() | ||
{ | ||
$link = AdminLink::create('Reset password', 'http://test.com'); | ||
$link->registerAbilityForAuthorization('testGate', $this->user); | ||
|
||
$serializedLink = serialize($link); | ||
$link->__wakeup(); // Restore any serialized models on the original object | ||
$unserializedLink = unserialize($serializedLink); | ||
|
||
$this->assertTrue($unserializedLink->isAuthorized($this->user)); | ||
$this->assertEquals($link, $unserializedLink, "Unserialization did not produce the orginal object structure"); | ||
} | ||
} |