@@ -215,6 +215,231 @@ public function test_post_type_supports() {
215215 $ this ->assertFalse ( post_type_supports ( 'notaposttype ' , 'notafeature ' ) );
216216 }
217217
218+ /**
219+ * Tests that add_post_type_support() merges array arguments on subsequent calls.
220+ *
221+ * @ticket 64156
222+ */
223+ public function test_add_post_type_support_merges_array_arguments () {
224+ register_post_type ( 'foo ' );
225+
226+ // First call with array arguments.
227+ add_post_type_support (
228+ 'foo ' ,
229+ 'editor ' ,
230+ array (
231+ 'default-mode ' => 'template-locked ' ,
232+ )
233+ );
234+
235+ $ support = get_all_post_type_supports ( 'foo ' );
236+ $ this ->assertIsArray ( $ support ['editor ' ] );
237+ $ this ->assertArrayHasKey ( 0 , $ support ['editor ' ] );
238+ $ this ->assertIsArray ( $ support ['editor ' ][0 ] );
239+ $ this ->assertSame ( 'template-locked ' , $ support ['editor ' ][0 ]['default-mode ' ] );
240+
241+ // Second call with different array arguments should merge.
242+ add_post_type_support (
243+ 'foo ' ,
244+ 'editor ' ,
245+ array (
246+ 'block-comments ' => true ,
247+ )
248+ );
249+
250+ $ support = get_all_post_type_supports ( 'foo ' );
251+ $ this ->assertIsArray ( $ support ['editor ' ] );
252+ $ this ->assertArrayHasKey ( 0 , $ support ['editor ' ] );
253+ $ this ->assertIsArray ( $ support ['editor ' ][0 ] );
254+ $ this ->assertSame ( 'template-locked ' , $ support ['editor ' ][0 ]['default-mode ' ] );
255+ $ this ->assertTrue ( $ support ['editor ' ][0 ]['block-comments ' ] );
256+
257+ // Third call with yet another property should merge with both previous.
258+ add_post_type_support (
259+ 'foo ' ,
260+ 'editor ' ,
261+ array (
262+ 'another-option ' => 'test-value ' ,
263+ )
264+ );
265+
266+ $ support = get_all_post_type_supports ( 'foo ' );
267+ $ this ->assertSame ( 'template-locked ' , $ support ['editor ' ][0 ]['default-mode ' ] );
268+ $ this ->assertTrue ( $ support ['editor ' ][0 ]['block-comments ' ] );
269+ $ this ->assertSame ( 'test-value ' , $ support ['editor ' ][0 ]['another-option ' ] );
270+
271+ _unregister_post_type ( 'foo ' );
272+ }
273+
274+ /**
275+ * Tests that add_post_type_support() overwrites values when called with the same key.
276+ *
277+ * @ticket 64156
278+ */
279+ public function test_add_post_type_support_overwrites_same_key () {
280+ register_post_type ( 'foo ' );
281+
282+ add_post_type_support (
283+ 'foo ' ,
284+ 'editor ' ,
285+ array (
286+ 'default-mode ' => 'template-locked ' ,
287+ )
288+ );
289+
290+ $ support = get_all_post_type_supports ( 'foo ' );
291+ $ this ->assertSame ( 'template-locked ' , $ support ['editor ' ][0 ]['default-mode ' ] );
292+
293+ // Calling with same key but different value should overwrite.
294+ add_post_type_support (
295+ 'foo ' ,
296+ 'editor ' ,
297+ array (
298+ 'default-mode ' => 'unlocked ' ,
299+ )
300+ );
301+
302+ $ support = get_all_post_type_supports ( 'foo ' );
303+ $ this ->assertSame ( 'unlocked ' , $ support ['editor ' ][0 ]['default-mode ' ] );
304+
305+ _unregister_post_type ( 'foo ' );
306+ }
307+
308+ /**
309+ * Tests backwards compatibility: calling add_post_type_support() without args still works.
310+ *
311+ * @ticket 64156
312+ */
313+ public function test_add_post_type_support_without_args () {
314+ register_post_type ( 'foo ' );
315+
316+ add_post_type_support ( 'foo ' , 'custom-fields ' );
317+
318+ $ this ->assertTrue ( post_type_supports ( 'foo ' , 'custom-fields ' ) );
319+ $ support = get_all_post_type_supports ( 'foo ' );
320+ $ this ->assertTrue ( $ support ['custom-fields ' ] );
321+
322+ _unregister_post_type ( 'foo ' );
323+ }
324+
325+ /**
326+ * Tests backwards compatibility: calling add_post_type_support() with args after
327+ * setting it to true should overwrite with args.
328+ *
329+ * @ticket 64156
330+ */
331+ public function test_add_post_type_support_with_args_after_true () {
332+ register_post_type ( 'foo ' );
333+
334+ // First call without args sets to true.
335+ add_post_type_support ( 'foo ' , 'editor ' );
336+
337+ $ support = get_all_post_type_supports ( 'foo ' );
338+ $ this ->assertTrue ( $ support ['editor ' ] );
339+
340+ // Second call with args should overwrite true with args.
341+ add_post_type_support (
342+ 'foo ' ,
343+ 'editor ' ,
344+ array (
345+ 'default-mode ' => 'template-locked ' ,
346+ )
347+ );
348+
349+ $ support = get_all_post_type_supports ( 'foo ' );
350+ $ this ->assertIsArray ( $ support ['editor ' ] );
351+ $ this ->assertSame ( 'template-locked ' , $ support ['editor ' ][0 ]['default-mode ' ] );
352+
353+ _unregister_post_type ( 'foo ' );
354+ }
355+
356+ /**
357+ * Tests backwards compatibility: calling add_post_type_support() without args after
358+ * setting it with args should overwrite with true.
359+ *
360+ * @ticket 64156
361+ */
362+ public function test_add_post_type_support_without_args_after_array () {
363+ register_post_type ( 'foo ' );
364+
365+ // First call with args.
366+ add_post_type_support (
367+ 'foo ' ,
368+ 'editor ' ,
369+ array (
370+ 'default-mode ' => 'template-locked ' ,
371+ )
372+ );
373+
374+ $ support = get_all_post_type_supports ( 'foo ' );
375+ $ this ->assertIsArray ( $ support ['editor ' ] );
376+ $ this ->assertSame ( 'template-locked ' , $ support ['editor ' ][0 ]['default-mode ' ] );
377+
378+ // Second call without args should overwrite args with true.
379+ add_post_type_support ( 'foo ' , 'editor ' );
380+
381+ $ support = get_all_post_type_supports ( 'foo ' );
382+ $ this ->assertTrue ( $ support ['editor ' ] );
383+
384+ _unregister_post_type ( 'foo ' );
385+ }
386+
387+ /**
388+ * Tests that add_post_type_support() can add multiple features at once.
389+ *
390+ * @ticket 64156
391+ */
392+ public function test_add_post_type_support_multiple_features () {
393+ register_post_type ( 'foo ' );
394+
395+ add_post_type_support ( 'foo ' , array ( 'title ' , 'editor ' , 'thumbnail ' ) );
396+
397+ $ this ->assertTrue ( post_type_supports ( 'foo ' , 'title ' ) );
398+ $ this ->assertTrue ( post_type_supports ( 'foo ' , 'editor ' ) );
399+ $ this ->assertTrue ( post_type_supports ( 'foo ' , 'thumbnail ' ) );
400+
401+ _unregister_post_type ( 'foo ' );
402+ }
403+
404+ /**
405+ * Tests that add_post_type_support() works with nested array arguments.
406+ *
407+ * @ticket 64156
408+ */
409+ public function test_add_post_type_support_with_nested_arrays () {
410+ register_post_type ( 'foo ' );
411+
412+ add_post_type_support (
413+ 'foo ' ,
414+ 'editor ' ,
415+ array (
416+ 'allowed_blocks ' => array ( 'core/paragraph ' , 'core/heading ' ),
417+ )
418+ );
419+
420+ $ support = get_all_post_type_supports ( 'foo ' );
421+ $ this ->assertIsArray ( $ support ['editor ' ][0 ]['allowed_blocks ' ] );
422+ $ this ->assertContains ( 'core/paragraph ' , $ support ['editor ' ][0 ]['allowed_blocks ' ] );
423+ $ this ->assertContains ( 'core/heading ' , $ support ['editor ' ][0 ]['allowed_blocks ' ] );
424+
425+ // Adding another nested array should merge.
426+ add_post_type_support (
427+ 'foo ' ,
428+ 'editor ' ,
429+ array (
430+ 'disallowed_blocks ' => array ( 'core/code ' ),
431+ )
432+ );
433+
434+ $ support = get_all_post_type_supports ( 'foo ' );
435+ $ this ->assertIsArray ( $ support ['editor ' ][0 ]['allowed_blocks ' ] );
436+ $ this ->assertIsArray ( $ support ['editor ' ][0 ]['disallowed_blocks ' ] );
437+ $ this ->assertContains ( 'core/paragraph ' , $ support ['editor ' ][0 ]['allowed_blocks ' ] );
438+ $ this ->assertContains ( 'core/code ' , $ support ['editor ' ][0 ]['disallowed_blocks ' ] );
439+
440+ _unregister_post_type ( 'foo ' );
441+ }
442+
218443 /**
219444 * @ticket 21586
220445 * @ticket 41172
0 commit comments