forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to defer permission and utility registration for translations (…
…statamic#7343) Co-authored-by: Jesse Leite <[email protected]> Co-authored-by: Jack McDade <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Fixes statamic#3467 Fixes statamic#2041
- Loading branch information
1 parent
62c6fed
commit 5d2d82e
Showing
12 changed files
with
234 additions
and
29 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
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,16 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Middleware\CP; | ||
|
||
use Closure; | ||
use Statamic\Facades\Permission; | ||
|
||
class BootPermissions | ||
{ | ||
public function handle($request, Closure $next) | ||
{ | ||
Permission::boot(); | ||
|
||
return $next($request); | ||
} | ||
} |
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 Statamic\Http\Middleware\CP; | ||
|
||
use Closure; | ||
use Statamic\Facades\Utility; | ||
|
||
class BootUtilities | ||
{ | ||
public function handle($request, Closure $next) | ||
{ | ||
Utility::boot(); | ||
|
||
return $next($request); | ||
} | ||
} |
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
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,96 @@ | ||
<?php | ||
|
||
namespace Tests\CP\Utilities; | ||
|
||
use Illuminate\Support\Collection; | ||
use Statamic\CP\Utilities\Utility; | ||
use Statamic\CP\Utilities\UtilityRepository; | ||
use Tests\TestCase; | ||
|
||
class UtilityRepositoryTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @dataProvider registerMethodProvider | ||
*/ | ||
public function it_registers_a_utility($registerMethod) | ||
{ | ||
$utilities = new UtilityRepository; | ||
$this->assertInstanceOf(Collection::class, $utilities->all()); | ||
$this->assertCount(0, $utilities->all()); | ||
|
||
$utility = $utilities->make('one'); | ||
$this->assertCount(0, $utilities->all()); | ||
|
||
$utilities->$registerMethod($utility); | ||
$this->assertEquals(['one' => $utility], $utilities->all()->all()); | ||
$this->assertEquals($utility, $utilities->find('one')); | ||
} | ||
|
||
public function registerMethodProvider() | ||
{ | ||
return [ | ||
'register' => ['register'], | ||
'push' => ['push'], // @deprecated | ||
]; | ||
} | ||
|
||
/** @test */ | ||
public function it_registers_a_utility_via_a_string() | ||
{ | ||
$utilities = new UtilityRepository; | ||
|
||
$utility = $utilities->register('one'); | ||
|
||
$this->assertInstanceOf(Utility::class, $utility); | ||
$this->assertEquals('one', $utility->handle()); | ||
$this->assertCount(1, $utilities->all()); | ||
$this->assertEquals(['one' => $utility], $utilities->all()->all()); | ||
} | ||
|
||
/** @test */ | ||
public function it_defers_registration_until_boot_using_extend_method() | ||
{ | ||
$utilities = new UtilityRepository; | ||
$callbackRan = false; | ||
|
||
$utilities->extend(function ($arg) use ($utilities, &$callbackRan) { | ||
$this->assertEquals($utilities, $arg); | ||
$callbackRan = true; | ||
}); | ||
|
||
$this->assertFalse($callbackRan); | ||
|
||
$utilities->boot(); | ||
|
||
$this->assertTrue($callbackRan); | ||
} | ||
|
||
/** @test */ | ||
public function booting_more_than_once_just_updates_the_utilities() | ||
{ | ||
// This makes sure that booting a second time doesn't duplicate | ||
// any utilities. It should just update/replace the existing ones. | ||
// We boot once early so that routes can get registered, and | ||
// then again after the user's locale preference is set so | ||
// that the translations for labels etc use the right language. | ||
|
||
$utilities = new UtilityRepository; | ||
|
||
$utilities->extend(function ($utilities) { | ||
$utilities->register('test') | ||
->title(__('and')); // using a translation that will likely never change. | ||
}); | ||
|
||
$utilities->boot(); | ||
|
||
$this->assertEquals(['and'], $utilities->all()->map->title()->values()->all()); | ||
|
||
app()->setLocale('fr'); | ||
|
||
$utilities->boot(); | ||
|
||
$this->assertEquals(['et'], $utilities->all()->map->title()->values()->all()); | ||
} | ||
} |
Oops, something went wrong.