Make WordPress Core


Ignore:
Timestamp:
02/17/2021 10:56:34 PM (5 years ago)
Author:
peterwilsoncc
Message:

Posts/Post Types: Prevent duplicates in sticky posts option.

In unstick_post() if a post ID is duplicated in the sticky_posts option remove all instances.

In both stick_post() and unstick_post() check for duplicate IDs already stored in the sticky_post option and remove them if the option is updated.

Props rahmohn, archon810.
Fixes #52007.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post.php

    r50284 r50380  
    16381638                $this->assertFalse( $resolved_post_date );
    16391639        }
     1640
     1641        /**
     1642         * Ensure sticking post updates option.
     1643         *
     1644         * @covers ::stick_post
     1645         */
     1646        function test_sticky_posts() {
     1647                stick_post( 1 );
     1648                $this->assertSameSets( array( 1 ), get_option( 'sticky_posts' ) );
     1649
     1650                stick_post( 2 );
     1651                $this->assertSameSets( array( 1, 2 ), get_option( 'sticky_posts' ) );
     1652        }
     1653
     1654        /**
     1655         * Ensure sticking posts can not duplicate options.
     1656         *
     1657         * @ticket 52007
     1658         * @covers ::stick_post
     1659         * @dataProvider data_sticky_posts_not_duplicate_with_the_same_value
     1660         *
     1661         * @param mixed $stick Value to pass to stick_post().
     1662         */
     1663        function test_sticky_posts_not_duplicate_with_the_same_value( $stick ) {
     1664                update_option( 'sticky_posts', array( 1, 2 ) );
     1665
     1666                stick_post( $stick );
     1667                $this->assertSameSets( array( 1, 2 ), get_option( 'sticky_posts' ) );
     1668        }
     1669
     1670        /**
     1671         * Data provider for test_sticky_posts_not_duplicate_with_the_same_value().
     1672         *
     1673         * @return array[] {
     1674         *     Arguments passed to test.
     1675         *
     1676         *     @type mixed $stick Value to pass to stick_post().
     1677         * }
     1678         */
     1679        function data_sticky_posts_not_duplicate_with_the_same_value() {
     1680                return array(
     1681                        array( 1 ),
     1682                        array( '1' ),
     1683                        array( 2.0 ),
     1684                );
     1685        }
     1686
     1687        /**
     1688         * Ensure sticking post removes other duplicates.
     1689         *
     1690         * @ticket 52007
     1691         * @covers ::stick_post
     1692         *
     1693         * @param mixed $stick Value to pass to stick_post().
     1694         */
     1695        function test_sticky_posts_remove_duplicate_ids_when_add_new_value() {
     1696                update_option( 'sticky_posts', array( 1, 1, 2, 2 ) );
     1697
     1698                stick_post( 3 );
     1699                $this->assertSameSets( array( 1, 2, 3 ), get_option( 'sticky_posts' ) );
     1700        }
     1701
     1702        function test_unsticky_posts() {
     1703                update_option( 'sticky_posts', array( 1 ) );
     1704                unstick_post( 1 );
     1705                $this->assertEmpty( get_option( 'sticky_posts' ) );
     1706
     1707                update_option( 'sticky_posts', array( 1, 2 ) );
     1708                unstick_post( 1 );
     1709                $this->assertSameSets( array( 2 ), get_option( 'sticky_posts' ) );
     1710        }
     1711
     1712        /**
     1713         * Ensure duplicates removed when unsticking posts.
     1714         *
     1715         * @ticket 52007
     1716         * @covers ::unstick_post
     1717         *
     1718         * @dataProvider data_unstick_posts_with_duplicate_id
     1719         *
     1720         * @param array $starting_option Original value of `sticky_posts` option.
     1721         * @param mixed $unstick         Parameter passed to `unstick_post()`
     1722         * @param array $expected
     1723         */
     1724        function test_unstick_posts_with_duplicate_id( $starting_option, $unstick, $expected ) {
     1725                update_option( 'sticky_posts', $starting_option );
     1726                unstick_post( $unstick );
     1727                $this->assertSameSets( $expected, get_option( 'sticky_posts' ) );
     1728        }
     1729
     1730        /**
     1731         * Data provider for test_unstick_posts_with_duplicate_id
     1732         *
     1733         * @return array[] {
     1734         *     Arguments passed to test.
     1735         *
     1736         *     @type array $starting_option Original value of `sticky_posts` option.
     1737         *     @type mixed $unstick         Parameter passed to `unstick_post()`
     1738         *     @type array $expected
     1739         * }
     1740         */
     1741        function data_unstick_posts_with_duplicate_id() {
     1742                return array(
     1743                        array(
     1744                                array( 1, 1 ),
     1745                                1,
     1746                                array(),
     1747                        ),
     1748                        array(
     1749                                array( 1, 1 ),
     1750                                '1',
     1751                                array(),
     1752                        ),
     1753                        array(
     1754                                array( 1, 2, 1 ),
     1755                                1,
     1756                                array( 2 ),
     1757                        ),
     1758                        array(
     1759                                array( 1, 2, 1 ),
     1760                                2,
     1761                                array( 1 ),
     1762                        ),
     1763                        array(
     1764                                array( 1, 2, 1 ),
     1765                                2.0,
     1766                                array( 1 ),
     1767                        ),
     1768                );
     1769        }
     1770
     1771        /**
     1772         * Ensure sticking duplicate does not trigger db update.
     1773         *
     1774         * @ticket 52007
     1775         * @covers ::stick_post
     1776         */
     1777        function test_sticking_dupes_does_not_trigger_update() {
     1778                update_option( 'sticky_posts', array( 1, 2, 2 ) );
     1779                stick_post( 2 );
     1780                $this->assertEquals( array( 1, 2, 2 ), get_option( 'sticky_posts' ) );
     1781        }
     1782
     1783        /**
     1784         * Ensure unsticking unstuck post does not trigger db update.
     1785         *
     1786         * @ticket 52007
     1787         * @covers ::unstick_post
     1788         */
     1789        function test_unsticking_unstuck_post_does_not_trigger_update() {
     1790                update_option( 'sticky_posts', array( 1, 2, 2 ) );
     1791                unstick_post( 3 );
     1792                $this->assertEquals( array( 1, 2, 2 ), get_option( 'sticky_posts' ) );
     1793        }
    16401794}
Note: See TracChangeset for help on using the changeset viewer.