diff --git a/tests/phpunit/tests/admin/includes/bookmark/addLink.php b/tests/phpunit/tests/admin/includes/bookmark/addLink.php new file mode 100644 index 0000000000000..f9f7cbce1aa5e --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/addLink.php @@ -0,0 +1,38 @@ +user->create( array( 'role' => 'administrator' ) ); + update_option( 'link_manager_enabled', 1 ); + wp_set_current_user( $user_id ); + $_POST = array( + 'link_name' => 'Example link', + 'link_url' => 'https://example.com', + 'link_image' => '', + 'link_rss' => '', + ); + + $link_id = add_link(); + + $this->assertIsInt( $link_id ); + $this->assertSame( 'Example link', get_bookmark( $link_id )->link_name ); + } + + public function tear_down() { + $_POST = array(); + wp_set_current_user( 0 ); + parent::tear_down(); + } +} diff --git a/tests/phpunit/tests/admin/includes/bookmark/editLink.php b/tests/phpunit/tests/admin/includes/bookmark/editLink.php new file mode 100644 index 0000000000000..fed8f62e25b9b --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/editLink.php @@ -0,0 +1,43 @@ +user->create( array( 'role' => 'administrator' ) ); + update_option( 'link_manager_enabled', 1 ); + wp_set_current_user( $user_id ); + $link_id = self::factory()->bookmark->create( + array( + 'link_name' => 'Original name', + 'link_url' => 'https://old.example.com', + ) + ); + $_POST = array( + 'link_name' => 'Updated name', + 'link_url' => 'https://new.example.com', + 'link_image' => '', + 'link_rss' => '', + ); + + $this->assertSame( $link_id, edit_link( $link_id ) ); + $this->assertSame( 'Updated name', get_bookmark( $link_id )->link_name ); + $this->assertSame( 'https://new.example.com', get_bookmark( $link_id )->link_url ); + } + + public function tear_down() { + $_POST = array(); + wp_set_current_user( 0 ); + parent::tear_down(); + } +} diff --git a/tests/phpunit/tests/admin/includes/bookmark/getDefaultLinkToEdit.php b/tests/phpunit/tests/admin/includes/bookmark/getDefaultLinkToEdit.php new file mode 100644 index 0000000000000..9c1447cb06ddd --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/getDefaultLinkToEdit.php @@ -0,0 +1,62 @@ +assertSame( $expected_url, $link->link_url ); + $this->assertSame( $expected_name, $link->link_name ); + $this->assertSame( 'Y', $link->link_visible ); + } + + /** + * Data provider for test_get_default_link_to_edit(). + * + * @return array, + * expected_url: string, + * expected_name: string, + * }> + */ + public function data_default_link_values(): array { + return array( + 'empty values' => array( + 'get' => array(), + 'expected_url' => '', + 'expected_name' => '', + ), + 'provided values' => array( + 'get' => array( + 'linkurl' => 'https://example.com/?a=1&b=2', + 'name' => 'Example & name', + ), + 'expected_url' => 'https://example.com/?a=1&b=2', + 'expected_name' => 'Example & name', + ), + ); + } + + public function tear_down() { + $_GET = array(); + parent::tear_down(); + } +} diff --git a/tests/phpunit/tests/admin/includes/bookmark/getLinkToEdit.php b/tests/phpunit/tests/admin/includes/bookmark/getLinkToEdit.php new file mode 100644 index 0000000000000..23e837ae8a9eb --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/getLinkToEdit.php @@ -0,0 +1,30 @@ +bookmark->create( + array( + 'link_name' => 'A link', + 'link_url' => 'https://example.com', + ) + ); + + $link = get_link_to_edit( $link_id ); + + $this->assertInstanceOf( stdClass::class, $link ); + $this->assertSame( $link_id, $link->link_id ); + $this->assertSame( 'A link', $link->link_name ); + } +} diff --git a/tests/phpunit/tests/admin/includes/bookmark/wpDeleteLink.php b/tests/phpunit/tests/admin/includes/bookmark/wpDeleteLink.php new file mode 100644 index 0000000000000..1eece47921d1d --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/wpDeleteLink.php @@ -0,0 +1,44 @@ +bookmark->create(); + $category_id = self::factory()->term->create( array( 'taxonomy' => 'link_category' ) ); + wp_set_object_terms( $link_id, $category_id, 'link_category' ); + add_action( 'delete_link', array( $this, 'record_deleted_link' ) ); + add_action( 'deleted_link', array( $this, 'record_deleted_link' ) ); + + $this->assertTrue( wp_delete_link( $link_id ) ); + $this->assertNull( get_bookmark( $link_id ) ); + $this->assertSame( array(), wp_get_link_cats( $link_id ) ); + $this->assertSame( array( $link_id, $link_id ), $this->deleted_link_ids ); + } + + /** + * Records a link ID passed to a deletion action. + * + * @param int $link_id Deleted link ID. + */ + public function record_deleted_link( $link_id ) { + $this->deleted_link_ids[] = $link_id; + } + + /** + * Link IDs recorded by the deletion hooks. + * + * @var int[] + */ + private $deleted_link_ids = array(); +} diff --git a/tests/phpunit/tests/admin/includes/bookmark/wpGetLinkCats.php b/tests/phpunit/tests/admin/includes/bookmark/wpGetLinkCats.php new file mode 100644 index 0000000000000..9e41af12edc29 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/wpGetLinkCats.php @@ -0,0 +1,23 @@ +bookmark->create(); + $category_ids = self::factory()->term->create_many( 2, array( 'taxonomy' => 'link_category' ) ); + wp_set_object_terms( $link_id, $category_ids, 'link_category' ); + + $this->assertSameSets( $category_ids, wp_get_link_cats( $link_id ) ); + } +} diff --git a/tests/phpunit/tests/admin/includes/bookmark/wpInsertLink.php b/tests/phpunit/tests/admin/includes/bookmark/wpInsertLink.php new file mode 100644 index 0000000000000..ef693f07ca6af --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/wpInsertLink.php @@ -0,0 +1,61 @@ +term->create( array( 'taxonomy' => 'link_category' ) ); + + $link_id = wp_insert_link( + array( + 'link_name' => 'Inserted link', + 'link_url' => 'https://example.com', + 'link_category' => array( $category_id ), + ) + ); + + $this->assertIsInt( $link_id ); + $this->assertSame( 'Inserted link', get_bookmark( $link_id )->link_name ); + $this->assertSame( array( $category_id ), wp_get_link_cats( $link_id ) ); + } + + /** + * Tests that wp_insert_link() rejects links without a URL. + * + * @ticket 66019 + * @dataProvider data_invalid_link_data + * + * @param array $link_data Invalid link data. + */ + public function test_wp_insert_link_rejects_invalid_data( $link_data ) { + $this->assertSame( 0, wp_insert_link( $link_data ) ); + } + + /** + * Data provider for test_wp_insert_link_rejects_invalid_data(). + * + * @return array, + * }> + */ + public function data_invalid_link_data(): array { + return array( + 'no URL' => array( + 'link_data' => array( 'link_name' => 'Missing URL' ), + ), + 'no name or URL' => array( + 'link_data' => array(), + ), + ); + } +} diff --git a/tests/phpunit/tests/admin/includes/bookmark/wpLinkManagerDisabledMessage.php b/tests/phpunit/tests/admin/includes/bookmark/wpLinkManagerDisabledMessage.php new file mode 100644 index 0000000000000..5a903cf805e0f --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/wpLinkManagerDisabledMessage.php @@ -0,0 +1,23 @@ +assertNull( wp_link_manager_disabled_message() ); + } +} diff --git a/tests/phpunit/tests/admin/includes/bookmark/wpSetLinkCats.php b/tests/phpunit/tests/admin/includes/bookmark/wpSetLinkCats.php new file mode 100644 index 0000000000000..5d52065e61837 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/wpSetLinkCats.php @@ -0,0 +1,24 @@ +bookmark->create(); + $category_ids = self::factory()->term->create_many( 2, array( 'taxonomy' => 'link_category' ) ); + + wp_set_link_cats( $link_id, array( $category_ids[0], $category_ids[1], $category_ids[0] ) ); + + $this->assertSameSets( $category_ids, wp_get_link_cats( $link_id ) ); + } +} diff --git a/tests/phpunit/tests/admin/includes/bookmark/wpUpdateLink.php b/tests/phpunit/tests/admin/includes/bookmark/wpUpdateLink.php new file mode 100644 index 0000000000000..254751644c42e --- /dev/null +++ b/tests/phpunit/tests/admin/includes/bookmark/wpUpdateLink.php @@ -0,0 +1,42 @@ +bookmark->create( + array( + 'link_name' => 'Original link', + 'link_url' => 'https://old.example.com', + ) + ); + $category_id = self::factory()->term->create( array( 'taxonomy' => 'link_category' ) ); + + $this->assertSame( + $link_id, + wp_update_link( + array( + 'link_id' => $link_id, + 'link_name' => 'Updated link', + 'link_url' => 'https://new.example.com', + 'link_category' => array( $category_id ), + ) + ) + ); + + $link = get_bookmark( $link_id ); + $this->assertSame( 'Updated link', $link->link_name ); + $this->assertSame( 'https://new.example.com', $link->link_url ); + $this->assertSame( array( $category_id ), wp_get_link_cats( $link_id ) ); + } +}