1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Backpack\CRUD\Tests\Unit\Models\Article; |
7
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
8
|
|
|
|
9
|
|
|
class CrudPanelFakeFieldsTest extends BaseDBCrudPanelTest |
10
|
|
|
{ |
11
|
|
|
private $fakeFieldsArray = [ |
12
|
|
|
[ |
13
|
|
|
'name' => 'field', |
14
|
|
|
'label' => 'Normal Field', |
15
|
|
|
], |
16
|
|
|
[ |
17
|
|
|
'name' => 'meta_title', |
18
|
|
|
'label' => 'Meta Title', |
19
|
|
|
'fake' => true, |
20
|
|
|
'store_in' => 'metas', |
21
|
|
|
], |
22
|
|
|
[ |
23
|
|
|
'name' => 'meta_description', |
24
|
|
|
'label' => 'Meta Description', |
25
|
|
|
'fake' => true, |
26
|
|
|
'store_in' => 'metas', |
27
|
|
|
], |
28
|
|
|
[ |
29
|
|
|
'name' => 'meta_keywords', |
30
|
|
|
'label' => 'Meta Keywords', |
31
|
|
|
'fake' => true, |
32
|
|
|
'store_in' => 'metas', |
33
|
|
|
], |
34
|
|
|
[ |
35
|
|
|
'name' => 'tags', |
36
|
|
|
'label' => 'Tags', |
37
|
|
|
'fake' => true, |
38
|
|
|
'store_in' => 'tags', |
39
|
|
|
], |
40
|
|
|
[ |
41
|
|
|
'name' => 'extra_details', |
42
|
|
|
'label' => 'Extra Details', |
43
|
|
|
'fake' => true, |
44
|
|
|
], |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
private $noFakeFieldsInputData = [ |
48
|
|
|
'value1' => 'Value 1', |
49
|
|
|
'value2' => 'Value 2', |
50
|
|
|
'value3' => 'Value 3', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
private $fakeFieldsInputData = [ |
54
|
|
|
'value1' => 'Value 1', |
55
|
|
|
'value2' => 'Value 2', |
56
|
|
|
'value3' => 'Value 3', |
57
|
|
|
'meta_title' => 'Meta Title Value', |
58
|
|
|
'meta_description' => 'Meta Description Value', |
59
|
|
|
'tags' => ['tag1', 'tag2', 'tag3'], |
60
|
|
|
'extra_details' => ['detail1', 'detail2', 'detail3'], |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
private $expectedInputDataWithCompactedFakeFields = [ |
64
|
|
|
'value1' => 'Value 1', |
65
|
|
|
'value2' => 'Value 2', |
66
|
|
|
'value3' => 'Value 3', |
67
|
|
|
'metas' => '{"meta_title":"Meta Title Value","meta_description":"Meta Description Value"}', |
68
|
|
|
'tags' => '{"tags":["tag1","tag2","tag3"]}', |
69
|
|
|
'extras' => '{"extra_details":["detail1","detail2","detail3"]}', |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
public function testCompactFakeFieldsFromCreateForm() |
73
|
|
|
{ |
74
|
|
|
$this->crudPanel->addFields($this->fakeFieldsArray); |
75
|
|
|
|
76
|
|
|
$compactedFakeFields = $this->crudPanel->compactFakeFields($this->fakeFieldsInputData, 'create'); |
77
|
|
|
|
78
|
|
|
$this->assertEquals($this->expectedInputDataWithCompactedFakeFields, $compactedFakeFields); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function testCompactFakeFieldsFromUpdateForm() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$article = DB::table('articles')->where('id', 1)->first(); |
84
|
|
|
$this->crudPanel->setModel(Article::class); |
85
|
|
|
$this->crudPanel->addFields($this->fakeFieldsArray, 'update'); |
86
|
|
|
|
87
|
|
|
$compactedFakeFields = $this->crudPanel->compactFakeFields($this->fakeFieldsInputData, 'update', $article->id); |
88
|
|
|
|
89
|
|
|
$this->assertEquals($this->expectedInputDataWithCompactedFakeFields, $compactedFakeFields); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testCompactFakeFieldsFromUpdateFormWithoutId() |
93
|
|
|
{ |
94
|
|
|
$this->setExpectedException(ModelNotFoundException::class); |
95
|
|
|
|
96
|
|
|
$this->crudPanel->setModel(Article::class); |
97
|
|
|
$this->crudPanel->addFields($this->fakeFieldsArray, 'update'); |
98
|
|
|
|
99
|
|
|
$compactedFakeFields = $this->crudPanel->compactFakeFields($this->fakeFieldsInputData, 'update'); |
100
|
|
|
|
101
|
|
|
$this->assertEquals($this->expectedInputDataWithCompactedFakeFields, $compactedFakeFields); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function testCompactFakeFieldsFromUpdateFormWithUnknownId() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->setExpectedException(ModelNotFoundException::class); |
107
|
|
|
|
108
|
|
|
$unknownId = DB::getPdo()->lastInsertId() + 1; |
109
|
|
|
$this->crudPanel->setModel(Article::class); |
110
|
|
|
$this->crudPanel->addFields($this->fakeFieldsArray, 'update'); |
111
|
|
|
|
112
|
|
|
$compactedFakeFields = $this->crudPanel->compactFakeFields($this->fakeFieldsInputData, 'update', $unknownId); |
113
|
|
|
|
114
|
|
|
$this->assertEquals($this->expectedInputDataWithCompactedFakeFields, $compactedFakeFields); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testCompactFakeFieldsFromEmptyRequest() |
118
|
|
|
{ |
119
|
|
|
$compactedFakeFields = $this->crudPanel->compactFakeFields([]); |
120
|
|
|
|
121
|
|
|
$this->assertEmpty($compactedFakeFields); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testCompactFakeFieldsFromRequestWithNoFakes() |
125
|
|
|
{ |
126
|
|
|
$compactedFakeFields = $this->crudPanel->compactFakeFields($this->noFakeFieldsInputData); |
127
|
|
|
|
128
|
|
|
$this->assertEquals($this->noFakeFieldsInputData, $compactedFakeFields); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testCompactFakeFieldsFromUnknownForm() |
132
|
|
|
{ |
133
|
|
|
$this->markTestIncomplete('Not correctly implemented'); |
134
|
|
|
|
135
|
|
|
$this->setExpectedException(\InvalidArgumentException::class); |
136
|
|
|
|
137
|
|
|
// TODO: this should throw an invalid argument exception but doesn't because of the getFields method in the |
138
|
|
|
// read trait, which returns the create fields in case of an unknown form type. |
139
|
|
|
$this->crudPanel->compactFakeFields($this->fakeFieldsInputData, 'unknownForm'); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.