Skip to content

Commit

Permalink
test: add phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Mar 4, 2024
1 parent 687357b commit 62ad64c
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 15 deletions.
13 changes: 4 additions & 9 deletions wp/headless-wp/includes/classes/Preview/PreviewLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function register() {
add_filter( 'template_include', [ $this, 'handle_preview' ], 20 );

add_filter( 'post_link', [ $this, 'force_posts_drafts_to_have_permalinks' ], 10, 2 );
add_filter( 'post_permalink', [ $this, 'force_cpts_drafts_to_have_permalinks' ], 10, 2 );
add_filter( 'post_type_link', [ $this, 'force_cpts_drafts_to_have_permalinks' ], 10, 2 );
add_action( 'page_link', [ $this, 'force_page_drafts_to_have_permalinks' ], 10, 2 );
}

Expand All @@ -32,11 +32,6 @@ public function register() {
* @return string
*/
protected function force_drafts_to_have_permalinks( string $permalink, \WP_Post $post ): string {
// If this isn't a rest request do nothing
if ( ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
return $permalink;
}

if ( 'draft' !== $post->post_status ) {
return $permalink;
}
Expand All @@ -55,7 +50,7 @@ protected function force_drafts_to_have_permalinks( string $permalink, \WP_Post

[$permastruct, $post_name] = \get_sample_permalink( $post->ID );
$link = str_replace( '%postname%', $post_name, $permastruct );
$link = str_replace( '%pagename%', $post_name, $permastruct );
$link = str_replace( '%pagename%', $post_name, $link );

return $link;
}
Expand Down Expand Up @@ -91,9 +86,9 @@ public function force_posts_drafts_to_have_permalinks( string $permalink, \WP_Po
* @return string
*/
public function force_cpts_drafts_to_have_permalinks( string $permalink, \WP_Post $post ): string {
remove_filter( 'post_permalink', [ $this, 'force_cpts_drafts_to_have_permalinks' ], 10, 2 );
remove_filter( 'post_type_link', [ $this, 'force_cpts_drafts_to_have_permalinks' ], 10, 2 );
$link = $this->force_drafts_to_have_permalinks( $permalink, $post );
add_filter( 'post_permalink', [ $this, 'force_cpts_drafts_to_have_permalinks' ], 10, 2 );
add_filter( 'post_type_link', [ $this, 'force_cpts_drafts_to_have_permalinks' ], 10, 2 );

return $link;
}
Expand Down
172 changes: 166 additions & 6 deletions wp/headless-wp/tests/php/includes/TestPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@

namespace HeadlessWP\Tests;

use Exception;
use DomainException;
use DateTime;
use HeadlessWP\Preview\PreviewLink;
use HeadlessWP\Preview\PreviewToken;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use PHPUnit\Framework\ExpectationFailedException;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use WP_UnitTestCase;

/**
* Covers the test for the Preview functionality
*/
class TestPreview extends TestCase {
class TestPreview extends WP_UnitTestCase {

/**
* The PreviewLink class parser
Expand All @@ -27,13 +24,37 @@ class TestPreview extends TestCase {
*/
protected $preview;

/**
* wp_rewrite class
*
* @var \WP_Rewrite
*/
protected $wp_rewrite;

/**
* Sets up the Test class
*
* @return void
*/
public function set_up() {
parent::set_up();
$this->preview = new PreviewLink();
$this->preview->register();

/**
* The rewrite class
*
* @var \WP_Rewrite $wp_rewrite
*/
global $wp_rewrite;

$this->wp_rewrite = $wp_rewrite;

/**
* Change the permalink structure
*/
$this->wp_rewrite->init();
$this->wp_rewrite->set_permalink_structure( '/%postname%/' );
}

/**
Expand Down Expand Up @@ -118,4 +139,143 @@ public function test_get_payload_from_token_alternative_header() {

unset( $_SERVER['HTTP_X_HEADSTARTWP_AUTHORIZATION'] );
}

/**
* Tests draft posts links are not plain permalinks
*
* @return void
*/
public function test_draft_posts_permalink() {
$draft = $this->factory()->post->create_and_get(
[
'post_title' => 'Draft Post',
'post_status' => 'draft',
'post_content' => 'draf post',
'post_type' => 'post',
]
);

// simulate a REST request
$token = PreviewToken::generate(
[
'type' => 'preview',
'post_id' => $draft->ID,
]
);

$_SERVER['HTTP_AUTHORIZATION'] = "Bearer $token";

$this->assertEquals( 'http://localhost:8889/draft-post/', get_permalink( $draft ) );

unset( $_SERVER['HTTP_AUTHORIZATION'] );

// without authorization it shouldn't modify permalink
$this->assertEquals(
"http://localhost:8889/?p=$draft->ID",
get_permalink( $draft ),
"without authorization it shouldn't modify permalink"
);
}

/**
* Tests draft posts links are not plain permalinks
*
* @return void
*/
public function test_draft_posts_permalink_with_date() {
$this->wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );

$draft = $this->factory()->post->create_and_get(
[
'post_title' => 'Draft Post',
'post_status' => 'draft',
'post_content' => 'draf post',
'post_type' => 'post',
]
);

$date = DateTime::createFromFormat( 'Y-m-d H:i:s', $draft->post_date );

// simulate a REST request
$token = PreviewToken::generate(
[
'type' => 'preview',
'post_id' => $draft->ID,
]
);

$_SERVER['HTTP_AUTHORIZATION'] = "Bearer $token";

$this->assertEquals( "http://localhost:8889/{$date->format('Y/m/d')}/draft-post/", get_permalink( $draft ) );

unset( $_SERVER['HTTP_AUTHORIZATION'] );

$this->wp_rewrite->set_permalink_structure( '/%postname%/' );
}

/**
* Tests draft pages links are not plain permalinks
*
* @return void
*/
public function test_draft_pages_permalink() {
$draft = $this->factory()->post->create_and_get(
[
'post_title' => 'Draft Page',
'post_status' => 'draft',
'post_content' => 'draf Page',
'post_type' => 'page',
]
);

// simulate a REST request
$token = PreviewToken::generate(
[
'type' => 'preview',
'post_id' => $draft->ID,
]
);

$_SERVER['HTTP_AUTHORIZATION'] = "Bearer $token";

$this->assertEquals( 'http://localhost:8889/draft-page/', get_permalink( $draft ) );

unset( $_SERVER['HTTP_AUTHORIZATION'] );

$this->assertEquals( "http://localhost:8889/?page_id=$draft->ID", get_permalink( $draft ) );
}

/**
* Tests draft cpts are not plain permalinks
*
* @return void
*/
public function test_draft_cpt_permalink() {
register_post_type( 'book', [] );

$draft = $this->factory()->post->create_and_get(
[
'post_title' => 'Draft Book',
'post_status' => 'draft',
'post_content' => 'draf book',
'post_type' => 'book',
]
);

// simulate a REST request
$token = PreviewToken::generate(
[
'type' => 'preview',
'post_id' => $draft->ID,
]
);

$_SERVER['HTTP_AUTHORIZATION'] = "Bearer $token";

$this->assertEquals( 'http://localhost:8889/book/draft-book/', get_permalink( $draft ) );

unset( $_SERVER['HTTP_AUTHORIZATION'] );

$this->assertEquals( "http://localhost:8889/?post_type=book&p=$draft->ID", get_permalink( $draft ) );
}
}

0 comments on commit 62ad64c

Please sign in to comment.