Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — add-search-tests ( 0c8803...5679ad )
by Pedro
15:03
created

testItAppliesCustomOrderByPrimaryKeyForDatatables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
4
5
use Backpack\CRUD\Tests\config\Models\User;
6
use Backpack\CRUD\Tests\config\Models\UserWithTranslations;
7
use PHPUnit\Framework\Attributes\DataProvider;
8
9
/**
10
 * @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Search
11
 */
12
class CrudPanelSearchTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel
13
{
14
    private string $expectedDefaultColumnValue = '<span>'.PHP_EOL.'                        user'.PHP_EOL.'            </span>';
15
16
    public function setUp(): void
17
    {
18
        parent::setUp();
19
        $this->crudPanel->setModel(User::class);
20
    }
21
22
    #[DataProvider('columnsDefaultSearchLogic')]
23
    public function testItCanApplyCustomSearchLogicOnColumns($searchTerm, $columnType, $resultSql)
24
    {
25
        $this->crudPanel->addColumn([
26
            'name' => 'test',
27
            'type' => $columnType,
28
            'searchLogic' => $columnType,
29
            'tableColumn' => true,
30
            'entity' => $columnType === 'select' ? 'accountDetails' : ($columnType === 'select_multiple' ? 'articles' : false),
31
            'relation_type' => $columnType === 'select' ? 'HasOne' : ($columnType === 'select_multiple' ? 'HasMany' : false),
32
        ]);
33
34
        $this->crudPanel->applySearchTerm($searchTerm);
35
36
        $this->assertEquals($resultSql, $this->crudPanel->query->toRawSql());
37
    }
38
39
    public function testItDoesNotAttemptToSearchTheColumnIfSearchLogicIsDisabled()
40
    {
41
        $this->crudPanel->addColumn([
42
            'name' => 'test',
43
            'type' => 'text',
44
            'searchLogic' => false,
45
            'tableColumn' => true,
46
        ]);
47
48
        $this->crudPanel->applySearchTerm('test');
49
50
        $this->assertEquals('select * from "users"', $this->crudPanel->query->toRawSql());
51
    }
52
53
    public function testItDoesNotAttemptToApplyDefaultLogicIfColumnIsNotATableColumn()
54
    {
55
        $this->crudPanel->addColumn([
56
            'name' => 'test',
57
            'type' => 'text',
58
            'searchLogic' => 'text',
59
            'tableColumn' => false,
60
        ]);
61
62
        $this->crudPanel->applySearchTerm('test');
63
64
        $this->assertEquals('select * from "users"', $this->crudPanel->query->toRawSql());
65
    }
66
67
    public function testItValidateDateAndDatetimeSearchTermsAndDoesNotApplySearchIfValidationFails()
68
    {
69
        $this->crudPanel->addColumn([
70
            'name' => 'test',
71
            'type' => 'date',
72
            'searchLogic' => 'date',
73
            'tableColumn' => true,
74
        ]);
75
76
        $this->crudPanel->addColumn([
77
            'name' => 'test',
78
            'type' => 'datetime',
79
            'searchLogic' => 'datetime',
80
            'tableColumn' => true,
81
        ]);
82
83
        $this->crudPanel->applySearchTerm('invalid-date');
84
85
        $this->assertEquals('select * from "users"', $this->crudPanel->query->toRawSql());
86
    }
87
88
    public function testItCanApplySearchLogicFromClosure()
89
    {
90
        $this->crudPanel->addColumn([
91
            'name' => 'test',
92
            'type' => 'my_custom_type',
93
            'searchLogic' => function ($query, $column, $searchTerm) {
94
                $query->where($column['name'], 'like', "%{$searchTerm}%");
95
            },
96
            'tableColumn' => true,
97
        ]);
98
99
        $this->crudPanel->applySearchTerm('test');
100
101
        $this->assertEquals('select * from "users" where ("test" like \'%test%\')', $this->crudPanel->query->toRawSql());
102
    }
103
104
    public function testItCanGetAndSetPersistentTable()
105
    {
106
        $this->crudPanel->enablePersistentTable(true);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...enablePersistentTable() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        $this->crudPanel->/** @scrutinizer ignore-call */ 
107
                          enablePersistentTable(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
107
108
        $this->assertTrue($this->crudPanel->getPersistentTable());
109
110
        $this->crudPanel->disablePersistentTable();
111
112
        $this->assertFalse($this->crudPanel->getPersistentTable());
113
    }
114
115
    public function testItCanGetAndSetTheResponsiveTable()
116
    {
117
        $this->crudPanel->enableResponsiveTable(true);
0 ignored issues
show
Unused Code introduced by
The call to Backpack\CRUD\app\Librar...enableResponsiveTable() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
        $this->crudPanel->/** @scrutinizer ignore-call */ 
118
                          enableResponsiveTable(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
118
119
        $this->assertTrue($this->crudPanel->getResponsiveTable());
120
121
        $this->crudPanel->disableResponsiveTable();
122
123
        $this->assertFalse($this->crudPanel->getResponsiveTable());
124
    }
125
126
    public function testItCanGetPersistentTableDurationFromOperationSetting()
127
    {
128
        $this->crudPanel->setOperationSetting('persistentTableDuration', 10);
129
130
        $this->assertEquals(10, $this->crudPanel->getPersistentTableDuration());
131
    }
132
133
    public function testItCanGetPersistentTableDurantionFromConfig()
134
    {
135
        $this->assertEquals(false, $this->crudPanel->getPersistentTableDuration());
136
137
        config(['backpack.crud.operations.list.persistentTableDuration' => 10]);
138
139
        $this->assertEquals(10, $this->crudPanel->getPersistentTableDuration());
140
    }
141
142
    public function testItCanGetResponsiveTableFromConfig()
143
    {
144
        $this->assertEquals(false, $this->crudPanel->getResponsiveTable());
145
146
        config(['backpack.crud.operations.list.responsiveTable' => true]);
147
148
        $this->assertTrue($this->crudPanel->getResponsiveTable());
149
    }
150
151
    public function testItCanGetTheRenderedViewsForTheColumns()
152
    {
153
        $this->crudPanel->addColumn([
154
            'name' => 'name',
155
            'type' => 'test',
156
        ]);
157
158
        $entries = [$this->makeAUserModel()];
159
160
        $rowColumnsHtml = trim($this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0]);
161
162
        $this->assertEquals($this->expectedDefaultColumnValue, $rowColumnsHtml);
163
    }
164
165
    public function testItRendersTheDetailsRow()
166
    {
167
        $this->crudPanel->addColumn([
168
            'name' => 'name',
169
            'type' => 'test',
170
        ]);
171
172
        $this->crudPanel->setOperationSetting('detailsRow', true);
173
        $entries = [$this->makeAUserModel()];
174
175
        $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0];
176
177
        $rowColumnsHtml = str_replace($this->expectedDefaultColumnValue, '', $rowColumnsHtml);
178
179
        $this->assertStringContainsString('details-row-button', $rowColumnsHtml);
180
    }
181
182
    public function testItRendersTheBulkActions()
183
    {
184
        $this->crudPanel->addColumn([
185
            'name' => 'name',
186
            'type' => 'test',
187
        ]);
188
189
        $this->crudPanel->setOperationSetting('bulkActions', true);
190
        $entries = [$this->makeAUserModel()];
191
192
        $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0];
193
194
        $rowColumnsHtml = str_replace($this->expectedDefaultColumnValue, '', $rowColumnsHtml);
195
196
        $this->assertStringContainsString('crud_bulk_actions_line_checkbox', $rowColumnsHtml);
197
    }
