1530 | | public function verify_user_roundtrip( $input = array(), $expected_output = array() ) { |
1531 | | if ( isset( $input['id'] ) ) { |
1532 | | // Existing user; don't try to create one |
1533 | | $user_id = $input['id']; |
1534 | | } else { |
1535 | | // Create a new user |
1536 | | $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); |
1537 | | foreach ( $input as $name => $value ) { |
1538 | | $request->set_param( $name, $value ); |
1539 | | } |
1540 | | $request->set_param( 'email', 'cbg@androidsdungeon.com' ); |
1541 | | $response = $this->server->dispatch( $request ); |
1542 | | $this->assertEquals( 201, $response->get_status() ); |
1543 | | $actual_output = $response->get_data(); |
1544 | | |
1545 | | // Compare expected API output to actual API output |
1546 | | $this->assertEquals( $expected_output['username'] , $actual_output['username'] ); |
1547 | | $this->assertEquals( $expected_output['name'] , $actual_output['name'] ); |
1548 | | $this->assertEquals( $expected_output['first_name'] , $actual_output['first_name'] ); |
1549 | | $this->assertEquals( $expected_output['last_name'] , $actual_output['last_name'] ); |
1550 | | $this->assertEquals( $expected_output['url'] , $actual_output['url'] ); |
1551 | | $this->assertEquals( $expected_output['description'], $actual_output['description'] ); |
1552 | | $this->assertEquals( $expected_output['nickname'] , $actual_output['nickname'] ); |
1553 | | |
1554 | | // Compare expected API output to WP internal values |
1555 | | $user = get_userdata( $actual_output['id'] ); |
1556 | | $this->assertEquals( $expected_output['username'] , $user->user_login ); |
1557 | | $this->assertEquals( $expected_output['name'] , $user->display_name ); |
1558 | | $this->assertEquals( $expected_output['first_name'] , $user->first_name ); |
1559 | | $this->assertEquals( $expected_output['last_name'] , $user->last_name ); |
1560 | | $this->assertEquals( $expected_output['url'] , $user->user_url ); |
1561 | | $this->assertEquals( $expected_output['description'], $user->description ); |
1562 | | $this->assertEquals( $expected_output['nickname'] , $user->nickname ); |
1563 | | $this->assertTrue( wp_check_password( addslashes( $expected_output['password'] ), $user->user_pass ) ); |
1564 | | |
1565 | | $user_id = $actual_output['id']; |
1566 | | } |
1567 | | |
1568 | | // Update the user |
1569 | | $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) ); |
1570 | | foreach ( $input as $name => $value ) { |
1571 | | if ( 'username' !== $name ) { |
1572 | | $request->set_param( $name, $value ); |
1573 | | } |
1574 | | } |
1575 | | $response = $this->server->dispatch( $request ); |
1576 | | $this->assertEquals( 200, $response->get_status() ); |
1577 | | $actual_output = $response->get_data(); |
1578 | | |
1579 | | // Compare expected API output to actual API output |
1580 | | if ( isset( $expected_output['username'] ) ) { |
1581 | | $this->assertEquals( $expected_output['username'], $actual_output['username'] ); |
1582 | | } |
1583 | | $this->assertEquals( $expected_output['name'] , $actual_output['name'] ); |
1584 | | $this->assertEquals( $expected_output['first_name'] , $actual_output['first_name'] ); |
1585 | | $this->assertEquals( $expected_output['last_name'] , $actual_output['last_name'] ); |
1586 | | $this->assertEquals( $expected_output['url'] , $actual_output['url'] ); |
1587 | | $this->assertEquals( $expected_output['description'], $actual_output['description'] ); |
1588 | | $this->assertEquals( $expected_output['nickname'] , $actual_output['nickname'] ); |
1589 | | |
1590 | | // Compare expected API output to WP internal values |
1591 | | $user = get_userdata( $actual_output['id'] ); |
1592 | | if ( isset( $expected_output['username'] ) ) { |
1593 | | $this->assertEquals( $expected_output['username'], $user->user_login ); |
1594 | | } |
1595 | | $this->assertEquals( $expected_output['name'] , $user->display_name ); |
1596 | | $this->assertEquals( $expected_output['first_name'] , $user->first_name ); |
1597 | | $this->assertEquals( $expected_output['last_name'] , $user->last_name ); |
1598 | | $this->assertEquals( $expected_output['url'] , $user->user_url ); |
1599 | | $this->assertEquals( $expected_output['description'], $user->description ); |
1600 | | $this->assertEquals( $expected_output['nickname'] , $user->nickname ); |
1601 | | $this->assertTrue( wp_check_password( addslashes( $expected_output['password'] ), $user->user_pass ) ); |
1602 | | } |
1603 | | |
1604 | | public function test_user_roundtrip_as_editor() { |
1605 | | wp_set_current_user( self::$editor ); |
1606 | | $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) ); |
1607 | | $this->verify_user_roundtrip( array( |
1608 | | 'id' => self::$editor, |
1609 | | 'name' => '\o/ ¯\_(ツ)_/¯', |
1610 | | 'first_name' => '\o/ ¯\_(ツ)_/¯', |
1611 | | 'last_name' => '\o/ ¯\_(ツ)_/¯', |
1612 | | 'url' => '\o/ ¯\_(ツ)_/¯', |
1613 | | 'description' => '\o/ ¯\_(ツ)_/¯', |
1614 | | 'nickname' => '\o/ ¯\_(ツ)_/¯', |
1615 | | 'password' => 'o/ ¯_(ツ)_/¯ \'"', |
1616 | | ), array( |
1617 | | 'name' => '\o/ ¯\_(ツ)_/¯', |
1618 | | 'first_name' => '\o/ ¯\_(ツ)_/¯', |
1619 | | 'last_name' => '\o/ ¯\_(ツ)_/¯', |
1620 | | 'url' => 'http://o/%20¯_(ツ)_/¯', |
1621 | | 'description' => '\o/ ¯\_(ツ)_/¯', |
1622 | | 'nickname' => '\o/ ¯\_(ツ)_/¯', |
1623 | | 'password' => 'o/ ¯_(ツ)_/¯ \'"', |
1624 | | ) ); |
1625 | | } |
1626 | | |
1627 | | public function test_user_roundtrip_as_editor_html() { |
1628 | | wp_set_current_user( self::$editor ); |
1629 | | if ( is_multisite() ) { |
1630 | | $this->assertFalse( current_user_can( 'unfiltered_html' ) ); |
1631 | | $this->verify_user_roundtrip( array( |
1632 | | 'id' => self::$editor, |
1633 | | 'name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1634 | | 'first_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1635 | | 'last_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1636 | | 'url' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1637 | | 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1638 | | 'nickname' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1639 | | 'password' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1640 | | ), array( |
1641 | | 'name' => 'div strong', |
1642 | | 'first_name' => 'div strong', |
1643 | | 'last_name' => 'div strong', |
1644 | | 'url' => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script', |
1645 | | 'description' => 'div <strong>strong</strong> oh noes', |
1646 | | 'nickname' => 'div strong', |
1647 | | 'password' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1648 | | ) ); |
1649 | | } else { |
1650 | | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
1651 | | $this->verify_user_roundtrip( array( |
1652 | | 'id' => self::$editor, |
1653 | | 'name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1654 | | 'first_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1655 | | 'last_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1656 | | 'url' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1657 | | 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1658 | | 'nickname' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1659 | | 'password' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1660 | | ), array( |
1661 | | 'name' => 'div strong', |
1662 | | 'first_name' => 'div strong', |
1663 | | 'last_name' => 'div strong', |
1664 | | 'url' => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script', |
1665 | | 'description' => 'div <strong>strong</strong> oh noes', |
1666 | | 'nickname' => 'div strong', |
1667 | | 'password' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1668 | | ) ); |
1669 | | } |
1670 | | } |
1671 | | |
1672 | | public function test_user_roundtrip_as_superadmin() { |
1673 | | wp_set_current_user( self::$superadmin ); |
1674 | | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
1675 | | $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here'; |
1676 | | $this->verify_user_roundtrip( array( |
1677 | | 'username' => $valid_username, |
1678 | | 'name' => '\\\&\\\ & &invalid; < < &lt;', |
1679 | | 'first_name' => '\\\&\\\ & &invalid; < < &lt;', |
1680 | | 'last_name' => '\\\&\\\ & &invalid; < < &lt;', |
1681 | | 'url' => '\\\&\\\ & &invalid; < < &lt;', |
1682 | | 'description' => '\\\&\\\ & &invalid; < < &lt;', |
1683 | | 'nickname' => '\\\&\\\ & &invalid; < < &lt;', |
1684 | | 'password' => '& & &invalid; < < &lt;', |
1685 | | ), array( |
1686 | | 'username' => $valid_username, |
1687 | | 'name' => '\\\&\\\ & &invalid; < < &lt;', |
1688 | | 'first_name' => '\\\&\\\ & &invalid; < < &lt;', |
1689 | | 'last_name' => '\\\&\\\ & &invalid; < < &lt;', |
1690 | | 'url' => 'http://&%20&%20&invalid;%20%20<%20&lt;', |
1691 | | 'description' => '\\\&\\\ & &invalid; < < &lt;', |
1692 | | 'nickname' => '\\\&\\\ & &invalid; < < &lt;', |
1693 | | 'password' => '& & &invalid; < < &lt;', |
1694 | | ) ); |
1695 | | } |
1696 | | |
1697 | | public function test_user_roundtrip_as_superadmin_html() { |
1698 | | wp_set_current_user( self::$superadmin ); |
1699 | | $this->assertTrue( current_user_can( 'unfiltered_html' ) ); |
1700 | | $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here'; |
1701 | | $this->verify_user_roundtrip( array( |
1702 | | 'username' => $valid_username, |
1703 | | 'name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1704 | | 'first_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1705 | | 'last_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1706 | | 'url' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1707 | | 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1708 | | 'nickname' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1709 | | 'password' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1710 | | ), array( |
1711 | | 'username' => $valid_username, |
1712 | | 'name' => 'div strong', |
1713 | | 'first_name' => 'div strong', |
1714 | | 'last_name' => 'div strong', |
1715 | | 'url' => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script', |
1716 | | 'description' => 'div <strong>strong</strong> oh noes', |
1717 | | 'nickname' => 'div strong', |
1718 | | 'password' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>', |
1719 | | ) ); |
1720 | | } |
1721 | | |