-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathpost-meta.feature
More file actions
525 lines (447 loc) · 14.8 KB
/
post-meta.feature
File metadata and controls
525 lines (447 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
Feature: Manage post custom fields
# Windows likes to include the quotes of the `"via STDIN"` string.
@skip-windows
Scenario: Postmeta CRUD
Given a WP install
When I run `wp post-meta add 1 foo 'bar'`
Then STDOUT should not be empty
When I run `wp post-meta get 1 foo`
Then STDOUT should be:
"""
bar
"""
When I try `wp post meta get 999999 foo`
Then STDERR should be:
"""
Error: Could not find the post with ID 999999.
"""
And the return code should be 1
When I run `wp post-meta set 1 foo '[ "1", "2" ]' --format=json`
Then STDOUT should not be empty
When I run the previous command again
Then STDOUT should be:
"""
Success: Value passed for custom field 'foo' is unchanged.
"""
When I run `wp post-meta get 1 foo --format=json`
Then STDOUT should be:
"""
["1","2"]
"""
When I run `echo "via STDIN" | wp post-meta set 1 foo`
And I run `wp post-meta get 1 foo`
Then STDOUT should be:
"""
via STDIN
"""
When I run `wp post-meta delete 1 foo`
Then STDOUT should not be empty
When I try `wp post-meta get 1 foo`
Then the return code should be 1
Scenario: List post meta
Given a WP install
When I run `wp post meta add 1 apple banana`
And I run `wp post meta add 1 apple banana`
Then STDOUT should not be empty
When I run `wp post meta set 1 banana '["apple", "apple"]' --format=json`
Then STDOUT should not be empty
When I run `wp post meta list 1`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
When I run `wp post meta list 1 --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | ["apple","apple"] |
When I run `wp post meta list 1 --orderby=id --order=desc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| 1 | apple | banana |
| 1 | apple | banana |
When I run `wp post meta list 1 --orderby=id --order=desc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | ["apple","apple"] |
| 1 | apple | banana |
| 1 | apple | banana |
When I run `wp post meta list 1 --orderby=meta_key --order=asc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
When I run `wp post meta list 1 --orderby=meta_key --order=asc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | ["apple","apple"] |
When I run `wp post meta list 1 --orderby=meta_key --order=desc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| 1 | apple | banana |
| 1 | apple | banana |
When I run `wp post meta list 1 --orderby=meta_key --order=desc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | ["apple","apple"] |
| 1 | apple | banana |
| 1 | apple | banana |
When I run `wp post meta list 1 --orderby=meta_value --order=asc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
When I run `wp post meta list 1 --orderby=meta_value --order=asc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | apple | banana |
| 1 | apple | banana |
| 1 | banana | ["apple","apple"] |
When I run `wp post meta list 1 --orderby=meta_value --order=desc`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | a:2:{i:0;s:5:"apple";i:1;s:5:"apple";} |
| 1 | apple | banana |
| 1 | apple | banana |
When I run `wp post meta list 1 --orderby=meta_value --order=desc --unserialize`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | banana | ["apple","apple"] |
| 1 | apple | banana |
| 1 | apple | banana |
Scenario: Delete all post meta
Given a WP install
When I run `wp post meta add 1 apple banana`
And I run `wp post meta add 1 _foo banana`
Then STDOUT should not be empty
When I run `wp post meta list 1 --format=count`
Then STDOUT should be:
"""
2
"""
When I try `wp post meta delete 1`
Then STDERR should be:
"""
Error: Please specify a meta key, or use the --all flag.
"""
And the return code should be 1
When I run `wp post meta delete 1 --all`
Then STDOUT should contain:
"""
Deleted 'apple' custom field.
Deleted '_foo' custom field.
Success: Deleted all custom fields.
"""
When I run `wp post meta list 1 --format=count`
Then STDOUT should be:
"""
0
"""
Scenario: List post meta with a null value
Given a WP install
And a setup.php file:
"""
<?php
update_post_meta( 1, 'foo', NULL );
"""
And I run `wp eval-file setup.php`
When I run `wp post meta list 1`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | foo | |
Scenario: Make sure WordPress receives the slashed data it expects in meta fields
Given a WP install
When I run `wp post-meta add 1 foo 'My\Meta'`
Then STDOUT should not be empty
When I run `wp post-meta get 1 foo`
Then STDOUT should be:
"""
My\Meta
"""
When I run `wp post-meta update 1 foo 'My\New\Meta'`
Then STDOUT should be:
"""
Success: Updated custom field 'foo'.
"""
When I run the previous command again
Then STDOUT should be:
"""
Success: Value passed for custom field 'foo' is unchanged.
"""
When I run `wp post-meta get 1 foo`
Then STDOUT should be:
"""
My\New\Meta
"""
Scenario: List post meta with or without single flag
Given a WP install
When I run `wp post meta add 1 apple banana`
And I run `wp post meta add 1 apple mango`
Then STDOUT should not be empty
When I run `wp post meta get 1 apple`
Then STDOUT should be:
"""
banana
"""
When I run `wp post meta get 1 apple --single`
Then STDOUT should be:
"""
banana
"""
When I run `wp post meta get 1 apple --no-single`
Then STDOUT should be:
"""
array (
0 => 'banana',
1 => 'mango',
)
"""
When I run `wp post meta get 1 apple --no-single --format=json`
Then STDOUT should be:
"""
["banana","mango"]
"""
@pluck
Scenario: Nested values can be retrieved.
Given a WP install
And an input.json file:
"""
{
"foo": "bar"
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
When I run `wp post meta pluck 1 meta-key foo`
Then STDOUT should be:
"""
bar
"""
@pluck @pluck-deep
Scenario: A nested value can be retrieved at any depth.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": {
"baz": "some value"
}
},
"foo.com": {
"visitors": 999
}
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
When I run `wp post meta pluck 1 meta-key foo bar baz`
Then STDOUT should be:
"""
some value
"""
When I run `wp post meta pluck 1 meta-key foo.com visitors`
Then STDOUT should be:
"""
999
"""
@pluck @pluck-fail
Scenario: Attempting to pluck a non-existent nested value fails.
Given a WP install
And I run `wp post meta set 1 meta-key '{ "key": "value" }' --format=json`
When I run `wp post meta pluck 1 meta-key key`
Then STDOUT should be:
"""
value
"""
When I try `wp post meta pluck 1 meta-key foo`
Then STDOUT should be empty
And the return code should be 1
@pluck @pluck-fail
Scenario: Attempting to pluck from a primitive value fails.
Given a WP install
And I run `wp post meta set 1 meta-key simple-value`
When I try `wp post meta pluck 1 meta-key foo`
Then STDOUT should be empty
And the return code should be 1
@pluck @pluck-numeric
Scenario: A nested value can be retrieved from an integer key.
Given a WP install
And I run `wp post meta set 1 meta-key '[ "foo", "bar" ]' --format=json`
When I run `wp post meta pluck 1 meta-key 0`
Then STDOUT should be:
"""
foo
"""
@patch @patch-update @patch-arg
Scenario: Nested values can be changed.
Given a WP install
And an input.json file:
"""
{
"foo": "bar"
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
When I run `wp post meta patch update 1 meta-key foo baz`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""
When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
{
"foo": "baz"
}
"""
@patch @patch-update @patch-stdin
Scenario: Nested values can be set with a value from STDIN.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": "baz"
},
"bar": "bad"
}
"""
And a patch file:
"""
new value
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
When I run `wp post meta patch update 1 meta-key foo bar < patch`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""
When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
{
"foo": {
"bar": "new value"
},
"bar": "bad"
}
"""
@patch @patch-update @patch-fail
Scenario: Attempting to update a nested value fails if a parent's key does not exist.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": "baz"
},
"bar": "bad"
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
When I try `wp post meta patch update 1 meta-key foo not-a-key new-value`
Then STDOUT should be empty
And STDERR should contain:
"""
No data exists for key "not-a-key"
"""
And the return code should be 1
@patch @patch-delete
Scenario: A key can be deleted from a nested value.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": "baz",
"abe": "lincoln"
}
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
When I run `wp post meta patch delete 1 meta-key foo bar`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""
When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
{
"foo": {
"abe": "lincoln"
}
}
"""
@patch @patch-fail @patch-delete @patch-delete-fail
Scenario: A key cannot be deleted from a nested value from a non-existent key.
Given a WP install
And an input.json file:
"""
{
"foo": {
"bar": "baz"
}
}
"""
And I run `wp post meta set 1 meta-key --format=json < input.json`
When I try `wp post meta patch delete 1 meta-key foo not-a-key`
Then STDOUT should be empty
And STDERR should contain:
"""
No data exists for key "not-a-key"
"""
And the return code should be 1
@patch @patch-insert
Scenario: A new key can be inserted into a nested value.
Given a WP install
And I run `wp post meta set 1 meta-key '{}' --format=json`
When I run `wp post meta patch insert 1 meta-key foo bar`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""
When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
{
"foo": "bar"
}
"""
@patch @patch-fail @patch-insert @patch-insert-fail
Scenario: A new key cannot be inserted into a non-nested value.
Given a WP install
And I run `wp post meta set 1 meta-key 'a simple value'`
When I try `wp post meta patch insert 1 meta-key foo bar`
Then STDOUT should be empty
And STDERR should contain:
"""
Cannot create key "foo"
"""
And the return code should be 1
When I run `wp post meta get 1 meta-key`
Then STDOUT should be:
"""
a simple value
"""
@patch @patch-numeric
Scenario: A nested value can be updated using an integer key.
Given a WP install
And I run `wp post meta set 1 meta-key '[ "foo", "bar" ]' --format=json`
When I run `wp post meta patch update 1 meta-key 0 new`
Then STDOUT should be:
"""
Success: Updated custom field 'meta-key'.
"""
When I run `wp post meta get 1 meta-key --format=json`
Then STDOUT should be JSON containing:
"""
[ "new", "bar" ]
"""