198
199
    public function testItRendersTheLineStackButtons()
200
    {
201
        $this->crudPanel->addColumn([
202
            'name' => 'name',
203
            'type' => 'test',
204
        ]);
205
206
        $this->crudPanel->button('test')->stack('line')->type('view')->content('backpack.theme-coreuiv2::buttons.test');
207
        $entries = [$this->makeAUserModel()];
208
209
        $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][1];
210
211
        $this->assertStringContainsString('btn-secondary', $rowColumnsHtml);
212
    }
213
214
    public function testItAppliesCustomOrderByPrimaryKeyForDatatables()
215
    {
216
        $this->crudPanel->applyDatatableOrder();
217
218
        $this->assertEquals('select * from "users" order by "id" desc', $this->crudPanel->query->toRawSql());
219
    }
220
221
    public function testItCanApplyDatatableOrderFromRequest()
222
    {
223
        $this->crudPanel->addColumn([
224
            'name' => 'name',
225
            'type' => 'test',
226
            'tableColumn' => true,
227
        ]);
228
        $this->setupUserCreateRequest();
229
        $this->crudPanel->getRequest()->merge(['order' => [['column' => 0, 'dir' => 'asc']]]);
230
231
        $this->crudPanel->applyDatatableOrder();
232
233
        $this->assertEquals('select * from "users" order by "name" asc, "id" desc', $this->crudPanel->query->toRawSql());
234
235
    }
