diff --git a/includes/Story_Post_Type.php b/includes/Story_Post_Type.php index ce61604d52e0..6e760a6c3c69 100644 --- a/includes/Story_Post_Type.php +++ b/includes/Story_Post_Type.php @@ -39,6 +39,7 @@ * Class Story_Post_Type. * * @SuppressWarnings(PHPMD.TooManyPublicMethods) + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */ class Story_Post_Type extends Service_Base implements PluginDeactivationAware, SiteInitializationAware, HasRequirements { use Post_Type; @@ -78,17 +79,26 @@ class Story_Post_Type extends Service_Base implements PluginDeactivationAware, S */ private $settings; + /** + * Experiments instance. + * + * @var Experiments Experiments instance. + */ + private $experiments; + /** * Analytics constructor. * * @since 1.12.0 * - * @param Settings $settings Settings instance. + * @param Settings $settings Settings instance. + * @param Experiments $experiments Experiments instance. * * @return void */ - public function __construct( Settings $settings ) { - $this->settings = $settings; + public function __construct( Settings $settings, Experiments $experiments ) { + $this->settings = $settings; + $this->experiments = $experiments; } /** @@ -110,6 +120,7 @@ public function register() { add_filter( 'wp_insert_post_data', [ $this, 'change_default_title' ] ); add_filter( 'bulk_post_updated_messages', [ $this, 'bulk_post_updated_messages' ], 10, 2 ); add_action( 'clean_post_cache', [ $this, 'clear_user_posts_count' ], 10, 2 ); + add_filter( 'pre_handle_404', [ $this, 'redirect_post_type_archive_urls' ], 10, 2 ); add_action( 'add_option_' . $this->settings::SETTING_NAME_ARCHIVE, [ $this, 'update_archive_setting' ] ); add_action( 'update_option_' . $this->settings::SETTING_NAME_ARCHIVE, [ $this, 'update_archive_setting' ] ); @@ -349,6 +360,44 @@ public function change_default_title( $data ) { return $data; } + /** + * Handles redirects to the post type archive. + * + * @since 1.13.0 + * + * @param bool|mixed $bypass Pass-through of the pre_handle_404 filter value. + * @param \WP_Query $query The WP_Query object. + * @return bool|mixed Whether to pass-through or not. + */ + public function redirect_post_type_archive_urls( $bypass, $query ) { + global $wp_rewrite; + + if ( ! $this->experiments->is_experiment_enabled( 'archivePageCustomization' ) ) { + return $bypass; + } + + if ( $bypass || ! is_string( $this->get_has_archive() ) || ( ! $wp_rewrite instanceof \WP_Rewrite || ! $wp_rewrite->using_permalinks() ) ) { + return $bypass; + } + + // 'pagename' is for most permalink types, name is for when the %postname% is used as a top-level field. + if ( self::REWRITE_SLUG === $query->get( 'pagename' ) || self::REWRITE_SLUG === $query->get( 'name' ) ) { + $redirect_url = get_post_type_archive_link( self::POST_TYPE_SLUG ); + + if ( ! $redirect_url ) { + return $bypass; + } + + // Only exit if there was actually a location to redirect to. + // Allows filtering location in tests to verify behavior. + if ( wp_safe_redirect( $redirect_url, 301 ) ) { + exit; + } + } + + return $bypass; + } + /** * Invalid cache. * @@ -392,6 +441,10 @@ public function update_archive_setting() { * @return bool|string Whether the post type should have an archive, or archive slug. */ private function get_has_archive() { + if ( ! $this->experiments->is_experiment_enabled( 'archivePageCustomization' ) ) { + return true; + } + $archive_page_option = $this->settings->get_setting( $this->settings::SETTING_NAME_ARCHIVE ); $custom_archive_page_id = (int) $this->settings->get_setting( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); $has_archive = true; diff --git a/tests/phpunit/integration/includes/Kses_Setup.php b/tests/phpunit/integration/includes/Kses_Setup.php index 175a0a18b4fa..69ff627204cb 100644 --- a/tests/phpunit/integration/includes/Kses_Setup.php +++ b/tests/phpunit/integration/includes/Kses_Setup.php @@ -34,8 +34,10 @@ trait Kses_Setup { * Setup KSES init class. */ protected function kses_int() { - $this->kses = new \Google\Web_Stories\KSES( - new \Google\Web_Stories\Story_Post_Type( new \Google\Web_Stories\Settings() ) + $settings = new \Google\Web_Stories\Settings(); + $experiments = new \Google\Web_Stories\Experiments( $settings ); + $this->kses = new \Google\Web_Stories\KSES( + new \Google\Web_Stories\Story_Post_Type( $settings, $experiments ) ); $this->kses->register(); } diff --git a/tests/phpunit/integration/tests/REST_API/Embed_Controller.php b/tests/phpunit/integration/tests/REST_API/Embed_Controller.php index ada8b331cfe2..6f3649fadd9c 100644 --- a/tests/phpunit/integration/tests/REST_API/Embed_Controller.php +++ b/tests/phpunit/integration/tests/REST_API/Embed_Controller.php @@ -17,6 +17,7 @@ namespace Google\Web_Stories\Tests\Integration\REST_API; +use Google\Web_Stories\Experiments; use Google\Web_Stories\Settings; use Google\Web_Stories\Story_Post_Type; use Google\Web_Stories\Tests\Integration\Test_REST_TestCase; @@ -297,11 +298,13 @@ public function test_local_url_pretty_permalinks() { $this->set_permalink_structure( '/%postname%/' ); + $settings = new Settings(); + // Without (re-)registering the post type here there won't be any rewrite rules for it // and get_permalink() will return "http://example.org/?web-story=embed-controller-test-story" // instead of "http://example.org/web-stories/embed-controller-test-story/". // @todo Investigate why this is needed (leakage between tests?) - $story_post_type = new Story_Post_Type( new Settings() ); + $story_post_type = new Story_Post_Type( $settings, new Experiments( $settings ) ); $story_post_type->register(); flush_rewrite_rules( false ); @@ -329,11 +332,13 @@ public function test_local_url_pretty_permalinks_multisite() { $this->set_permalink_structure( '/%postname%/' ); + $settings = new Settings(); + // Without (re-)registering the post type here there won't be any rewrite rules for it // and get_permalink() will return "http://example.org/?web-story=embed-controller-test-story" // instead of "http://example.org/web-stories/embed-controller-test-story/". // @todo Investigate why this is needed (leakage between tests?). - $story_post_type = new Story_Post_Type( new Settings() ); + $story_post_type = new Story_Post_Type( $settings, new Experiments( $settings ) ); $story_post_type->register(); flush_rewrite_rules( false ); diff --git a/tests/phpunit/integration/tests/REST_API/Stories_Users_Controller.php b/tests/phpunit/integration/tests/REST_API/Stories_Users_Controller.php index f96ccefe27d8..7c4ad15dd9bd 100644 --- a/tests/phpunit/integration/tests/REST_API/Stories_Users_Controller.php +++ b/tests/phpunit/integration/tests/REST_API/Stories_Users_Controller.php @@ -17,6 +17,7 @@ namespace Google\Web_Stories\Tests\Integration\REST_API; +use Google\Web_Stories\Experiments; use Google\Web_Stories\Settings; use Google\Web_Stories\Tests\Integration\Test_REST_TestCase; @@ -84,8 +85,8 @@ public function test_register() { */ public function test_count_user_posts() { $this->controller->register(); - - $post_type = new \Google\Web_Stories\Story_Post_Type( new Settings() ); + $settings = new Settings(); + $post_type = new \Google\Web_Stories\Story_Post_Type( $settings, new Experiments( $settings ) ); $post_type->register(); $result1 = $this->call_private_method( @@ -138,7 +139,8 @@ public function test_count_user_posts_invalid() { $this->controller->register(); $controller = new \Google\Web_Stories\REST_API\Stories_Users_Controller(); - $post_type = new \Google\Web_Stories\Story_Post_Type( new Settings() ); + $settings = new Settings(); + $post_type = new \Google\Web_Stories\Story_Post_Type( $settings, new Experiments( $settings ) ); $post_type->register(); $result1 = $this->call_private_method( $controller, diff --git a/tests/phpunit/integration/tests/Story_Post_Type.php b/tests/phpunit/integration/tests/Story_Post_Type.php index 142d9dc9db90..99f4346382d3 100644 --- a/tests/phpunit/integration/tests/Story_Post_Type.php +++ b/tests/phpunit/integration/tests/Story_Post_Type.php @@ -17,8 +17,6 @@ namespace Google\Web_Stories\Tests\Integration; -use Google\Web_Stories\Settings; - /** * @coversDefaultClass \Google\Web_Stories\Story_Post_Type */ @@ -46,6 +44,11 @@ class Story_Post_Type extends DependencyInjectedTestCase { */ protected $instance; + /** + * @var \Google\Web_Stories\Settings + */ + private $settings; + /** * Archive page ID. * @@ -53,6 +56,11 @@ class Story_Post_Type extends DependencyInjectedTestCase { */ protected static $archive_page_id; + /** + * @var string + */ + protected $redirect_location; + /** * @param \WP_UnitTest_Factory $factory */ @@ -88,30 +96,85 @@ public static function wpSetUpBeforeClass( $factory ) { public function set_up() { parent::set_up(); - $this->instance = $this->injector->make( \Google\Web_Stories\Story_Post_Type::class ); + $experiments = $this->createMock( \Google\Web_Stories\Experiments::class ); + $experiments->method( 'is_experiment_enabled' ) + ->willReturn( true ); + + $this->settings = new \Google\Web_Stories\Settings(); + $this->instance = new \Google\Web_Stories\Story_Post_Type( $this->settings, $experiments ); $this->add_caps_to_roles(); + + add_filter( 'wp_redirect', [ $this, 'filter_wp_redirect' ] ); } public function tear_down() { $this->remove_caps_from_roles(); - delete_option( \Google\Web_Stories\Settings::SETTING_NAME_ARCHIVE ); + + $this->redirect_location = null; + remove_filter( 'wp_redirect', [ $this, 'filter_wp_redirect' ] ); + + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); parent::tear_down(); } + public function filter_wp_redirect( $location ): bool { + $this->redirect_location = $location; + + return false; + } + /** * @covers ::register */ public function test_register() { $this->instance->register(); - $this->assertSame( 10, has_filter( '_wp_post_revision_fields', [ $this->instance, 'filter_revision_fields' ] ) ); + $this->assertSame( + 10, + has_filter( + '_wp_post_revision_fields', + [ + $this->instance, + 'filter_revision_fields', + ] + ) + ); $this->assertSame( 10, has_filter( 'wp_insert_post_data', [ $this->instance, 'change_default_title' ] ) ); - $this->assertSame( 10, has_filter( 'bulk_post_updated_messages', [ $this->instance, 'bulk_post_updated_messages' ] ) ); - - $this->assertSame( 10, has_action( 'add_option_' . \Google\Web_Stories\Settings::SETTING_NAME_ARCHIVE, [ $this->instance, 'update_archive_setting' ] ) ); - $this->assertSame( 10, has_action( 'update_option_' . \Google\Web_Stories\Settings::SETTING_NAME_ARCHIVE, [ $this->instance, 'update_archive_setting' ] ) ); + $this->assertSame( + 10, + has_filter( + 'bulk_post_updated_messages', + [ + $this->instance, + 'bulk_post_updated_messages', + ] + ) + ); + $this->assertSame( 10, has_filter( 'pre_handle_404', [ $this->instance, 'redirect_post_type_archive_urls' ] ) ); + + $this->assertSame( + 10, + has_action( + 'add_option_' . $this->settings::SETTING_NAME_ARCHIVE, + [ + $this->instance, + 'update_archive_setting', + ] + ) + ); + $this->assertSame( + 10, + has_action( + 'update_option_' . $this->settings::SETTING_NAME_ARCHIVE, + [ + $this->instance, + 'update_archive_setting', + ] + ) + ); } /** @@ -126,6 +189,7 @@ public function test_get_post_type_icon() { * @covers ::register_post_type */ public function test_register_post_type() { + $post_type = $this->instance->register_post_type(); $this->assertTrue( $post_type->has_archive ); } @@ -134,7 +198,7 @@ public function test_register_post_type() { * @covers ::register_post_type */ public function test_register_post_type_disabled() { - update_option( \Google\Web_Stories\Settings::SETTING_NAME_ARCHIVE, 'disabled' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'disabled' ); $post_type = $this->instance->register_post_type(); $this->assertFalse( $post_type->has_archive ); } @@ -143,7 +207,7 @@ public function test_register_post_type_disabled() { * @covers ::register_post_type */ public function test_register_post_type_default() { - update_option( \Google\Web_Stories\Settings::SETTING_NAME_ARCHIVE, 'default' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'default' ); $post_type = $this->instance->register_post_type(); $this->assertTrue( $post_type->has_archive ); } @@ -181,15 +245,26 @@ public function test_get_has_archive_default() { $this->assertTrue( $actual ); } + /** + * @covers ::get_has_archive + */ + public function test_get_has_archive_disabled_experiments() { + $experiments = new \Google\Web_Stories\Experiments( $this->settings ); + $this->instance = new \Google\Web_Stories\Story_Post_Type( $this->settings, $experiments ); + + $actual = $this->call_private_method( $this->instance, 'get_has_archive' ); + $this->assertTrue( $actual ); + } + /** * @covers ::get_has_archive */ public function test_get_has_archive_disabled() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'disabled' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'disabled' ); $actual = $this->call_private_method( $this->instance, 'get_has_archive' ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); $this->assertFalse( $actual ); } @@ -198,11 +273,11 @@ public function test_get_has_archive_disabled() { * @covers ::get_has_archive */ public function test_get_has_archive_custom_but_no_page() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); $actual = $this->call_private_method( $this->instance, 'get_has_archive' ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); $this->assertTrue( $actual ); } @@ -211,13 +286,13 @@ public function test_get_has_archive_custom_but_no_page() { * @covers ::get_has_archive */ public function test_get_has_archive_custom_but_invalid_page() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'custom' ); - update_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID, PHP_INT_MAX ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, PHP_INT_MAX ); $actual = $this->call_private_method( $this->instance, 'get_has_archive' ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); - delete_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); $this->assertTrue( $actual ); } @@ -226,13 +301,13 @@ public function test_get_has_archive_custom_but_invalid_page() { * @covers ::get_has_archive */ public function test_get_has_archive_custom() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'custom' ); - update_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); $actual = $this->call_private_method( $this->instance, 'get_has_archive' ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); - delete_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); $this->assertIsString( $actual ); $this->assertSame( urldecode( get_page_uri( self::$archive_page_id ) ), $actual ); @@ -242,8 +317,8 @@ public function test_get_has_archive_custom() { * @covers ::get_has_archive */ public function test_get_has_archive_custom_not_published() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'custom' ); - update_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); wp_update_post( [ @@ -254,8 +329,8 @@ public function test_get_has_archive_custom_not_published() { $actual = $this->call_private_method( $this->instance, 'get_has_archive' ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); - delete_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); wp_update_post( [ @@ -271,13 +346,13 @@ public function test_get_has_archive_custom_not_published() { * @covers ::pre_get_posts */ public function test_pre_get_posts_default_archive() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'default' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'default' ); $archive_link = get_post_type_archive_link( \Google\Web_Stories\Story_Post_Type::POST_TYPE_SLUG ); $this->go_to( $archive_link ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); $this->assertQueryTrue( 'is_archive', 'is_post_type_archive' ); } @@ -286,15 +361,19 @@ public function test_pre_get_posts_default_archive() { * @covers ::pre_get_posts */ public function test_pre_get_posts_custom_archive() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'custom' ); - update_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); + $this->set_permalink_structure( '/%postname%/' ); + + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); + + $this->instance->register_post_type(); $archive_link = get_post_type_archive_link( \Google\Web_Stories\Story_Post_Type::POST_TYPE_SLUG ); $this->go_to( $archive_link ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); - delete_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); $this->assertQueryTrue( 'is_page', 'is_singular' ); } @@ -303,8 +382,8 @@ public function test_pre_get_posts_custom_archive() { * @covers ::pre_get_posts */ public function test_pre_get_posts_custom_archive_not_published() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'custom' ); - update_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); wp_update_post( [ @@ -317,8 +396,8 @@ public function test_pre_get_posts_custom_archive_not_published() { $this->go_to( $archive_link ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); - delete_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); wp_update_post( [ @@ -334,7 +413,14 @@ public function test_pre_get_posts_custom_archive_not_published() { * @covers ::filter_display_post_states */ public function test_filter_display_post_states() { - $actual = $this->call_private_method( $this->instance, 'filter_display_post_states', [ [], get_post( self::$archive_page_id ) ] ); + $actual = $this->call_private_method( + $this->instance, + 'filter_display_post_states', + [ + [], + get_post( self::$archive_page_id ), + ] + ); $this->assertSame( [], $actual ); } @@ -343,13 +429,20 @@ public function test_filter_display_post_states() { * @covers ::filter_display_post_states */ public function test_filter_display_post_states_custom_archive() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'custom' ); - update_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); - $actual = $this->call_private_method( $this->instance, 'filter_display_post_states', [ [], get_post( self::$archive_page_id ) ] ); + $actual = $this->call_private_method( + $this->instance, + 'filter_display_post_states', + [ + [], + get_post( self::$archive_page_id ), + ] + ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); - delete_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); $this->assertEqualSetsWithIndex( [ @@ -358,12 +451,13 @@ public function test_filter_display_post_states_custom_archive() { $actual ); } + /** * @covers ::filter_display_post_states */ public function test_filter_display_post_states_custom_archive_not_published() { - update_option( Settings::SETTING_NAME_ARCHIVE, 'custom' ); - update_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); wp_update_post( [ @@ -372,10 +466,17 @@ public function test_filter_display_post_states_custom_archive_not_published() { ] ); - $actual = $this->call_private_method( $this->instance, 'filter_display_post_states', [ [], get_post( self::$archive_page_id ) ] ); + $actual = $this->call_private_method( + $this->instance, + 'filter_display_post_states', + [ + [], + get_post( self::$archive_page_id ), + ] + ); - delete_option( Settings::SETTING_NAME_ARCHIVE ); - delete_option( Settings::SETTING_NAME_ARCHIVE_PAGE_ID ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE ); + delete_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID ); wp_update_post( [ @@ -386,4 +487,141 @@ public function test_filter_display_post_states_custom_archive_not_published() { $this->assertSame( [], $actual ); } + + /** + * @covers ::redirect_post_type_archive_urls + */ + public function test_redirect_post_type_archive_urls_experiment_disabled() { + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, PHP_INT_MAX ); + + $query = new \WP_Query(); + $result = $this->instance->redirect_post_type_archive_urls( true, $query ); + + $this->assertTrue( $result ); + $this->assertNull( $this->redirect_location ); + } + + /** + * @covers ::redirect_post_type_archive_urls + */ + public function test_redirect_post_type_archive_urls_bypass() { + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, PHP_INT_MAX ); + + $query = new \WP_Query(); + $result = $this->instance->redirect_post_type_archive_urls( true, $query ); + + $this->assertTrue( $result ); + $this->assertNull( $this->redirect_location ); + } + + /** + * @covers ::redirect_post_type_archive_urls + */ + public function test_redirect_post_type_archive_urls_ugly_permalinks() { + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, PHP_INT_MAX ); + + // Needed so that the archive page change takes effect. + $this->instance->register_post_type(); + + $query = new \WP_Query(); + $result = $this->instance->redirect_post_type_archive_urls( false, $query ); + + $this->assertFalse( $result ); + $this->assertNull( $this->redirect_location ); + } + + /** + * @covers ::redirect_post_type_archive_urls + */ + public function test_redirect_post_type_archive_urls_pretty_permalinks() { + $this->set_permalink_structure( '/%postname%/' ); + + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, PHP_INT_MAX ); + + // Needed so that the archive page change takes effect. + $this->instance->register_post_type(); + + $query = new \WP_Query(); + $result = $this->instance->redirect_post_type_archive_urls( false, $query ); + + $this->assertFalse( $result ); + $this->assertNull( $this->redirect_location ); + } + + /** + * @covers ::redirect_post_type_archive_urls + */ + public function test_redirect_post_type_archive_urls_page() { + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, PHP_INT_MAX ); + + // Needed so that the archive page change takes effect. + $this->instance->register_post_type(); + + $query = new \WP_Query(); + $query->query['pagename'] = $this->instance::REWRITE_SLUG; + $query->set( 'name', $this->instance::REWRITE_SLUG ); + $query->set( 'page', self::$story_id ); + + add_filter( 'post_type_link', '__return_false' ); + add_filter( 'post_type_archive_link', '__return_false' ); + + $result = $this->instance->redirect_post_type_archive_urls( false, $query ); + + remove_filter( 'post_type_link', '__return_false' ); + remove_filter( 'post_type_archive_link', '__return_false' ); + + $this->assertFalse( $result ); + $this->assertNull( $this->redirect_location ); + } + + /** + * @covers ::redirect_post_type_archive_urls + */ + public function test_redirect_post_type_archive_urls_pagename_set() { + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, PHP_INT_MAX ); + + // Needed so that the archive page change takes effect. + $this->instance->register_post_type(); + + $query = new \WP_Query(); + $query->query['pagename'] = $this->instance::REWRITE_SLUG; + $query->set( 'pagename', $this->instance::REWRITE_SLUG ); + + add_filter( 'post_type_archive_link', '__return_false' ); + + $result = $this->instance->redirect_post_type_archive_urls( false, $query ); + + remove_filter( 'post_type_archive_link', '__return_false' ); + + $this->assertFalse( $result ); + $this->assertNull( $this->redirect_location ); + } + + /** + * @covers ::redirect_post_type_archive_urls + */ + public function test_redirect_post_type_archive_urls_existing_custom_page() { + $this->set_permalink_structure( '/%postname%/' ); + + update_option( $this->settings::SETTING_NAME_ARCHIVE, 'custom' ); + update_option( $this->settings::SETTING_NAME_ARCHIVE_PAGE_ID, self::$archive_page_id ); + + // Needed so that the archive page change takes effect. + $this->instance->register_post_type(); + + $query = new \WP_Query(); + $query->query['pagename'] = $this->instance::REWRITE_SLUG; + $query->set( 'pagename', $this->instance::REWRITE_SLUG ); + + $result = $this->instance->redirect_post_type_archive_urls( false, $query ); + + $this->assertFalse( $result ); + $this->assertSame( get_permalink( self::$archive_page_id ), $this->redirect_location ); + } }