| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * Test WP_Export_Query class |
| | 5 | * |
| | 6 | * @group export |
| | 7 | * @ticket 22435 |
| | 8 | */ |
| | 9 | class Test_WP_Export_Query extends WP_UnitTestCase { |
| | 10 | function setUp() { |
| | 11 | if ( ! class_exists( 'WP_Export_Query' ) ) { |
| | 12 | $this->markTestSkipped( "WP_Export_Query class doesn't exist" ); |
| | 13 | } |
| | 14 | |
| | 15 | parent::setUp(); |
| | 16 | } |
| | 17 | |
| | 18 | function test_WP_Export_Query_should_be_initialized_with_an_array() { |
| | 19 | $export = new WP_Export_Query( array( 'author' => 'all' ) ); |
| | 20 | $this->assertTrue( (bool) $export ); |
| | 21 | } |
| | 22 | |
| | 23 | function test_WP_Export_Query_should_use_post_ids_if_passed() { |
| | 24 | $export = new WP_Export_Query( array( 'post_ids' => array( 1, 2, 3 ) ) ); |
| | 25 | $this->assertEquals( array( 1, 2, 3 ), $export->post_ids() ); |
| | 26 | } |
| | 27 | |
| | 28 | function test_WP_Export_Query_should_filter_all_posts_if_all_arg_is_true() { |
| | 29 | $post_id = $this->factory->post->create(); |
| | 30 | $export = new WP_Export_Query(); |
| | 31 | $this->assertEquals( array( $post_id ), $export->post_ids() ); |
| | 32 | } |
| | 33 | |
| | 34 | function test_WP_Export_Query_should_filter_all_posts_if_no_args_passed() { |
| | 35 | $post_id = $this->factory->post->create(); |
| | 36 | $export = new WP_Export_Query(); |
| | 37 | $this->assertEquals( array( $post_id ), $export->post_ids() ); |
| | 38 | } |
| | 39 | |
| | 40 | function test_WP_Export_Query_should_not_export_anything_if_post_type_arg_is_set_to_non_existing_post_type() { |
| | 41 | $post_id = $this->factory->post->create(); |
| | 42 | $export = new WP_Export_Query( array( 'post_type' => 'baba' ) ); |
| | 43 | $this->assertEquals( array(), $export->post_ids() ); |
| | 44 | } |
| | 45 | |
| | 46 | function test_WP_Export_Query_should_filter_only_posts_with_a_certain_post_type_if_the_post_type_arg_is_set() { |
| | 47 | register_post_type( 'baba' ); |
| | 48 | $post_id = $this->factory->post->create( array( 'post_type' => 'baba' ) ); |
| | 49 | $_ = $this->factory->post->create( array( 'post_type' => 'dyado' ) ); |
| | 50 | $export = new WP_Export_Query( array( 'post_type' => 'baba' ) ); |
| | 51 | $this->assertEquals( array( $post_id ), $export->post_ids() ); |
| | 52 | _unregister_post_type( 'baba' ); |
| | 53 | } |
| | 54 | |
| | 55 | function test_WP_Export_Query_should_not_export_post_types_with_can_export_set_to_false() { |
| | 56 | register_post_type( 'non-exportable', array( 'can_export' => false ) ); |
| | 57 | register_post_type( 'exportable', array( 'can_export' => true ) ); |
| | 58 | $non_exportable_post_id = $this->factory->post->create( array( 'post_type' => 'non-exportable' ) ); |
| | 59 | $exportable_post_id = $this->factory->post->create( array( 'post_type' => 'exportable' ) ); |
| | 60 | $export = new WP_Export_Query(); |
| | 61 | $this->assertEquals( array( $exportable_post_id ), $export->post_ids() ); |
| | 62 | _unregister_post_type( 'non-exportable' ); |
| | 63 | _unregister_post_type( 'exportable' ); |
| | 64 | } |
| | 65 | |
| | 66 | function test_WP_Export_Query_should_not_export_auto_drafts_by_default() { |
| | 67 | $post_id = $this->factory->post->create( array( 'post_status' => 'auto-draft' ) ); |
| | 68 | $export = new WP_Export_Query(); |
| | 69 | $this->assertEquals( array(), $export->post_ids() ); |
| | 70 | } |
| | 71 | |
| | 72 | function test_WP_Export_Query_should_filter_only_posts_with_certain_status_if_status_arg_is_set() { |
| | 73 | $post_id_baba = $this->factory->post->create( array( 'post_status' => 'baba' ) ); |
| | 74 | $post_id_dudu = $this->factory->post->create( array( 'post_status' => 'dudu' ) ); |
| | 75 | $export = new WP_Export_Query( array( 'status' => 'baba' ) ); |
| | 76 | $this->assertEquals( array( $post_id_baba ), $export->post_ids() ); |
| | 77 | } |
| | 78 | |
| | 79 | function test_WP_Export_Query_should_filter_only_posts_with_certain_author_id_if_status_arg_is_a_number() { |
| | 80 | $user_id = $this->factory->user->create(); |
| | 81 | $post_by_user = $this->factory->post->create( array( 'post_author' => $user_id ) ); |
| | 82 | $other_post = $this->factory->post->create( array( 'post_author' => $user_id + 1 ) ); |
| | 83 | $export = new WP_Export_Query( array( 'author' => $user_id ) ); |
| | 84 | $this->assertEquals( array( $post_by_user ), $export->post_ids() ); |
| | 85 | } |
| | 86 | |
| | 87 | function test_WP_Export_Query_should_filter_only_posts_with_certain_author_name_if_status_arg_is_a_username() { |
| | 88 | $user = $this->factory->user->create_and_get( array( 'user_login' => 'baba' ) ); |
| | 89 | $post_by_user = $this->factory->post->create( array( 'post_author' => $user->ID ) ); |
| | 90 | $other_post = $this->factory->post->create( array( 'post_author' => $user->ID + 1 ) ); |
| | 91 | $export = new WP_Export_Query( array( 'author' => 'baba' ) ); |
| | 92 | $this->assertEquals( array( $post_by_user ), $export->post_ids() ); |
| | 93 | } |
| | 94 | |
| | 95 | function test_WP_Export_Query_should_filter_only_posts_with_certain_author_object_if_author_is_an_object_with_ID_member_variable() { |
| | 96 | $user = $this->factory->user->create_and_get(); |
| | 97 | $post_by_user = $this->factory->post->create( array( 'post_author' => $user->ID ) ); |
| | 98 | $other_post = $this->factory->post->create( array( 'post_author' => $user->ID + 1 ) ); |
| | 99 | $export = new WP_Export_Query( array( 'author' => $user ) ); |
| | 100 | $this->assertEquals( array( $post_by_user ), $export->post_ids() ); |
| | 101 | } |
| | 102 | |
| | 103 | function test_WP_Export_Query_should_filter_only_posts_after_certain_start_date_if_start_date_arg_is_passed() { |
| | 104 | $post_before = $this->factory->post->create( array( 'post_date' => '2012-11-10 23:59:59' ) ); |
| | 105 | $post_after = $this->factory->post->create( array( 'post_date' => '2012-11-11 00:00:00' ) ); |
| | 106 | $export = new WP_Export_Query( array( 'start_date' => '2012-11-11' ) ); |
| | 107 | $this->assertEquals( array( $post_after ), $export->post_ids() ); |
| | 108 | } |
| | 109 | |
| | 110 | function test_WP_Export_Query_should_filter_only_posts_after_certain_end_date_if_end_date_arg_is_passed() { |
| | 111 | $post_before = $this->factory->post->create( array( 'post_date' => '2012-11-10 23:59:59' ) ); |
| | 112 | $post_after = $this->factory->post->create( array( 'post_date' => '2012-11-11 00:00:00' ) ); |
| | 113 | $export = new WP_Export_Query( array( 'end_date' => '2012-11-10' ) ); |
| | 114 | $this->assertEquals( array( $post_before ), $export->post_ids() ); |
| | 115 | } |
| | 116 | |
| | 117 | function test_WP_Export_Query_should_filter_only_posts_with_certain_category_if_category_arg_is_passed() { |
| | 118 | $category_id = $this->factory->category->create( array( 'name' => 'baba' ) ); |
| | 119 | $post_with_category = $this->factory->post->create( array( 'post_category' => array( $category_id ) ) ); |
| | 120 | $post_without = $this->factory->post->create(); |
| | 121 | $export = new WP_Export_Query( array( 'post_type' => 'post', 'category' => 'baba' ) ); |
| | 122 | $this->assertEquals( array( $post_with_category ), $export->post_ids() ); |
| | 123 | } |
| | 124 | |
| | 125 | function test_WP_Export_Query_should_filter_only_posts_with_certain_category_id_if_category_arg_is_passed() { |
| | 126 | $category_id = $this->factory->category->create( array( 'name' => 'baba' ) ); |
| | 127 | $post_with_category = $this->factory->post->create( array( 'post_category' => array( $category_id ) ) ); |
| | 128 | $post_without = $this->factory->post->create(); |
| | 129 | $export = new WP_Export_Query( array( 'post_type' => 'post', 'category' => $category_id ) ); |
| | 130 | $this->assertEquals( array( $post_with_category ), $export->post_ids() ); |
| | 131 | } |
| | 132 | |
| | 133 | function test_WP_Export_Query_should_filter_posts_by_category_only_for_post_post_type() { |
| | 134 | $category_id = $this->factory->category->create( array( 'name' => 'baba' ) ); |
| | 135 | $post_with_category = $this->factory->post->create( array( 'post_category' => array( $category_id ) ) ); |
| | 136 | $post_without = $this->factory->post->create(); |
| | 137 | $different_post_type = $this->factory->post->create( array( 'post_type' => 'page' ) ); |
| | 138 | $export = new WP_Export_Query( array( 'category' => $category_id ) ); |
| | 139 | $this->assertEqualSets( array( $post_with_category, $post_without, $different_post_type ), $export->post_ids() ); |
| | 140 | } |
| | 141 | |
| | 142 | function test_WP_Export_Query_should_include_attachments_of_posts_if_we_are_filtering_only_some_post_types() { |
| | 143 | register_post_type( 'baba' ); |
| | 144 | $post_id = $this->factory->post->create( array( 'post_type' => 'baba' ) ); |
| | 145 | $attachment_post_id = $this->factory->post->create( array( 'post_type' => 'attachment', 'post_parent' => $post_id ) ); |
| | 146 | $export = new WP_Export_Query( array( 'post_type' => 'baba' ) ); |
| | 147 | $this->assertEquals( array( $post_id, $attachment_post_id ), $export->post_ids() ); |
| | 148 | _unregister_post_type( 'baba' ); |
| | 149 | } |
| | 150 | |
| | 151 | function test_authors_should_return_list_of_users_for_each_post_author() { |
| | 152 | $user_id = $this->factory->user->create(); |
| | 153 | $this->factory->post->create( array( 'post_author' => $user_id ) ); |
| | 154 | $export = new WP_Export_Query(); |
| | 155 | $authors = $export->authors(); |
| | 156 | $this->assertEquals( 1, count( $authors ) ); |
| | 157 | $this->assertEquals( $user_id, $authors[0]->ID ); |
| | 158 | } |
| | 159 | |
| | 160 | function test_authors_should_skip_non_existing_authors() { |
| | 161 | $this->factory->post->create( array( 'post_author' => 11 ) ); |
| | 162 | $export = new WP_Export_Query(); |
| | 163 | $this->assertEquals( array(), $export->authors() ); |
| | 164 | } |
| | 165 | |
| | 166 | function test_authors_should_skip_auto_draft_authors() { |
| | 167 | $user_id = $this->factory->user->create(); |
| | 168 | $this->factory->post->create( array( 'post_author' => $user_id, 'post_status' => 'auto-draft' ) ); |
| | 169 | $export = new WP_Export_Query(); |
| | 170 | $this->assertEquals( array(), $export->authors() ); |
| | 171 | } |
| | 172 | |
| | 173 | function test_categories_should_return_only_the_category_we_are_filtering_on() { |
| | 174 | $category_id = $this->factory->category->create( array( 'name' => 'baba' ) ); |
| | 175 | $other_category_id = $this->factory->category->create( array( 'name' => 'dyado' ) ); |
| | 176 | $export = new WP_Export_Query( array( 'post_type' => 'post', 'category' => $category_id ) ); |
| | 177 | $this->assertEquals( 1, count( $export->categories() ) ); |
| | 178 | } |
| | 179 | |
| | 180 | function test_categories_should_return_no_categories_if_we_are_requesting_only_one_post_type() { |
| | 181 | $category_id = $this->factory->category->create(); |
| | 182 | $export = new WP_Export_Query( array( 'post_type' => 'post' ) ); |
| | 183 | $this->assertEquals( array(), $export->categories() ); |
| | 184 | } |
| | 185 | |
| | 186 | function test_categories_should_return_all_categories_if_we_are_requesting_all_post_types() { |
| | 187 | $category_id = $this->factory->category->create(); |
| | 188 | $another_category_id = $this->factory->category->create(); |
| | 189 | $export = new WP_Export_Query(); |
| | 190 | $this->assertEqualSets( array( 1, $category_id, $another_category_id ), self::get_term_ids( $export->categories() ) ); |
| | 191 | } |
| | 192 | |
| | 193 | function test_categories_should_not_return_a_child_before_its_parent_category() { |
| | 194 | $child_category_id = $this->factory->category->create(); |
| | 195 | $top_category_id = $this->factory->category->create(); |
| | 196 | wp_update_term( $child_category_id, 'category', array( 'parent' => $top_category_id ) ); |
| | 197 | $export = new WP_Export_Query(); |
| | 198 | $this->assertNoChildBeforeParent( $export->categories() ); |
| | 199 | } |
| | 200 | |
| | 201 | function test_tags_should_return_all_tags() { |
| | 202 | $tag_id = $this->factory->tag->create(); |
| | 203 | $export = new WP_Export_Query(); |
| | 204 | $this->assertEquals( array( $tag_id ), self::get_term_ids( $export->tags() ) ); |
| | 205 | } |
| | 206 | |
| | 207 | function test_tags_should_return_no_tags_if_we_are_requesting_only_one_post_type() { |
| | 208 | $category_id = $this->factory->tag->create(); |
| | 209 | $export = new WP_Export_Query( array( 'post_type' => 'post' ) ); |
| | 210 | $this->assertEquals( array(), $export->tags() ); |
| | 211 | } |
| | 212 | |
| | 213 | function test_custom_taxonomies_terms_should_return_all_terms() { |
| | 214 | register_taxonomy( 'taxonomy_all', 'post' ); |
| | 215 | $term_id = $this->factory->term->create( array( 'taxonomy' => 'taxonomy_all' ) ); |
| | 216 | $export = new WP_Export_Query(); |
| | 217 | $this->assertEquals( array( $term_id ), self::get_term_ids( $export->custom_taxonomies_terms() ) ); |
| | 218 | _unregister_taxonomy( 'taxonomy_all' ); |
| | 219 | } |
| | 220 | |
| | 221 | function test_custom_taxonomes_terms_should_return_no_terms_if_we_are_requesting_only_one_post_type() { |
| | 222 | register_taxonomy( 'taxonomy_one_post_type', 'post' ); |
| | 223 | $term_id = $this->factory->term->create( array( 'taxonomy' => 'taxonomy_one_post_type' ) ); |
| | 224 | $export = new WP_Export_Query( array( 'post_type' => 'post' ) ); |
| | 225 | $this->assertEquals( array(), $export->custom_taxonomies_terms() ); |
| | 226 | _unregister_taxonomy( 'taxonomy_one_post_type' ); |
| | 227 | } |
| | 228 | |
| | 229 | function test_custom_taxonomies_terms_should_not_return_a_child_before_its_parent_term() { |
| | 230 | register_taxonomy( 'heir', 'post', array( 'hierarchical' => true ) ); |
| | 231 | $child_term_id = $this->factory->term->create( array( 'taxonomy' => 'heir' ) ); |
| | 232 | $top_term_id = $this->factory->term->create( array( 'taxonomy' => 'heir' ) ); |
| | 233 | wp_update_term( $child_term_id, 'heir', array( 'parent' => $top_term_id ) ); |
| | 234 | $export = new WP_Export_Query(); |
| | 235 | $this->assertNoChildBeforeParent( $export->custom_taxonomies_terms() ); |
| | 236 | _unregister_taxonomy( 'heir' ); |
| | 237 | } |
| | 238 | |
| | 239 | private function assertNoChildBeforeParent( $terms ) { |
| | 240 | $visited = array(); |
| | 241 | foreach( $terms as $term ) { |
| | 242 | $this->assertTrue( isset( $visited[$term->parent] ) || !$term->parent ); |
| | 243 | $visited[$term->term_id] = true; |
| | 244 | } |
| | 245 | } |
| | 246 | |
| | 247 | private static function get_term_ids( $terms ) { |
| | 248 | return array_values( array_map( array( __CLASS__, '_get_term_ids_cb' ), $terms ) ); |
| | 249 | } |
| | 250 | |
| | 251 | private static function _get_term_ids_cb( $c ) { |
| | 252 | return intval( $c->term_id ); |
| | 253 | } |
| | 254 | |
| | 255 | } |
| | 256 | |