Skip to content

Nova 4 policies #31

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 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/vendor
/vendor
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
"psr-4": {
"NovaTesting\\": "src/"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
18 changes: 18 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 72 additions & 43 deletions src/Assert/AssertPolicies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,126 @@

namespace NovaTesting\Assert;

use Laravel\Nova\Nova;
use Illuminate\Support\Arr;
use Illuminate\Testing\Assert as PHPUnit;
use Laravel\Nova\Nova;

trait AssertPolicies
{
public function assertCanView()
{
$model = $this->resolveNovaModel();

PHPUnit::assertTrue($model->authorizedToView);
}

public function assertCannotView()
{
$model = $this->resolveNovaModel();

PHPUnit::assertFalse($model->authorizedToView);
}

public function assertCanCreate()
{
PHPUnit::assertTrue(
collect(Nova::jsonVariables(request())['resources'])
->where('uriKey', $this->novaParameters['resource'])
->first()['authorizedToCreate']
);
$model = $this->resolveNovaModel();

PHPUnit::assertTrue($model->authorizedToCreate);
}

public function assertCannotCreate()
{
PHPUnit::assertFalse(
collect(Nova::jsonVariables(request())['resources'])
->where('uriKey', $this->novaParameters['resource'])
->first()['authorizedToCreate']
);
$model = $this->resolveNovaModel();

PHPUnit::assertFalse($model->authorizedToCreate);
}

public function assertCanDelete()
{
return $this->assertJsonFragment([
'authorizedToDelete' => true
]);
$model = $this->resolveNovaModel();

PHPUnit::assertTrue($model->authorizedToDelete);
}

public function assertCannotDelete()
{
return $this->assertJsonFragment([
'authorizedToDelete' => false
]);
$model = $this->resolveNovaModel();

PHPUnit::assertFalse($model->authorizedToDelete);
}

public function assertCanForceDelete()
{
return $this->assertJsonFragment([
'authorizedToForceDelete' => true
]);
$model = $this->resolveNovaModel();

PHPUnit::assertTrue($model->authorizedToForceDelete);
}

public function assertCannotForceDelete()
{
return $this->assertJsonFragment([
'authorizedToForceDelete' => false
]);
$model = $this->resolveNovaModel();

PHPUnit::assertFalse($model->authorizedToForceDelete);
}

public function assertCanRestore()
{
return $this->assertJsonFragment([
'authorizedToRestore' => true
]);
$model = $this->resolveNovaModel();

PHPUnit::assertTrue($model->authorizedToRestore);
}

public function assertCannotRestore()
{
return $this->assertJsonFragment([
'authorizedToRestore' => false
]);
$model = $this->resolveNovaModel();

PHPUnit::assertFalse($model->authorizedToRestore);
}

public function assertCanUpdate()
{
return $this->assertJsonFragment([
'authorizedToUpdate' => true
]);
$model = $this->resolveNovaModel();

PHPUnit::assertTrue($model->authorizedToUpdate);
}

public function assertCannotUpdate()
{
return $this->assertJsonFragment([
'authorizedToUpdate' => false
]);
$model = $this->resolveNovaModel();

PHPUnit::assertFalse($model->authorizedToUpdate);
}

public function assertCanView()
public function assertCanReplicate()
{
return $this->assertJsonFragment([
'authorizedToView' => true
]);
$model = $this->resolveNovaModel();

PHPUnit::assertTrue($model->authorizedToReplicate);
}

public function assertCannotView()
public function assertCannotReplicate()
{
$model = $this->resolveNovaModel();

PHPUnit::assertFalse($model->authorizedToReplicate);
}

public function assertCanImpersonate()
{
$model = $this->resolveNovaModel();

PHPUnit::assertTrue($model->authorizedToImpersonate);
}

public function assertCannotImpersonate()
{
$model = $this->resolveNovaModel();

PHPUnit::assertFalse($model->authorizedToImpersonate);
}

private function resolveNovaModel()
{
return $this->assertJsonFragment([
'authorizedToView' => false
]);
return reset($this->originalResponse()->baseResponse->getData()->resources);
}
}
5 changes: 3 additions & 2 deletions src/NovaResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace NovaTesting;

use Illuminate\Database\Eloquent\Model;
use NovaTesting\Assert\AssertActions;
use NovaTesting\Assert\AssertCards;
use NovaTesting\Assert\AssertFields;
use NovaTesting\Assert\AssertLenses;
use NovaTesting\Assert\AssertActions;
use NovaTesting\Assert\AssertFilters;
use NovaTesting\Assert\AssertLenses;
use NovaTesting\Assert\AssertPolicies;
use NovaTesting\Assert\AssertRelations;
use NovaTesting\Assert\AssertResources;
Expand Down