| | 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', new Basic_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', new Basic_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_value() { |
| | 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 | $this->assertEquals( [ 'project' => 'WordPress' ], $data['meta']['object'] ); |
| | 1650 | } |
| | 1651 | |
| | 1652 | /** |
| | 1653 | * @ticket 43392 |
| | 1654 | */ |
| | 1655 | public function test_updating_meta_to_null_for_key_with_existing_php_object_does_not_delete_meta_value() { |
| | 1656 | $this->grant_write_permission(); |
| | 1657 | |
| | 1658 | register_post_meta( 'post', 'object', [ |
| | 1659 | 'single' => true, |
| | 1660 | 'type' => 'object', |
| | 1661 | 'show_in_rest' => [ |
| | 1662 | 'schema' => [ |
| | 1663 | 'type' => 'object', |
| | 1664 | 'properties' => [ |
| | 1665 | 'project' => [ |
| | 1666 | 'type' => 'string', |
| | 1667 | ], |
| | 1668 | ], |
| | 1669 | ], |
| | 1670 | ], |
| | 1671 | ] ); |
| | 1672 | |
| | 1673 | update_post_meta( self::$post_id, 'object', new Basic_Object( [ 'project' => 'WordPress' ] ) ); |
| | 1674 | |
| | 1675 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1676 | $request->set_body_params( [ |
| | 1677 | 'meta' => [ |
| | 1678 | 'object' => null, |
| | 1679 | ] |
| | 1680 | ] ); |
| | 1681 | |
| | 1682 | $response = rest_get_server()->dispatch( $request ); |
| | 1683 | $this->assertEquals( 500, $response->get_status() ); |
| | 1684 | } |
| | 1685 | |
| | 1686 | /** |
| | 1687 | * @ticket 43392 |
| | 1688 | */ |
| | 1689 | public function test_updating_non_single_meta_to_null_for_key_with_existing_php_object_does_not_set_meta_value_to_null() { |
| | 1690 | $this->grant_write_permission(); |
| | 1691 | |
| | 1692 | register_post_meta( 'post', 'object', [ |
| | 1693 | 'single' => false, |
| | 1694 | 'type' => 'object', |
| | 1695 | 'show_in_rest' => [ |
| | 1696 | 'schema' => [ |
| | 1697 | 'type' => 'object', |
| | 1698 | 'properties' => [ |
| | 1699 | 'project' => [ |
| | 1700 | 'type' => 'string', |
| | 1701 | ], |
| | 1702 | ], |
| | 1703 | ], |
| | 1704 | ], |
| | 1705 | ] ); |
| | 1706 | |
| | 1707 | add_post_meta( self::$post_id, 'object', [ 'project' => 'bbPress' ] ); |
| | 1708 | add_post_meta( self::$post_id, 'object', new Basic_Object( [ 'project' => 'WordPress' ] ) ); |
| | 1709 | |
| | 1710 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1711 | $request->set_body_params( [ |
| | 1712 | 'meta' => [ |
| | 1713 | 'object' => [ |
| | 1714 | [ 'project' => 'BuddyPress' ], |
| | 1715 | null, |
| | 1716 | ], |
| | 1717 | ] |
| | 1718 | ] ); |
| | 1719 | |
| | 1720 | $response = rest_get_server()->dispatch( $request ); |
| | 1721 | $this->assertEquals( 500, $response->get_status() ); |
| | 1722 | } |
| | 1723 | |
| | 1724 | /** |
| | 1725 | * @ticket 43392 |
| | 1726 | */ |
| | 1727 | public function test_object_rejects_additional_properties_by_default() { |
| | 1728 | $this->grant_write_permission(); |
| | 1729 | |
| | 1730 | register_post_meta( 'post', 'object', [ |
| | 1731 | 'single' => true, |
| | 1732 | 'type' => 'object', |
| | 1733 | 'show_in_rest' => [ |
| | 1734 | 'schema' => [ |
| | 1735 | 'type' => 'object', |
| | 1736 | 'properties' => [ |
| | 1737 | 'project' => [ |
| | 1738 | 'type' => 'string', |
| | 1739 | ], |
| | 1740 | ], |
| | 1741 | ], |
| | 1742 | ], |
| | 1743 | ] ); |
| | 1744 | |
| | 1745 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1746 | $request->set_body_params( [ |
| | 1747 | 'meta' => [ |
| | 1748 | 'object' => [ |
| | 1749 | 'project' => 'BuddyPress', |
| | 1750 | 'awesomeness' => 100, |
| | 1751 | ], |
| | 1752 | ] |
| | 1753 | ] ); |
| | 1754 | |
| | 1755 | $response = rest_get_server()->dispatch( $request ); |
| | 1756 | $this->assertEquals( 400, $response->get_status() ); |
| | 1757 | } |
| | 1758 | |
| | 1759 | /** |
| | 1760 | * @ticket 43392 |
| | 1761 | */ |
| | 1762 | public function test_object_allows_additional_properties_if_explicitly_set() { |
| | 1763 | $this->grant_write_permission(); |
| | 1764 | |
| | 1765 | $value = [ |
| | 1766 | 'project' => 'BuddyPress', |
| | 1767 | 'awesomeness' => 100, |
| | 1768 | ]; |
| | 1769 | |
| | 1770 | register_post_meta( 'post', 'object', [ |
| | 1771 | 'single' => true, |
| | 1772 | 'type' => 'object', |
| | 1773 | 'show_in_rest' => [ |
| | 1774 | 'schema' => [ |
| | 1775 | 'type' => 'object', |
| | 1776 | 'additionalProperties' => true, |
| | 1777 | 'properties' => [ |
| | 1778 | 'project' => [ |
| | 1779 | 'type' => 'string', |
| | 1780 | ], |
| | 1781 | ], |
| | 1782 | ], |
| | 1783 | ], |
| | 1784 | ] ); |
| | 1785 | |
| | 1786 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1787 | $request->set_body_params( [ |
| | 1788 | 'meta' => [ |
| | 1789 | 'object' => $value, |
| | 1790 | ] |
| | 1791 | ] ); |
| | 1792 | |
| | 1793 | $response = rest_get_server()->dispatch( $request ); |
| | 1794 | $this->assertEquals( 200, $response->get_status() ); |
| | 1795 | $this->assertEquals( $value, $response->get_data()['meta']['object'] ); |
| | 1796 | |
| | 1797 | $this->assertEquals( $value, get_post_meta( self::$post_id, 'object', true ) ); |
| | 1798 | } |
| | 1799 | |
| | 1800 | /** |
| | 1801 | * @ticket 43392 |
| | 1802 | */ |
| | 1803 | public function test_object_allows_additional_properties_and_uses_its_schema() { |
| | 1804 | $this->grant_write_permission(); |
| | 1805 | |
| | 1806 | $value = [ |
| | 1807 | 'project' => 'BuddyPress', |
| | 1808 | 'awesomeness' => 'fabulous', |
| | 1809 | ]; |
| | 1810 | |
| | 1811 | register_post_meta( 'post', 'object', [ |
| | 1812 | 'single' => true, |
| | 1813 | 'type' => 'object', |
| | 1814 | 'show_in_rest' => [ |
| | 1815 | 'schema' => [ |
| | 1816 | 'type' => 'object', |
| | 1817 | 'additionalProperties' => [ |
| | 1818 | 'type' => 'number', |
| | 1819 | ], |
| | 1820 | 'properties' => [ |
| | 1821 | 'project' => [ |
| | 1822 | 'type' => 'string', |
| | 1823 | ], |
| | 1824 | ], |
| | 1825 | ], |
| | 1826 | ], |
| | 1827 | ] ); |
| | 1828 | |
| | 1829 | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1830 | $request->set_body_params( [ |
| | 1831 | 'meta' => [ |
| | 1832 | 'object' => $value, |
| | 1833 | ] |
| | 1834 | ] ); |
| | 1835 | |
| | 1836 | $response = rest_get_server()->dispatch( $request ); |
| | 1837 | $this->assertEquals( 400, $response->get_status() ); |
| | 1838 | } |
| | 1839 | |
| | 1840 | public function test_invalid_meta_value_are_set_to_null_in_response() { |
| | 1841 | register_post_meta( 'post', 'email', [ |
| | 1842 | 'single' => true, |
| | 1843 | 'type' => 'string', |
| | 1844 | 'show_in_rest' => [ |
| | 1845 | 'schema' => [ |
| | 1846 | 'type' => 'string', |
| | 1847 | 'format' => 'email', |
| | 1848 | ], |
| | 1849 | ], |
| | 1850 | ] ); |
| | 1851 | |
| | 1852 | update_post_meta( self::$post_id, 'email', 'invalid_meta_value' ); |
| | 1853 | |
| | 1854 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1855 | $response = rest_get_server()->dispatch( $request ); |
| | 1856 | |
| | 1857 | $this->assertNull( $response->get_data()['meta']['email'] ); |
| | 1858 | } |
| | 1859 | |
| | 1860 | public function test_meta_values_are_not_set_to_null_in_response_if_type_safe() { |
| | 1861 | register_post_meta( 'post', 'boolean', [ |
| | 1862 | 'single' => true, |
| | 1863 | 'show_in_rest' => true, |
| | 1864 | 'type' => 'boolean', |
| | 1865 | ] ); |
| | 1866 | |
| | 1867 | update_post_meta( self::$post_id, 'boolean', 'true' ); |
| | 1868 | |
| | 1869 | $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); |
| | 1870 | $response = rest_get_server()->dispatch( $request ); |
| | 1871 | |
| | 1872 | $this->assertTrue( $response->get_data()['meta']['boolean'] ); |
| | 1873 | } |
| | 1874 | |