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
31 changes: 31 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/AddLink_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* @group admin
* @group bookmark
*
* @covers ::add_link
*/
class Tests_Admin_Includes_Bookmark_AddLink_Test extends WP_UnitTestCase {

/**
* @ticket 66019
*/
public function test_should_insert_a_link_from_the_post_data() {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );

add_filter( 'pre_option_link_manager_enabled', '__return_true' );

$_POST = array(
'link_url' => 'https://example.com/',
'link_name' => 'Example',
'link_image' => '',
'link_rss' => '',
);

$link_id = add_link();

$this->assertIsInt( $link_id, 'The link ID should be an integer.' );
$this->assertSame( 'Example', get_bookmark( $link_id )->link_name, 'The link name was not stored.' );
}
}
156 changes: 156 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/EditLink_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

/**
* @group admin
* @group bookmark
*
* @covers ::edit_link
*/
class Tests_Admin_Includes_Bookmark_EditLink_Test extends WP_UnitTestCase {

/**
* User ID of an administrator.
*
* @var int
*/
private static $admin_id;

/**
* User ID of a subscriber.
*
* @var int
*/
private static $subscriber_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) );
}

public function set_up() {
parent::set_up();

// The 'manage_links' capability is only granted while the Link Manager is enabled.
add_filter( 'pre_option_link_manager_enabled', '__return_true' );
}

/**
* @ticket 66019
*/
public function test_should_die_for_users_who_cannot_manage_links() {
wp_set_current_user( self::$subscriber_id );
$this->set_up_post_data();

$this->expectException( 'WPDieException' );
$this->expectExceptionCode( 403 );

edit_link();
}

/**
* @ticket 66019
*/
public function test_should_insert_a_link_from_the_post_data() {
wp_set_current_user( self::$admin_id );
$this->set_up_post_data();

$link_id = edit_link();

$this->assertIsInt( $link_id, 'The link ID should be an integer.' );
$this->assertSame( 'Example', get_bookmark( $link_id )->link_name, 'The link name was not stored.' );
}

/**
* @ticket 66019
*/
public function test_should_update_the_link_when_a_link_id_is_passed() {
wp_set_current_user( self::$admin_id );
$link_id = self::factory()->bookmark->create( array( 'link_name' => 'Original' ) );
$this->set_up_post_data( array( 'link_name' => 'Updated' ) );

$result = edit_link( $link_id );

$this->assertSame( $link_id, $result, 'The existing link ID should be returned.' );
$this->assertSame( 'Updated', get_bookmark( $link_id )->link_name, 'The link was not updated.' );
}

/**
* @ticket 66019
*/
public function test_should_escape_the_posted_link_name() {
wp_set_current_user( self::$admin_id );
$this->set_up_post_data( array( 'link_name' => '<b>Bold</b>' ) );

$link_id = edit_link();

$this->assertSame( '&lt;b&gt;Bold&lt;/b&gt;', get_bookmark( $link_id )->link_name );
}

/**
* @ticket 66019
*/
public function test_should_escape_the_posted_link_url() {
wp_set_current_user( self::$admin_id );
$this->set_up_post_data( array( 'link_url' => 'https://example.com/?a=1&b=2' ) );

$link_id = edit_link();

$this->assertSame( 'https://example.com/?a=1&#038;b=2', get_bookmark( $link_id )->link_url );
}

/**
* @ticket 66019
*
* @dataProvider data_link_visible
*
* @param array $post_data Values to add to the posted link data.
* @param string $expected The expected stored visibility.
*/
public function test_should_normalize_the_posted_visibility( $post_data, $expected ) {
wp_set_current_user( self::$admin_id );
$this->set_up_post_data( $post_data );

$link_id = edit_link();

$this->assertSame( $expected, get_bookmark( $link_id )->link_visible );
}

/**
* Data provider.
*
* @return array<string, array{post_data: array<string, string>, expected: string}>
*/
public function data_link_visible() {
return array(
'no value' => array(
'post_data' => array(),
'expected' => 'Y',
),
'an uppercase N' => array(
'post_data' => array( 'link_visible' => 'N' ),
'expected' => 'N',
),
'a lowercase n' => array(
'post_data' => array( 'link_visible' => 'n' ),
'expected' => 'Y',
),
);
}

