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 ( 6cfc2c )
by Pedro
14:25
created

testItCanGetResponsiveTableFromConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

105
        $this->crudPanel->/** @scrutinizer ignore-call */ 
106
                          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...
106
107
        $this->assertTrue($this->crudPanel->getPersistentTable());
108
109
        $this->crudPanel->disablePersistentTable();
110
111
        $this->assertFalse($this->crudPanel->getPersistentTable());
112
    }
113
114
    public function testItCanGetAndSetTheResponsiveTable()
115
    {
116
        $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

116
        $this->crudPanel->/** @scrutinizer ignore-call */ 
117
                          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...
117
118
        $this->assertTrue($this->crudPanel->getResponsiveTable());
119
120
        $this->crudPanel->disableResponsiveTable();
121
122
        $this->assertFalse($this->crudPanel->getResponsiveTable());
123
    }
124
125
    public function testItCanGetPersistentTableDurationFromOperationSetting()
126
    {
127
        $this->crudPanel->setOperationSetting('persistentTableDuration', 10);
128
129
        $this->assertEquals(10, $this->crudPanel->getPersistentTableDuration());
130
    }
131
132
    public function testItCanGetPersistentTableDurantionFromConfig()
133
    {
134
        $this->assertEquals(false, $this->crudPanel->getPersistentTableDuration());
135
136
        config(['backpack.crud.operations.list.persistentTableDuration' => 10]);
137
138
        $this->assertEquals(10, $this->crudPanel->getPersistentTableDuration());
139
    }
140
141
    public function testItCanGetResponsiveTableFromConfig()
142
    {
143
        $this->assertEquals(false, $this->crudPanel->getResponsiveTable());
144
145
        config(['backpack.crud.operations.list.responsiveTable' => true]);
146
147
        $this->assertTrue($this->crudPanel->getResponsiveTable());
148
    }
149
150
    public function testItCanGetTheRenderedViewsForTheColumns()
151
    {
152
        $this->crudPanel->addColumn([
153
            'name' => 'name',
154
            'type' => 'test',
155
        ]);
156
157
        $entries = [$this->makeAUserModel()];
158
159
        $rowColumnsHtml = trim($this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0]);
160
161
        $this->assertEquals($this->expectedDefaultColumnValue, $rowColumnsHtml);
162
    }
163
164
    public function testItRendersTheDetailsRow()
165
    {
166
        $this->crudPanel->addColumn([
167
            'name' => 'name',
168
            'type' => 'test',
169
        ]);
170
171
        $this->crudPanel->setOperationSetting('detailsRow', true);
172
        $entries = [$this->makeAUserModel()];
173
174
        $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0];
175
176
        $rowColumnsHtml = str_replace($this->expectedDefaultColumnValue, '', $rowColumnsHtml);
177
178
        $this->assertStringContainsString('details-row-button', $rowColumnsHtml);
179
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
200
    public function testItRendersTheLineStackButtons()
201
    {
202
        $this->crudPanel->addColumn([
203
            'name' => 'name',
204
            'type' => 'test',
205
        ]);
206
207
        $this->crudPanel->button('test')->stack('line')->type('view')->content('backpack.theme-coreuiv2::buttons.test');
208
        $entries = [$this->makeAUserModel()];
209
210
        $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][1];
211
212
        $this->assertStringContainsString('btn-secondary', $rowColumnsHtml);
213
214
    }
215
    
216
    public static function columnsDefaultSearchLogic()
217
    {
218
        return [
219
            ['test', 'text', 'select * from "users" where ("users"."test" like \'%test%\')'],
220
            ['test', 'email', 'select * from "users" where ("users"."test" like \'%test%\')'],
221
            ['test', 'textarea', 'select * from "users" where ("users"."test" like \'%test%\')'],
222
            ['2023-12-24', 'date', 'select * from "users" where (strftime(\'%Y-%m-%d\', "users"."test") = cast(\'2023-12-24\' as text))'],
223
            ['2023-12-24', 'datetime', 'select * from "users" where (strftime(\'%Y-%m-%d\', "users"."test") = cast(\'2023-12-24\' as text))'],
224
            ['test', 'select', 'select * from "users" where (exists (select * from "account_details" where "users"."id" = "account_details"."user_id" and "account_details"."nickname" like \'%test%\'))'],
225
            ['test', 'select_multiple', 'select * from "users" where (exists (select * from "articles" where "users"."id" = "articles"."user_id" and "articles"."content" like \'%test%\'))']
226
        ];
227
    }
228
}
229