2021-06-26 17:23:15 +02:00
< ? php
namespace Tests\Commands ;
2021-02-11 23:42:36 +01:00
use BookStack\Entities\Models\Bookshelf ;
use Tests\TestCase ;
class CopyShelfPermissionsCommandTest extends TestCase
{
public function test_copy_shelf_permissions_command_shows_error_when_no_required_option_given ()
{
$this -> artisan ( 'bookstack:copy-shelf-permissions' )
-> expectsOutput ( 'Either a --slug or --all option must be provided.' )
2023-05-24 13:59:50 +02:00
-> assertExitCode ( 1 );
2021-02-11 23:42:36 +01:00
}
public function test_copy_shelf_permissions_command_using_slug ()
{
2022-09-29 23:11:16 +02:00
$shelf = $this -> entities -> shelf ();
2021-02-11 23:42:36 +01:00
$child = $shelf -> books () -> first ();
2023-01-21 12:08:34 +01:00
$editorRole = $this -> users -> editor () -> roles () -> first ();
2022-10-12 13:12:36 +02:00
$this -> assertFalse ( $child -> hasPermissions (), 'Child book should not be restricted by default' );
2021-06-26 17:23:15 +02:00
$this -> assertTrue ( $child -> permissions () -> count () === 0 , 'Child book should have no permissions by default' );
2021-02-11 23:42:36 +01:00
2023-01-21 12:08:34 +01:00
$this -> permissions -> setEntityPermissions ( $shelf , [ 'view' , 'update' ], [ $editorRole ]);
2021-02-11 23:42:36 +01:00
$this -> artisan ( 'bookstack:copy-shelf-permissions' , [
'--slug' => $shelf -> slug ,
]);
$child = $shelf -> books () -> first ();
2022-10-12 13:12:36 +02:00
$this -> assertTrue ( $child -> hasPermissions (), 'Child book should now be restricted' );
$this -> assertEquals ( 2 , $child -> permissions () -> count (), 'Child book should have copied permissions' );
$this -> assertDatabaseHas ( 'entity_permissions' , [
'entity_type' => 'book' ,
'entity_id' => $child -> id ,
'role_id' => $editorRole -> id ,
'view' => true , 'update' => true , 'create' => false , 'delete' => false ,
]);
2021-02-11 23:42:36 +01:00
}
public function test_copy_shelf_permissions_command_using_all ()
{
2022-09-29 18:31:38 +02:00
$shelf = $this -> entities -> shelf ();
2021-02-11 23:42:36 +01:00
Bookshelf :: query () -> where ( 'id' , '!=' , $shelf -> id ) -> delete ();
$child = $shelf -> books () -> first ();
2023-01-21 12:08:34 +01:00
$editorRole = $this -> users -> editor () -> roles () -> first ();
2022-10-12 13:12:36 +02:00
$this -> assertFalse ( $child -> hasPermissions (), 'Child book should not be restricted by default' );
2021-06-26 17:23:15 +02:00
$this -> assertTrue ( $child -> permissions () -> count () === 0 , 'Child book should have no permissions by default' );
2021-02-11 23:42:36 +01:00
2023-01-21 12:08:34 +01:00
$this -> permissions -> setEntityPermissions ( $shelf , [ 'view' , 'update' ], [ $editorRole ]);
2021-02-11 23:42:36 +01:00
$this -> artisan ( 'bookstack:copy-shelf-permissions --all' )
-> expectsQuestion ( 'Permission settings for all shelves will be cascaded. Books assigned to multiple shelves will receive only the permissions of it\'s last processed shelf. Are you sure you want to proceed?' , 'y' );
$child = $shelf -> books () -> first ();
2022-10-12 13:12:36 +02:00
$this -> assertTrue ( $child -> hasPermissions (), 'Child book should now be restricted' );
$this -> assertEquals ( 2 , $child -> permissions () -> count (), 'Child book should have copied permissions' );
$this -> assertDatabaseHas ( 'entity_permissions' , [
'entity_type' => 'book' ,
'entity_id' => $child -> id ,
'role_id' => $editorRole -> id ,
'view' => true , 'update' => true , 'create' => false , 'delete' => false ,
]);
2021-02-11 23:42:36 +01:00
}
2021-06-26 17:23:15 +02:00
}