-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
shared-tests.js
382 lines (353 loc) · 10.3 KB
/
shared-tests.js
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
export const jsTester = ( parse ) => () => {
describe( 'output structure', () => {
test( 'output is an array', () => {
expect( parse( '' ) ).toEqual( expect.any( Array ) );
expect( parse( 'test' ) ).toEqual( expect.any( Array ) );
expect( parse( '<!-- wp:void /-->' ) ).toEqual(
expect.any( Array )
);
expect(
parse( '<!-- wp:block --><!-- wp:inner /--><!-- /wp:block -->' )
).toEqual( expect.any( Array ) );
expect( parse( '<!-- wp:first /--><!-- wp:second /-->' ) ).toEqual(
expect.any( Array )
);
} );
test( 'parses blocks of various types', () => {
expect( parse( '<!-- wp:void /-->' )[ 0 ] ).toHaveProperty(
'blockName',
'core/void'
);
expect( parse( '<!-- wp:void {} /-->' )[ 0 ] ).toHaveProperty(
'blockName',
'core/void'
);
expect(
parse( '<!-- wp:void {"value":true} /-->' )[ 0 ]
).toHaveProperty( 'blockName', 'core/void' );
expect( parse( '<!-- wp:void {"a":{}} /-->' )[ 0 ] ).toHaveProperty(
'blockName',
'core/void'
);
expect(
parse( '<!-- wp:void { "value" : true } /-->' )[ 0 ]
).toHaveProperty( 'blockName', 'core/void' );
expect(
parse( '<!-- wp:void {\n\t"value" : true\n} /-->' )[ 0 ]
).toHaveProperty( 'blockName', 'core/void' );
expect(
parse( '<!-- wp:block --><!-- /wp:block -->' )[ 0 ]
).toHaveProperty( 'blockName', 'core/block' );
expect(
parse( '<!-- wp:block {} --><!-- /wp:block -->' )[ 0 ]
).toHaveProperty( 'blockName', 'core/block' );
expect(
parse(
'<!-- wp:block {"value":true} --><!-- /wp:block -->'
)[ 0 ]
).toHaveProperty( 'blockName', 'core/block' );
expect(
parse( '<!-- wp:block {} -->inner<!-- /wp:block -->' )[ 0 ]
).toHaveProperty( 'blockName', 'core/block' );
expect(
parse(
'<!-- wp:block {"value":{"a" : "true"}} -->inner<!-- /wp:block -->'
)[ 0 ]
).toHaveProperty( 'blockName', 'core/block' );
} );
test( 'blockName is namespaced string (except freeform)', () => {
expect( parse( 'freeform has null name' )[ 0 ] ).toHaveProperty(
'blockName',
null
);
expect( parse( '<!-- wp:more /-->' )[ 0 ] ).toHaveProperty(
'blockName',
'core/more'
);
expect( parse( '<!-- wp:core/more /-->' )[ 0 ] ).toHaveProperty(
'blockName',
'core/more'
);
expect( parse( '<!-- wp:my/more /-->' )[ 0 ] ).toHaveProperty(
'blockName',
'my/more'
);
} );
test( 'JSON attributes are key/value object', () => {
expect( parse( 'freeform has empty attrs' )[ 0 ] ).toHaveProperty(
'attrs',
{}
);
expect( parse( '<!-- wp:void /-->' )[ 0 ] ).toHaveProperty(
'attrs',
{}
);
expect( parse( '<!-- wp:void {} /-->' )[ 0 ] ).toHaveProperty(
'blockName',
'core/void'
);
expect( parse( '<!-- wp:void {} /-->' )[ 0 ] ).toHaveProperty(
'attrs',
{}
);
expect(
parse( '<!-- wp:void {"key": "value"} /-->' )[ 0 ]
).toHaveProperty( 'attrs', {
key: 'value',
} );
expect(
parse( '<!-- wp:block --><!-- /wp:block -->' )[ 0 ]
).toHaveProperty( 'attrs', {} );
expect(
parse( '<!-- wp:block {} --><!-- /wp:block -->' )[ 0 ]
).toHaveProperty( 'attrs', {} );
expect(
parse(
'<!-- wp:block {"key": "value"} --><!-- /wp:block -->'
)[ 0 ]
).toHaveProperty( 'attrs', { key: 'value' } );
} );
test( 'innerBlocks is a list', () => {
expect(
parse( 'freeform has empty innerBlocks' )[ 0 ]
).toHaveProperty( 'innerBlocks', [] );
expect( parse( '<!-- wp:void /-->' )[ 0 ] ).toHaveProperty(
'innerBlocks',
[]
);
expect(
parse( '<!-- wp:block --><!-- /wp:block -->' )[ 0 ]
).toHaveProperty( 'innerBlocks', [] );
const withInner = parse(
'<!-- wp:block --><!-- wp:inner /--><!-- /wp:block -->'
)[ 0 ];
expect( withInner ).toHaveProperty(
'innerBlocks',
expect.any( Array )
);
expect( withInner.innerBlocks ).toHaveLength( 1 );
const withTwoInner = parse(
'<!-- wp:block -->a<!-- wp:first /-->b<!-- wp:second /-->c<!-- /wp:block -->'
)[ 0 ];
expect( withTwoInner ).toHaveProperty(
'innerBlocks',
expect.any( Array )
);
expect( withTwoInner.innerBlocks ).toHaveLength( 2 );
} );
test( 'innerHTML is a string', () => {
expect( parse( 'test' )[ 0 ] ).toHaveProperty(
'innerHTML',
expect.any( String )
);
expect( parse( '<!-- wp:test /-->' )[ 0 ] ).toHaveProperty(
'innerHTML',
expect.any( String )
);
expect(
parse( '<!-- wp:test --><!-- /wp:test -->' )[ 0 ]
).toHaveProperty( 'innerHTML', expect.any( String ) );
expect(
parse( '<!-- wp:test -->test<!-- /wp:test -->' )[ 0 ]
).toHaveProperty( 'innerHTML', expect.any( String ) );
} );
} );
describe( 'generic tests', () => {
test( 'parse() accepts inputs with multiple Reusable blocks', () => {
expect(
parse(
'<!-- wp:block {"ref":313} /--><!-- wp:block {"ref":482} /-->'
)
).toEqual( [
expect.objectContaining( {
blockName: 'core/block',
attrs: { ref: 313 },
} ),
expect.objectContaining( {
blockName: 'core/block',
attrs: { ref: 482 },
} ),
] );
} );
test( 'treats void blocks and empty blocks identically', () => {
expect( parse( '<!-- wp:block /-->' ) ).toEqual(
parse( '<!-- wp:block --><!-- /wp:block -->' )
);
expect( parse( '<!-- wp:my/bus { "is": "fast" } /-->' ) ).toEqual(
parse(
'<!-- wp:my/bus { "is": "fast" } --><!-- /wp:my/bus -->'
)
);
} );
test( 'should grab HTML soup before block openers', () => {
[
'<p>Break me</p><!-- wp:block /-->',
'<p>Break me</p><!-- wp:block --><!-- /wp:block -->',
].forEach( ( input ) =>
expect( parse( input ) ).toEqual( [
expect.objectContaining( { innerHTML: '<p>Break me</p>' } ),
expect.objectContaining( {
blockName: 'core/block',
innerHTML: '',
} ),
] )
);
} );
test( 'should grab HTML soup before inner block openers', () =>
[
'<!-- wp:outer --><p>Break me</p><!-- wp:block /--><!-- /wp:outer -->',
'<!-- wp:outer --><p>Break me</p><!-- wp:block --><!-- /wp:block --><!-- /wp:outer -->',
].forEach( ( input ) =>
expect( parse( input ) ).toEqual( [
expect.objectContaining( {
innerBlocks: [
expect.objectContaining( {
blockName: 'core/block',
innerHTML: '',
} ),
],
innerHTML: '<p>Break me</p>',
} ),
] )
) );
test( 'should grab HTML soup after blocks', () =>
[
'<!-- wp:block /--><p>Break me</p>',
'<!-- wp:block --><!-- /wp:block --><p>Break me</p>',
].forEach( ( input ) =>
expect( parse( input ) ).toEqual( [
expect.objectContaining( {
blockName: 'core/block',
innerHTML: '',
} ),
expect.objectContaining( { innerHTML: '<p>Break me</p>' } ),
] )
) );
} );
describe( 'innerBlock placemarkers', () => {
test( 'innerContent exists', () => {
expect( parse( 'test' )[ 0 ] ).toHaveProperty( 'innerContent', [
'test',
] );
expect( parse( '<!-- wp:void /-->' )[ 0 ] ).toHaveProperty(
'innerContent',
[]
);
} );
test( 'innerContent contains innerHTML', () => {
expect(
parse( '<!-- wp:block -->Inner<!-- /wp:block -->' )[ 0 ]
).toHaveProperty( 'innerContent', [ 'Inner' ] );
} );
test( 'block locations become null', () => {
expect(
parse(
'<!-- wp:block --><!-- wp:void /--><!-- /wp:block -->'
)[ 0 ]
).toHaveProperty( 'innerContent', [ null ] );
} );
test( 'HTML soup appears after blocks', () => {
expect(
parse(
'<!-- wp:block --><!-- wp:void /-->After<!-- /wp:block -->'
)[ 0 ]
).toHaveProperty( 'innerContent', [ null, 'After' ] );
} );
test( 'HTML soup appears before blocks', () => {
expect(
parse(
'<!-- wp:block -->Before<!-- wp:void /--><!-- /wp:block -->'
)[ 0 ]
).toHaveProperty( 'innerContent', [ 'Before', null ] );
} );
test( 'blocks follow each other', () => {
expect(
parse(
'<!-- wp:block --><!-- wp:void /--><!-- wp:void /--><!-- /wp:block -->'
)[ 0 ]
).toHaveProperty( 'innerContent', [ null, null ] );
} );
} );
describe( 'attack vectors', () => {
test( 'really long JSON attribute sections', () => {
const length = 100000;
const as = 'a'.repeat( length );
let parsed;
expect(
() =>
( parsed = parse(
`<!-- wp:fake {"a":"${ as }"} /-->`
)[ 0 ] )
).not.toThrow();
expect( parsed.attrs.a ).toHaveLength( length );
} );
describe( 'invalid block comment syntax', () => {
test( 'extra space after void closer', () => {
let parsed;
expect(
() => ( parsed = parse( '<!-- wp:block / -->' )[ 0 ] )
).not.toThrow();
expect( parsed.blockName ).toBeNull();
} );
} );
} );
};
const hasPHP =
'test' === process.env.NODE_ENV
? ( () => {
const process = require( 'child_process' ).spawnSync(
'php',
[ '-r', 'echo 1;' ],
{
encoding: 'utf8',
}
);
return process.status === 0 && process.stdout === '1';
} )()
: false;
// Skipping if `php` isn't available to us, such as in local dev without it
// skipping preserves snapshots while commenting out or simply
// not injecting the tests prompts `jest` to remove "obsolete snapshots"
const makeTest = hasPHP
? // eslint-disable-next-line jest/valid-describe-callback, jest/valid-title
( ...args ) => describe( ...args )
: // eslint-disable-next-line jest/no-disabled-tests, jest/valid-describe-callback, jest/valid-title
( ...args ) => describe.skip( ...args );
export const phpTester = ( name, filename ) =>
makeTest(
name,
'test' === process.env.NODE_ENV
? jsTester( ( doc ) => {
const process = require( 'child_process' ).spawnSync(
'php',
[ '-f', filename ],
{
input: doc,
encoding: 'utf8',
timeout: 30 * 1000, // Abort after 30 seconds, that's too long anyway.
}
);
if ( process.status !== 0 ) {
throw new Error( process.stderr || process.stdout );
}
try {
/*
* Due to an issue with PHP's json_encode() serializing an empty associative array
* as an empty list `[]` we're manually replacing the already-encoded bit here.
*
* This is an issue with the test runner, not with the parser.
*/
return JSON.parse(
process.stdout.replace(
/"attrs":\s*\[\]/g,
'"attrs":{}'
)
);
} catch ( e ) {
throw new Error(
'failed to parse JSON:\n' + process.stdout
);
}
} )
: () => {}
);