Skip to content

Commit

Permalink
test: confirm post_link filtering behavior.
Browse files Browse the repository at this point in the history
Confirms links are not rewritten on post-new.php pages when $_POST is empty. Also confirms Ajax requests to generate sample permalinks are not rewritten. These checks prevent the incorrect guid from being written to the database.

Confirms `wp-link-ajax` Ajax requests are not rewritten. This is used by the Classic Editor when linking content.

chore: update `enable_rewrites` values in tests to be `1` instead of `true`, for consistency.
  • Loading branch information
mindctrl committed Oct 31, 2023
1 parent f2f4249 commit f86c9a5
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
content_replacement,
post_preview_link,
image_source_replacement,
image_source_srcset_replacement
image_source_srcset_replacement,
post_link
};
use function WPE\FaustWP\Settings\faustwp_update_setting;

Expand Down Expand Up @@ -156,11 +157,51 @@ public function test_post_link_returns_unfiltered_link_when_content_replacement_
*/
public function test_post_link_returns_filtered_link_when_content_replacement_is_enabled() {
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', true );
faustwp_update_setting( 'enable_rewrites', '1' );

$this->assertSame( 'http://moo/?p=' . $this->post_id, get_permalink( $this->post_id ) );
}

public function test_post_link_returns_unfiltered_link_when_on_post_new_page(): void {
global $pagenow;
$pagenow = 'post-new.php';
self::assertSame( 'http://example.org/hello-world', post_link( 'http://example.org/hello-world' ) );
}

public function test_post_link_returns_unfiltered_link_on_ajax_requests_to_generate_permalinks_using_samplepermalinknonce(): void {
global $pagenow, $_REQUEST, $_POST;
$pagenow = 'admin-ajax.php';
wp_set_current_user( 1 );
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', '1' );
$_REQUEST['samplepermalinknonce'] = wp_create_nonce( 'samplepermalink' );
$_POST['samplepermalinknonce'] = $_REQUEST['samplepermalinknonce'];

self::assertSame( 'http://example.org/hello-world', post_link( 'http://example.org/hello-world' ) );

unset( $_REQUEST['samplepermalinknonce'], $_POST['samplepermalinknonce'] );
unset( $pagenow );
wp_set_current_user( null );
}

public function test_post_link_returns_unfiltered_link_on_ajax_requests_to_generate_permalinks_using_ajax_linking_nonce(): void {
global $pagenow, $_POST;
$pagenow = 'admin-ajax.php';
wp_set_current_user( 1 );
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', '1' );
add_filter( 'wp_doing_ajax', '__return_true' );
$_POST['_ajax_linking_nonce'] = wp_create_nonce( 'internal-linking' );
$_POST['action'] = 'wp-link-ajax';

self::assertSame( 'http://example.org/hello-world', post_link( 'http://example.org/hello-world' ) );

unset( $_POST['_ajax_linking_nonce'], $_POST['action'] );
unset( $pagenow );
remove_filter( 'wp_doing_ajax', '__return_true' );
wp_set_current_user( null );
}

/**
* Tests get_preview_post_link() returns rewritten value.
*/
Expand Down Expand Up @@ -200,7 +241,7 @@ public function test_post_preview_link_uses_frontend_uri_scheme() {
public function test_custom_post_type_post_preview_link_returns_filtered_link_when_content_replacement_is_enabled()
{
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', true );
faustwp_update_setting( 'enable_rewrites', '1' );
$post_id = $this->getCustomPostType();
$this->assertSame( 'http://moo/?document=' . $post_id . '&preview=true&previewPathname=' . rawurlencode( wp_make_link_relative( get_permalink( $post_id ) ) ) . '&p=' . $post_id . '&typeName=Document', get_preview_post_link( $post_id ) );
faustwp_update_setting( 'frontend_uri', null );
Expand All @@ -213,7 +254,7 @@ public function test_custom_post_type_post_preview_link_returns_filtered_link_wh
public function test_custom_post_type_post_link_returns_unfiltered_link_when_content_replacement_is_enabled()
{
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', true );
faustwp_update_setting( 'enable_rewrites', '1' );
$post_id = $this->getCustomPostType();
$this->assertSame( 'http://example.org/?document=' . $post_id, get_permalink($post_id) );
faustwp_update_setting( 'frontend_uri', null );
Expand Down

0 comments on commit f86c9a5

Please sign in to comment.