-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds more tests * Tests around authorization logic
- Loading branch information
1 parent
31c4bbe
commit 51a0cfc
Showing
8 changed files
with
144 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
|
||
class Book extends Model | ||
{ | ||
// | ||
protected $guarded = []; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Illuminate\Foundation\Auth\User as BaseUser; | ||
|
||
class User extends BaseUser | ||
{ | ||
protected $guarded = []; | ||
|
||
public function books(): HasMany | ||
{ | ||
return $this->hasMany(Book::class); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Gate; | ||
use Illuminate\Support\Facades\Schema; | ||
use Laravel\Folio\Folio; | ||
use Tests\Feature\Fixtures\Book; | ||
use Tests\Feature\Fixtures\User; | ||
|
||
it('may have blade php blocks', function () { | ||
Folio::route(__DIR__.'/resources/views/pages'); | ||
|
||
$response = $this->get('/users/nuno'); | ||
|
||
$response->assertSee('Hello, Nuno Maduro from PHP block.'); | ||
}); | ||
|
||
it('blade php blocks are only executed when rendering the view', function () { | ||
$_SERVER['__folio_rendered_count'] = 0; | ||
|
||
Folio::route(__DIR__.'/resources/views/pages'); | ||
|
||
$this->get('/users/nuno'); | ||
$response = $this->get('/users/nuno'); | ||
|
||
$response->assertSee('Rendered [2] time from PHP block.'); | ||
}); | ||
|
||
it('may have blade php blocks with authorization logic', function () { | ||
Folio::route(__DIR__.'/resources/views/pages'); | ||
|
||
Schema::create('users', function ($table) { | ||
$table->id(); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('books', function ($table) { | ||
$table->id(); | ||
$table->string('title'); | ||
$table->foreignId('user_id'); | ||
$table->timestamps(); | ||
}); | ||
|
||
$user = User::create(); | ||
|
||
Book::create([ | ||
'title' => 'test-book-title', | ||
'user_id' => $user->id, | ||
]); | ||
|
||
Gate::define('view-books', fn () => true); | ||
$this->actingAs($user)->get('/books')->assertStatus(200); | ||
|
||
Gate::define('view-books', fn () => false); | ||
$this->actingAs($user)->get('/books')->assertStatus(403); | ||
}); |
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,17 @@ | ||
@php | ||
use Illuminate\Support\Facades\Gate; | ||
if (! Gate::check('view-books')) { | ||
abort(403); | ||
} | ||
$user = auth()->user(); | ||
$books = $user->books; | ||
@endphp | ||
|
||
@foreach ($books as $book) | ||
<div> | ||
{{ $book->title }} | ||
</div> | ||
@endforeach |
17 changes: 17 additions & 0 deletions
17
tests/Feature/resources/views/pages/podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php
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 |
---|---|---|
@@ -1,3 +1,20 @@ | ||
<?php | ||
use function Laravel\Folio\middleware; | ||
middleware(function ($request, $next) { | ||
$_SERVER['__folio_podcasts_inline_middleware'] = $request->route('podcast'); | ||
return $next($request); | ||
}); | ||
?> | ||
|
||
@php | ||
$_SERVER['__folio_podcasts_php_blade_block'] = $podcast; | ||
@endphp | ||
|
||
|
||
<div> | ||
{{ $podcast->name }} | ||
</div> |
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,8 @@ | ||
@php | ||
$title = 'Nuno Maduro'; | ||
$renderedCount = $_SERVER['__folio_rendered_count'] = ($_SERVER['__folio_rendered_count'] ?? 0) + 1; | ||
@endphp | ||
|
||
<div>Hello, {{ $title }} from PHP block.</div> | ||
<div>Rendered [{{ $renderedCount }}] time from PHP block.</div> |