Skip to content

Commit d1bd61e

Browse files
committed
Tests: Cover font collection response preparation.
Verify collection data, REST links, and requested-field filtering in `WP_REST_Font_Collections_Controller::prepare_item_for_response()`. Use a nonempty font-family fixture to detect missing response content. Developed in: #14697 Props devmuhib, shailu25, huzaifaalmesbah. See #40538. git-svn-id: https://develop.svn.wordpress.org/trunk@65107 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 900283f commit d1bd61e

1 file changed

Lines changed: 92 additions & 2 deletions

File tree

‎tests/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,100 @@ public function test_delete_item() {
322322
}
323323

324324
/**
325-
* @doesNotPerformAssertions
325+
* @ticket 40538
326+
*
327+
* @covers WP_REST_Font_Collections_Controller::prepare_item_for_response
326328
*/
327329
public function test_prepare_item() {
328-
// Controller does not use test_prepare_item().
330+
$font_families = array(
331+
array(
332+
'font_family_settings' => array(
333+
'name' => 'Open Sans',
334+
'slug' => 'open-sans',
335+
'fontFamily' => '"Open Sans", sans-serif',
336+
'preview' => 'https://example.com/open-sans.png',
337+
'fontFace' => array(
338+
array(
339+
'fontFamily' => 'Open Sans',
340+
'fontStyle' => 'normal',
341+
'fontWeight' => '400',
342+
'src' => 'https://example.com/open-sans.woff2',
343+
),
344+
),
345+
),
346+
'categories' => array( 'sans-serif' ),
347+
),
348+
);
349+
350+
wp_register_font_collection(
351+
'test-prepare-item',
352+
array(
353+
'name' => 'Test Collection',
354+
'description' => 'Test description',
355+
'font_families' => $font_families,
356+
'categories' => array(
357+
array(
358+
'name' => 'Sans Serif',
359+
'slug' => 'sans-serif',
360+
),
361+
),
362+
)
363+
);
364+
365+
try {
366+
$collection = WP_Font_Library::get_instance()->get_font_collection( 'test-prepare-item' );
367+
$controller = new WP_REST_Font_Collections_Controller();
368+
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections/test-prepare-item' );
369+
$response = $controller->prepare_item_for_response( $collection, $request );
370+
} finally {
371+
wp_unregister_font_collection( 'test-prepare-item' );
372+
}
373+
374+
$this->assertInstanceOf( WP_REST_Response::class, $response );
375+
$this->assertSame( 200, $response->get_status() );
376+
377+
$data = $response->get_data();
378+
$this->assertSame( 'test-prepare-item', $data['slug'] );
379+
$this->assertSame( 'Test Collection', $data['name'] );
380+
$this->assertSame( 'Test description', $data['description'] );
381+
$this->assertSame( $font_families, $data['font_families'] );
382+
$this->assertSame(
383+
array(
384+
array(
385+
'name' => 'Sans Serif',
386+
'slug' => 'sans-serif',
387+
),
388+
),
389+
$data['categories']
390+
);
391+
392+
$links = $response->get_links();
393+
$this->assertArrayHasKey( 'self', $links );
394+
$this->assertSame( rest_url( 'wp/v2/font-collections/test-prepare-item' ), $links['self'][0]['href'] );
395+
$this->assertArrayHasKey( 'collection', $links );
396+
$this->assertSame( rest_url( 'wp/v2/font-collections' ), $links['collection'][0]['href'] );
397+
}
398+
399+
/**
400+
* @ticket 40538
401+
*
402+
* @covers WP_REST_Font_Collections_Controller::prepare_item_for_response
403+
*/
404+
public function test_prepare_item_limit_fields() {
405+
$collection = WP_Font_Library::get_instance()->get_font_collection( 'mock-col-slug' );
406+
$controller = new WP_REST_Font_Collections_Controller();
407+
$request = new WP_REST_Request( 'GET', '/wp/v2/font-collections/mock-col-slug' );
408+
$request->set_param( '_fields', 'slug,name' );
409+
410+
$response = $controller->prepare_item_for_response( $collection, $request );
411+
$this->assertSame(
412+
array(
413+
'slug' => 'mock-col-slug',
414+
'name' => 'My collection',
415+
),
416+
$response->get_data()
417+
);
418+
$this->assertEmpty( $response->get_links() );
329419
}
330420

331421
public function test_get_item_schema() {

0 commit comments

Comments
 (0)