| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group post |
| 5 | */ |
| 6 | class Tests_Post_GetPageByTitle extends WP_UnitTestCase { |
| 7 | |
| 8 | /** |
| 9 | * @ticket 36905 |
| 10 | */ |
| 11 | public function test_get_page_by_title() { |
| 12 | $page_id = self::factory()->post->create_object( array( 'post_title' => 'Some Page', 'post_type' => 'page' ) ); |
| 13 | $page = get_page_by_title( 'some Page' ); |
| 14 | $this->assertEquals( $page_id, $page->ID ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * @ticket 36905 |
| 19 | */ |
| 20 | public function test_should_not_care_about_cases() { |
| 21 | $page_id = self::factory()->post->create_object( array( 'post_title' => 'Some Page', 'post_type' => 'page' ) ); |
| 22 | $page = get_page_by_title( 'soMe paGE' ); |
| 23 | $this->assertEquals( $page_id, $page->ID ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @ticket 36905 |
| 28 | */ |
| 29 | public function test_should_match_exact_title() { |
| 30 | $page_id = self::factory()->post->create_object( array( 'post_title' => 'Some Page', 'post_type' => 'page' ) ); |
| 31 | |
| 32 | $page = get_page_by_title( 'Some Page Yeah' ); |
| 33 | $this->assertNull( $page ); |
| 34 | |
| 35 | $page = get_page_by_title( 'Yeah Some Page' ); |
| 36 | $this->assertNull( $page ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @ticket 36905 |
| 41 | */ |
| 42 | public function test_should_return_one_result_if_some_posts_has_the_same_title() { |
| 43 | $post_id_1 = self::factory()->post->create_object( array( 'post_title' => 'Some Title', 'post_type' => 'page' ) ); |
| 44 | $post_id_2 = self::factory()->post->create_object( array( 'post_title' => 'Some Title', 'post_type' => 'page' ) ); |
| 45 | $post = get_page_by_title( 'Some Title' ); |
| 46 | $this->assertEquals( $post_id_1, $post->ID ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @ticket 36905 |
| 51 | */ |
| 52 | public function test_should_obey_post_type() { |
| 53 | register_post_type( 'my-cpt' ); |
| 54 | $post_id = self::factory()->post->create_object( array( 'post_title' => 'Some CPT', 'post_type' => 'my-cpt' ) ); |
| 55 | |
| 56 | $post = get_page_by_title( 'Some CPT', OBJECT, 'my-cpt' ); |
| 57 | $this->assertEquals( $post_id, $post->ID ); |
| 58 | |
| 59 | $post = get_page_by_title( 'Some CPT', OBJECT, 'page' ); |
| 60 | $this->assertNull( $post ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @ticket 36905 |
| 65 | */ |
| 66 | public function test_should_get_different_post_types() { |
| 67 | register_post_type( 'my-cpt' ); |
| 68 | $post_id_1 = self::factory()->post->create_object( array( 'post_title' => 'Some CPT', 'post_type' => 'my-cpt' ) ); |
| 69 | $post_id_2 = self::factory()->post->create_object( array( 'post_title' => 'Some Page', 'post_type' => 'page' ) ); |
| 70 | $post = get_page_by_title( 'Some Page', OBJECT, array( 'my-cpt', 'page' ) ); |
| 71 | $this->assertEquals( $post_id_2, $post->ID ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @ticket 36905 |
| 76 | */ |
| 77 | public function test_should_get_different_post_statuses() { |
| 78 | register_post_type( 'my-cpt' ); |
| 79 | $post_id = self::factory()->post->create_object( array( 'post_title' => 'Some Trashed post', 'post_type' => 'page', 'post_status' => 'trash' ) ); |
| 80 | $found = get_page_by_title( 'Some Trashed post' ); |
| 81 | $this->assertEquals( $post_id, $found->ID ); |
| 82 | |
| 83 | $post_id = self::factory()->post->create_object( array( 'post_title' => 'Some Auto Draft post', 'post_type' => 'page', 'post_status' => 'auto-draft' ) ); |
| 84 | $found = get_page_by_title( 'Some Auto Draft post' ); |
| 85 | $this->assertEquals( $post_id, $found->ID ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @ticket 36905 |
| 90 | */ |
| 91 | public function test_output_param_should_be_obeyed() { |
| 92 | $post_id = self::factory()->post->create_object( array( 'post_title' => 'Some Page', 'post_type' => 'page' ) ); |
| 93 | |
| 94 | $found = get_page_by_title( 'Some Page' ); |
| 95 | $this->assertInternalType( 'object', $found ); |
| 96 | $this->assertSame( $post_id, $found->ID ); |
| 97 | |
| 98 | $found = get_page_by_title( 'Some Page', OBJECT ); |
| 99 | $this->assertInternalType( 'object', $found ); |
| 100 | $this->assertSame( $post_id, $found->ID ); |
| 101 | |
| 102 | $found = get_page_by_title( 'Some Page', ARRAY_N ); |
| 103 | $this->assertInternalType( 'array', $found ); |
| 104 | $this->assertSame( $post_id, $found[0] ); |
| 105 | |
| 106 | $found = get_page_by_title( 'Some Page', ARRAY_A ); |
| 107 | $this->assertInternalType( 'array', $found ); |
| 108 | $this->assertSame( $post_id, $found['ID'] ); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | } |