WordPress.org

Make WordPress Core

Ticket #22435: 22435-unittests.patch

File 22435-unittests.patch, 16.9 KB (added by boonebgorges, 5 years ago)

Unit tests, previously in the repo but removed as per #30284.

  • new file tests/phpunit/tests/export/class-wp-export-query.php

    diff --git tests/phpunit/tests/export/class-wp-export-query.php tests/phpunit/tests/export/class-wp-export-query.php
    new file mode 100644
    index 0000000..0fd90d6
    - +  
     1<?php
     2
     3/**
     4 * Test WP_Export_Query class
     5 *
     6 * @group export
     7 * @ticket 22435
     8 */
     9class 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
  • new file tests/phpunit/tests/export/functions.export.php

    diff --git tests/phpunit/tests/export/functions.export.php tests/phpunit/tests/export/functions.export.php
    new file mode 100644
    index 0000000..e3b77e7
    - +  
     1<?php
     2
     3/**
     4 * Test export functions
     5 *
     6 * @group export
     7 * @ticket 22435
     8 */
     9class Test_WP_Export_Functions extends WP_UnitTestCase {
     10        function setUp() {
     11                if ( ! function_exists( 'wp_export' ) ) {
     12                        $this->markTestSkipped( "wp_export function doesn't exist" );
     13                }
     14
     15                parent::setUp();
     16        }
     17       
     18        function test_wp_export_returns_wp_error_if_the_writer_throws_Export_exception() {
     19                $this->assertTrue( is_wp_error( wp_export( array( 'writer' => 'Test_WP_Export_Stub_Writer_Throws_Export_Exception' ) ) ) );
     20        }
     21
     22        function test_wp_export_passes_the_exception_if_the_writer_throws_other_exception() {
     23                $this->setExpectedException( 'Exception' );
     24                wp_export( array( 'writer' => 'Test_WP_Export_Stub_Writer_Throws_Other_Exception' ) );
     25        }
     26
     27}
     28
     29class Test_WP_Export_Stub_Writer_Throws_Export_Exception {
     30        function __construct( $formatter ) {
     31        }
     32        function export() {
     33                throw new WP_Export_Exception( 'baba' );
     34        }
     35}
     36
     37class Test_WP_Export_Stub_Writer_Throws_Other_Exception {
     38        function __construct( $formatter ) {
     39        }
     40        function export() {
     41                throw new Exception( 'baba' );
     42        }
     43}
  • new file tests/phpunit/tests/export/writers.php

    diff --git tests/phpunit/tests/export/writers.php tests/phpunit/tests/export/writers.php
    new file mode 100644
    index 0000000..6715d17
    - +  
     1<?php
     2
     3/**
     4 * Test WP_Export_*_Writer classes
     5 *
     6 * @group export
     7 * @ticket 22435
     8 */
     9class Test_WP_Export_Writers extends WP_UnitTestCase {
     10        function test_export_returner_returns_all_the_return_values() {
     11                if ( ! class_exists( 'WP_Export_Returner' ) ) {
     12                        $this->markTestSkipped( "WP_Export_Returner class doesn't exist" );
     13                }
     14                $returner = new WP_Export_Returner( $this->get_x_formatter() );
     15                $this->assertEquals( 'xxx' , $returner->export() );
     16        }
     17
     18        private function get_x_formatter() {
     19                $methods = array( 'before_posts', 'posts', 'after_posts' );
     20                $formatter = $this->getMock( 'WP_Export_WXR_Formatter', $methods, array( null ) );
     21                foreach( $methods as $method ) {
     22                        $return = 'posts' == $method? array( 'x' ) : 'x';
     23                        $formatter->expects( $this->once() )->method( $method )->with()->will( $this->returnValue( $return ) );
     24                }
     25                return $formatter;
     26        }
     27}
     28
  • new file tests/phpunit/tests/iterators.php

    diff --git tests/phpunit/tests/iterators.php tests/phpunit/tests/iterators.php
    new file mode 100644
    index 0000000..bd5f0d7
    - +  
     1<?php
     2
     3/**
     4 * @ticket 22435
     5 */
     6class Test_WP_Post_IDs_Iterator extends WP_UnitTestCase {
     7        function setUp() {
     8                if ( ! class_exists( 'WP_Post_IDs_Iterator' ) ) {
     9                        $this->markTestSkipped( "WP_Post_IDs_Iterator class doesn't exist" );
     10                }
     11
     12                parent::setUp();
     13        }
     14
     15        function test_create() {
     16                new WP_Post_IDs_Iterator( array( 1, 2, 3 ) );
     17        }
     18
     19        function test_no_posts() {
     20                $this->assertIteratorReturnsSamePostIDs( array() );
     21        }
     22
     23        function test_less_ids_than_limit() {
     24                $post_id_0 = $this->factory->post->create();
     25                $post_id_1 = $this->factory->post->create();
     26                $this->assertIteratorReturnsSamePostIDs( array( $post_id_0, $post_id_1 ), 10 );
     27        }
     28
     29        function test_ids_exactly_as_limit() {
     30                $post_id_0 = $this->factory->post->create();
     31                $post_id_1 = $this->factory->post->create();
     32                $this->assertIteratorReturnsSamePostIDs( array( $post_id_0, $post_id_1 ), 2 );
     33        }
     34
     35        function test_more_ids_than_limit() {
     36                $post_id_0 = $this->factory->post->create();
     37                $post_id_1 = $this->factory->post->create();
     38                $post_id_2 = $this->factory->post->create();
     39                $this->assertIteratorReturnsSamePostIDs( array( $post_id_0, $post_id_1, $post_id_2 ), 2 );
     40        }
     41
     42        function test_ids_exactly_twice_more_than_limit() {
     43                $post_id_0 = $this->factory->post->create();
     44                $post_id_1 = $this->factory->post->create();
     45                $post_id_2 = $this->factory->post->create();
     46                $post_id_3 = $this->factory->post->create();
     47                $this->assertIteratorReturnsSamePostIDs( array( $post_id_0, $post_id_1, $post_id_2, $post_id_3 ), 2 );
     48        }
     49
     50        private function assertIteratorReturnsSamePostIDs( $post_ids, $limit = 2 ) {
     51                $this->assertEquals( $post_ids, wp_list_pluck( iterator_to_array( new WP_Post_IDs_Iterator( $post_ids, $limit ) ), 'ID' ) );
     52        }
     53}