Editor: Migrate old to the new pattern categories.

Adds a new non-public `WP_REST_Block_Patterns_Controller::migrate_pattern_categories()` method to automatically migrate existing content's pattern categories to the new ones introduced in [55098].

Old to  New
`'buttons'` to `'call-to-action'`
`'columns'` to `'text'`
`'query'`   to `'posts'`

Reference:
* Part of [https://github.com/WordPress/gutenberg/pull/46144 Gutenberg PR 46144]

Follow-up to [55098], [53152].

Props ntsekouras, annezazu, jameskoster, joen, hellofromTonya, mcsf, paaljoachim, ryelle.
Fixes #57532.

git-svn-id: https://develop.svn.wordpress.org/trunk@55125 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork
2023-01-24 14:34:10 +00:00
parent f7de6aa943
commit 2bcd6390b2
2 changed files with 99 additions and 1 deletions

View File

@@ -92,6 +92,15 @@ class Tests_REST_WpRestBlockPatternsController extends WP_Test_REST_Controller_T
'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
)
);
$test_registry->register(
'test/three',
array(
'title' => 'Pattern Three',
'categories' => array( 'test', 'buttons', 'query' ),
'content' => '<!-- wp:paragraph --><p>Three</p><!-- /wp:paragraph -->',
)
);
}
public static function wpTearDownAfterClass() {
@@ -171,6 +180,51 @@ class Tests_REST_WpRestBlockPatternsController extends WP_Test_REST_Controller_T
$this->assertSame( 403, $response->get_status() );
}
/**
* Tests the proper migration of old core pattern categories to new ones.
*
* @since 6.2.0
*
* @ticket 57532
*
* @covers WP_REST_Block_Patterns_Controller::get_items
*/
public function test_get_items_migrate_pattern_categories() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
$request['_fields'] = 'name,categories';
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertIsArray( $data, 'WP_REST_Block_Patterns_Controller::get_items() should return an array' );
$this->assertGreaterThanOrEqual( 3, count( $data ), 'WP_REST_Block_Patterns_Controller::get_items() should return at least 3 items' );
$this->assertSame(
array(
'name' => 'test/one',
'categories' => array( 'test' ),
),
$data[0],
'WP_REST_Block_Patterns_Controller::get_items() should return test/one'
);
$this->assertSame(
array(
'name' => 'test/two',
'categories' => array( 'test' ),
),
$data[1],
'WP_REST_Block_Patterns_Controller::get_items() should return test/two'
);
$this->assertSame(
array(
'name' => 'test/three',
'categories' => array( 'test', 'call-to-action', 'posts' ),
),
$data[2],
'WP_REST_Block_Patterns_Controller::get_items() should return test/three'
);
}
/**
* @doesNotPerformAssertions
*/