Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/addLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Test the `add_link()` function.
*
* @group bookmark
* @covers ::add_link
*/
class Tests_Admin_Includes_Bookmark_addLink extends WP_UnitTestCase {

/**
* Tests that add_link() inserts a link from the submitted values.
*
* @ticket 66019
*/
public function test_add_link_inserts_submitted_link() {
$user_id = self::factory()->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();
}
}
43 changes: 43 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/editLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Test the `edit_link()` function.
*
* @group bookmark
* @covers ::edit_link
*/
class Tests_Admin_Includes_Bookmark_editLink extends WP_UnitTestCase {

/**
* Tests that edit_link() updates an existing link from the submitted values.
*
* @ticket 66019
*/
public function test_edit_link_updates_existing_link() {
$user_id = self::factory()->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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Test the `get_default_link_to_edit()` function.
*
* @group bookmark
* @covers ::get_default_link_to_edit
*/
class Tests_Admin_Includes_Bookmark_getDefaultLinkToEdit extends WP_UnitTestCase {

/**
* Tests that get_default_link_to_edit() builds the expected default object.
*
* @ticket 66019
* @dataProvider data_default_link_values
*
* @param array $get Query-string values.
* @param string $expected_url Expected link URL.
* @param string $expected_name Expected link name.
*/
public function test_get_default_link_to_edit( $get, $expected_url, $expected_name ) {
$_GET = $get;

$link = get_default_link_to_edit();

$this->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<string, array{
* get: array<string, string>,
* 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&amp;b=2',
'name' => 'Example &amp; name',
),
'expected_url' => 'https://example.com/?a=1&#038;b=2',
'expected_name' => 'Example &amp; name',
),
);
}

public function tear_down() {
$_GET = array();
parent::tear_down();
}
}
30 changes: 30 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/getLinkToEdit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* Test the `get_link_to_edit()` function.
*
* @group bookmark
* @covers ::get_link_to_edit
*/
class Tests_Admin_Includes_Bookmark_getLinkToEdit extends WP_UnitTestCase {

/**
* Tests that get_link_to_edit() returns the link in edit context.
*
* @ticket 66019
*/
public function test_get_link_to_edit_returns_link_for_editing() {
$link_id = self::factory()->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 );
}
}
44 changes: 44 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/wpDeleteLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Test the `wp_delete_link()` function.
*
* @group bookmark
* @covers ::wp_delete_link
*/
class Tests_Admin_Includes_Bookmark_wpDeleteLink extends WP_UnitTestCase {

/**
* Tests that wp_delete_link() removes the link and its category relationships.
*
* @ticket 66019
*/
public function test_wp_delete_link_removes_link_and_categories() {
$link_id = self::factory()->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();
}
23 changes: 23 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/wpGetLinkCats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Test the `wp_get_link_cats()` function.
*
* @group bookmark
* @covers ::wp_get_link_cats
*/
class Tests_Admin_Includes_Bookmark_wpGetLinkCats extends WP_UnitTestCase {

/**
* Tests that wp_get_link_cats() returns the IDs of a link's categories.
*
* @ticket 66019
*/
public function test_wp_get_link_cats_returns_category_ids() {
$link_id = self::factory()->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 ) );
}
}
61 changes: 61 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/wpInsertLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* Test the `wp_insert_link()` function.
*
* @group bookmark
* @covers ::wp_insert_link
*/
class Tests_Admin_Includes_Bookmark_wpInsertLink extends WP_UnitTestCase {

/**
* Tests that wp_insert_link() inserts a link and assigns its category.
*
* @ticket 66019
*/
public function test_wp_insert_link_inserts_link_with_category() {
$category_id = self::factory()->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<string, array{
* link_data: array<string, string>,
* }>
*/
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(),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* Test the `wp_link_manager_disabled_message()` function.
*
* @group bookmark
* @covers ::wp_link_manager_disabled_message
*/
class Tests_Admin_Includes_Bookmark_wpLinkManagerDisabledMessage extends WP_UnitTestCase {

/**
* Tests that wp_link_manager_disabled_message() does nothing on other screens.
*
* @ticket 66019
*/
public function test_wp_link_manager_disabled_message_ignores_other_screens() {
global $pagenow;

$pagenow = 'index.php';

$this->assertNull( wp_link_manager_disabled_message() );
}
}
24 changes: 24 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/wpSetLinkCats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Test the `wp_set_link_cats()` function.
*
* @group bookmark
* @covers ::wp_set_link_cats
*/
class Tests_Admin_Includes_Bookmark_wpSetLinkCats extends WP_UnitTestCase {

/**
* Tests that wp_set_link_cats() assigns unique category IDs to a link.
*
* @ticket 66019
*/
public function test_wp_set_link_cats_assigns_unique_categories() {
$link_id = self::factory()->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 ) );
}
}
42 changes: 42 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/wpUpdateLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Test the `wp_update_link()` function.
*
* @group bookmark
* @covers ::wp_update_link
*/
class Tests_Admin_Includes_Bookmark_wpUpdateLink extends WP_UnitTestCase {

/**
* Tests that wp_update_link() updates link fields and categories.
*
* @ticket 66019
*/
public function test_wp_update_link_updates_link_data() {
$link_id = self::factory()->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 ) );
}
}
Loading