1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\Tests\Unit\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudColumn; |
6
|
|
|
use Backpack\CRUD\Tests\config\Models\Article; |
7
|
|
|
use Backpack\CRUD\Tests\config\Models\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Columns |
11
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\ColumnsProtectedMethods |
12
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\CrudColumn |
13
|
|
|
* @covers Backpack\CRUD\app\Library\CrudPanel\CrudPanel |
14
|
|
|
*/ |
15
|
|
|
class CrudPanelColumnsLinkToTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseDBCrudPanel |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Setup the test environment. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
protected function setUp(): void |
23
|
|
|
{ |
24
|
|
|
parent::setUp(); |
25
|
|
|
|
26
|
|
|
$this->crudPanel->setOperation('list'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testColumnLinkToThrowsExceptionWhenNotAllRequiredParametersAreFilled() |
30
|
|
|
{ |
31
|
|
|
$this->expectException(\Exception::class); |
32
|
|
|
$this->expectExceptionMessage('Route [article.show.detail] expects parameters [id, detail]. Insufficient parameters provided in column: [articles].'); |
33
|
|
|
$this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['test' => 'testing']); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testItThrowsExceptionIfRouteNotFound() |
37
|
|
|
{ |
38
|
|
|
$this->expectException(\Exception::class); |
39
|
|
|
$this->expectExceptionMessage('Route [users.route.doesnt.exist] not found while building the link for column [id].'); |
40
|
|
|
|
41
|
|
|
CrudColumn::name('id')->linkTo('users.route.doesnt.exist')->toArray(); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testColumnLinkToWithRouteNameOnly() |
45
|
|
|
{ |
46
|
|
|
$this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show'); |
47
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
48
|
|
|
$reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
49
|
|
|
$arguments = $reflection->getClosureUsedVariables(); |
|
|
|
|
50
|
|
|
$this->crudPanel->entry = Article::first(); |
51
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
52
|
|
|
$this->assertEquals('articles.show', $arguments['route']); |
53
|
|
|
$this->assertCount(1, $arguments['parameters']); |
54
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show', $url); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testColumnLinkToWithRouteNameAndAdditionalParameters() |
58
|
|
|
{ |
59
|
|
|
$this->crudPanel->column('articles')->entity('articles')->linkTo('articles.show', ['test' => 'testing', 'test2' => 'testing2']); |
60
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
61
|
|
|
$reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
62
|
|
|
$arguments = $reflection->getClosureUsedVariables(); |
63
|
|
|
$this->assertEquals('articles.show', $arguments['route']); |
64
|
|
|
$this->assertCount(3, $arguments['parameters']); |
65
|
|
|
$this->crudPanel->entry = Article::first(); |
66
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
67
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=testing2', $url); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testColumnLinkToWithCustomParameters() |
71
|
|
|
{ |
72
|
|
|
$this->crudPanel->column('articles')->entity('articles')->linkTo('article.show.detail', ['detail' => 'testing', 'otherParam' => 'test']); |
73
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
74
|
|
|
$this->crudPanel->entry = Article::first(); |
75
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
76
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show/testing?otherParam=test', $url); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testColumnLinkToWithCustomClosureParameters() |
80
|
|
|
{ |
81
|
|
|
$this->crudPanel->column('articles') |
82
|
|
|
->entity('articles') |
83
|
|
|
->linkTo('article.show.detail', ['detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]); |
84
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
85
|
|
|
$this->crudPanel->entry = Article::first(); |
86
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
87
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show/1?otherParam=Some%20Content', $url); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testColumnLinkToDontAutoInferParametersIfAllProvided() |
91
|
|
|
{ |
92
|
|
|
$this->crudPanel->column('articles') |
93
|
|
|
->entity('articles') |
94
|
|
|
->linkTo('article.show.detail', ['id' => 123, 'detail' => fn ($entry, $related_key) => $related_key, 'otherParam' => fn ($entry) => $entry->content]); |
95
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
96
|
|
|
$this->crudPanel->entry = Article::first(); |
97
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
98
|
|
|
$this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testColumnLinkToAutoInferAnySingleParameter() |
102
|
|
|
{ |
103
|
|
|
$this->crudPanel->column('articles') |
104
|
|
|
->entity('articles') |
105
|
|
|
->linkTo('article.show.detail', ['id' => 123, 'otherParam' => fn ($entry) => $entry->content]); |
106
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
107
|
|
|
$this->crudPanel->entry = Article::first(); |
108
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
109
|
|
|
$this->assertEquals('http://localhost/admin/articles/123/show/1?otherParam=Some%20Content', $url); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testColumnLinkToWithClosure() |
113
|
|
|
{ |
114
|
|
|
$this->crudPanel->column('articles') |
115
|
|
|
->entity('articles') |
116
|
|
|
->linkTo(fn ($entry) => route('articles.show', $entry->content)); |
|
|
|
|
117
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
118
|
|
|
$this->crudPanel->entry = Article::first(); |
119
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
120
|
|
|
$this->assertEquals('http://localhost/admin/articles/Some%20Content/show', $url); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testColumnArrayDefinitionLinkToRouteAsClosure() |
124
|
|
|
{ |
125
|
|
|
$this->crudPanel->setModel(User::class); |
126
|
|
|
$this->crudPanel->column([ |
127
|
|
|
'name' => 'articles', |
128
|
|
|
'entity' => 'articles', |
129
|
|
|
'linkTo' => fn ($entry) => route('articles.show', ['id' => $entry->id, 'test' => 'testing']), |
130
|
|
|
]); |
131
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
132
|
|
|
$this->crudPanel->entry = Article::first(); |
133
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
134
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show?test=testing', $url); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testColumnArrayDefinitionLinkToRouteNameOnly() |
138
|
|
|
{ |
139
|
|
|
$this->crudPanel->setModel(User::class); |
140
|
|
|
$this->crudPanel->column([ |
141
|
|
|
'name' => 'articles', |
142
|
|
|
'entity' => 'articles', |
143
|
|
|
'linkTo' => 'articles.show', |
144
|
|
|
]); |
145
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
146
|
|
|
$this->crudPanel->entry = Article::first(); |
147
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
148
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show', $url); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testColumnArrayDefinitionLinkToRouteNameAndAdditionalParameters() |
152
|
|
|
{ |
153
|
|
|
$this->crudPanel->setModel(User::class); |
154
|
|
|
$this->crudPanel->column([ |
155
|
|
|
'name' => 'articles', |
156
|
|
|
'entity' => 'articles', |
157
|
|
|
'linkTo' => [ |
158
|
|
|
'route' => 'articles.show', |
159
|
|
|
'parameters' => [ |
160
|
|
|
'test' => 'testing', |
161
|
|
|
'test2' => fn ($entry) => $entry->content, |
162
|
|
|
], |
163
|
|
|
], |
164
|
|
|
]); |
165
|
|
|
$columnArray = $this->crudPanel->columns()['articles']; |
166
|
|
|
$reflection = new \ReflectionFunction($columnArray['wrapper']['href']); |
167
|
|
|
$arguments = $reflection->getClosureUsedVariables(); |
168
|
|
|
$this->assertEquals('articles.show', $arguments['route']); |
169
|
|
|
$this->assertCount(3, $arguments['parameters']); |
170
|
|
|
$this->crudPanel->entry = Article::first(); |
171
|
|
|
$url = $columnArray['wrapper']['href']($this->crudPanel, $columnArray, $this->crudPanel->entry, 1); |
172
|
|
|
$this->assertEquals('http://localhost/admin/articles/1/show?test=testing&test2=Some%20Content', $url); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|