/**
* Populates $_POST with the fields edit_link() reads unconditionally.
*
* @param array $post_data Optional. Values to add to the posted link data.
*/
private function set_up_post_data( $post_data = array() ) {
$_POST = array_merge(
array(
'link_url' => 'https://example.com/',
'link_name' => 'Example',
'link_image' => '',
'link_rss' => '',
),
$post_data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* @group admin
* @group bookmark
*
* @covers ::get_default_link_to_edit
*/
class Tests_Admin_Includes_Bookmark_GetDefaultLinkToEdit_Test extends WP_UnitTestCase {

/**
* @ticket 66019
*/
public function test_should_return_an_empty_visible_link() {
$link = get_default_link_to_edit();

$this->assertSame( '', $link->link_url, 'The link URL should be empty.' );
$this->assertSame( '', $link->link_name, 'The link name should be empty.' );
$this->assertSame( 'Y', $link->link_visible, 'The link should be visible.' );
}

/**
* @ticket 66019
*/
public function test_should_use_the_url_and_name_from_the_request() {
$_GET['linkurl'] = 'https://example.com/?a=1&b=2';
$_GET['name'] = 'O\\\'Brien & Sons';

$link = get_default_link_to_edit();

$this->assertSame( 'https://example.com/?a=1&#038;b=2', $link->link_url, 'The link URL was not escaped.' );
$this->assertSame( 'O&#039;Brien &amp; Sons', $link->link_name, 'The link name was not unslashed and escaped.' );
}
}
28 changes: 28 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/GetLinkToEdit_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @group admin
* @group bookmark
*
* @covers ::get_link_to_edit
*/
class Tests_Admin_Includes_Bookmark_GetLinkToEdit_Test extends WP_UnitTestCase {

/**
* @ticket 66019
*/
public function test_should_return_the_link_prepared_for_the_edit_context() {
$link_id = self::factory()->bookmark->create( array( 'link_name' => 'Tom & "Jerry"' ) );

$this->assertSame( 'Tom &amp; &quot;Jerry&quot;', get_link_to_edit( $link_id )->link_name );
}

/**
* @ticket 66019
*/
public function test_should_accept_a_link_object() {
$link_id = self::factory()->bookmark->create( array( 'link_name' => 'Tom & "Jerry"' ) );

$this->assertSame( 'Tom &amp; &quot;Jerry&quot;', get_link_to_edit( get_bookmark( $link_id ) )->link_name );
}
}
83 changes: 83 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/WpDeleteLink_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* @group admin
* @group bookmark
*
* @covers ::wp_delete_link
*/
class Tests_Admin_Includes_Bookmark_WpDeleteLink_Test extends WP_UnitTestCase {

/**
* @ticket 66019
*/
public function test_should_delete_the_link_and_return_true() {
$link_id = self::factory()->bookmark->create();

$this->assertTrue( wp_delete_link( $link_id ), 'wp_delete_link() should return true.' );
$this->assertNull( get_bookmark( $link_id ), 'The link was not deleted.' );
}

/**
* @ticket 66019
*/
public function test_should_remove_the_link_category_relationships() {
$term_id = self::factory()->term->create( array( 'taxonomy' => 'link_category' ) );
$link_id = self::factory()->bookmark->create( array( 'link_category' => array( $term_id ) ) );

wp_delete_link( $link_id );

$this->assertSame( array(), wp_get_object_terms( $link_id, 'link_category', array( 'fields' => 'ids' ) ) );
}

/**
* @ticket 66019
*/
public function test_should_clear_the_bookmark_cache() {
$link_id = self::factory()->bookmark->create();

// Prime the cache.
get_bookmark( $link_id );

wp_delete_link( $link_id );

$this->assertFalse( wp_cache_get( $link_id, 'bookmark' ) );
}

/**
* @ticket 66019
*/
public function test_should_fire_the_delete_actions_around_the_deletion() {
global $wpdb;

$link_id = self::factory()->bookmark->create();

$link_exists = static function () use ( $wpdb, $link_id ) {
return (bool) $wpdb->get_var( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_id = %d", $link_id ) );
};

$fired = array();
add_action(
'delete_link',
static function ( $id ) use ( &$fired, $link_exists ) {
$fired[] = array( 'delete_link', $id, $link_exists() );
}
);
add_action(
'deleted_link',
static function ( $id ) use ( &$fired, $link_exists ) {
$fired[] = array( 'deleted_link', $id, $link_exists() );
}
);

wp_delete_link( $link_id );

$this->assertSame(
array(
array( 'delete_link', $link_id, true ),
array( 'deleted_link', $link_id, false ),
),
$fired
);
}
}
32 changes: 32 additions & 0 deletions tests/phpunit/tests/admin/includes/bookmark/WpGetLinkCats_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @group admin
* @group bookmark
*
* @covers ::wp_get_link_cats
*/
class Tests_Admin_Includes_Bookmark_WpGetLinkCats_Test extends WP_UnitTestCase {

/**
* @ticket 66019
*/
public function test_should_return_the_link_category_ids() {
$alpha = self::factory()->term->create(
array(
'taxonomy' => 'link_category',
'name' => 'Alpha',
)
);
$beta = self::factory()->term->create(
array(
'taxonomy' => 'link_category',
'name' => 'Beta',
)
);

$link_id = self::factory()->bookmark->create( array( 'link_category' => array( $alpha, $beta ) ) );

$this->assertSame( array( $alpha, $beta ), wp_get_link_cats( $link_id ) );
}
}
Loading
Loading