Skip to content

Commit 61fc999

Browse files
committed
Authorization and Editor tests
1 parent c0ed12c commit 61fc999

File tree

3 files changed

+123
-14
lines changed

3 files changed

+123
-14
lines changed

src/Html/Editor/Editor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
* @property string|array|null $ajax
1515
* @property array $fields
1616
* @property string|null $template
17+
* @property string $idSrc
18+
* @property string $display
19+
* @property string $scripts
20+
* @property array $formOptions
1721
*/
1822
class Editor extends Fluent
1923
{
@@ -237,6 +241,10 @@ public function template(string $template): static
237241
*/
238242
public function toArray(): array
239243
{
244+
if (! $this->isAuthorized()) {
245+
return [];
246+
}
247+
240248
$array = parent::toArray();
241249

242250
unset($array['events']);

src/Html/HasAuthorizations.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function makeIf(callable|bool $condition, array|string $options =
3030
return static::make($options);
3131
}
3232

33-
return static::make([])->authorized(false);
33+
return app(static::class)->authorized(false);
3434
}
3535

3636
/**
@@ -98,11 +98,11 @@ public static function makeIfCannot(
9898
*/
9999
public function toArray(): array
100100
{
101-
if ($this->isAuthorized()) {
102-
return parent::toArray();
101+
if (! $this->isAuthorized()) {
102+
return [];
103103
}
104104

105-
return [];
105+
return parent::toArray();
106106
}
107107

108108
/**

tests/EditorTest.php

Lines changed: 111 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function it_can_create_an_editor()
1919
$this->assertEmpty($editor->template);
2020

2121
$editor
22+
->idSrc()
2223
->fields([
2324
Text::make('name'),
2425
])
@@ -28,6 +29,16 @@ public function it_can_create_an_editor()
2829
$this->assertCount(1, $editor->fields);
2930
$this->assertEquals('/test', $editor->ajax);
3031
$this->assertEquals('#template', $editor->template);
32+
$this->assertEquals('DT_RowId', $editor->idSrc);
33+
}
34+
35+
/**
36+
* @param string $instance
37+
* @return \Yajra\DataTables\Html\Editor\Editor
38+
*/
39+
protected function getEditor(string $instance = 'editor'): Editor
40+
{
41+
return Editor::make($instance);
3142
}
3243

3344
/** @test */
@@ -51,29 +62,119 @@ public function it_can_have_events()
5162
/** @test */
5263
public function it_has_authorizations()
5364
{
54-
$editor = Editor::makeIf(true, 'editor');
65+
$editor = Editor::makeIf(true, 'editor')
66+
->fields([
67+
Text::make('name'),
68+
]);
5569
$this->assertInstanceOf(Editor::class, $editor);
5670
$this->assertEquals(true, $editor->isAuthorized());
57-
58-
$editor = Editor::makeIf(false, 'editor');
71+
$this->assertEquals([
72+
'instance' => 'editor',
73+
'fields' => [
74+
Text::make('name')->toArray(),
75+
],
76+
], $editor->toArray());
77+
78+
$editor = Editor::makeIf(false, 'editor')
79+
->fields([
80+
Text::make('name'),
81+
]);
5982
$this->assertInstanceOf(Editor::class, $editor);
6083
$this->assertEquals(false, $editor->isAuthorized());
84+
$this->assertCount(1, $editor->fields);
85+
$this->assertEquals([], $editor->toArray());
6186

6287
$editor = Editor::makeIfCan('ability', 'editor');
6388
$this->assertInstanceOf(Editor::class, $editor);
6489
$this->assertEquals(false, $editor->isAuthorized());
90+
$this->assertEquals([], $editor->toArray());
6591

6692
$editor = Editor::makeIfCannot('ability', 'editor');
6793
$this->assertInstanceOf(Editor::class, $editor);
6894
$this->assertEquals(false, $editor->isAuthorized());
95+
$this->assertEquals([], $editor->toArray());
6996
}
7097

71-
/**
72-
* @param string $instance
73-
* @return \Yajra\DataTables\Html\Editor\Editor
74-
*/
75-
protected function getEditor(string $instance = 'editor'): Editor
98+
/** @test */
99+
public function it_can_be_serialized_to_array()
76100
{
77-
return Editor::make($instance);
101+
$editor = Editor::make()
102+
->ajax('')
103+
->fields([
104+
Text::make('name'),
105+
]);
106+
107+
$this->assertEquals([
108+
'instance' => 'editor',
109+
'ajax' => '',
110+
'fields' => [
111+
Text::make('name')->toArray(),
112+
],
113+
], $editor->toArray());
114+
}
115+
116+
/** @test */
117+
public function it_can_be_serialized_to_json_string()
118+
{
119+
$editor = Editor::make()
120+
->ajax('')
121+
->fields([
122+
Text::make('name'),
123+
]);
124+
125+
$expected = '{"instance":"editor","ajax":"","fields":[{"name":"name","label":"Name","type":"text"}]}';
126+
$this->assertEquals($expected, $editor->toJson());
127+
}
128+
129+
/** @test */
130+
public function it_has_form_options()
131+
{
132+
$editor = Editor::make()
133+
->formOptions([
134+
'main' => [
135+
'esc' => true,
136+
],
137+
]);
138+
139+
$this->assertEquals([
140+
'main' => [
141+
'esc' => true,
142+
],
143+
], $editor->formOptions);
144+
145+
$editor->formOptionsMain(['esc' => true]);
146+
$this->assertEquals(['esc' => true], $editor->formOptions['main']);
147+
148+
$editor->formOptionsBubble(['esc' => true]);
149+
$this->assertEquals(['esc' => true], $editor->formOptions['bubble']);
150+
151+
$editor->formOptionsInline(['esc' => true]);
152+
$this->assertEquals(['esc' => true], $editor->formOptions['inline']);
153+
}
154+
155+
/** @test */
156+
public function it_has_display_constants()
157+
{
158+
$editor = Editor::make()->display(Editor::DISPLAY_BOOTSTRAP);
159+
$this->assertEquals('bootstrap', $editor->display);
160+
161+
$editor->display(Editor::DISPLAY_ENVELOPE);
162+
$this->assertEquals('envelope', $editor->display);
163+
164+
$editor->display(Editor::DISPLAY_FOUNDATION);
165+
$this->assertEquals('foundation', $editor->display);
166+
167+
$editor->display(Editor::DISPLAY_JQUERYUI);
168+
$this->assertEquals('jqueryui', $editor->display);
169+
170+
$editor->display(Editor::DISPLAY_LIGHTBOX);
171+
$this->assertEquals('lightbox', $editor->display);
172+
}
173+
174+
/** @test */
175+
public function it_has_scripts()
176+
{
177+
$editor = Editor::make()->scripts('fn');
178+
$this->assertEquals('fn', $editor->scripts);
78179
}
79-
}
180+
}

0 commit comments

Comments
 (0)