236
237
    public function testItCanApplySearchLogicForTranslatableJsonColumns()
238
    {
239
        $this->crudPanel->setModel(UserWithTranslations::class);
240
241
        $this->crudPanel->addColumn([
242
            'name' => 'json',
243
            'type' => 'json',
244
            'tableColumn' => true,
245
        ]);
246
        $this->setupUserCreateRequest();
247
        $this->crudPanel->getRequest()->merge(['order' => [['column' => 0, 'dir' => 'asc']]]);
248
249
        $this->crudPanel->applyDatatableOrder();
250
251
        $this->assertEquals('select * from "users" order by json_extract("json", \'$."en"\') asc, "id" desc', $this->crudPanel->query->toRawSql());
252
253
    }
254
255
    public function testItCanApplySearchLogicForTranslatableColumns()
256
    {
257
        $this->crudPanel->setModel(UserWithTranslations::class);
258
259
        $this->crudPanel->addColumn([
260
            'name' => 'name',
261
            'type' => 'text',
262
            'tableColumn' => true,
263
        ]);
264
        $this->setupUserCreateRequest();
265
        $this->crudPanel->getRequest()->merge(['order' => [['column' => 0, 'dir' => 'asc']]]);
266
267
        $this->crudPanel->applyDatatableOrder();
268
269
        $this->assertEquals('select * from "users" order by "name" asc, "id" desc', $this->crudPanel->query->toRawSql());
270
271
    }
272
273
    public static function columnsDefaultSearchLogic()
274
    {
275
        return [
276
            ['test', 'text', 'select * from "users" where ("users"."test" like \'%test%\')'],
277
            ['test', 'email', 'select * from "users" where ("users"."test" like \'%test%\')'],
278
            ['test', 'textarea', 'select * from "users" where ("users"."test" like \'%test%\')'],
279
            ['2023-12-24', 'date', 'select * from "users" where (strftime(\'%Y-%m-%d\', "users"."test") = cast(\'2023-12-24\' as text))'],
280
            ['2023-12-24', 'datetime', 'select * from "users" where (strftime(\'%Y-%m-%d\', "users"."test") = cast(\'2023-12-24\' as text))'],
281
            ['test', 'select', 'select * from "users" where (exists (select * from "account_details" where "users"."id" = "account_details"."user_id" and "account_details"."nickname" like \'%test%\'))'],
282
            ['test', 'select_multiple', 'select * from "users" where (exists (select * from "articles" where "users"."id" = "articles"."user_id" and "articles"."content" like \'%test%\'))'],
283
        ];
284
    }
285
}
286