Passed
Branch develop (bae466)
by Paul
06:21
created

ConditionsTest   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 297
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 192
dl 0
loc 297
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testConditionContains() 0 16 2
A testConditionOverridesRequired() 0 26 1
A testConditionNotWithMultiField() 0 21 2
A testConditionContainsWithMultiField() 0 17 2
A testConditionLess() 0 17 2
A testConditionEquals() 0 30 3
A field() 0 3 1
A overrideConfig() 0 6 1
A testMultiFieldConditionOverridesRequired() 0 38 1
A restoreConfig() 0 7 3
A testConditionEqualsWithMultiField() 0 19 2
A testCondition() 0 6 1
A testConditionNot() 0 30 3
A testConditionGreater() 0 19 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Tests;
4
5
use GeminiLabs\SiteReviews\Contracts\FieldContract;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Modules\Html\FieldCondition;
8
use GeminiLabs\SiteReviews\Modules\Html\ReviewForm;
9
use GeminiLabs\SiteReviews\Modules\Validator\DefaultValidator;
10
use GeminiLabs\SiteReviews\Request;
11
12
class ConditionsTest extends \WP_UnitTestCase
0 ignored issues
show
Bug introduced by
The type WP_UnitTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
{
14
    use Setup;
15
16
    protected array $required;
17
    protected $config;
18
19
    public function testConditionContains()
20
    {
21
        $field = $this->field([
22
            'type' => 'text',
23
            'value' => 'foo bar',
24
        ]);
25
        $values = [
26
            '' => true,
27
            'foo' => true,
28
            'bar' => true,
29
            'foo bar' => true,
30
            'foo,bar' => false,
31
            'foobar' => false,
32
        ];
33
        foreach ($values as $value => $expected) {
34
            $this->assertTrue($expected === $this->testCondition($field, 'contains', $value));
35
        }
36
    }
37
38
    public function testConditionContainsWithMultiField()
39
    {
40
        $field = $this->field([
41
            'type' => 'select',
42
            'multiple' => true,
43
            'options' => ['Foo', 'Bar', 'Abc', 'Xyz'],
44
            'value' => ['Foo', 'Bar'],
45
        ]);
46
        $values = [
47
            '' => true,
48
            'Foo' => true,
49
            'Bar' => true,
50
            'Abc, Foo' => true,
51
            'Abc' => false,
52
        ];
53
        foreach ($values as $value => $expected) {
54
            $this->assertTrue($expected === $this->testCondition($field, 'contains', $value));
55
        }
56
    }
57
58
    public function testConditionEquals()
59
    {
60
        $field = $this->field([
61
            'type' => 'text',
62
            'value' => 'foo bar',
63
        ]);
64
        $values = [
65
            '' => false,
66
            'foo' => false,
67
            'bar' => false,
68
            'foo bar' => true,
69
        ];
70
        foreach ($values as $value => $expected) {
71
            $this->assertTrue($expected === $this->testCondition($field, 'equals', $value));
72
        }
73
        $field = $this->field([
74
            'type' => 'rating',
75
            'value' => 5,
76
        ]);
77
        $values = [
78
            '' => false,
79
            0 => false,
80
            '0' => false,
81
            4 => false,
82
            '4' => false,
83
            5 => true,
84
            '5' => true,
85
        ];
86
        foreach ($values as $value => $expected) {
87
            $this->assertTrue($expected === $this->testCondition($field, 'equals', $value));
88
        }
89
    }
90
91
    public function testConditionEqualsWithMultiField()
92
    {
93
        $field = $this->field([
94
            'type' => 'select',
95
            'multiple' => true,
96
            'options' => ['Foo', 'Bar', 'Abc', 'Xyz'],
97
            'value' => ['Foo', 'Bar'],
98
        ]);
99
        $values = [
100
            '' => false,
101
            'Foo' => false,
102
            'Bar' => false,
103
            'Abc, Foo' => false,
104
            'Foo,Bar' => true,
105
            'Foo, Bar' => true,
106
            'Bar, Foo' => true,
107
        ];
108
        foreach ($values as $value => $expected) {
109
            $this->assertTrue($expected === $this->testCondition($field, 'equals', $value));
110
        }
111
    }
112
113
    public function testConditionGreater()
114
    {
115
        $field = $this->field([
116
            'type' => 'rating',
117
            'value' => 3,
118
        ]);
119
        $values = [
120
            '' => true,
121
            '0' => true,
122
            '1' => true,
123
            '4' => false,
124
            '5' => false,
125
            0 => true,
126
            1 => true,
127
            4 => false,
128
            5 => false,
129
        ];
130
        foreach ($values as $value => $expected) {
131
            $this->assertTrue($expected === $this->testCondition($field, 'greater', $value));
132
        }
133
    }
134
135
    public function testConditionLess()
136
    {
137
        $field = $this->field([
138
            'type' => 'rating',
139
            'value' => 3,
140
        ]);
141
        $values = [
142
            '' => false,
143
            '0' => false,
144
            '4' => true,
145
            '5' => true,
146
            0 => false,
147
            4 => true,
148
            5 => true,
149
        ];
150
        foreach ($values as $value => $expected) {
151
            $this->assertTrue($expected === $this->testCondition($field, 'less', $value));
152
        }
153
    }
154
155
    public function testConditionNot()
156
    {
157
        $field = $this->field([
158
            'type' => 'text',
159
            'value' => 'foo bar',
160
        ]);
161
        $values = [
162
            '' => true,
163
            'foo' => true,
164
            'bar' => true,
165
            'foo bar' => false,
166
        ];
167
        foreach ($values as $value => $expected) {
168
            $this->assertTrue($expected === $this->testCondition($field, 'not', $value));
169
        }
170
        $field = $this->field([
171
            'type' => 'rating',
172
            'value' => 5,
173
        ]);
174
        $values = [
175
            '' => true,
176
            0 => true,
177
            '0' => true,
178
            4 => true,
179
            '4' => true,
180
            5 => false,
181
            '5' => false,
182
        ];
183
        foreach ($values as $value => $expected) {
184
            $this->assertTrue($expected === $this->testCondition($field, 'not', $value));
185
        }
186
    }
187
188
    public function testConditionNotWithMultiField()
189
    {
190
        $field = $this->field([
191
            'type' => 'select',
192
            'multiple' => true,
193
            'options' => ['Foo', 'Bar', 'Abc', 'Xyz'],
194
            'value' => ['Foo', 'Bar'],
195
        ]);
196
        $values = [
197
            '' => true,
198
            'Foo' => false,
199
            'Bar' => false,
200
            'Abc, Foo' => false,
201
            'Foo,Bar' => false,
202
            'Foo, Bar' => false,
203
            'Bar, Foo' => false,
204
            'Abc' => true,
205
            'Abc, Xyz' => true,
206
        ];
207
        foreach ($values as $value => $expected) {
208
            $this->assertTrue($expected === $this->testCondition($field, 'not', $value));
209
        }
210
    }
211
212
    public function testConditionOverridesRequired()
213
    {
214
        $config = [
215
            'rating' => [
216
                'type' => 'rating',
217
                'required' => true,
218
            ],
219
            'title' => [
220
                'type' => 'text',
221
                'conditions' => 'all|rating:equals:5',
222
                'required' => true,
223
            ],
224
            'content' => [
225
                'type' => 'textarea',
226
                'conditions' => 'any|rating:equals:5',
227
                'required' => true,
228
            ],
229
        ];
230
        $this->overrideConfig($config);
231
        $request = new Request([
232
            'rating' => 1,
233
        ]);
234
        $this->assertTrue(
235
            glsr(DefaultValidator::class, compact('request'))->isValid()
236
        );
237
        $this->restoreConfig();
238
    }
239
240
    public function testMultiFieldConditionOverridesRequired()
241
    {
242
        $config = [
243
            'rating' => [
244
                'type' => 'rating',
245
                'required' => true,
246
            ],
247
            'title' => [
248
                'type' => 'text',
249
                'conditions' => 'any|rating:equals:5|foo:equals:Bar',
250
                'required' => true,
251
            ],
252
            'foo' => [
253
                'type' => 'checkbox',
254
                'options' => [
255
                    'Foo' => 'Foo',
256
                    'Bar' => 'Bar',
257
                    'Xyz' => 'Xyz',
258
                ],
259
                'required' => true,
260
            ],
261
        ];
262
        $this->overrideConfig($config);
263
        $request = new Request([
264
            'rating' => 1,
265
            'foo' => 'Bar',
266
        ]);
267
        $this->assertFalse(
268
            glsr(DefaultValidator::class, compact('request'))->isValid()
269
        );
270
        $request = new Request([
271
            'rating' => 1,
272
            'foo' => 'Xyz',
273
        ]);
274
        $this->assertTrue(
275
            glsr(DefaultValidator::class, compact('request'))->isValid()
276
        );
277
        $this->restoreConfig();
278
    }
279
280
    protected function field(array $args): FieldContract
281
    {
282
        return glsr(ReviewForm::class)->field('foobar', $args);
283
    }
284
285
    protected function overrideConfig(array $config): void
286
    {
287
        $this->config = fn () => $config;
288
        $this->required = glsr(OptionManager::class)->getArray('settings.forms.required');
289
        add_filter('site-reviews/review-form/config', $this->config, 10);
290
        glsr(OptionManager::class)->set('settings.forms.required', []);
291
    }
292
293
    protected function restoreConfig(): void
294
    {
295
        if ($this->config) {
296
            remove_filter('site-reviews/review-form/config', $this->config, 10);
297
        }
298
        if (isset($this->required)) {
299
            glsr(OptionManager::class)->set('settings.forms.required', $this->required);
300
        }
301
    }
302
303
    protected function testCondition(FieldContract $field, string $operator, $value): bool
304
    {
305
        $values = wp_parse_args(compact('operator', 'value'), [
306
            'name' => 'foobar',
307
        ]);
308
        return (new FieldCondition($values, $field))->isValid();
309
    }
310
}
311