-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Merge sub properties in add_post_type_support().
#10426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 4 commits
72c5c5c
0aa95a8
0f59639
4b7c59a
2d1e260
c776613
e3bbb10
72051a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,6 +215,231 @@ public function test_post_type_supports() { | |
| $this->assertFalse( post_type_supports( 'notaposttype', 'notafeature' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that add_post_type_support() merges array arguments on subsequent calls. | ||
| * | ||
| * @ticket 64156 | ||
| */ | ||
| public function test_add_post_type_support_merges_array_arguments() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Also add tests for theme features merging non-associative arrays. |
||
| register_post_type( 'foo' ); | ||
|
|
||
| // First call with array arguments. | ||
| add_post_type_support( | ||
| 'foo', | ||
| 'editor', | ||
| array( | ||
| 'default-mode' => 'template-locked', | ||
| ) | ||
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertIsArray( $support['editor'] ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The best practice in core is to add custom assertion messages (the last argument) when a test contains numerous assertions. Makes it much easier to debug a failing test and figuring out which assertion is actually failing. |
||
| $this->assertArrayHasKey( 0, $support['editor'] ); | ||
| $this->assertIsArray( $support['editor'][0] ); | ||
| $this->assertSame( 'template-locked', $support['editor'][0]['default-mode'] ); | ||
|
|
||
| // Second call with different array arguments should merge. | ||
| add_post_type_support( | ||
| 'foo', | ||
| 'editor', | ||
| array( | ||
| 'block-comments' => true, | ||
| ) | ||
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertIsArray( $support['editor'] ); | ||
| $this->assertArrayHasKey( 0, $support['editor'] ); | ||
| $this->assertIsArray( $support['editor'][0] ); | ||
| $this->assertSame( 'template-locked', $support['editor'][0]['default-mode'] ); | ||
| $this->assertTrue( $support['editor'][0]['block-comments'] ); | ||
|
|
||
| // Third call with yet another property should merge with both previous. | ||
| add_post_type_support( | ||
| 'foo', | ||
| 'editor', | ||
| array( | ||
| 'another-option' => 'test-value', | ||
| ) | ||
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertSame( 'template-locked', $support['editor'][0]['default-mode'] ); | ||
| $this->assertTrue( $support['editor'][0]['block-comments'] ); | ||
| $this->assertSame( 'test-value', $support['editor'][0]['another-option'] ); | ||
|
|
||
| _unregister_post_type( 'foo' ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noting that this code will never run if any of the above assertions fails. Best to move this to |
||
| } | ||
|
|
||
| /** | ||
| * Tests that add_post_type_support() overwrites values when called with the same key. | ||
| * | ||
| * @ticket 64156 | ||
| */ | ||
| public function test_add_post_type_support_overwrites_same_key() { | ||
| register_post_type( 'foo' ); | ||
|
|
||
| add_post_type_support( | ||
| 'foo', | ||
| 'editor', | ||
| array( | ||
| 'default-mode' => 'template-locked', | ||
| ) | ||
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertSame( 'template-locked', $support['editor'][0]['default-mode'] ); | ||
|
|
||
| // Calling with same key but different value should overwrite. | ||
| add_post_type_support( | ||
| 'foo', | ||
| 'editor', | ||
| array( | ||
| 'default-mode' => 'unlocked', | ||
| ) | ||
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertSame( 'unlocked', $support['editor'][0]['default-mode'] ); | ||
|
|
||
| _unregister_post_type( 'foo' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests backwards compatibility: calling add_post_type_support() without args still works. | ||
| * | ||
| * @ticket 64156 | ||
| */ | ||
| public function test_add_post_type_support_without_args() { | ||
| register_post_type( 'foo' ); | ||
|
|
||
| add_post_type_support( 'foo', 'custom-fields' ); | ||
|
|
||
| $this->assertTrue( post_type_supports( 'foo', 'custom-fields' ) ); | ||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertTrue( $support['custom-fields'] ); | ||
|
|
||
| _unregister_post_type( 'foo' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests backwards compatibility: calling add_post_type_support() with args after | ||
| * setting it to true should overwrite with args. | ||
| * | ||
| * @ticket 64156 | ||
| */ | ||
| public function test_add_post_type_support_with_args_after_true() { | ||
| register_post_type( 'foo' ); | ||
|
|
||
| // First call without args sets to true. | ||
| add_post_type_support( 'foo', 'editor' ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertTrue( $support['editor'] ); | ||
|
|
||
| // Second call with args should overwrite true with args. | ||
| add_post_type_support( | ||
| 'foo', | ||
| 'editor', | ||
| array( | ||
| 'default-mode' => 'template-locked', | ||
| ) | ||
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertIsArray( $support['editor'] ); | ||
| $this->assertSame( 'template-locked', $support['editor'][0]['default-mode'] ); | ||
|
|
||
| _unregister_post_type( 'foo' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests backwards compatibility: calling add_post_type_support() without args after | ||
| * setting it with args should overwrite with true. | ||
| * | ||
| * @ticket 64156 | ||
| */ | ||
| public function test_add_post_type_support_without_args_after_array() { | ||
| register_post_type( 'foo' ); | ||
|
|
||
| // First call with args. | ||
| add_post_type_support( | ||
| 'foo', | ||
| 'editor', | ||
| array( | ||
| 'default-mode' => 'template-locked', | ||
| ) | ||
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertIsArray( $support['editor'] ); | ||
| $this->assertSame( 'template-locked', $support['editor'][0]['default-mode'] ); | ||
|
|
||
| // Second call without args should overwrite args with true. | ||
| add_post_type_support( 'foo', 'editor' ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertTrue( $support['editor'] ); | ||
|
|
||
| _unregister_post_type( 'foo' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that add_post_type_support() can add multiple features at once. | ||
| * | ||
| * @ticket 64156 | ||
| */ | ||
| public function test_add_post_type_support_multiple_features() { | ||
| register_post_type( 'foo' ); | ||
|
|
||
| add_post_type_support( 'foo', array( 'title', 'editor', 'thumbnail' ) ); | ||
|
|
||
| $this->assertTrue( post_type_supports( 'foo', 'title' ) ); | ||
| $this->assertTrue( post_type_supports( 'foo', 'editor' ) ); | ||
| $this->assertTrue( post_type_supports( 'foo', 'thumbnail' ) ); | ||
|
|
||
| _unregister_post_type( 'foo' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that add_post_type_support() works with nested array arguments. | ||
| * | ||
| * @ticket 64156 | ||
| */ | ||
| public function test_add_post_type_support_with_nested_arrays() { | ||
| register_post_type( 'foo' ); | ||
|
|
||
| add_post_type_support( | ||
| 'foo', | ||
| 'editor', | ||
| array( | ||
| 'allowed_blocks' => array( 'core/paragraph', 'core/heading' ), | ||
| ) | ||
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertIsArray( $support['editor'][0]['allowed_blocks'] ); | ||
| $this->assertContains( 'core/paragraph', $support['editor'][0]['allowed_blocks'] ); | ||
| $this->assertContains( 'core/heading', $support['editor'][0]['allowed_blocks'] ); | ||
|
|
||
| // Adding another nested array should merge. | ||
| add_post_type_support( | ||
| 'foo', | ||
| 'editor', | ||
| array( | ||
| 'disallowed_blocks' => array( 'core/code' ), | ||
| ) | ||
| ); | ||
|
|
||
| $support = get_all_post_type_supports( 'foo' ); | ||
| $this->assertIsArray( $support['editor'][0]['allowed_blocks'] ); | ||
| $this->assertIsArray( $support['editor'][0]['disallowed_blocks'] ); | ||
| $this->assertContains( 'core/paragraph', $support['editor'][0]['allowed_blocks'] ); | ||
| $this->assertContains( 'core/code', $support['editor'][0]['disallowed_blocks'] ); | ||
|
|
||
| _unregister_post_type( 'foo' ); | ||
| } | ||
|
|
||
| /** | ||
| * @ticket 21586 | ||
| * @ticket 41172 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.