From dc08225cb7844c438041e83ec00ba2dce481f833 Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Tue, 28 Jun 2022 19:25:51 -0700 Subject: [PATCH] Test coverage --- tests/test-automatically-paginate-posts.php | 106 ++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/tests/test-automatically-paginate-posts.php b/tests/test-automatically-paginate-posts.php index a31f49f..c3fad45 100755 --- a/tests/test-automatically-paginate-posts.php +++ b/tests/test-automatically-paginate-posts.php @@ -27,6 +27,7 @@ class Test_Automatically_Paginate_Posts extends WP_UnitTestCase { parent::set_up(); $this->_instance = new Automatically_Paginate_Posts(); + $this->_instance->action_init(); } /** @@ -74,4 +75,109 @@ class Test_Automatically_Paginate_Posts extends WP_UnitTestCase { 'Failed to assert that settings link is added for this plugin.' ); } + + /** + * Test `the_post` filtering when in admin. + * + * @covers ::filter_the_posts() + */ + public function test_filter_the_posts_admin() { + $old_screen = $GLOBALS['current_screen']; + + $GLOBALS['current_screen'] = new Test_Autopaging_Admin(); + + $test_posts = [ + 'unit-test', + ]; + + $this->assertEquals( + $test_posts, + $this->_instance->filter_the_posts( $test_posts ), + 'Failed to assert that posts are not modified in admin.' + ); + + $GLOBALS['current_screen'] = $old_screen; + } + + /** + * Test modifications to various posts. + * + * @covers ::filter_the_posts() + * @dataProvider data_provider_filter_the_posts + * + * @param string $expected Expected post content. + * @param array $input Test arguments. + */ + public function test_filter_the_posts( $expected, $input ) { + $post = $this->factory->post->create_and_get( $input['post_args'] ); + + update_option( 'autopaging_paging_type', $input['type'] ); + update_option( 'autopaging_num_pages', $input['num_pages'] ); + update_option( 'autopaging_num_words', $input['num_words'] ); + + $this->assertEquals( + $expected, + $this->_instance->filter_the_posts( [ $post ] )[0]->post_content + ); + } + + /** + * Data provide to test post filtering. + * + * @return array + */ + public function data_provider_filter_the_posts() { + return array( + 'Unsupported type' => array( + "I am a page.\r\n\r\nI should not be paginated.", + array( + 'post_args' => array( + 'post_type' => 'page', + 'post_content' => "I am a page.\r\n\r\nI should not be paginated.", + ), + 'type' => 'pages', + 'num_pages' => 2, + 'num_words' => 2, + ), + ), + 'Already paginated' => array( + "1\r\n\r\n<!--nextpage-->\r\n\r\n2\r\n\r\n3", + array( + 'post_args' => array( + 'post_type' => 'post', + 'post_content' => "1\r\n\r\n<!--nextpage-->\r\n\r\n2\r\n\r\n3", + ), + 'type' => 'pages', + 'num_pages' => 2, + 'num_words' => 2, + ), + ), + 'Classic post, two pages' => array( + "1\r\n\r\n2<!--nextpage-->\r\n\r\n3", + array( + 'post_args' => array( + 'post_type' => 'post', + 'post_content' => "1\r\n\r\n2\r\n\r\n3", + ), + 'type' => 'pages', + 'num_pages' => 2, + 'num_words' => 2, + ), + ), + ); + } +} + +/** + * Test class for admin-related restrictions. + */ +class Test_Autopaging_Admin { + /** + * Mock being in wp-admin. + * + * @return bool + */ + public function in_admin() { + return true; + } } -- GitLab