| | 1350 | /** |
| | 1351 | * @ticket 43392 |
| | 1352 | */ |
| | 1353 | public function test_object_single() { |
| | 1354 | $this->grant_write_permission(); |
| | 1355 | |
| | 1356 | register_post_meta( 'post', 'object', [ |
| | 1357 | 'single' => true, |
| | 1358 | 'type' => 'object', |
| | 1359 | 'show_in_rest' => [ |
| | 1360 | 'schema' => [ |
| | 1361 | 'type' => 'object', |
| | 1362 | 'properties' => [ |
| | 1363 | 'project' => [ |
| | 1364 | 'type' => 'string', |
| | 1365 | ], |
| | 1366 | ], |
| | 1367 | ], |
| | 1368 | ], |
| | 1369 | ] ); |
| | 1370 | |
| | 1371 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1372 | $request->set_body_params( [ |
| | 1373 | 'meta' => [ |
| | 1374 | 'object' => [ |
| | 1375 | 'project' => 'WordPress', |
| | 1376 | ], |
| | 1377 | ], |
| | 1378 | ] ); |
| | 1379 | |
| | 1380 | $response = rest_get_server()->dispatch( $request ); |
| | 1381 | $data = $response->get_data(); |
| | 1382 | |
| | 1383 | $this->assertArrayHasKey( 'object', $data['meta'] ); |
| | 1384 | $this->assertArrayHasKey( 'project', $data['meta']['object'] ); |
| | 1385 | $this->assertEquals( 'WordPress', $data['meta']['object']['project'] ); |
| | 1386 | |
| | 1387 | $meta = get_post_meta( self::$post_id, 'object', true ); |
| | 1388 | $this->assertArrayHasKey( 'project', $meta ); |
| | 1389 | $this->assertEquals( 'WordPress', $meta['project'] ); |
| | 1390 | } |
| | 1391 | |
| | 1392 | /** |
| | 1393 | * @ticket 43392 |
| | 1394 | */ |
| | 1395 | public function test_object_multiple() { |
| | 1396 | $this->grant_write_permission(); |
| | 1397 | |
| | 1398 | register_post_meta( 'post', 'object', [ |
| | 1399 | 'single' => false, |
| | 1400 | 'type' => 'object', |
| | 1401 | 'show_in_rest' => [ |
| | 1402 | 'schema' => [ |
| | 1403 | 'type' => 'object', |
| | 1404 | 'properties' => [ |
| | 1405 | 'project' => [ |
| | 1406 | 'type' => 'string', |
| | 1407 | ], |
| | 1408 | ], |
| | 1409 | ], |
| | 1410 | ], |
| | 1411 | ] ); |
| | 1412 | |
| | 1413 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1414 | $request->set_body_params( [ |
| | 1415 | 'meta' => [ |
| | 1416 | 'object' => [ |
| | 1417 | [ |
| | 1418 | 'project' => 'WordPress', |
| | 1419 | ], |
| | 1420 | [ |
| | 1421 | 'project' => 'bbPress', |
| | 1422 | ], |
| | 1423 | ], |
| | 1424 | ], |
| | 1425 | ] ); |
| | 1426 | |
| | 1427 | $response = rest_get_server()->dispatch( $request ); |
| | 1428 | $data = $response->get_data(); |
| | 1429 | |
| | 1430 | $this->assertArrayHasKey( 'object', $data['meta'] ); |
| | 1431 | $this->assertCount( 2, $data['meta']['object'] ); |
| | 1432 | |
| | 1433 | $this->assertArrayHasKey( 'project', $data['meta']['object'][0] ); |
| | 1434 | $this->assertEquals( 'WordPress', $data['meta']['object'][0]['project'] ); |
| | 1435 | |
| | 1436 | $this->assertArrayHasKey( 'project', $data['meta']['object'][1] ); |
| | 1437 | $this->assertEquals( 'bbPress', $data['meta']['object'][1]['project'] ); |
| | 1438 | |
| | 1439 | $meta = get_post_meta( self::$post_id, 'object' ); |
| | 1440 | |
| | 1441 | $this->assertCount( 2, $meta ); |
| | 1442 | |
| | 1443 | $this->assertArrayHasKey( 'project', $meta[0] ); |
| | 1444 | $this->assertEquals( 'WordPress', $meta[0]['project'] ); |
| | 1445 | |
| | 1446 | $this->assertArrayHasKey( 'project', $meta[1] ); |
| | 1447 | $this->assertEquals( 'bbPress', $meta[1]['project'] ); |
| | 1448 | } |
| | 1449 | |
| | 1450 | /** |
| | 1451 | * @ticket 43392 |
| | 1452 | */ |
| | 1453 | public function test_array_single() { |
| | 1454 | $this->grant_write_permission(); |
| | 1455 | |
| | 1456 | register_post_meta( 'post', 'list', [ |
| | 1457 | 'single' => true, |
| | 1458 | 'type' => 'array', |
| | 1459 | 'show_in_rest' => [ |
| | 1460 | 'schema' => [ |
| | 1461 | 'type' => 'array', |
| | 1462 | 'items' => [ |
| | 1463 | 'type' => 'string', |
| | 1464 | ], |
| | 1465 | ], |
| | 1466 | ], |
| | 1467 | ] ); |
| | 1468 | |
| | 1469 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1470 | $request->set_body_params( [ |
| | 1471 | 'meta' => [ |
| | 1472 | 'list' => [ 'WordPress', 'bbPress' ], |
| | 1473 | ], |
| | 1474 | ] ); |
| | 1475 | |
| | 1476 | $response = rest_get_server()->dispatch( $request ); |
| | 1477 | $data = $response->get_data(); |
| | 1478 | |
| | 1479 | $this->assertArrayHasKey( 'list', $data['meta'] ); |
| | 1480 | $this->assertEquals( [ 'WordPress', 'bbPress' ], $data['meta']['list'] ); |
| | 1481 | |
| | 1482 | $meta = get_post_meta( self::$post_id, 'list', true ); |
| | 1483 | $this->assertEquals( [ 'WordPress', 'bbPress' ], $meta ); |
| | 1484 | } |
| | 1485 | |
| | 1486 | /** |
| | 1487 | * @ticket 43392 |
| | 1488 | */ |
| | 1489 | public function test_array_of_objects_multiple() { |
| | 1490 | $this->grant_write_permission(); |
| | 1491 | |
| | 1492 | register_post_meta( 'post', 'list_of_objects', [ |
| | 1493 | 'single' => false, |
| | 1494 | 'type' => 'array', |
| | 1495 | 'show_in_rest' => [ |
| | 1496 | 'schema' => [ |
| | 1497 | 'type' => 'array', |
| | 1498 | 'items' => [ |
| | 1499 | 'type' => 'object', |
| | 1500 | 'properties' => [ |
| | 1501 | 'version' => [ |
| | 1502 | 'type' => 'string', |
| | 1503 | ], |
| | 1504 | 'artist' => [ |
| | 1505 | 'type' => 'string', |
| | 1506 | ] |
| | 1507 | ] |
| | 1508 | ], |
| | 1509 | ], |
| | 1510 | ], |
| | 1511 | ] ); |
| | 1512 | |
| | 1513 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1514 | $request->set_body_params( [ |
| | 1515 | 'meta' => [ |
| | 1516 | 'list_of_objects' => [ |
| | 1517 | // Meta 1 |
| | 1518 | [ |
| | 1519 | [ 'version' => '5.2', 'artist' => 'Jaco' ], |
| | 1520 | [ 'version' => '5.1', 'artist' => 'Betty' ], |
| | 1521 | ], |
| | 1522 | // Meta 2 |
| | 1523 | [ |
| | 1524 | [ 'version' => '4.9', 'artist' => 'Tipton' ], |
| | 1525 | ], |
| | 1526 | ], |
| | 1527 | ], |
| | 1528 | ] ); |
| | 1529 | |
| | 1530 | |
| | 1531 | $response = rest_get_server()->dispatch( $request ); |
| | 1532 | $data = $response->get_data(); |
| | 1533 | |
| | 1534 | $this->assertArrayHasKey( 'list_of_objects', $data['meta'] ); |
| | 1535 | $this->assertCount( 2, $data['meta']['list_of_objects'] ); |
| | 1536 | |
| | 1537 | $this->assertEquals( [ |
| | 1538 | [ 'version' => '5.2', 'artist' => 'Jaco' ], |
| | 1539 | [ 'version' => '5.1', 'artist' => 'Betty' ], |
| | 1540 | ], $data['meta']['list_of_objects'][0] ); |
| | 1541 | |
| | 1542 | $this->assertEquals( [ |
| | 1543 | [ 'version' => '4.9', 'artist' => 'Tipton' ], |
| | 1544 | ], $data['meta']['list_of_objects'][1] ); |
| | 1545 | |
| | 1546 | $meta = get_post_meta( self::$post_id, 'list_of_objects' ); |
| | 1547 | |
| | 1548 | $this->assertCount( 2, $meta ); |
| | 1549 | |
| | 1550 | $this->assertEquals( [ |
| | 1551 | [ 'version' => '5.2', 'artist' => 'Jaco' ], |
| | 1552 | [ 'version' => '5.1', 'artist' => 'Betty' ], |
| | 1553 | ], $meta[0] ); |
| | 1554 | |
| | 1555 | $this->assertEquals( [ |
| | 1556 | [ 'version' => '4.9', 'artist' => 'Tipton' ], |
| | 1557 | ], $meta[1] ); |
| | 1558 | } |
| | 1559 | |
| | 1560 | /** |
| | 1561 | * @ticket 43392 |
| | 1562 | */ |
| | 1563 | public function test_php_objects_returned_as_null() { |
| | 1564 | register_post_meta( 'post', 'object', [ |
| | 1565 | 'single' => true, |
| | 1566 | 'type' => 'object', |
| | 1567 | 'show_in_rest' => [ |
| | 1568 | 'schema' => [ |
| | 1569 | 'type' => 'object', |
| | 1570 | 'properties' => [ |
| | 1571 | 'project' => [ |
| | 1572 | 'type' => 'string', |
| | 1573 | ], |
| | 1574 | ], |
| | 1575 | ], |
| | 1576 | ], |
| | 1577 | ] ); |
| | 1578 | |
| | 1579 | update_post_meta( self::$post_id, 'object', (object) [ 'project' => 'WordPress' ] ); |
| | 1580 | |
| | 1581 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1582 | $response = rest_get_server()->dispatch( $request ); |
| | 1583 | $data = $response->get_data(); |
| | 1584 | |
| | 1585 | $this->assertArrayHasKey( 'object', $data['meta'] ); |
| | 1586 | $this->assertNull( $data['meta']['object'] ); |
| | 1587 | } |
| | 1588 | |
| | 1589 | /** |
| | 1590 | * @ticket 43392 |
| | 1591 | */ |
| | 1592 | public function test_php_objects_returned_as_null_multiple() { |
| | 1593 | register_post_meta( 'post', 'object', [ |
| | 1594 | 'single' => false, |
| | 1595 | 'type' => 'object', |
| | 1596 | 'show_in_rest' => [ |
| | 1597 | 'schema' => [ |
| | 1598 | 'type' => 'object', |
| | 1599 | 'properties' => [ |
| | 1600 | 'project' => [ |
| | 1601 | 'type' => 'string', |
| | 1602 | ], |
| | 1603 | ], |
| | 1604 | ], |
| | 1605 | ], |
| | 1606 | ] ); |
| | 1607 | |
| | 1608 | add_post_meta( self::$post_id, 'object', [ 'project' => 'bbPress' ] ); |
| | 1609 | add_post_meta( self::$post_id, 'object', (object) [ 'project' => 'WordPress' ] ); |
| | 1610 | |
| | 1611 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1612 | $response = rest_get_server()->dispatch( $request ); |
| | 1613 | $data = $response->get_data(); |
| | 1614 | |
| | 1615 | $this->assertArrayHasKey( 'object', $data['meta'] ); |
| | 1616 | $this->assertCount( 2, $data['meta']['object'] ); |
| | 1617 | $this->assertEquals( [ 'project' => 'bbPress' ], $data['meta']['object'][0] ); |
| | 1618 | $this->assertNull( $data['meta']['object'][1] ); |
| | 1619 | } |
| | 1620 | |
| | 1621 | /** |
| | 1622 | * @ticket 43392 |
| | 1623 | */ |
| | 1624 | public function test_php_jsonserializable_object_returns_json() { |
| | 1625 | require_once __DIR__ . '/../../includes/class-jsonserializable-object.php'; |
| | 1626 | |
| | 1627 | register_post_meta( 'post', 'object', [ |
| | 1628 | 'single' => true, |
| | 1629 | 'type' => 'object', |
| | 1630 | 'show_in_rest' => [ |
| | 1631 | 'schema' => [ |
| | 1632 | 'type' => 'object', |
| | 1633 | 'properties' => [ |
| | 1634 | 'project' => [ |
| | 1635 | 'type' => 'string', |
| | 1636 | ], |
| | 1637 | ], |
| | 1638 | ], |
| | 1639 | ], |
| | 1640 | ] ); |
| | 1641 | |
| | 1642 | update_post_meta( self::$post_id, 'object', new JsonSerializable_Object( [ 'project' => 'WordPress' ] ) ); |
| | 1643 | |
| | 1644 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1645 | $response = rest_get_server()->dispatch( $request ); |
| | 1646 | $data = $response->get_data(); |
| | 1647 | |
| | 1648 | $this->assertArrayHasKey( 'object', $data['meta'] ); |
| | 1649 | |
| | 1650 | // Object doesn't get JsonSerializable until the REST API server prints the response. |
| | 1651 | // Instead, the full object is still present at this point. |
| | 1652 | $this->assertInstanceOf( JsonSerializable_Object::class, $data['meta']['object'] ); |
| | 1653 | $this->assertEquals( [ 'project' => 'WordPress' ], $data['meta']['object']->jsonSerialize() ); |
| | 1654 | } |
| | 1655 | |
| | 1656 | /** |
| | 1657 | * @ticket 43392 |
| | 1658 | */ |
| | 1659 | public function test_updating_meta_to_null_for_key_with_existing_php_object_does_not_delete_meta_value() { |
| | 1660 | $this->grant_write_permission(); |
| | 1661 | |
| | 1662 | register_post_meta( 'post', 'object', [ |
| | 1663 | 'single' => true, |
| | 1664 | 'type' => 'object', |
| | 1665 | 'show_in_rest' => [ |
| | 1666 | 'schema' => [ |
| | 1667 | 'type' => 'object', |
| | 1668 | 'properties' => [ |
| | 1669 | 'project' => [ |
| | 1670 | 'type' => 'string', |
| | 1671 | ], |
| | 1672 | ], |
| | 1673 | ], |
| | 1674 | ], |
| | 1675 | ] ); |
| | 1676 | |
| | 1677 | update_post_meta( self::$post_id, 'object', (object) [ 'project' => 'WordPress' ] ); |
| | 1678 | |
| | 1679 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1680 | $request->set_body_params( [ |
| | 1681 | 'meta' => [ |
| | 1682 | 'object' => null, |
| | 1683 | ] |
| | 1684 | ] ); |
| | 1685 | |
| | 1686 | $response = rest_get_server()->dispatch( $request ); |
| | 1687 | $this->assertEquals( 500, $response->get_status() ); |
| | 1688 | } |
| | 1689 | |
| | 1690 | /** |
| | 1691 | * @ticket 43392 |
| | 1692 | */ |
| | 1693 | public function test_updating_non_single_meta_to_null_for_key_with_existing_php_object_does_not_set_meta_value_to_null() { |
| | 1694 | $this->grant_write_permission(); |
| | 1695 | |
| | 1696 | register_post_meta( 'post', 'object', [ |
| | 1697 | 'single' => false, |
| | 1698 | 'type' => 'object', |
| | 1699 | 'show_in_rest' => [ |
| | 1700 | 'schema' => [ |
| | 1701 | 'type' => 'object', |
| | 1702 | 'properties' => [ |
| | 1703 | 'project' => [ |
| | 1704 | 'type' => 'string', |
| | 1705 | ], |
| | 1706 | ], |
| | 1707 | ], |
| | 1708 | ], |
| | 1709 | ] ); |
| | 1710 | |
| | 1711 | add_post_meta( self::$post_id, 'object', [ 'project' => 'bbPress' ] ); |
| | 1712 | add_post_meta( self::$post_id, 'object', (object) [ 'project' => 'WordPress' ] ); |
| | 1713 | |
| | 1714 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1715 | $request->set_body_params( [ |
| | 1716 | 'meta' => [ |
| | 1717 | 'object' => [ |
| | 1718 | [ 'project' => 'BuddyPress' ], |
| | 1719 | null, |
| | 1720 | ], |
| | 1721 | ] |
| | 1722 | ] ); |
| | 1723 | |
| | 1724 | $response = rest_get_server()->dispatch( $request ); |
| | 1725 | $this->assertEquals( 400, $response->get_status() ); |
| | 1726 | } |
| | 1727 | |
| | 1728 | /** |
| | 1729 | * @ticket 43392 |
| | 1730 | */ |
| | 1731 | public function test_object_rejects_additional_properties_by_default() { |
| | 1732 | $this->grant_write_permission(); |
| | 1733 | |
| | 1734 | register_post_meta( 'post', 'object', [ |
| | 1735 | 'single' => true, |
| | 1736 | 'type' => 'object', |
| | 1737 | 'show_in_rest' => [ |
| | 1738 | 'schema' => [ |
| | 1739 | 'type' => 'object', |
| | 1740 | 'properties' => [ |
| | 1741 | 'project' => [ |
| | 1742 | 'type' => 'string', |
| | 1743 | ], |
| | 1744 | ], |
| | 1745 | ], |
| | 1746 | ], |
| | 1747 | ] ); |
| | 1748 | |
| | 1749 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1750 | $request->set_body_params( [ |
| | 1751 | 'meta' => [ |
| | 1752 | 'object' => [ |
| | 1753 | 'project' => 'BuddyPress', |
| | 1754 | 'awesomeness' => 100, |
| | 1755 | ], |
| | 1756 | ] |
| | 1757 | ] ); |
| | 1758 | |
| | 1759 | $response = rest_get_server()->dispatch( $request ); |
| | 1760 | $this->assertEquals( 400, $response->get_status() ); |
| | 1761 | } |
| | 1762 | |
| | 1763 | /** |
| | 1764 | * @ticket 43392 |
| | 1765 | */ |
| | 1766 | public function test_object_allows_additional_properties_if_explicitly_set() { |
| | 1767 | $this->grant_write_permission(); |
| | 1768 | |
| | 1769 | $value = [ |
| | 1770 | 'project' => 'BuddyPress', |
| | 1771 | 'awesomeness' => 100, |
| | 1772 | ]; |
| | 1773 | |
| | 1774 | register_post_meta( 'post', 'object', [ |
| | 1775 | 'single' => true, |
| | 1776 | 'type' => 'object', |
| | 1777 | 'show_in_rest' => [ |
| | 1778 | 'schema' => [ |
| | 1779 | 'type' => 'object', |
| | 1780 | 'additionalProperties' => true, |
| | 1781 | 'properties' => [ |
| | 1782 | 'project' => [ |
| | 1783 | 'type' => 'string', |
| | 1784 | ], |
| | 1785 | ], |
| | 1786 | ], |
| | 1787 | ], |
| | 1788 | ] ); |
| | 1789 | |
| | 1790 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1791 | $request->set_body_params( [ |
| | 1792 | 'meta' => [ |
| | 1793 | 'object' => $value, |
| | 1794 | ] |
| | 1795 | ] ); |
| | 1796 | |
| | 1797 | $response = rest_get_server()->dispatch( $request ); |
| | 1798 | $this->assertEquals( 200, $response->get_status() ); |
| | 1799 | $this->assertEquals( $value, $response->get_data()['meta']['object'] ); |
| | 1800 | |
| | 1801 | $this->assertEquals( $value, get_post_meta( self::$post_id, 'object', true ) ); |
| | 1802 | } |
| | 1803 | |
| | 1804 | /** |
| | 1805 | * @ticket 43392 |
| | 1806 | */ |
| | 1807 | public function test_object_allows_additional_properties_and_uses_its_schema() { |
| | 1808 | $this->grant_write_permission(); |
| | 1809 | |
| | 1810 | $value = [ |
| | 1811 | 'project' => 'BuddyPress', |
| | 1812 | 'awesomeness' => 'fabulous', |
| | 1813 | ]; |
| | 1814 | |
| | 1815 | register_post_meta( 'post', 'object', [ |
| | 1816 | 'single' => true, |
| | 1817 | 'type' => 'object', |
| | 1818 | 'show_in_rest' => [ |
| | 1819 | 'schema' => [ |
| | 1820 | 'type' => 'object', |
| | 1821 | 'additionalProperties' => [ |
| | 1822 | 'type' => 'number', |
| | 1823 | ], |
| | 1824 | 'properties' => [ |
| | 1825 | 'project' => [ |
| | 1826 | 'type' => 'string', |
| | 1827 | ], |
| | 1828 | ], |
| | 1829 | ], |
| | 1830 | ], |
| | 1831 | ] ); |
| | 1832 | |
| | 1833 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1834 | $request->set_body_params( [ |
| | 1835 | 'meta' => [ |
| | 1836 | 'object' => $value, |
| | 1837 | ] |
| | 1838 | ] ); |
| | 1839 | |
| | 1840 | $response = rest_get_server()->dispatch( $request ); |
| | 1841 | $this->assertEquals( 400, $response->get_status() ); |
| | 1842 | } |
| | 1843 | |