|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @group link |
| 4 | + * @covers ::get_preview_post_link |
| 5 | + */ |
| 6 | +class Tests_Link_GetPreviewPostLink extends WP_UnitTestCase { |
| 7 | + |
| 8 | + public function test_get_preview_post_link() { |
| 9 | + $post = self::factory()->post->create(); |
| 10 | + |
| 11 | + $this->assertEquals( add_query_arg( 'preview', 'true', get_permalink( $post ) ), get_preview_post_link( $post) ); |
| 12 | + } |
| 13 | + |
| 14 | + public function test_get_preview_post_link_should_add_additional_query_vars() { |
| 15 | + $post = self::factory()->post->create(); |
| 16 | + |
| 17 | + $expected = add_query_arg( array( |
| 18 | + 'foo' => 'bar', |
| 19 | + 'bar' => 'baz', |
| 20 | + 'preview' => 'true', |
| 21 | + ), get_permalink( $post ) ); |
| 22 | + |
| 23 | + $this->assertEquals( $expected, get_preview_post_link( $post, array( |
| 24 | + 'foo' => 'bar', |
| 25 | + 'bar' => 'baz', |
| 26 | + ) ) ); |
| 27 | + } |
| 28 | + |
| 29 | + public function test_get_preview_post_link_should_use_custom_base_preview_link() { |
| 30 | + $post = self::factory()->post->create(); |
| 31 | + |
| 32 | + $expected = 'https://google.com/?foo=bar&bar=baz&preview=true'; |
| 33 | + |
| 34 | + $this->assertEquals( $expected, get_preview_post_link( $post, array( |
| 35 | + 'foo' => 'bar', |
| 36 | + 'bar' => 'baz', |
| 37 | + ), 'https://google.com/' ) ); |
| 38 | + } |
| 39 | + |
| 40 | + public function test_get_preview_post_link_should_return_null_for_non_existent_post() { |
| 41 | + $this->assertNull( get_preview_post_link() ); |
| 42 | + $this->assertNull( get_preview_post_link( 9999 ) ); |
| 43 | + $this->assertNull( get_preview_post_link( 'foo' ) ); |
| 44 | + } |
| 45 | + |
| 46 | + public function test_get_preview_post_link_for_global_post() { |
| 47 | + $post = self::factory()->post->create_and_get(); |
| 48 | + |
| 49 | + $GLOBALS['post'] = $post; |
| 50 | + |
| 51 | + $this->assertEquals( add_query_arg( 'preview', 'true', get_permalink( $post ) ), get_preview_post_link() ); |
| 52 | + } |
| 53 | + |
| 54 | + public function test_get_preview_post_link_should_return_empty_string_for_non_viewable_post_type() { |
| 55 | + $post_type = register_post_type( rand_str(12), array( |
| 56 | + 'public' => false, |
| 57 | + ) ); |
| 58 | + |
| 59 | + $post = self::factory()->post->create( array( |
| 60 | + 'post_type' => $post_type->name |
| 61 | + ) ); |
| 62 | + |
| 63 | + $this->assertSame( '', get_preview_post_link( $post ) ); |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments