diff --git a/tests/Feature/AdminFunctionTest.php b/tests/Feature/AdminFunctionTest.php index a9af465..ab2cf43 100644 --- a/tests/Feature/AdminFunctionTest.php +++ b/tests/Feature/AdminFunctionTest.php @@ -8,6 +8,7 @@ namespace Alley\WP\WP_SEO\Tests\Feature; use Alley\WP\WP_SEO\Tests\TestCase; +use Mantle\Testing\Utils; class AdminFunctionTest extends TestCase { /** @@ -17,11 +18,7 @@ class AdminFunctionTest extends TestCase { */ function test_admin_functions_contain( $function, $should, $contain, $args ) { // Capture the output of the function. - ob_start(); - $function( ...$args ); - $output = ob_get_clean(); - - self::assertStringContainsString( $contain, $output, $should ); + self::assertStringContainsString( $contain, Utils::get_echo( $function, $args ), $should ); } /** diff --git a/tests/Feature/MetaboxesTest.php b/tests/Feature/MetaboxesTest.php index b7d26ea..b85bdae 100644 --- a/tests/Feature/MetaboxesTest.php +++ b/tests/Feature/MetaboxesTest.php @@ -8,6 +8,7 @@ namespace Alley\WP\WP_SEO\Tests\Feature; use Alley\WP\WP_SEO\Tests\TestCase; +use Mantle\Testing\Utils; use WP_SEO_Settings; use WP_SEO; @@ -72,9 +73,7 @@ function test_post_meta_fields() { $post = get_post( $post_ID ); // Capture the output of the function. - ob_start(); - WP_SEO()->post_meta_fields( $post ); - $html = ob_get_clean(); + $html = Utils::get_echo( [ WP_SEO(), 'post_meta_fields' ], [ $post ] ); self::assertStringContainsString( 'name="seo_meta[title]" value="' . $title . '" size="96"', $html ); self::assertMatchesRegularExpression( '/]+type="hidden"[^>]+name="wp-seo-nonce"/', $html ); @@ -92,9 +91,7 @@ function test_save_post_fields() { $post = get_post( $post_ID ); // Capture the output of the function. - ob_start(); - WP_SEO()->post_meta_fields( $post ); - $html = ob_get_clean(); + $html = Utils::get_echo( [ WP_SEO(), 'post_meta_fields' ], [ $post ] ); // No $_POST. $this->assertNull( WP_SEO()->save_post_fields( $post_ID ) ); @@ -181,9 +178,7 @@ function test_add_term_boxes() { */ function test_add_term_meta_fields() { // Capture the output of the function. - ob_start(); - WP_SEO()->add_term_meta_fields( 'category' ); - $html = ob_get_clean(); + $html = Utils::get_echo( [ WP_SEO(), 'add_term_meta_fields' ], [ 'category' ] ); self::assertMatchesRegularExpression( '/]+type="hidden"[^>]+name="wp-seo-nonce"/', $html ); self::assertStringContainsString( 'name="seo_meta[title]"', $html ); @@ -209,9 +204,7 @@ function test_edit_term_meta_fields() { ); // Capture the output of the function. - ob_start(); - WP_SEO()->edit_term_meta_fields( $category, 'category' ); - $html = ob_get_clean(); + $html = Utils::get_echo( [ WP_SEO(), 'edit_term_meta_fields' ], [ $category, 'category' ] ); self::assertMatchesRegularExpression( '/]+type="hidden"[^>]+name="wp-seo-nonce"/', $html ); self::assertMatchesRegularExpression( "/{$description}<\/textarea>/", $html ); @@ -226,9 +219,7 @@ function test_save_term_fields() { $category = get_term( $category_ID, 'category' ); // Capture the output of the function. - ob_start(); - WP_SEO()->edit_term_meta_fields( $category, 'category' ); - $html = ob_get_clean(); + $html = Utils::get_echo( [ WP_SEO(), 'add_term_meta_fields' ], [ $category, 'category' ] ); // No $_POST. $this->assertNull( WP_SEO()->save_term_fields( $category_ID, $category->term_taxonomy_id, 'category' ) ); diff --git a/tests/Feature/PropertiesTest.php b/tests/Feature/PropertiesTest.php new file mode 100644 index 0000000..80ce474 --- /dev/null +++ b/tests/Feature/PropertiesTest.php @@ -0,0 +1,52 @@ +set_properties(); + } + + function tearDown(): void { + parent::tearDown(); + // Leave the place as we found it. + remove_filter( 'wp_seo_formatting_tags', [ $this, '_add_mock' ] ); + remove_filter( 'wp_seo_formatting_tags', [ $this, '_add_illegals' ] ); + WP_SEO()->set_properties(); + } + + function _add_mock( $tags ) { + $tags['is_a_tag'] = $this->getMockForAbstractClass( 'WP_SEO_Formatting_Tag' ); + + return $tags; + } + + function _add_illegals( $tags ) { + $tags['is_non_object'] = true; + $tags['is_wrong_object'] = new \stdClass; + + return $tags; + } + + function test_legal_tag() { + $this->assertArrayHasKey( 'is_a_tag', WP_SEO()->formatting_tags ); + } + + function test_illegal_tags() { + $this->assertArrayNotHasKey( 'is_non_object', WP_SEO()->formatting_tags ); + $this->assertArrayNotHasKey( 'is_wrong_object', WP_SEO()->formatting_tags ); + } + +} diff --git a/tests/Feature/SettingsPageTest.php b/tests/Feature/SettingsPageTest.php new file mode 100644 index 0000000..1af8532 --- /dev/null +++ b/tests/Feature/SettingsPageTest.php @@ -0,0 +1,370 @@ +add_options_page(); + + global $submenu; + $this->assertContains( + [ + 'SEO', + 'manage_options', + 'wp-seo', + 'WP SEO Settings', + ], + $submenu['options-general.php'] + ); + } + + /** + * Make sure we have a help tab. + */ + function test_add_help_tab() { + set_current_screen( 'front' ); + WP_SEO_Settings()->add_help_tab(); + + $actual = get_current_screen()->get_help_tab( 'formatting-tags' ); + // Not all versions we test against include the priority. + if ( isset( $actual['priority'] ) ) { + unset( $actual['priority'] ); + } + + $this->assertEquals( $actual, [ + 'id' => 'formatting-tags', + 'title' => 'Formatting Tags', + 'content' => '', + 'callback' => [ WP_SEO_Settings(), 'view_formatting_tags_help_tab' ], + ] ); + } + + /** + * Test that the "example URL" method includes the text and any included link. + */ + function test_example_url() { + + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_url' ], [ 'Demo text' ] ); + $this->assertSame( '

Demo text

', $html ); + + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_url' ], [ 'Demo text', 'http://wordpress.org' ] ); + $this->assertStringContainsString( 'http://wordpress.org', $html ); + } + + /** + * Test that the example of a Post includes a link to the latest post. + */ + function test_example_permalink() { + $post_ID = $this->factory->post->create(); + + $section = [ 'id' => 'single_post' ]; + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_permalink' ], [ $section ] ); + + $this->assertStringContainsString( get_permalink( $post_ID ), $html ); + } + + /** + * Test that the example of a new custom post type displays the fallback string. + */ + function test_example_permalink_no_posts() { + register_post_type( 'demo' ); + + $section = [ 'id' => 'single_demo' ]; + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_permalink' ], [ $section ] ); + + $this->assertStringContainsString( 'No posts yet.', $html ); + } + + /** + * Test that the example of a term archive includes a link to the newest term. + */ + function test_example_term_archive() { + $category_ID = $this->factory->term->create( [ 'taxonomy' => 'category' ] ); + wp_set_object_terms( $this->factory->post->create(), $category_ID, 'category' ); + + $section = [ 'id' => 'archive_category' ]; + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_term_archive' ], [ $section ] ); + + $this->assertStringContainsString( get_term_link( $category_ID, 'category' ), $html ); + } + + /** + * Test that the example of a new taxonomy displays the fallback string. + */ + function test_example_term_archive_no_terms() { + register_taxonomy( 'demo', 'post' ); + + $section = [ 'id' => 'archive_demo' ]; + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_term_archive' ], [ $section ] ); + + $this->assertStringContainsString( 'No terms yet.', $html ); + } + + /** + * Test that the example of a post type archive includes the right link. + */ + function test_example_post_type_archive() { + register_post_type( 'demo', [ 'has_archive' => true ] ); + $this->factory->post->create( [ 'post_type' => 'demo' ] ); + + $section = [ 'id' => 'archive_demo' ]; + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_post_type_archive' ], [ $section ] ); + + $this->assertStringContainsString( get_post_type_archive_link( 'demo' ), $html ); + } + + /** + * Test that the example of a post type without archive support is blank. + */ + function test_example_post_type_archive_no_support() { + register_post_type( 'demo' ); + $this->factory->post->create( [ 'post_type' => 'demo' ] ); + + $section = [ 'id' => 'archive_demo' ]; + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_post_type_archive' ], [ $section ] ); + + $this->assertEmpty( $html ); + } + + /** + * Test that the example of a date archive includes this year and month. + * + * Before testing for the current month, remove the current year to avoid a + * false positive in January. + */ + function test_example_date_archive() { + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_date_archive' ] ); + + $this->assertStringContainsString( date( 'Y' ), $html ); + $this->assertStringContainsString( date( 'm' ), str_replace( date( 'Y' ), '', $html ) ); + } + + /** + * Test that the example of an author archive includes the URL for this user. + */ + function test_example_author_archive() { + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_author_archive' ] ); + + $this->assertStringContainsString( get_author_posts_url( get_current_user_id() ), $html ); + } + + /** + * Test that the example of a search link includes a search query string. + */ + function test_example_search_page() { + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_search_page' ] ); + + $this->assertStringContainsString( get_search_link( 'wordpress' ), $html ); + } + + /** + * Test that the example 404 page includes the hashed blog URL. + */ + function test_example_404_page() { + $html = Utils::get_echo( [ WP_SEO_Settings(), 'example_404_page' ] ); + + $this->assertStringContainsString( md5( get_bloginfo( 'url' ) ), $html ); + } + + /** + * Test the various states of the field() helper method. + */ + function test_field() { + // No field. + $html = Utils::get_echo( [ WP_SEO_Settings(), 'field' ], [ [] ] ); + + $this->assertEmpty( $html ); + + // No type? Use a text field. + $html = Utils::get_echo( [ WP_SEO_Settings(), 'field' ], [ [ 'field' => 'demo' ] ] ); + $this->assertMatchesRegularExpression( '/]+type="text"[^>]+name="wp-seo\[demo\]"/', $html ); + + // Check that a value is passed. + WP_SEO_Settings()->options['demo'] = 'demo value'; + $html = Utils::get_echo( [ WP_SEO_Settings(), 'field' ], [ [ 'field' => 'demo' ] ] ); + + $this->assertMatchesRegularExpression( '/]+type="text"[^>]+value="demo value"/', $html ); + + // Check the rendered field types. + $html = Utils::get_echo( [ WP_SEO_Settings(), 'field' ], [ + [ + 'field' => 'demo', + 'type' => 'textarea', + ] + ] ); + + $this->assertStringContainsString( ' 'demo', + 'type' => 'checkboxes', + 'boxes' => [ 'foo' => 'bar' ], + ] + ] ); + + $this->assertMatchesRegularExpression( '/]+type="checkbox"/', $html ); + + $html = Utils::get_echo( [ WP_SEO_Settings(), 'field' ], [ + [ + 'field' => 'demo_repeatable', // Not "demo," which does have a value. + 'type' => 'repeatable', + 'repeat' => [ 'foo' => 'bar' ], + ] + ] ); + + $this->assertMatchesRegularExpression( '/]+type="text"/', $html ); + } + + /** + * Test the default text field output and the output with all args. + */ + function test_render_text_field() { + + $html = Utils::get_echo( [ WP_SEO_Settings(), 'render_text_field' ], [ + [ 'field' => 'demo', 'type' => 'text' ], + 'demo value', + ] ); + + // Look for the correct attributes: type of "text," field name and value. + $this->assertStringContainsString( 'type="text"', $html ); + $this->assertStringContainsString( 'name="wp-seo[demo]"', $html ); + $this->assertStringContainsString( 'value="demo value"', $html ); + + // Expect the field to have some size. + $this->assertMatchesRegularExpression( '/size="\d+"/', $html ); + + $html = Utils::get_echo( [ WP_SEO_Settings(), 'render_text_field' ], [ + [ 'type' => 'number', 'field' => 'demo', 'size' => 5 ], + '40', + ] ); + + $this->assertStringContainsString( 'type="number"', $html ); + $this->assertStringContainsString( 'value="40"', $html ); + $this->assertStringContainsString( 'size="5"', $html ); + } + + /** + * Test the default textarea output and the output with all args. + */ + function test_render_textarea() { + $html = Utils::get_echo( [ WP_SEO_Settings(), 'render_textarea' ], [ + [ 'field' => 'demo' ], + 'demo value' + ] ); + + // Expect at least a textarea, with with the field name and the correct value. + $this->assertMatchesRegularExpression( '/]+name="wp-seo\[demo\]"[^>]+>demo value<\/textarea>/', $html ); + + // Expect some row and column sizes. + $this->assertMatchesRegularExpression( '/rows="\d+"/', $html ); + $this->assertMatchesRegularExpression( '/cols="\d+"/', $html ); + } + + function test_render_checkboxes() { + $html = Utils::get_echo( [ WP_SEO_Settings(), 'render_checkboxes' ], [ + [ + 'field' => 'demo', + 'boxes' => [ + 'page' => 'Page', + 'post_tag' => 'Tag', + ], + ], + [ 'post_tag' ], + ] ); + + // Expect two checkboxes. + $this->assertSame( 2, substr_count( $html, 'type="checkbox"' ) ); + $this->assertStringContainsString( 'Page', $html ); + $this->assertStringContainsString( 'Tag', $html ); + // Expect only the "Tag" checkbox to be checked. + $this->assertMatchesRegularExpression( '/]+type="checkbox"[^>]+value="post_tag"[^>]+checked/', $html ); + $this->assertDoesNotMatchRegularExpression( '/]+type="checkbox"[^>]+value="page"[^>]+checked/', $html ); + } + + /** + * Test the default repeatable field output and the output with args. + */ + function test_render_repeatable() { + $args = [ + 'field' => 'demo', + 'repeat' => [ + 'first_name' => 'First name', + 'last_name' => 'Last Name', + ], + ]; + + $html = Utils::get_echo( [ WP_SEO_Settings(), 'render_repeatable_field' ], [ + $args, [] + ] ); + + // Expect a "name" attribute in with the counter for the template. + $this->assertStringContainsString( 'name="wp-seo[demo][<%= i %>][first_name]"', $html ); + + // No values: We repeat two fields, so expect four inputs, two each for input and the template. + $this->assertSame( 4, substr_count( $html, 'assertStringContainsString( 'name="wp-seo[demo][0][first_name]"', $html ); + $this->assertStringNotContainsString( 'name="wp-seo[demo][1][first_name]"', $html ); + // No values: Expect that the template starts at "1." + $this->assertStringContainsString( 'data-start="1"', $html ); + + $args['size'] = '40'; + + $html = Utils::get_echo( [ WP_SEO_Settings(), 'render_repeatable_field' ], [ + $args, + [ + [ + 'first_name' => 'Millard', + 'last_name' => 'Fillmore', + ], + [ + 'first_name' => 'William Howard', + 'last_name' => 'Taft' + ], + [ + 'first_name' => 'James', + 'last_name' => 'Buchanan', + ], + ] + ] ); + + // Three values: Expect eight inputs (there isn't a blank one). + $this->assertSame( 8, substr_count( $html, 'assertSame( 8, substr_count( $html, 'size="40"' ) ); + // The "name" attribute should go up to "2." + $this->assertStringContainsString( 'name="wp-seo[demo][2][first_name]"', $html ); + $this->assertStringNotContainsString( 'name="wp-seo[demo][3][first_name]"', $html ); + // Expect that the template starts at "3." + $this->assertStringContainsString( 'data-start="3"', $html ); + // Look for our field values. + $this->assertMatchesRegularExpression( '/]+class="repeatable"[^>]+\[1\]\[first_name\][^>]+value="William Howard"[^>]+>/', $html ); + $this->assertMatchesRegularExpression( '/]+class="repeatable"[^>]+\[2\]\[last_name\][^>]+value="Buchanan"[^>]+>/', $html ); + } + + protected function tearDown(): void { + parent::tearDown(); + // Clean up after ourselves. + _wp_seo_reset_post_types(); + _wp_seo_reset_taxonomies(); + } + +} diff --git a/tests/test-wp-seo-settings-properties.php b/tests/Feature/SettingsPropertiesTest.php similarity index 61% rename from tests/test-wp-seo-settings-properties.php rename to tests/Feature/SettingsPropertiesTest.php index 789e1d3..23a918f 100644 --- a/tests/test-wp-seo-settings-properties.php +++ b/tests/Feature/SettingsPropertiesTest.php @@ -1,12 +1,18 @@ true, 'has_archive' => true, 'public' => true ) ); - register_taxonomy( 'demo_public', 'post', array( 'public' => true ) ); + register_post_type( 'demo_public', [ 'rewrite' => true, 'has_archive' => true, 'public' => true ] ); + register_taxonomy( 'demo_public', 'post', [ 'public' => true ] ); WP_SEO_Settings()->set_properties(); @@ -26,8 +32,8 @@ function test_allow_public_objects() { } function test_disallow_private_objects() { - register_post_type( 'demo_private', array( 'public' => false ) ); - register_taxonomy ( 'demo_private', 'post', array( 'public' => false ) ); + register_post_type( 'demo_private', [ 'public' => false ] ); + register_taxonomy( 'demo_private', 'post', [ 'public' => false ] ); WP_SEO_Settings()->set_properties(); @@ -37,7 +43,7 @@ function test_disallow_private_objects() { } function test_handle_post_types_without_archives() { - register_post_type( 'demo_no_archive', array( 'rewrite' => true, 'has_archive' => false, 'public' => true ) ); + register_post_type( 'demo_no_archive', [ 'rewrite' => true, 'has_archive' => false, 'public' => true ] ); WP_SEO_Settings()->set_properties(); @@ -46,8 +52,16 @@ function test_handle_post_types_without_archives() { } function test_disallow_unlabeled_objects() { - register_post_type( 'demo_no_label', array( 'label' => false, 'rewrite' => true, 'has_archive' => true, 'public' => true ) ); - register_taxonomy( 'demo_no_label', 'post', array( 'label' => false, 'public' => true ) ); + register_post_type( 'demo_no_label', [ + 'label' => false, + 'rewrite' => true, + 'has_archive' => true, + 'public' => true + ] ); + register_taxonomy( 'demo_no_label', 'post', [ + 'label' => false, + 'public' => true + ] ); WP_SEO_Settings()->set_properties(); @@ -56,4 +70,4 @@ function test_disallow_unlabeled_objects() { $this->assertArrayNotHasKey( 'demo_no_label', WP_SEO_Settings()->get_taxonomies() ); } -} \ No newline at end of file +} diff --git a/tests/test-wp-seo.php b/tests/Feature/WPSEOTest.php similarity index 58% rename from tests/test-wp-seo.php rename to tests/Feature/WPSEOTest.php index 2737c48..69ad443 100644 --- a/tests/test-wp-seo.php +++ b/tests/Feature/WPSEOTest.php @@ -1,20 +1,27 @@ assertEmpty( - WP_SEO::instance()->get_term_option( rand( -1, -100 ), rand_str() ), + WP_SEO::instance()->get_term_option( rand( - 1, - 100 ), rand_str() ), 'Non-existent terms should not return term option data' ); } function test_get_term_option() { $option_value = rand_str(); - $term = $this->create_and_get_term_with_option( $option_value ); + $term = $this->create_and_get_term_with_option( $option_value ); $this->assertSame( WP_SEO::instance()->get_term_option( $term->term_id, $term->taxonomy ), $option_value, @@ -24,21 +31,22 @@ function test_get_term_option() { function test_intersect_term_option() { $this->assertCount( - 3, - WP_SEO::instance()->intersect_term_option( array() ), + 2, + WP_SEO::instance()->intersect_term_option( [] ), 'Unexpected term option default key' ); $this->assertArrayHasKey( 'title', - WP_SEO::instance()->intersect_term_option( array() ), + WP_SEO::instance()->intersect_term_option( [] ), 'Unexpectedly missing default term option key' ); $this->assertArrayHasKey( 'description', - WP_SEO::instance()->intersect_term_option( array() ), + WP_SEO::instance()->intersect_term_option( [] ), 'Unexpectedly missing default term option key' ); } + } diff --git a/tests/test-wp-title-wp-head.php b/tests/Feature/WPTitleWPHeadTest.php similarity index 75% rename from tests/test-wp-title-wp-head.php rename to tests/Feature/WPTitleWPHeadTest.php index c9234dc..d9b10c7 100644 --- a/tests/test-wp-title-wp-head.php +++ b/tests/Feature/WPTitleWPHeadTest.php @@ -1,20 +1,27 @@ taxonomy, 'post' ); - register_post_type( $this->post_type, array( 'rewrite' => true, 'has_archive' => true, 'public' => true ) ); + register_post_type( $this->post_type, [ 'rewrite' => true, 'has_archive' => true, 'public' => true ] ); WP_SEO_Settings()->set_properties(); $this->_update_option_for_tests(); @@ -25,7 +32,7 @@ function setUp() { $wp_rewrite->flush_rules(); } - function tearDown() { + function tearDown(): void { parent::tearDown(); // Leave the place as we found it. _wp_seo_reset_post_types(); @@ -43,16 +50,16 @@ function tearDown() { * cleaner, and the option has to be set one way or another. */ function _update_option_for_tests() { - $this->options['post_types'] = array( 'post' ); - $this->options['taxonomies'] = array( 'category' ); - $this->options['arbitrary_tags'] = array( - array( + $this->options['post_types'] = [ 'post' ]; + $this->options['taxonomies'] = [ 'category' ]; + $this->options['arbitrary_tags'] = [ + [ 'name' => 'demo arbitrary title', 'content' => 'demo arbitrary content', - ), - ); + ], + ]; - foreach ( array( + foreach ( [ 'home', 'single_post', "single_{$this->post_type}", @@ -64,7 +71,7 @@ function _update_option_for_tests() { 'search', '404', 'feed', - ) as $key ) { + ] as $key ) { $this->options[ "{$key}_title" ] = "demo_{$key}_title"; $this->options[ "{$key}_description" ] = "demo_{$key}_description"; } @@ -95,17 +102,19 @@ function _assert_all_meta( $description ) { EOF; - $this->assertSame( strip_ws( $expected ), strip_ws( get_echo( array( WP_SEO(), 'wp_head' ) ) ) ); + + $this->assertSame( strip_ws( $expected ), strip_ws( Utils::get_echo( [ WP_SEO(), 'wp_head' ] ) ) ); } /** * Test that WP_SEO::wp_head() echoes only the arbitrary tags. */ function _assert_arbitrary_meta() { - $expected = << EOF; - $this->assertSame( strip_ws( $expected ), strip_ws( get_echo( array( WP_SEO(), 'wp_head' ) ) ) ); + + $this->assertSame( strip_ws( $expected ), strip_ws( Utils::get_echo( [ WP_SEO(), 'wp_head' ] ) ) ); } /** @@ -131,7 +140,7 @@ function test_single() { } function test_singular() { - $this->go_to( get_permalink( $this->factory->post->create( array( 'post_type' => $this->post_type ) ) ) ); + $this->go_to( get_permalink( $this->factory->post->create( [ 'post_type' => $this->post_type ] ) ) ); $this->_assert_option_filters( "single_{$this->post_type}" ); } @@ -148,9 +157,9 @@ function test_single_custom() { function test_no_format_string() { add_filter( 'wp_seo_title_tag_format', '__return_false' ); $title = rand_str(); - $this->go_to( get_permalink( $this->factory->post->create( array( 'post_title' => $title ) ) ) ); + $this->go_to( get_permalink( $this->factory->post->create( [ 'post_title' => $title ] ) ) ); // The site name doesn't appear in all versions we test against; just check for our title. - $this->assertContains( $title, wp_title( '»', false ) ); + $this->assertStringContainsString( $title, wp_title( '»', false ) ); // WP_UnitTestCase::_restore_hooks() was introduced in 4.0. remove_filter( 'wp_seo_title_tag_format', '__return_false' ); } @@ -161,28 +170,28 @@ function test_home() { } function test_author_archive() { - $author_ID = $this->factory->user->create( array( 'user_login' => 'user-a' ) ); - $this->factory->post->create( array( 'post_author' => $author_ID ) ); + $author_ID = $this->factory->user->create( [ 'user_login' => 'user-a' ] ); + $this->factory->post->create( [ 'post_author' => $author_ID ] ); $this->go_to( get_author_posts_url( $author_ID ) ); $this->_assert_option_filters( 'archive_author' ); } function test_category() { - $category_ID = $this->factory->term->create( array( 'name' => 'cat-a', 'taxonomy' => 'category' ) ); + $category_ID = $this->factory->term->create( [ 'name' => 'cat-a', 'taxonomy' => 'category' ] ); $this->go_to( get_term_link( $category_ID, 'category' ) ); $this->_assert_option_filters( 'archive_category' ); } function test_tax() { - $term_ID = $this->factory->term->create( array( 'name' => 'demo-a', 'taxonomy' => $this->taxonomy ) ); + $term_ID = $this->factory->term->create( [ 'name' => 'demo-a', 'taxonomy' => $this->taxonomy ] ); $this->go_to( get_term_link( $term_ID, $this->taxonomy ) ); $this->_assert_option_filters( "archive_{$this->taxonomy}" ); } // A term with custom values should not use the archive_{taxonomy}_ fields. function test_category_custom() { - $term_ID = $this->factory->term->create( array( 'name' => 'cat-b', 'taxonomy' => 'category' ) ); - update_option( WP_SEO()->get_term_option_name( get_term( $term_ID, 'category' ) ), array( 'title' => '_custom_title', 'description' => '_custom_description' ) ); + $term_ID = $this->factory->term->create( [ 'name' => 'cat-b', 'taxonomy' => 'category' ] ); + update_option( WP_SEO()->get_term_option_name( get_term( $term_ID, 'category' ) ), [ 'title' => '_custom_title', 'description' => '_custom_description' ] ); $this->go_to( get_term_link( $term_ID, 'category' ) ); $this->_assert_title( '_custom_title' ); $this->_assert_all_meta( '_custom_description' ); @@ -194,7 +203,7 @@ function test_post_type_archive() { } function test_date_archive() { - $this->factory->post->create( array( 'post_date' => '2007-09-04 12:34' ) ); + $this->factory->post->create( [ 'post_date' => '2007-09-04 12:34' ] ); $this->go_to( get_day_link( '2007', '09', '04' ) ); $this->_assert_option_filters( 'archive_date' ); } @@ -242,9 +251,9 @@ function test_no_option() { // Uses a random $sep to be sure it couldn't have come from us. $sep = rand_str(); - $this->assertContains( $sep, wp_title( $sep, false ) ); + $this->assertStringContainsString( $sep, wp_title( $sep, false ) ); - $this->assertEmpty( get_echo( array( WP_SEO(), 'wp_head' ) ) ); + $this->assertEmpty( Utils::get_echo( [ WP_SEO(), 'wp_head' ] ) ); } /** @@ -254,16 +263,16 @@ function test_invalid_meta_field() { delete_option( WP_SEO_Settings::SLUG ); WP_SEO_Settings()->set_options(); - update_option( WP_SEO_Settings::SLUG, array( - 'arbitrary_tags' => array( + update_option( WP_SEO_Settings::SLUG, [ + 'arbitrary_tags' => [ 'name' => 'foo', - 'value' => new WP_Error(), - ), - ) ); + 'value' => new \WP_Error(), + ], + ] ); $this->go_to( '/' ); - $this->assertEmpty( get_echo( array( WP_SEO(), 'wp_head' ) ) ); + $this->assertEmpty( Utils::get_echo( [ WP_SEO(), 'wp_head' ] ) ); } } diff --git a/tests/test-settings-page.php b/tests/test-settings-page.php deleted file mode 100644 index c3ca361..0000000 --- a/tests/test-settings-page.php +++ /dev/null @@ -1,331 +0,0 @@ -add_options_page(); - - global $submenu; - $this->assertContains( - array( - 'SEO', - 'manage_options', - 'wp-seo', - 'WP SEO Settings', - ), - $submenu['options-general.php'] - ); - } - - /** - * Make sure we have a help tab. - */ - function test_add_help_tab() { - set_current_screen( 'front' ); - WP_SEO_Settings()->add_help_tab(); - - $actual = get_current_screen()->get_help_tab( 'formatting-tags' ); - // Not all versions we test against include the priority. - if ( isset( $actual['priority'] ) ) { - unset( $actual['priority'] ); - } - - $this->assertEquals( $actual, array( - 'id' => 'formatting-tags', - 'title' => 'Formatting Tags', - 'content' => '', - 'callback' => array( WP_SEO_Settings(), 'view_formatting_tags_help_tab' ), - ) ); - } - - /** - * Test that the "example URL" method includes the text and any included link. - */ - function test_example_url() { - $html = get_echo( array( WP_SEO_Settings(), 'example_url' ), array( 'Demo text' ) ); - $this->assertSame( '

Demo text

', $html ); - - $html = get_echo( array( WP_SEO_Settings(), 'example_url' ), array( 'Demo text', 'http://wordpress.org' ) ); - $this->assertContains( 'http://wordpress.org', $html ); - } - - /** - * Test that the example of a Post includes a link to the latest post. - */ - function test_example_permalink() { - $post_ID = $this->factory->post->create(); - $section = array( 'id' => 'single_post' ); - $html = get_echo( array( WP_SEO_Settings(), 'example_permalink' ), array( $section ) ); - $this->assertContains( get_permalink( $post_ID ), $html ); - } - - /** - * Test that the example of a new custom post type displays the fallback string. - */ - function test_example_permalink_no_posts() { - register_post_type( 'demo' ); - $section = array( 'id' => 'single_demo' ); - $html = get_echo( array( WP_SEO_Settings(), 'example_permalink' ), array( $section ) ); - $this->assertContains( 'No posts yet.', $html ); - } - - /** - * Test that the example of a term archive includes a link to the newest term. - */ - function test_example_term_archive() { - $category_ID = $this->factory->term->create( array( 'taxonomy' => 'category' ) ); - wp_set_object_terms( $this->factory->post->create(), $category_ID, 'category' ); - - $section = array( 'id' => 'archive_category' ); - $html = get_echo( array( WP_SEO_Settings(), 'example_term_archive' ), array( $section ) ); - - $this->assertContains( get_term_link( $category_ID, 'category' ), $html ); - } - - /** - * Test that the example of a new taxonomy displays the fallback string. - */ - function test_example_term_archive_no_terms() { - register_taxonomy( 'demo', 'post' ); - - $section = array( 'id' => 'archive_demo' ); - $html = get_echo( array( WP_SEO_Settings(), 'example_term_archive' ), array( $section ) ); - - $this->assertContains( 'No terms yet.', $html ); - } - - /** - * Test that the example of a post type archive includes the right link. - */ - function test_example_post_type_archive() { - register_post_type( 'demo', array( 'has_archive' => true ) ); - $this->factory->post->create( array( 'post_type' => 'demo' ) ); - - $section = array( 'id' => 'archive_demo' ); - $html = get_echo( array( WP_SEO_Settings(), 'example_post_type_archive' ), array( $section ) ); - - $this->assertContains( get_post_type_archive_link( 'demo' ), $html ); - } - - /** - * Test that the example of a post type without archive support is blank. - */ - function test_example_post_type_archive_no_support() { - register_post_type( 'demo' ); - $this->factory->post->create( array( 'post_type' => 'demo' ) ); - - $section = array( 'id' => 'archive_demo' ); - $html = get_echo( array( WP_SEO_Settings(), 'example_post_type_archive' ), array( $section ) ); - - $this->assertEmpty( $html ); - } - - /** - * Test that the example of a date archive includes this year and month. - * - * Before testing for the current month, remove the current year to avoid a - * false positive in January. - */ - function test_example_date_archive() { - $html = get_echo( array( WP_SEO_Settings(), 'example_date_archive' ) ); - $this->assertContains( date( 'Y' ), $html ); - $this->assertContains( date( 'm' ), str_replace( date( 'Y' ), '', $html ) ); - } - - /** - * Test that the example of an author archive includes the URL for this user. - */ - function test_example_author_archive() { - $html = get_echo( array( WP_SEO_Settings(), 'example_author_archive' ) ); - $this->assertContains( get_author_posts_url( get_current_user_id() ), $html ); - } - - /** - * Test that the example of a search link includes a search query string. - */ - function test_example_search_page() { - $html = get_echo( array( WP_SEO_Settings(), 'example_search_page' ) ); - $this->assertContains( get_search_link( 'wordpress' ), $html ); - } - - /** - * Test that the example 404 page includes the hashed blog URL. - */ - function test_example_404_page() { - $html = get_echo( array( WP_SEO_Settings(), 'example_404_page' ) ); - $this->assertContains( md5( get_bloginfo( 'url' ) ), $html ); - } - - /** - * Test the various states of the field() helper method. - */ - function test_field() { - // No field. - $html = get_echo( array( WP_SEO_Settings(), 'field' ), array( array() ) ); - $this->assertEmpty( $html ); - - // No type? Use a text field. - $html = get_echo( array( WP_SEO_Settings(), 'field' ), array( array( 'field' => 'demo' ) ) ); - $this->assertRegExp( '/]+type="text"[^>]+name="wp-seo\[demo\]"/', $html ); - - // Check that a value is passed. - WP_SEO_Settings()->options['demo'] = 'demo value'; - $html = get_echo( array( WP_SEO_Settings(), 'field' ), array( array( 'field' => 'demo' ) ) ); - $this->assertRegExp( '/]+type="text"[^>]+value="demo value"/', $html ); - - // Check the rendered field types. - $html = get_echo( array( WP_SEO_Settings(), 'field' ), array( array( 'field' => 'demo', 'type' => 'textarea' ) ) ); - $this->assertContains( ' 'demo', - 'type' => 'checkboxes', - 'boxes' => array( 'foo' => 'bar' ) - ) ) ); - $this->assertRegExp( '/]+type="checkbox"/', $html ); - - $html = get_echo( array( WP_SEO_Settings(), 'field' ), array( - array( - 'field' => 'demo_repeatable', // Not "demo," which does have a value. - 'type' => 'repeatable', - 'repeat' => array( 'foo' => 'bar' ) - ), - ) ); - $this->assertRegExp( '/]+type="text"/', $html ); - } - - /** - * Test the default text field output and the output with all args. - */ - function test_render_text_field() { - $html = get_echo( array( WP_SEO_Settings(), 'render_text_field' ), array( - array( 'field' => 'demo', 'type' => 'text' ), - 'demo value', - ) ); - - // Look for the correct attributes: type of "text," field name and value. - $this->assertContains( 'type="text"', $html ); - $this->assertContains( 'name="wp-seo[demo]"', $html ); - $this->assertContains( 'value="demo value"', $html ); - - // Expect the field to have some size. - $this->assertRegExp( '/size="\d+"/', $html ); - - $html = get_echo( array( WP_SEO_Settings(), 'render_text_field' ), array( - array( 'type' => 'number', 'field' => 'demo', 'size' => 5 ), - '40', - ) ); - $this->assertContains( 'type="number"', $html ); - $this->assertContains( 'value="40"', $html ); - $this->assertContains( 'size="5"', $html ); - } - - /** - * Test the default textarea output and the output with all args. - */ - function test_render_textarea() { - $html = get_echo( array( WP_SEO_Settings(), 'render_textarea' ), array( - array( 'field' => 'demo', ), - 'demo value', - ) ); - - // Expect at least a textarea, with with the field name and the correct value. - $this->assertRegExp( '/]+name="wp-seo\[demo\]"[^>]+>demo value<\/textarea>/', $html ); - - // Expect some row and column sizes. - $this->assertRegExp( '/rows="\d+"/', $html ); - $this->assertRegExp( '/cols="\d+"/', $html ); - } - - function test_render_checkboxes() { - $html = get_echo( array( WP_SEO_Settings(), 'render_checkboxes' ), array( - array( - 'field' => 'demo', - 'boxes' => array( - 'page' => 'Page', - 'post_tag' => 'Tag', - ), - ), - array( 'post_tag' ), - ) ); - - // Expect two checkboxes. - $this->assertSame( substr_count( $html, 'type="checkbox"' ), 2 ); - $this->assertContains( 'Page', $html ); - $this->assertContains( 'Tag', $html ); - // Expect only the "Tag" checkbox to be checked. - $this->assertRegExp( '/]+type="checkbox"[^>]+value="post_tag"[^>]+checked/', $html ); - $this->assertNotRegExp( '/]+type="checkbox"[^>]+value="page"[^>]+checked/', $html ); - } - - /** - * Test the default repeatable field output and the output with args. - */ - function test_render_repeatable() { - $args = array( - 'field' => 'demo', - 'repeat' => array( - 'first_name' => 'First name', - 'last_name' => 'Last Name', - ), - ); - - $html = get_echo( array( WP_SEO_Settings(), 'render_repeatable_field' ), array( $args, array() ) ); - - // Expect a "name" attribute in with the counter for the template. - $this->assertContains( 'name="wp-seo[demo][<%= i %>][first_name]"', $html ); - - // No values: We repeat two fields, so expect four inputs, two each for input and the template. - $this->assertSame( substr_count( $html, 'assertContains( 'name="wp-seo[demo][0][first_name]"', $html ); - $this->assertNotContains( 'name="wp-seo[demo][1][first_name]"', $html ); - // No values: Expect that the template starts at "1." - $this->assertContains( 'data-start="1"', $html ); - - $args['size'] = '40'; - $html = get_echo( array( WP_SEO_Settings(), 'render_repeatable_field' ), array( $args, array( - array( - 'first_name' => 'Millard', - 'last_name' => 'Fillmore', - ), - array( - 'first_name' => 'William Howard', - 'last_name' => 'Taft' - ), - array( - 'first_name' => 'James', - 'last_name' => 'Buchanan', - ), - ) ) ); - - // Three values: Expect eight inputs (there isn't a blank one). - $this->assertSame( substr_count( $html, 'assertSame( substr_count( $html, 'size="40"' ), 8 ); - // The "name" attribute should go up to "2." - $this->assertContains( 'name="wp-seo[demo][2][first_name]"', $html ); - $this->assertNotContains( 'name="wp-seo[demo][3][first_name]"', $html ); - // Expect that the template starts at "3." - $this->assertContains( 'data-start="3"', $html ); - // Look for our field values. - $this->assertRegExp( '/]+class="repeatable"[^>]+\[1\]\[first_name\][^>]+value="William Howard"[^>]+>/', $html ); - $this->assertRegExp( '/]+class="repeatable"[^>]+\[2\]\[last_name\][^>]+value="Buchanan"[^>]+>/', $html ); - } - -} diff --git a/tests/test-wp-seo-properties.php b/tests/test-wp-seo-properties.php deleted file mode 100644 index 5455277..0000000 --- a/tests/test-wp-seo-properties.php +++ /dev/null @@ -1,44 +0,0 @@ -set_properties(); - } - - function tearDown() { - parent::tearDown(); - // Leave the place as we found it. - remove_filter( 'wp_seo_formatting_tags', array( $this, '_add_mock' ) ); - remove_filter( 'wp_seo_formatting_tags', array( $this, '_add_illegals' ) ); - WP_SEO()->set_properties(); - } - - function _add_mock( $tags ) { - $tags['is_a_tag'] = $this->getMockForAbstractClass( 'WP_SEO_Formatting_Tag' ); - return $tags; - } - - function _add_illegals( $tags ) { - $tags['is_non_object'] = true; - $tags['is_wrong_object'] = new stdClass; - return $tags; - } - - function test_legal_tag() { - $this->assertArrayHasKey( 'is_a_tag', WP_SEO()->formatting_tags ); - } - - function test_illegal_tags() { - $this->assertArrayNotHasKey( 'is_non_object', WP_SEO()->formatting_tags ); - $this->assertArrayNotHasKey( 'is_wrong_object', WP_SEO()->formatting_tags ); - } - -} \ No newline at end of file