Make WordPress Core


Ignore:
Timestamp:
07/02/2020 05:55:04 AM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Link to the REST route for the currently queried resource.

This allows for programatically determining the REST version of the current page. The links also aid human discovery of the REST API in general.

Props dshanske, tfrommen, TimothyBlynJacobs.
Fixes #49116.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api.php

    r48121 r48273  
    13501350        );
    13511351    }
     1352
     1353    /**
     1354     * @ticket 49116
     1355     */
     1356    public function test_rest_get_route_for_post_non_post() {
     1357        $this->assertEquals( '', rest_get_route_for_post( 'garbage' ) );
     1358    }
     1359
     1360    /**
     1361     * @ticket 49116
     1362     */
     1363    public function test_rest_get_route_for_post_invalid_post_type() {
     1364        register_post_type( 'invalid' );
     1365        $post = self::factory()->post->create_and_get( array( 'post_type' => 'invalid' ) );
     1366        unregister_post_type( 'invalid' );
     1367
     1368        $this->assertEquals( '', rest_get_route_for_post( $post ) );
     1369    }
     1370
     1371    /**
     1372     * @ticket 49116
     1373     */
     1374    public function test_rest_get_route_for_post_non_rest() {
     1375        $post = self::factory()->post->create_and_get( array( 'post_type' => 'custom_css' ) );
     1376        $this->assertEquals( '', rest_get_route_for_post( $post ) );
     1377    }
     1378
     1379    /**
     1380     * @ticket 49116
     1381     */
     1382    public function test_rest_get_route_for_post_custom_controller() {
     1383        $post = self::factory()->post->create_and_get( array( 'post_type' => 'wp_block' ) );
     1384        $this->assertEquals( '', rest_get_route_for_post( $post ) );
     1385    }
     1386
     1387    /**
     1388     * @ticket 49116
     1389     */
     1390    public function test_rest_get_route_for_post() {
     1391        $post = self::factory()->post->create_and_get();
     1392        $this->assertEquals( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post ) );
     1393    }
     1394
     1395    /**
     1396     * @ticket 49116
     1397     */
     1398    public function test_rest_get_route_for_media() {
     1399        $post = self::factory()->attachment->create_and_get();
     1400        $this->assertEquals( '/wp/v2/media/' . $post->ID, rest_get_route_for_post( $post ) );
     1401    }
     1402
     1403    /**
     1404     * @ticket 49116
     1405     */
     1406    public function test_rest_get_route_for_post_id() {
     1407        $post = self::factory()->post->create_and_get();
     1408        $this->assertEquals( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post->ID ) );
     1409    }
     1410
     1411    /**
     1412     * @ticket 49116
     1413     */
     1414    public function test_rest_get_route_for_term_non_term() {
     1415        $this->assertEquals( '', rest_get_route_for_term( 'garbage' ) );
     1416    }
     1417
     1418    /**
     1419     * @ticket 49116
     1420     */
     1421    public function test_rest_get_route_for_term_invalid_term_type() {
     1422        register_taxonomy( 'invalid', 'post' );
     1423        $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'invalid' ) );
     1424        unregister_taxonomy( 'invalid' );
     1425
     1426        $this->assertEquals( '', rest_get_route_for_term( $term ) );
     1427    }
     1428
     1429    /**
     1430     * @ticket 49116
     1431     */
     1432    public function test_rest_get_route_for_term_non_rest() {
     1433        $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'post_format' ) );
     1434        $this->assertEquals( '', rest_get_route_for_term( $term ) );
     1435    }
     1436
     1437    /**
     1438     * @ticket 49116
     1439     */
     1440    public function test_rest_get_route_for_term() {
     1441        $term = self::factory()->term->create_and_get();
     1442        $this->assertEquals( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term ) );
     1443    }
     1444
     1445    /**
     1446     * @ticket 49116
     1447     */
     1448    public function test_rest_get_route_for_category() {
     1449        $term = self::factory()->category->create_and_get();
     1450        $this->assertEquals( '/wp/v2/categories/' . $term->term_id, rest_get_route_for_term( $term ) );
     1451    }
     1452
     1453    /**
     1454     * @ticket 49116
     1455     */
     1456    public function test_rest_get_route_for_term_id() {
     1457        $term = self::factory()->term->create_and_get();
     1458        $this->assertEquals( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term->term_id ) );
     1459    }
    13521460}
Note: See TracChangeset for help on using the changeset viewer.