From a47979c8ebf64a9a62213899efaeb66e2af85d81 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 17:37:50 -0400 Subject: [PATCH 1/4] Add unit tests for update_recently_edited() in wp-admin/includes/misc.php --- .../includes/misc/updateRecentlyEdited.php | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php diff --git a/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php b/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php new file mode 100644 index 0000000000000..f5bfb1ea98992 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php @@ -0,0 +1,141 @@ +assertSame( array( $file ), array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + } + + /** + * Tests that update_recently_edited() maintains a maximum of 5 files. + * + * @ticket 65174 + */ + public function test_update_recently_edited_maintains_max_5_files() { + $files = array( + 'file1.php', + 'file2.php', + 'file3.php', + 'file4.php', + 'file5.php', + ); + + foreach ( $files as $file ) { + update_recently_edited( $file ); + } + + $this->assertSame( array_reverse( $files ), array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + + $new_file = 'file6.php'; + update_recently_edited( $new_file ); + + $expected = array( + 'file6.php', + 'file5.php', + 'file4.php', + 'file3.php', + 'file2.php', + ); + + $this->assertSame( $expected, array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + } + + /** + * Tests that update_recently_edited() moves an existing file to the top. + * + * @ticket 65174 + */ + public function test_update_recently_edited_moves_existing_file_to_top() { + $files = array( + 'file1.php', + 'file2.php', + 'file3.php', + ); + + foreach ( $files as $file ) { + update_recently_edited( $file ); + } + + // Current state: array( 'file3.php', 'file2.php', 'file1.php' ) + update_recently_edited( 'file1.php' ); + + $expected = array( + 'file1.php', + 'file3.php', + 'file2.php', + ); + + $this->assertSame( $expected, array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + } + + /** + * Tests update_recently_edited() with various inputs using a data provider. + * + * @ticket 65174 + * @dataProvider data_update_recently_edited + * + * @param array $initial_option The initial 'recently_edited' option. + * @param string $new_file The new file to add. + * @param array $expected The expected 'recently_edited' option. + */ + public function test_update_recently_edited_cases( $initial_option, $new_file, $expected ) { + if ( false !== $initial_option ) { + update_option( 'recently_edited', $initial_option ); + } else { + delete_option( 'recently_edited' ); + } + + update_recently_edited( $new_file ); + + $this->assertSame( $expected, array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + } + + /** + * Data provider for test_update_recently_edited_cases. + * + * @return array + */ + public function data_update_recently_edited() { + return array( + 'empty_option' => array( + 'initial_option' => false, + 'new_file' => 'new.php', + 'expected' => array( 'new.php' ), + ), + 'one_item' => array( + 'initial_option' => array( 'old.php' ), + 'new_file' => 'new.php', + 'expected' => array( 'new.php', 'old.php' ), + ), + 'duplicate_item' => array( + 'initial_option' => array( 'old.php', 'new.php' ), + 'new_file' => 'new.php', + 'expected' => array( 'new.php', 'old.php' ), + ), + 'max_items_at_limit' => array( + 'initial_option' => array( '1.php', '2.php', '3.php', '4.php' ), + 'new_file' => '5.php', + 'expected' => array( '5.php', '1.php', '2.php', '3.php', '4.php' ), + ), + 'max_items_exceed_limit' => array( + 'initial_option' => array( '1.php', '2.php', '3.php', '4.php', '5.php' ), + 'new_file' => '6.php', + 'expected' => array( '6.php', '1.php', '2.php', '3.php', '4.php' ), + ), + ); + } +} From 7501e8cdaad6220edb627c69dc91f82a7f8c96c7 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 17:41:57 -0400 Subject: [PATCH 2/4] Update updateRecentlyEdited.php wightspace --- .../tests/admin/includes/misc/updateRecentlyEdited.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php b/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php index f5bfb1ea98992..e4f3ebca143fe 100644 --- a/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php +++ b/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php @@ -111,22 +111,22 @@ public function test_update_recently_edited_cases( $initial_option, $new_file, $ */ public function data_update_recently_edited() { return array( - 'empty_option' => array( + 'empty_option' => array( 'initial_option' => false, 'new_file' => 'new.php', 'expected' => array( 'new.php' ), ), - 'one_item' => array( + 'one_item' => array( 'initial_option' => array( 'old.php' ), 'new_file' => 'new.php', 'expected' => array( 'new.php', 'old.php' ), ), - 'duplicate_item' => array( + 'duplicate_item' => array( 'initial_option' => array( 'old.php', 'new.php' ), 'new_file' => 'new.php', 'expected' => array( 'new.php', 'old.php' ), ), - 'max_items_at_limit' => array( + 'max_items_at_limit' => array( 'initial_option' => array( '1.php', '2.php', '3.php', '4.php' ), 'new_file' => '5.php', 'expected' => array( '5.php', '1.php', '2.php', '3.php', '4.php' ), From 7075b4267758de2bfa7c451c998cbfeec16d3c70 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 17:37:50 -0400 Subject: [PATCH 3/4] Add unit tests for update_recently_edited() in wp-admin/includes/misc.php --- .../includes/misc/updateRecentlyEdited.php | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php diff --git a/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php b/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php new file mode 100644 index 0000000000000..b583199193c70 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php @@ -0,0 +1,145 @@ +assertSame( array( $file ), array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + } + + /** + * Tests that update_recently_edited() maintains a maximum of 5 files. + * + * @ticket 65174 + */ + public function test_update_recently_edited_maintains_max_5_files() { + $files = array( + 'file1.php', + 'file2.php', + 'file3.php', + 'file4.php', + 'file5.php', + ); + + foreach ( $files as $file ) { + update_recently_edited( $file ); + } + + $this->assertSame( array_reverse( $files ), array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + + $new_file = 'file6.php'; + update_recently_edited( $new_file ); + + $expected = array( + 'file6.php', + 'file5.php', + 'file4.php', + 'file3.php', + 'file2.php', + ); + + $this->assertSame( $expected, array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + } + + /** + * Tests that update_recently_edited() moves an existing file to the top. + * + * @ticket 65174 + */ + public function test_update_recently_edited_moves_existing_file_to_top() { + $files = array( + 'file1.php', + 'file2.php', + 'file3.php', + ); + + foreach ( $files as $file ) { + update_recently_edited( $file ); + } + + // Current state: array( 'file3.php', 'file2.php', 'file1.php' ) + update_recently_edited( 'file1.php' ); + + $expected = array( + 'file1.php', + 'file3.php', + 'file2.php', + ); + + $this->assertSame( $expected, array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + } + + /** + * Tests update_recently_edited() with various inputs using a data provider. + * + * @ticket 65174 + * @dataProvider data_update_recently_edited + * + * @param array $initial_option The initial 'recently_edited' option. + * @param string $new_file The new file to add. + * @param array $expected The expected 'recently_edited' option. + */ + public function test_update_recently_edited_cases( $initial_option, $new_file, $expected ) { + if ( false !== $initial_option ) { + update_option( 'recently_edited', $initial_option ); + } else { + delete_option( 'recently_edited' ); + } + + update_recently_edited( $new_file ); + + $this->assertSame( $expected, array_filter( array_values( get_option( 'recently_edited' ) ) ) ); + } + + /** + * Data provider for test_update_recently_edited_cases. + * + * @return array|false, + * new_file: string, + * expected: array, + * }> + */ + public function data_update_recently_edited(): array { + return array( + 'empty_option' => array( + 'initial_option' => false, + 'new_file' => 'new.php', + 'expected' => array( 'new.php' ), + ), + 'one_item' => array( + 'initial_option' => array( 'old.php' ), + 'new_file' => 'new.php', + 'expected' => array( 'new.php', 'old.php' ), + ), + 'duplicate_item' => array( + 'initial_option' => array( 'old.php', 'new.php' ), + 'new_file' => 'new.php', + 'expected' => array( 'new.php', 'old.php' ), + ), + 'max_items_at_limit' => array( + 'initial_option' => array( '1.php', '2.php', '3.php', '4.php' ), + 'new_file' => '5.php', + 'expected' => array( '5.php', '1.php', '2.php', '3.php', '4.php' ), + ), + 'max_items_exceed_limit' => array( + 'initial_option' => array( '1.php', '2.php', '3.php', '4.php', '5.php' ), + 'new_file' => '6.php', + 'expected' => array( '6.php', '1.php', '2.php', '3.php', '4.php' ), + ), + ); + } +} From 4cf1f525c299cb928099143ead9921f090c23eda Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 5 May 2026 18:03:07 -0400 Subject: [PATCH 4/4] Fix array keys formatting in updateRecentlyEdited test --- .../tests/admin/includes/misc/updateRecentlyEdited.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php b/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php index b583199193c70..ed02efd2dec78 100644 --- a/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php +++ b/tests/phpunit/tests/admin/includes/misc/updateRecentlyEdited.php @@ -115,22 +115,22 @@ public function test_update_recently_edited_cases( $initial_option, $new_file, $ */ public function data_update_recently_edited(): array { return array( - 'empty_option' => array( + 'empty_option' => array( 'initial_option' => false, 'new_file' => 'new.php', 'expected' => array( 'new.php' ), ), - 'one_item' => array( + 'one_item' => array( 'initial_option' => array( 'old.php' ), 'new_file' => 'new.php', 'expected' => array( 'new.php', 'old.php' ), ), - 'duplicate_item' => array( + 'duplicate_item' => array( 'initial_option' => array( 'old.php', 'new.php' ), 'new_file' => 'new.php', 'expected' => array( 'new.php', 'old.php' ), ), - 'max_items_at_limit' => array( + 'max_items_at_limit' => array( 'initial_option' => array( '1.php', '2.php', '3.php', '4.php' ), 'new_file' => '5.php', 'expected' => array( '5.php', '1.php', '2.php', '3.php', '4.php' ),