Skip to content

Commit c0ed12c

Browse files
committed
Authorization tests
1 parent cb19287 commit c0ed12c

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

src/Html/HasAuthorizations.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ public static function makeIf(callable|bool $condition, array|string $options =
3333
return static::make([])->authorized(false);
3434
}
3535

36+
/**
37+
* Set authorization status of the button.
38+
*
39+
* @param callable|bool $bool
40+
* @return static
41+
*/
42+
public function authorized(callable|bool $bool): static
43+
{
44+
$this->authorized = (bool) value($bool);
45+
46+
return $this;
47+
}
48+
3649
/**
3750
* Make a button if the user is authorized.
3851
*
@@ -78,30 +91,27 @@ public static function makeIfCannot(
7891
return static::make([])->authorized(false);
7992
}
8093

81-
/**
82-
* Set authorization status of the button.
83-
*
84-
* @param callable|bool $bool
85-
* @return static
86-
*/
87-
public function authorized(callable|bool $bool): static
88-
{
89-
$this->authorized = (bool) value($bool);
90-
91-
return $this;
92-
}
93-
9494
/**
9595
* Convert the Fluent instance to an array.
9696
*
9797
* @return array
9898
*/
9999
public function toArray(): array
100100
{
101-
if ($this->authorized) {
101+
if ($this->isAuthorized()) {
102102
return parent::toArray();
103103
}
104104

105105
return [];
106106
}
107+
108+
/**
109+
* Check if instance is authorized
110+
*
111+
* @return bool
112+
*/
113+
public function isAuthorized(): bool
114+
{
115+
return $this->authorized;
116+
}
107117
}

tests/EditorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ public function it_can_have_events()
4848
$this->assertEquals($event, $editor->events[0]);
4949
}
5050

51+
/** @test */
52+
public function it_has_authorizations()
53+
{
54+
$editor = Editor::makeIf(true, 'editor');
55+
$this->assertInstanceOf(Editor::class, $editor);
56+
$this->assertEquals(true, $editor->isAuthorized());
57+
58+
$editor = Editor::makeIf(false, 'editor');
59+
$this->assertInstanceOf(Editor::class, $editor);
60+
$this->assertEquals(false, $editor->isAuthorized());
61+
62+
$editor = Editor::makeIfCan('ability', 'editor');
63+
$this->assertInstanceOf(Editor::class, $editor);
64+
$this->assertEquals(false, $editor->isAuthorized());
65+
66+
$editor = Editor::makeIfCannot('ability', 'editor');
67+
$this->assertInstanceOf(Editor::class, $editor);
68+
$this->assertEquals(false, $editor->isAuthorized());
69+
}
70+
5171
/**
5272
* @param string $instance
5373
* @return \Yajra\DataTables\Html\Editor\Editor

0 commit comments

Comments
 (0)