diff --git a/admin/formatter/class-metabox-formatter.php b/admin/formatter/class-metabox-formatter.php index 3a248fd8d5d..0d0012a2d87 100644 --- a/admin/formatter/class-metabox-formatter.php +++ b/admin/formatter/class-metabox-formatter.php @@ -53,24 +53,15 @@ private function get_defaults() { $defaults = [ 'author_name' => get_the_author_meta( 'display_name' ), - 'site_name' => YoastSEO()->meta->for_current_page()->site_name, 'sitewide_social_image' => WPSEO_Options::get( 'og_default_image' ), - 'search_url' => '', - 'post_edit_url' => '', - 'base_url' => '', 'contentTab' => __( 'Readability', 'wordpress-seo' ), 'keywordTab' => __( 'Keyphrase:', 'wordpress-seo' ), 'removeKeyword' => __( 'Remove keyphrase', 'wordpress-seo' ), - 'contentLocale' => get_locale(), - 'userLocale' => get_user_locale(), 'translations' => $this->get_translations(), 'keyword_usage' => [], 'title_template' => '', 'metadesc_template' => '', 'intl' => $this->get_content_analysis_component_translations(), - 'isRtl' => is_rtl(), - 'isPremium' => YoastSEO()->helpers->product->is_premium(), - 'siteIconUrl' => get_site_icon_url(), 'showSocial' => [ 'facebook' => WPSEO_Options::get( 'opengraph', false ), 'twitter' => WPSEO_Options::get( 'twitter', false ), diff --git a/admin/formatter/class-post-metabox-formatter.php b/admin/formatter/class-post-metabox-formatter.php index aa4eb2f296f..b5ecc516dcc 100644 --- a/admin/formatter/class-post-metabox-formatter.php +++ b/admin/formatter/class-post-metabox-formatter.php @@ -62,9 +62,6 @@ public function use_social_templates() { public function get_values() { $values = [ - 'search_url' => $this->search_url(), - 'post_edit_url' => $this->edit_url(), - 'base_url' => $this->base_url_for_js(), 'metaDescriptionDate' => '', ]; @@ -106,52 +103,6 @@ protected function get_image_url() { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $this->post->ID ); } - /** - * Returns the url to search for keyword for the post. - * - * @return string - */ - private function search_url() { - return admin_url( 'edit.php?seo_kw_filter={keyword}' ); - } - - /** - * Returns the url to edit the taxonomy. - * - * @return string - */ - private function edit_url() { - return admin_url( 'post.php?post={id}&action=edit' ); - } - - /** - * Returns a base URL for use in the JS, takes permalink structure into account. - * - * @return string - */ - private function base_url_for_js() { - global $pagenow; - - // The default base is the home_url. - $base_url = home_url( '/', null ); - - if ( $pagenow === 'post-new.php' ) { - return $base_url; - } - - // If %postname% is the last tag, just strip it and use that as a base. - if ( preg_match( '#%postname%/?$#', $this->permalink ) === 1 ) { - $base_url = preg_replace( '#%postname%/?$#', '', $this->permalink ); - } - - // If %pagename% is the last tag, just strip it and use that as a base. - if ( preg_match( '#%pagename%/?$#', $this->permalink ) === 1 ) { - $base_url = preg_replace( '#%pagename%/?$#', '', $this->permalink ); - } - - return $base_url; - } - /** * Counts the number of given keywords used for other posts other than the given post_id. * diff --git a/admin/formatter/class-term-metabox-formatter.php b/admin/formatter/class-term-metabox-formatter.php index aa596cdcaa2..cec44471c1a 100644 --- a/admin/formatter/class-term-metabox-formatter.php +++ b/admin/formatter/class-term-metabox-formatter.php @@ -71,9 +71,6 @@ public function get_values() { // Todo: a column needs to be added on the termpages to add a filter for the keyword, so this can be used in the focus keyphrase doubles. if ( is_object( $this->term ) && property_exists( $this->term, 'taxonomy' ) ) { $values = [ - 'search_url' => $this->search_url(), - 'post_edit_url' => $this->edit_url(), - 'base_url' => $this->base_url_for_js(), 'taxonomy' => $this->term->taxonomy, 'keyword_usage' => $this->get_focus_keyword_usage(), 'title_template' => $this->get_title_template(), @@ -101,41 +98,6 @@ protected function get_image_url() { return WPSEO_Image_Utils::get_first_content_image_for_term( $this->term->term_id ); } - /** - * Returns the url to search for keyword for the taxonomy. - * - * @return string - */ - private function search_url() { - return admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' ); - } - - /** - * Returns the url to edit the taxonomy. - * - * @return string - */ - private function edit_url() { - return admin_url( 'term.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' ); - } - - /** - * Returns a base URL for use in the JS, takes permalink structure into account. - * - * @return string - */ - private function base_url_for_js() { - - $base_url = home_url( '/', null ); - if ( ! WPSEO_Options::get( 'stripcategorybase', false ) ) { - if ( $this->taxonomy->rewrite ) { - $base_url = trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] ); - } - } - - return $base_url; - } - /** * Counting the number of given keyword used for other term than given term_id. * diff --git a/admin/metabox/class-metabox.php b/admin/metabox/class-metabox.php index 7dc1329ff3d..10c3a921da5 100644 --- a/admin/metabox/class-metabox.php +++ b/admin/metabox/class-metabox.php @@ -5,14 +5,12 @@ * @package WPSEO\Admin */ -use Yoast\WP\SEO\Actions\Alert_Dismissal_Action; use Yoast\WP\SEO\Conditionals\Third_Party\Jetpack_Boost_Active_Conditional; use Yoast\WP\SEO\Conditionals\Third_Party\Jetpack_Boost_Not_Premium_Conditional; use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional; -use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; +use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository; use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter; use Yoast\WP\SEO\Presenters\Admin\Meta_Fields_Presenter; -use Yoast\WP\SEO\Promotions\Application\Promotion_Manager; /** * This class generates the metabox on the edit post / page as well as contains all page analysis functionality. @@ -285,12 +283,7 @@ public function wpseo_metabox_class( $classes ) { * @return array|bool|int> */ public function get_metabox_script_data() { - $permalink = ''; - - if ( is_object( $this->get_metabox_post() ) ) { - $permalink = get_sample_permalink( $this->get_metabox_post()->ID ); - $permalink = $permalink[0]; - } + $permalink = $this->get_permalink(); $post_formatter = new WPSEO_Metabox_Formatter( new WPSEO_Post_Metabox_Formatter( $this->get_metabox_post(), [], $permalink ) @@ -913,8 +906,6 @@ public function enqueue() { 'log_level' => WPSEO_Utils::get_analysis_worker_log_level(), ]; - $alert_dismissal_action = YoastSEO()->classes->get( Alert_Dismissal_Action::class ); - $dismissed_alerts = $alert_dismissal_action->all_dismissed(); $woocommerce_conditional = new WooCommerce_Conditional(); $woocommerce_active = $woocommerce_conditional->is_met(); $wpseo_plugin_availability_checker = new WPSEO_Plugin_Availability(); @@ -936,20 +927,21 @@ public function enqueue() { 'plugins' => $plugins_script_data, 'worker' => $worker_script_data, ], - 'dismissedAlerts' => $dismissed_alerts, - 'currentPromotions' => YoastSEO()->classes->get( Promotion_Manager::class )->get_current_promotions(), - 'webinarIntroBlockEditorUrl' => WPSEO_Shortlinker::get( 'https://yoa.st/webinar-intro-block-editor' ), - 'blackFridayBlockEditorUrl' => ( YoastSEO()->classes->get( Promotion_Manager::class )->is( 'black-friday-2023-checklist' ) ) ? WPSEO_Shortlinker::get( 'https://yoa.st/black-friday-checklist' ) : '', 'isJetpackBoostActive' => ( $is_block_editor ) ? YoastSEO()->classes->get( Jetpack_Boost_Active_Conditional::class )->is_met() : false, 'isJetpackBoostNotPremium' => ( $is_block_editor ) ? YoastSEO()->classes->get( Jetpack_Boost_Not_Premium_Conditional::class )->is_met() : false, 'isWooCommerceSeoActive' => $woocommerce_seo_active, 'isWooCommerceActive' => $woocommerce_active, 'woocommerceUpsell' => get_post_type( $post_id ) === 'product' && ! $woocommerce_seo_active && $woocommerce_active, - 'linkParams' => WPSEO_Shortlinker::get_query_params(), - 'pluginUrl' => plugins_url( '', WPSEO_FILE ), - 'wistiaEmbedPermission' => YoastSEO()->classes->get( Wistia_Embed_Permission_Repository::class )->get_value_for_user( get_current_user_id() ), ]; - + /** + * The website information repository. + * + * @var $repo Website_Information_Repository + */ + $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); + $site_information = $repo->get_post_site_information(); + $site_information->set_permalink( $this->get_permalink() ); + $script_data = array_merge_recursive( $site_information->get_legacy_site_information(), $script_data ); if ( post_type_supports( get_post_type(), 'thumbnail' ) ) { $asset_manager->enqueue_style( 'featured-image' ); @@ -1207,4 +1199,20 @@ public static function is_post_edit( $page ) { protected function get_product_title() { return YoastSEO()->helpers->product->get_product_name(); } + + /** + * Gets the permalink. + * + * @return string + */ + protected function get_permalink() { + $permalink = ''; + + if ( is_object( $this->get_metabox_post() ) ) { + $permalink = get_sample_permalink( $this->get_metabox_post()->ID ); + $permalink = $permalink[0]; + } + + return $permalink; + } } diff --git a/admin/taxonomy/class-taxonomy.php b/admin/taxonomy/class-taxonomy.php index 423b02d9bb2..a0f1fd40b73 100644 --- a/admin/taxonomy/class-taxonomy.php +++ b/admin/taxonomy/class-taxonomy.php @@ -5,7 +5,7 @@ * @package WPSEO\Admin */ -use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; +use Yoast\WP\SEO\Editors\Application\Site\Website_Information_Repository; use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter; /** @@ -192,10 +192,18 @@ public function admin_enqueue_scripts() { 'postId' => $tag_id, 'termType' => $this->get_taxonomy(), 'usedKeywordsNonce' => wp_create_nonce( 'wpseo-keyword-usage' ), - 'linkParams' => WPSEO_Shortlinker::get_query_params(), - 'pluginUrl' => plugins_url( '', WPSEO_FILE ), - 'wistiaEmbedPermission' => YoastSEO()->classes->get( Wistia_Embed_Permission_Repository::class )->get_value_for_user( get_current_user_id() ), ]; + + /** + * The website information repository. + * + * @var $repo Website_Information_Repository + */ + $repo = YoastSEO()->classes->get( Website_Information_Repository::class ); + $term_information = $repo->get_term_site_information(); + $term_information->set_term( get_term_by( 'id', $tag_id, $this::get_taxonomy() ) ); + $script_data = array_merge_recursive( $term_information->get_site_information(), $script_data ); + $asset_manager->localize_script( 'term-edit', 'wpseoScriptData', $script_data ); $asset_manager->enqueue_user_language_script(); } diff --git a/composer.json b/composer.json index cc188b0e052..bd9aec02bd4 100644 --- a/composer.json +++ b/composer.json @@ -92,7 +92,7 @@ "Yoast\\WP\\SEO\\Composer\\Actions::check_coding_standards" ], "check-cs-thresholds": [ - "@putenv YOASTCS_THRESHOLD_ERRORS=2545", + "@putenv YOASTCS_THRESHOLD_ERRORS=2544", "@putenv YOASTCS_THRESHOLD_WARNINGS=253", "Yoast\\WP\\SEO\\Composer\\Actions::check_cs_thresholds" ], diff --git a/src/editors/application/integrations/integration-information-repository.php b/src/editors/application/integrations/integration-information-repository.php index 373cc0021d6..c066fc2f740 100644 --- a/src/editors/application/integrations/integration-information-repository.php +++ b/src/editors/application/integrations/integration-information-repository.php @@ -4,7 +4,6 @@ namespace Yoast\WP\SEO\Editors\Application\Integrations; use Yoast\WP\SEO\Editors\Domain\Integrations\Integration_Data_Provider_Interface; -use Yoast\WP\SEO\Editors\Framework\Analysis_Feature_Interface; /** * The repository to get all enabled integrations. @@ -16,7 +15,7 @@ class Integration_Information_Repository { /** * All plugin integrations. * - * @var Analysis_Feature_Interface[] $plugin_integrations + * @var Integration_Data_Provider_Interface[] $plugin_integrations */ private $plugin_integrations; diff --git a/src/editors/application/site/website-information-repository.php b/src/editors/application/site/website-information-repository.php new file mode 100644 index 00000000000..6ff099f9175 --- /dev/null +++ b/src/editors/application/site/website-information-repository.php @@ -0,0 +1,61 @@ +post_site_information = $post_site_information; + $this->term_site_information = $term_site_information; + } + + /** + * Returns the Post Site Information container. + * + * @return Post_Site_Information + */ + public function get_post_site_information(): Post_Site_Information { + return $this->post_site_information; + } + + /** + * Returns the Term Site Information container. + * + * @return Term_Site_Information + */ + public function get_term_site_information(): Term_Site_Information { + return $this->term_site_information; + } +} diff --git a/src/editors/framework/site/base-site-information.php b/src/editors/framework/site/base-site-information.php new file mode 100644 index 00000000000..c87587c9d8c --- /dev/null +++ b/src/editors/framework/site/base-site-information.php @@ -0,0 +1,129 @@ +promotion_manager = $promotion_manager; + $this->short_link_helper = $short_link_helper; + $this->wistia_embed_permission_repository = $wistia_embed_permission_repository; + $this->meta = $meta; + $this->product_helper = $product_helper; + $this->alert_dismissal_action = $alert_dismissal_action; + } + + /** + * Returns site information that is the + * + * @throws Exception If an invalid user ID is supplied to the wistia repository. + * @return array + */ + public function get_site_information(): array { + return [ + 'linkParams' => $this->short_link_helper->get_query_params(), + 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), + 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), + 'site_name' => $this->meta->for_current_page()->site_name, + 'contentLocale' => \get_locale(), + 'userLocale' => \get_user_locale(), + 'isRtl' => \is_rtl(), + 'isPremium' => $this->product_helper->is_premium(), + 'siteIconUrl' => \get_site_icon_url(), + ]; + } + + /** + * Returns site information that is the + * + * @throws Exception If an invalid user ID is supplied to the wistia repository. + * @return array + */ + public function get_legacy_site_information(): array { + + return [ + 'linkParams' => $this->short_link_helper->get_query_params(), + 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), + 'wistiaEmbedPermission' => $this->wistia_embed_permission_repository->get_value_for_user( \get_current_user_id() ), + 'metabox' => [ + 'site_name' => $this->meta->for_current_page()->site_name, + 'contentLocale' => \get_locale(), + 'userLocale' => \get_user_locale(), + 'isRtl' => \is_rtl(), + 'isPremium' => $this->product_helper->is_premium(), + 'siteIconUrl' => \get_site_icon_url(), + ], + ]; + } +} diff --git a/src/editors/framework/site/post-site-information.php b/src/editors/framework/site/post-site-information.php new file mode 100644 index 00000000000..454ae44dcc4 --- /dev/null +++ b/src/editors/framework/site/post-site-information.php @@ -0,0 +1,118 @@ +permalink = $permalink; + } + + /** + * Returns post specific site information together with the generic site information. + * + * @return array + */ + public function get_legacy_site_information(): array { + $dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); + + $data = [ + 'dismissedAlerts' => $dismissed_alerts, + 'currentPromotions' => $this->promotion_manager->get_current_promotions(), + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), + 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', + + 'metabox' => [ + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), + ], + ]; + + return \array_merge_recursive( $data, parent::get_legacy_site_information() ); + } + + /** + * Returns post specific site information together with the generic site information. + * + * @return array + */ + public function get_site_information(): array { + $dismissed_alerts = $this->alert_dismissal_action->all_dismissed(); + + $data = [ + 'dismissedAlerts' => $dismissed_alerts, + 'currentPromotions' => $this->promotion_manager->get_current_promotions(), + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), + 'blackFridayBlockEditorUrl' => ( $this->promotion_manager->is( 'black-friday-2023-checklist' ) ) ? $this->short_link_helper->get( 'https://yoa.st/black-friday-checklist' ) : '', + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), + ]; + + return \array_merge( $data, parent::get_site_information() ); + } + + /** + * Returns the url to search for keyword for the post. + * + * @return string + */ + private function search_url(): string { + return \admin_url( 'edit.php?seo_kw_filter={keyword}' ); + } + + /** + * Returns the url to edit the taxonomy. + * + * @return string + */ + private function edit_url(): string { + return \admin_url( 'post.php?post={id}&action=edit' ); + } + + /** + * Returns a base URL for use in the JS, takes permalink structure into account. + * + * @return string + */ + private function base_url_for_js(): string { + global $pagenow; + + // The default base is the home_url. + $base_url = \home_url( '/', null ); + + if ( $pagenow === 'post-new.php' ) { + return $base_url; + } + + // If %postname% is the last tag, just strip it and use that as a base. + if ( \preg_match( '#%postname%/?$#', $this->permalink ) === 1 ) { + $base_url = \preg_replace( '#%postname%/?$#', '', $this->permalink ); + } + + // If %pagename% is the last tag, just strip it and use that as a base. + if ( \preg_match( '#%pagename%/?$#', $this->permalink ) === 1 ) { + $base_url = \preg_replace( '#%pagename%/?$#', '', $this->permalink ); + } + + return $base_url; + } +} diff --git a/src/editors/framework/site/term-site-information.php b/src/editors/framework/site/term-site-information.php new file mode 100644 index 00000000000..939b36007d5 --- /dev/null +++ b/src/editors/framework/site/term-site-information.php @@ -0,0 +1,142 @@ +options_helper = $options_helper; + } + + /** + * Sets the term for the information object and retrieves its taxonomy. + * + * @param WP_Term|string|false $term The term. + * + * @return void + */ + public function set_term( $term ) { + $this->term = $term; + $this->taxonomy = \get_taxonomy( $term->taxonomy ); + } + + /** + * Returns term specific site information together with the generic site information. + * + * @return array + */ + public function get_site_information(): array { + $data = [ + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), + ]; + + return \array_merge_recursive( $data, parent::get_site_information() ); + } + + /** + * Returns term specific site information together with the generic site information. + * + * @return array + */ + public function get_legacy_site_information(): array { + $data = [ + 'metabox' => [ + 'search_url' => $this->search_url(), + 'post_edit_url' => $this->edit_url(), + 'base_url' => $this->base_url_for_js(), + ], + ]; + + return \array_merge_recursive( $data, parent::get_legacy_site_information() ); + } + + /** + * Returns the url to search for keyword for the taxonomy. + * + * @return string + */ + private function search_url(): string { + return \admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' ); + } + + /** + * Returns the url to edit the taxonomy. + * + * @return string + */ + private function edit_url(): string { + return \admin_url( 'term.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' ); + } + + /** + * Returns a base URL for use in the JS, takes permalink structure into account. + * + * @return string + */ + private function base_url_for_js(): string { + $base_url = \home_url( '/', null ); + if ( ! $this->options_helper->get( 'stripcategorybase', false ) ) { + if ( $this->taxonomy->rewrite ) { + $base_url = \trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] ); + } + } + + return $base_url; + } +} diff --git a/tests/Unit/Admin/User_Profile_Test.php b/tests/Unit/Admin/User_Profile_Test.php index 9f70d799c26..7a8c945b7b5 100644 --- a/tests/Unit/Admin/User_Profile_Test.php +++ b/tests/Unit/Admin/User_Profile_Test.php @@ -2,7 +2,6 @@ namespace Yoast\WP\SEO\Tests\Unit\Admin; -use Brain\Monkey; use WPSEO_Admin_User_Profile; use Yoast\WP\SEO\Tests\Unit\TestCase; diff --git a/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php b/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php new file mode 100644 index 00000000000..b0685b617cf --- /dev/null +++ b/tests/Unit/Doubles/Editors/Site_Information_Mocks_Trait.php @@ -0,0 +1,40 @@ +site_name = 'examepl.com'; + $locale = 'nl_NL'; + + Monkey\Functions\expect( 'plugins_url' )->andReturn( $plugin_url ); + Monkey\Functions\expect( 'get_current_user_id' )->andReturn( 1 ); + Monkey\Functions\expect( 'get_locale' )->andReturn( $locale ); + Monkey\Functions\expect( 'get_user_locale' )->andReturn( $locale ); + Monkey\Functions\expect( 'is_rtl' )->andReturnFalse(); + Monkey\Functions\expect( 'get_site_icon_url' )->andReturn( 'https://example.org' ); + + $this->alert_dismissal_action->expects( 'all_dismissed' )->andReturn( $dismissed_alerts ); + $this->promotion_manager->expects( 'get_current_promotions' )->andReturn( $promotions ); + $this->promotion_manager->expects( 'is' )->andReturnFalse(); + $this->short_link_helper->expects( 'get' )->andReturn( $short_link ); + $this->short_link_helper->expects( 'get_query_params' )->andReturn( $query_params ); + $this->wistia_embed_repo->expects( 'get_value_for_user' )->with( 1 )->andReturnTrue(); + $this->meta_surface->expects( 'for_current_page' )->andReturn( $meta_surface ); + $this->product_helper->expects( 'is_premium' )->andReturnTrue(); + } +} diff --git a/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php new file mode 100644 index 00000000000..ef4259feeb9 --- /dev/null +++ b/tests/Unit/Editors/Framework/Site/Post_Site_Information_Test.php @@ -0,0 +1,194 @@ +promotion_manager = Mockery::mock( Promotion_Manager::class ); + $this->short_link_helper = Mockery::mock( Short_Link_Helper::class ); + $this->wistia_embed_repo = Mockery::mock( Wistia_Embed_Permission_Repository::class ); + $this->meta_surface = Mockery::mock( Meta_Surface::class ); + $this->product_helper = Mockery::mock( Product_Helper::class ); + $this->alert_dismissal_action = Mockery::mock( Alert_Dismissal_Action::class ); + + $this->instance = new Post_Site_Information( $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + $this->instance->set_permalink( 'perma' ); + $this->set_mocks(); + } + + /** + * Tests the get_legacy_site_information. + * + * @covers ::__construct + * @covers ::get_legacy_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_permalink + * + * @return void + */ + public function test_legacy_site_information() { + $expected = [ + 'dismissedAlerts' => [ + 'the alert', + ], + 'currentPromotions' => [ + 'the promotion', + 'another one', + ], + 'webinarIntroBlockEditorUrl' => 'https://expl.c', + 'blackFridayBlockEditorUrl' => '', + 'metabox' => [ + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', + 'site_name' => 'examepl.com', + 'contentLocale' => 'nl_NL', + 'userLocale' => 'nl_NL', + 'isRtl' => false, + 'isPremium' => true, + 'siteIconUrl' => 'https://example.org', + ], + 'linkParams' => [ + 'param', + 'param2', + ], + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, + + ]; + + Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); + Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + + $this->assertSame( $expected, $this->instance->get_legacy_site_information() ); + } + + /** + * Tests the get_site_information. + * + * @covers ::__construct + * @covers ::get_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_permalink + * + * @return void + */ + public function test_site_information() { + $expected = [ + + 'dismissedAlerts' => [ + 'the alert', + ], + 'currentPromotions' => [ + 'the promotion', + 'another one', + ], + 'webinarIntroBlockEditorUrl' => 'https://expl.c', + 'blackFridayBlockEditorUrl' => '', + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', + + 'linkParams' => [ + 'param', + 'param2', + ], + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, + 'site_name' => 'examepl.com', + 'contentLocale' => 'nl_NL', + 'userLocale' => 'nl_NL', + 'isRtl' => false, + 'isPremium' => true, + 'siteIconUrl' => 'https://example.org', + + ]; + + Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); + Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + + $this->assertSame( $expected, $this->instance->get_site_information() ); + } +} diff --git a/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php new file mode 100644 index 00000000000..310200fd200 --- /dev/null +++ b/tests/Unit/Editors/Framework/Site/Term_Site_Information_Test.php @@ -0,0 +1,214 @@ +options_helper = Mockery::mock( Options_Helper::class ); + $this->promotion_manager = Mockery::mock( Promotion_Manager::class ); + $this->short_link_helper = Mockery::mock( Short_Link_Helper::class ); + $this->wistia_embed_repo = Mockery::mock( Wistia_Embed_Permission_Repository::class ); + $this->meta_surface = Mockery::mock( Meta_Surface::class ); + $this->product_helper = Mockery::mock( Product_Helper::class ); + $this->alert_dismissal_action = Mockery::mock( Alert_Dismissal_Action::class ); + + $this->instance = new Term_Site_Information( $this->options_helper, $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + $taxonomy = Mockery::mock( WP_Taxonomy::class )->makePartial(); + $taxonomy->rewrite = false; + $mock_term = Mockery::mock( WP_Term::class )->makePartial(); + $mock_term->taxonomy = 'tax'; + $mock_term->term_id = 1; + + Monkey\Functions\expect( 'get_taxonomy' )->andReturn( $taxonomy ); + + $this->instance->set_term( $mock_term ); + $this->options_helper->expects( 'get' )->with( 'stripcategorybase', false )->andReturnFalse(); + + $this->set_mocks(); + } + + /** + * Tests the get_site_information. + * + * @covers ::__construct + * @covers ::get_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_term + * + * @return void + */ + public function test_site_information() { + $expected = [ + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', + + 'dismissedAlerts' => [ + 'the alert', + ], + 'currentPromotions' => [ + 'the promotion', + 'another one', + ], + 'webinarIntroBlockEditorUrl' => 'https://expl.c', + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => [ + 'param', + 'param2', + ], + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, + 'site_name' => 'examepl.com', + 'contentLocale' => 'nl_NL', + 'userLocale' => 'nl_NL', + 'isRtl' => false, + 'isPremium' => true, + 'siteIconUrl' => 'https://example.org', + ]; + + Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); + Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + + $this->assertSame( $expected, $this->instance->get_site_information() ); + } + + /** + * Tests the get_legacy_site_information. + * + * @covers ::__construct + * @covers ::get_legacy_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_term + * + * @return void + */ + public function test_legacy_site_information() { + + $expected = [ + 'metabox' => [ + 'search_url' => 'https://example.org', + 'post_edit_url' => 'https://example.org', + 'base_url' => 'https://example.org', + 'site_name' => 'examepl.com', + 'contentLocale' => 'nl_NL', + 'userLocale' => 'nl_NL', + 'isRtl' => false, + 'isPremium' => true, + 'siteIconUrl' => 'https://example.org', + ], + 'dismissedAlerts' => [ + 'the alert', + ], + 'currentPromotions' => [ + 'the promotion', + 'another one', + ], + 'webinarIntroBlockEditorUrl' => 'https://expl.c', + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => [ + 'param', + 'param2', + ], + 'pluginUrl' => '/location', + 'wistiaEmbedPermission' => true, + + ]; + + Monkey\Functions\expect( 'admin_url' )->andReturn( 'https://example.org' ); + Monkey\Functions\expect( 'home_url' )->andReturn( 'https://example.org' ); + + $this->assertSame( $expected, $this->instance->get_legacy_site_information() ); + } +} diff --git a/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php b/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php new file mode 100644 index 00000000000..45f75860f69 --- /dev/null +++ b/tests/WP/Editors/Framework/Site/Post_Site_Information_Test.php @@ -0,0 +1,166 @@ +promotion_manager = \YoastSEO()->classes->get( Promotion_Manager::class ); + $this->short_link_helper = \YoastSEO()->helpers->short_link; + $this->wistia_embed_repo = Mockery::mock( Wistia_Embed_Permission_Repository::class ); + $this->wistia_embed_repo->expects( 'get_value_for_user' )->with( 0 )->andReturnTrue(); + $this->meta_surface = \YoastSEO()->meta; + $this->product_helper = \YoastSEO()->helpers->product; + $this->alert_dismissal_action = \YoastSEO()->classes->get( Alert_Dismissal_Action::class ); + + $this->instance = new Post_Site_Information( $this->promotion_manager, $this->short_link_helper, $this->wistia_embed_repo, $this->meta_surface, $this->product_helper, $this->alert_dismissal_action ); + $this->instance->set_permalink( 'perma' ); + } + + /** + * Tests the get_legacy_site_information. + * + * @covers ::__construct + * @covers ::get_legacy_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_permalink + * + * @return void + */ + public function test_legacy_site_information() { + $expected = [ + 'metabox' => + [ + 'search_url' => 'http://example.org/wp-admin/edit.php?seo_kw_filter={keyword}', + 'post_edit_url' => 'http://example.org/wp-admin/post.php?post={id}&action=edit', + 'base_url' => 'http://example.org/', + 'site_name' => 'Test Blog', + 'contentLocale' => 'en_US', + 'userLocale' => 'en_US', + 'isRtl' => false, + 'isPremium' => false, + 'siteIconUrl' => '', + ], + 'dismissedAlerts' => false, + 'currentPromotions' => [], + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => $this->short_link_helper->get_query_params(), + 'pluginUrl' => 'http://example.org/wp-content/plugins/wordpress-seo', + 'wistiaEmbedPermission' => true, + ]; + + $this->assertSame( $expected, $this->instance->get_legacy_site_information() ); + } + + /** + * Tests the get_site_information. + * + * @covers ::__construct + * @covers ::get_site_information + * @covers ::search_url + * @covers ::base_url_for_js + * @covers ::edit_url + * @covers ::set_permalink + * + * @return void + */ + public function test_site_information() { + $expected = [ + 'search_url' => 'http://example.org/wp-admin/edit.php?seo_kw_filter={keyword}', + 'post_edit_url' => 'http://example.org/wp-admin/post.php?post={id}&action=edit', + 'base_url' => 'http://example.org/', + 'dismissedAlerts' => false, + 'currentPromotions' => [], + 'webinarIntroBlockEditorUrl' => $this->short_link_helper->get( 'https://yoa.st/webinar-intro-block-editor' ), + 'blackFridayBlockEditorUrl' => '', + 'linkParams' => $this->short_link_helper->get_query_params(), + 'pluginUrl' => 'http://example.org/wp-content/plugins/wordpress-seo', + 'wistiaEmbedPermission' => true, + 'site_name' => 'Test Blog', + 'contentLocale' => 'en_US', + 'userLocale' => 'en_US', + 'isRtl' => false, + 'isPremium' => false, + 'siteIconUrl' => '', + + ]; + + $this->assertSame( $expected, $this->instance->get_site_information() ); + } +} diff --git a/tests/WP/Formatter/Metabox_Formatter_Test.php b/tests/WP/Formatter/Metabox_Formatter_Test.php index 60c4225ad1f..80aa4a9ba5c 100644 --- a/tests/WP/Formatter/Metabox_Formatter_Test.php +++ b/tests/WP/Formatter/Metabox_Formatter_Test.php @@ -34,7 +34,6 @@ public function test_getting_the_values() { $result = $class_instance->get_values(); $this->assertEquals( 'Readability', $result['contentTab'] ); - $this->assertTrue( \array_key_exists( 'contentLocale', $result ) ); $this->assertTrue( \array_key_exists( 'translations', $result ) ); $this->assertTrue( \is_array( $result['translations'] ) ); } diff --git a/tests/WP/Formatter/Post_Metabox_Formatter_Test.php b/tests/WP/Formatter/Post_Metabox_Formatter_Test.php index ec909cf80c6..25845667e20 100644 --- a/tests/WP/Formatter/Post_Metabox_Formatter_Test.php +++ b/tests/WP/Formatter/Post_Metabox_Formatter_Test.php @@ -29,26 +29,6 @@ public function set_up() { $this->post = $this->factory->post->create_and_get(); } - /** - * Test the formatter when there isn't a post object and without any options. - * - * @covers WPSEO_Post_Metabox_Formatter::__construct - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::search_url - * @covers WPSEO_Post_Metabox_Formatter::edit_url - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_no_post_with_empty_options() { - $instance = new WPSEO_Post_Metabox_Formatter( null, [], '' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['search_url'], \admin_url( 'edit.php?seo_kw_filter={keyword}' ) ); - $this->assertEquals( $result['post_edit_url'], \admin_url( 'post.php?post={id}&action=edit' ) ); - $this->assertEquals( $result['base_url'], \YoastSEO()->helpers->url->home() ); - } - /** * Test with a post being set but with no options being set. * @@ -90,69 +70,4 @@ public function test_metabox_metadescription_date() { $this->assertEquals( $result['title_template'], 'This is the title' ); $this->assertEquals( $result['metadesc_template'], 'This is the metadescription' ); } - - /** - * Testing the formatter when 'being' on the new post page. - * - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_post_on_add_page() { - - $GLOBALS['pagenow'] = 'post-new.php'; - - $instance = new WPSEO_Post_Metabox_Formatter( $this->post, [], '' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['base_url'], \YoastSEO()->helpers->url->home() ); - - unset( $GLOBALS['pagenow'] ); - } - - /** - * Testing when the permalink structure contains '%postname%/'. This should be stripped. - * - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_with_permalink_structure() { - $instance = new WPSEO_Post_Metabox_Formatter( $this->post, [], 'http://example.org/test/%postname%/' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['base_url'], 'http://example.org/test/' ); - } - - /** - * Testing when the permalink structure contains '%pagename%/'. This should be stripped. - * - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_with_page_permalink_structure() { - $instance = new WPSEO_Post_Metabox_Formatter( $this->post, [], 'http://example.org/test/%pagename%/' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['base_url'], 'http://example.org/test/' ); - } - - /** - * Testing when the permalink structure contains '%postname%/'. This should be stripped. - * - * @covers WPSEO_Post_Metabox_Formatter::get_values - * @covers WPSEO_Post_Metabox_Formatter::base_url_for_js - * - * @return void - */ - public function test_with_unreplaceble_permalink_structure() { - $instance = new WPSEO_Post_Metabox_Formatter( $this->post, [], '%isnotreplaced%/' ); - $result = $instance->get_values(); - - $this->assertEquals( $result['base_url'], \YoastSEO()->helpers->url->home() ); - } } diff --git a/tests/WP/Formatter/Term_Metabox_Formatter_Test.php b/tests/WP/Formatter/Term_Metabox_Formatter_Test.php index 8b902914ba0..bb19baaba5a 100644 --- a/tests/WP/Formatter/Term_Metabox_Formatter_Test.php +++ b/tests/WP/Formatter/Term_Metabox_Formatter_Test.php @@ -76,10 +76,6 @@ public function test_with_taxonomy_and_term_and_without_options() { $result = $instance->get_values(); - $this->assertEquals( $result['search_url'], \admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' ) ); - $this->assertEquals( $result['post_edit_url'], \admin_url( 'term.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' ) ); - - $this->assertEquals( \trailingslashit( \home_url( 'tag' ) ), $result['base_url'] ); $this->assertEquals( [ '' => [] ], $result['keyword_usage'] ); $this->assertEquals( '%%term_title%% Archives %%page%% %%sep%% %%sitename%%', $result['title_template'] ); $this->assertEquals( '', $result['metadesc_template'] );