Skip to content

Commit 05e669f

Browse files
Add tests for child note deletion
1 parent 3912b78 commit 05e669f

File tree

1 file changed

+221
-0
lines changed

1 file changed

+221
-0
lines changed

tests/phpunit/tests/comment.php

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,4 +1672,225 @@ public function test_unspam_should_invalidate_comment_cache() {
16721672

16731673
$this->assertSame( '1', $comment->comment_approved );
16741674
}
1675+
1676+
/**
1677+
* Tests that trashing a top-level note also trashes all direct child notes.
1678+
*
1679+
* @covers ::wp_trash_comment
1680+
* @covers ::wp_trash_comment_children
1681+
*/
1682+
public function test_wp_trash_comment_trashes_child_notes() {
1683+
// Create a parent note (top-level, comment_parent=0).
1684+
$parent_note = self::factory()->comment->create(
1685+
array(
1686+
'comment_post_ID' => self::$post_id,
1687+
'comment_type' => 'note',
1688+
'comment_parent' => 0,
1689+
'comment_approved' => '1',
1690+
)
1691+
);
1692+
1693+
// Create child notes under the parent.
1694+
$child_note_1 = self::factory()->comment->create(
1695+
array(
1696+
'comment_post_ID' => self::$post_id,
1697+
'comment_type' => 'note',
1698+
'comment_parent' => $parent_note,
1699+
'comment_approved' => '1',
1700+
)
1701+
);
1702+
1703+
$child_note_2 = self::factory()->comment->create(
1704+
array(
1705+
'comment_post_ID' => self::$post_id,
1706+
'comment_type' => 'note',
1707+
'comment_parent' => $parent_note,
1708+
'comment_approved' => '1',
1709+
)
1710+
);
1711+
1712+
$child_note_3 = self::factory()->comment->create(
1713+
array(
1714+
'comment_post_ID' => self::$post_id,
1715+
'comment_type' => 'note',
1716+
'comment_parent' => $parent_note,
1717+
'comment_approved' => '1',
1718+
)
1719+
);
1720+
1721+
// Verify initial state - all notes are approved.
1722+
$this->assertSame( '1', get_comment( $parent_note )->comment_approved );
1723+
$this->assertSame( '1', get_comment( $child_note_1 )->comment_approved );
1724+
$this->assertSame( '1', get_comment( $child_note_2 )->comment_approved );
1725+
$this->assertSame( '1', get_comment( $child_note_3 )->comment_approved );
1726+
1727+
// Trash the parent note.
1728+
wp_trash_comment( $parent_note );
1729+
1730+
// Verify parent note is trashed.
1731+
$this->assertSame( 'trash', get_comment( $parent_note )->comment_approved );
1732+
1733+
// Verify all child notes are also trashed.
1734+
$this->assertSame( 'trash', get_comment( $child_note_1 )->comment_approved );
1735+
$this->assertSame( 'trash', get_comment( $child_note_2 )->comment_approved );
1736+
$this->assertSame( 'trash', get_comment( $child_note_3 )->comment_approved );
1737+
}
1738+
1739+
/**
1740+
* Tests that trashing a regular comment does NOT trash its children.
1741+
*
1742+
* @covers ::wp_trash_comment
1743+
*/
1744+
public function test_wp_trash_comment_does_not_trash_child_comments() {
1745+
// Create a parent comment (default type='comment').
1746+
$parent_comment = self::factory()->comment->create(
1747+
array(
1748+
'comment_post_ID' => self::$post_id,
1749+
'comment_type' => 'comment',
1750+
'comment_parent' => 0,
1751+
'comment_approved' => '1',
1752+
)
1753+
);
1754+
1755+
// Create child comments under the parent.
1756+
$child_comment_1 = self::factory()->comment->create(
1757+
array(
1758+
'comment_post_ID' => self::$post_id,
1759+
'comment_type' => 'comment',
1760+
'comment_parent' => $parent_comment,
1761+
'comment_approved' => '1',
1762+
)
1763+
);
1764+
1765+
$child_comment_2 = self::factory()->comment->create(
1766+
array(
1767+
'comment_post_ID' => self::$post_id,
1768+
'comment_type' => 'comment',
1769+
'comment_parent' => $parent_comment,
1770+
'comment_approved' => '1',
1771+
)
1772+
);
1773+
1774+
// Verify initial state - all comments are approved.
1775+
$this->assertSame( '1', get_comment( $parent_comment )->comment_approved );
1776+
$this->assertSame( '1', get_comment( $child_comment_1 )->comment_approved );
1777+
$this->assertSame( '1', get_comment( $child_comment_2 )->comment_approved );
1778+
1779+
// Trash the parent comment.
1780+
wp_trash_comment( $parent_comment );
1781+
1782+
// Verify parent comment is trashed.
1783+
$this->assertSame( 'trash', get_comment( $parent_comment )->comment_approved );
1784+
1785+
// Verify child comments are NOT trashed (maintaining existing behavior).
1786+
$this->assertSame( '1', get_comment( $child_comment_1 )->comment_approved );
1787+
$this->assertSame( '1', get_comment( $child_comment_2 )->comment_approved );
1788+
}
1789+
1790+
/**
1791+
* Tests that trashing a child note does not affect parent or siblings.
1792+
*
1793+
* @covers ::wp_trash_comment
1794+
*/
1795+
public function test_wp_trash_comment_child_note_does_not_affect_parent_or_siblings() {
1796+
// Create a parent note.
1797+
$parent_note = self::factory()->comment->create(
1798+
array(
1799+
'comment_post_ID' => self::$post_id,
1800+
'comment_type' => 'note',
1801+
'comment_parent' => 0,
1802+
'comment_approved' => '1',
1803+
)
1804+
);
1805+
1806+
// Create multiple child notes.
1807+
$child_note_1 = self::factory()->comment->create(
1808+
array(
1809+
'comment_post_ID' => self::$post_id,
1810+
'comment_type' => 'note',
1811+
'comment_parent' => $parent_note,
1812+
'comment_approved' => '1',
1813+
)
1814+
);
1815+
1816+
$child_note_2 = self::factory()->comment->create(
1817+
array(
1818+
'comment_post_ID' => self::$post_id,
1819+
'comment_type' => 'note',
1820+
'comment_parent' => $parent_note,
1821+
'comment_approved' => '1',
1822+
)
1823+
);
1824+
1825+
$child_note_3 = self::factory()->comment->create(
1826+
array(
1827+
'comment_post_ID' => self::$post_id,
1828+
'comment_type' => 'note',
1829+
'comment_parent' => $parent_note,
1830+
'comment_approved' => '1',
1831+
)
1832+
);
1833+
1834+
// Trash only one child note.
1835+
wp_trash_comment( $child_note_2 );
1836+
1837+
// Verify the parent note is still approved.
1838+
$this->assertSame( '1', get_comment( $parent_note )->comment_approved );
1839+
1840+
// Verify the trashed child is trashed.
1841+
$this->assertSame( 'trash', get_comment( $child_note_2 )->comment_approved );
1842+
1843+
// Verify sibling notes are still approved.
1844+
$this->assertSame( '1', get_comment( $child_note_1 )->comment_approved );
1845+
$this->assertSame( '1', get_comment( $child_note_3 )->comment_approved );
1846+
}
1847+
1848+
/**
1849+
* Tests that only top-level notes trigger child deletion.
1850+
*
1851+
* @covers ::wp_trash_comment
1852+
*/
1853+
public function test_wp_trash_comment_only_top_level_notes_trigger_child_deletion() {
1854+
// Create a parent note.
1855+
$parent_note = self::factory()->comment->create(
1856+
array(
1857+
'comment_post_ID' => self::$post_id,
1858+
'comment_type' => 'note',
1859+
'comment_parent' => 0,
1860+
'comment_approved' => '1',
1861+
)
1862+
);
1863+
1864+
// Create a child note (not top-level, has comment_parent > 0).
1865+
$child_note = self::factory()->comment->create(
1866+
array(
1867+
'comment_post_ID' => self::$post_id,
1868+
'comment_type' => 'note',
1869+
'comment_parent' => $parent_note,
1870+
'comment_approved' => '1',
1871+
)
1872+
);
1873+
1874+
// Create a sibling note (also not top-level).
1875+
$sibling_note = self::factory()->comment->create(
1876+
array(
1877+
'comment_post_ID' => self::$post_id,
1878+
'comment_type' => 'note',
1879+
'comment_parent' => $parent_note,
1880+
'comment_approved' => '1',
1881+
)
1882+
);
1883+
1884+
// Trash the child note (which has comment_parent > 0).
1885+
wp_trash_comment( $child_note );
1886+
1887+
// Verify the child note is trashed.
1888+
$this->assertSame( 'trash', get_comment( $child_note )->comment_approved );
1889+
1890+
// Verify the parent note is NOT trashed.
1891+
$this->assertSame( '1', get_comment( $parent_note )->comment_approved );
1892+
1893+
// Verify the sibling note is NOT trashed (no cascade since child is not top-level).
1894+
$this->assertSame( '1', get_comment( $sibling_note )->comment_approved );
1895+
}
16751896
}

0 commit comments

Comments
 (0)