From 8b982697ced209e0b34e5891f783d05d11e37eaa Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 19 Jan 2024 09:10:01 +0100 Subject: [PATCH 01/18] Fix typo --- inc/sitemaps/class-sitemaps-cache-validator.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/inc/sitemaps/class-sitemaps-cache-validator.php b/inc/sitemaps/class-sitemaps-cache-validator.php index a1f5a67ccce..271e94998f4 100644 --- a/inc/sitemaps/class-sitemaps-cache-validator.php +++ b/inc/sitemaps/class-sitemaps-cache-validator.php @@ -180,21 +180,22 @@ public static function cleanup_database( $type = null, $validator = null ) { } /* - * Add slashes to the LIKE "_" single character wildcard. - * - * We can't use `esc_like` here because we need the % in the query. - */ + * Add slashes to the LIKE "_" single character wildcard. + * + * We can't use `esc_like` here because we need the % in the query. + */ $where = []; $where[] = sprintf( "option_name LIKE '%s'", addcslashes( '_transient_' . $like, '_' ) ); $where[] = sprintf( "option_name LIKE '%s'", addcslashes( '_transient_timeout_' . $like, '_' ) ); - + + $query = sprintf( 'DELETE FROM %1$s WHERE %2$s', $wpdb->options, implode( ' OR ', $where ) ); // Delete transients. //phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- We need to use a direct query here. //phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches. $wpdb->query( $wpdb->prepare( //phpcs:disable WordPress.DB.PreparedSQLPlaceholders -- %i placeholder is still not recognized. - 'DELETE FROM %i WHERE ' . implode( ' OR', array_fill( 0, count( $where ), '%s' ) ), + 'DELETE FROM %i WHERE ' . implode( ' OR ', array_fill( 0, count( $where ), '%s' ) ), array_merge( [ $wpdb->options ], $where ) ) ); From dd07e68660fe8c14cf3cdadcf94b083189e7a93a Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 19 Jan 2024 09:10:59 +0100 Subject: [PATCH 02/18] Change method visibility to protected when they need to be tested --- inc/class-upgrade.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/class-upgrade.php b/inc/class-upgrade.php index a358692ea48..aad671e25cc 100644 --- a/inc/class-upgrade.php +++ b/inc/class-upgrade.php @@ -304,7 +304,7 @@ private function upgrade_33() { * * @return void */ - private function upgrade_36() { + protected function upgrade_36() { global $wpdb; // Between 3.2 and 3.4 the sitemap options were saved with autoloading enabled. @@ -358,7 +358,7 @@ private function upgrade_47() { * * @return void */ - private function upgrade_49() { + protected function upgrade_49() { global $wpdb; /* @@ -423,7 +423,7 @@ public function remove_about_notice( $notifications ) { * * @return void */ - private function upgrade_50() { + protected function upgrade_50() { global $wpdb; // Deletes the post meta value, which might created in the RC. @@ -612,7 +612,7 @@ private function upgrade_772() { * * @return void */ - private function upgrade_90() { + protected function upgrade_90() { global $wpdb; // Invalidate all sitemap cache transients. From f4320c3c7013d00471cc7b0248489f0e4ca96104 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 19 Jan 2024 09:11:14 +0100 Subject: [PATCH 03/18] Add methods to be tested --- tests/WP/Doubles/Inc/Upgrade_Double.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/WP/Doubles/Inc/Upgrade_Double.php b/tests/WP/Doubles/Inc/Upgrade_Double.php index fc5b7b39be5..2e8f58f1c89 100644 --- a/tests/WP/Doubles/Inc/Upgrade_Double.php +++ b/tests/WP/Doubles/Inc/Upgrade_Double.php @@ -61,4 +61,24 @@ public function save_option_setting( $source_data, $source_setting, $target_sett public function finish_up( $previous_version = null ) { parent::finish_up( $previous_version ); } + + public function upgrade_36() { + parent::upgrade_36(); + } + + public function upgrade_49() { + parent::upgrade_49(); + } + + public function upgrade_50() { + parent::upgrade_50(); + } + + public function upgrade_90() { + parent::upgrade_90(); + } + + public function clean_up_private_taxonomies_for_141() { + parent::clean_up_private_taxonomies_for_141(); + } } From fccac1fba52a8dddec7c980eb43a06885bda7e78 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 19 Jan 2024 09:11:19 +0100 Subject: [PATCH 04/18] Add tests --- tests/WP/Inc/Upgrade_Test.php | 202 ++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index eef3175aee8..297952a04d1 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -4,6 +4,13 @@ use stdClass; use WPSEO_Options; +use Yoast_Notification; +use Yoast_Notification_Center; +use Yoast\WP\Lib\Model; +use Yoast\WP\Lib\ORM; +use Yoast\WP\SEO\Builders\Indexable_Term_Builder; +use Yoast\WP\SEO\Models\Indexable; +use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions; use Yoast\WP\SEO\Tests\WP\Doubles\Inc\Upgrade_Double; use Yoast\WP\SEO\Tests\WP\TestCase; @@ -199,6 +206,201 @@ public function test_finish_up() { $this->assertNotEmpty( \get_option( 'wpseo_social' ) ); } + public function test_upgrade_36() { + global $wpdb; + + \add_option( 'wpseo_sitemap_test', 'test', '', 'yes' ); + \add_option( 'wpseo_sitemap_test_no_autoload', 'test no autoload', '', 'no' ); + + $instance = $this->get_instance(); + $instance->upgrade_36(); + + $number_of_sitemap_options = $wpdb->query( + $wpdb->prepare( "SELECT * + FROM %i + WHERE %i LIKE %s + AND %i = 'yes'", + [ $wpdb->options, 'option_name', "wpseo_sitemap_%", 'autoload' ] + ) + ); + + $number_of_sitemap_options_no_autoload = $wpdb->query( + $wpdb->prepare( "SELECT * + FROM %i + WHERE %i LIKE %s + AND %i = 'no'", + [ $wpdb->options, 'option_name', 'wpseo_sitemap_%', 'autoload' ] + ) + ); + $this->assertEquals( 0, $number_of_sitemap_options ); + $this->assertEquals( 1, $number_of_sitemap_options_no_autoload ); + } + + public function test_upgrade_49() { + \wp_create_user( 'test_user', 'test password', 'test@test.org' ); + + $notifications = [ + [ + 'options' => [ 'id' => 'wpseo-dismiss-about', 'is_dismissed' => false ], + 'message' => 'Notification 1', + ], + [ + 'options' => [ 'is_dismissed' => false ], + 'message' => 'Notification 2', + ], + [ + 'options' => [ 'id' => 'not-wpseo-dismiss-about', 'is_dismissed' => false ], + 'message' => 'Notification 3', + ], + ]; + + \update_user_option( 1, Yoast_Notification_Center::STORAGE_KEY, array_values( $notifications ) ); + + $notifications = [ + [ + 'options' => [ 'id' => 'wpseo-dismiss-about', 'is_dismissed' => false ], + 'message' => 'Notification 4', + ], + [ + 'options' => [ 'is_dismissed' => false ], + 'message' => 'Notification 5', + ] + ]; + + \update_user_option( 2, Yoast_Notification_Center::STORAGE_KEY, array_values( $notifications ) ); + + $instance = $this->get_instance(); + + $instance->upgrade_49(); + + $user_id1_notifications = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, 1 ); + $user_id2_notification = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, 2 ); + + $this->assertEquals( 2, count( $user_id1_notifications ) ); + $this->assertEquals( 1, count( $user_id2_notification ) ); + } + + public function test_upgrade_50() { + global $wpdb; + + $post_id1 = self::factory()->post->create(); + add_post_meta( $post_id1, '_yst_content_links_processed', true ); + $post_id2 = self::factory()->post->create(); + add_post_meta( $post_id2, '_yst_content_links_processed', false ); + + $instance = $this->get_instance(); + + $instance->upgrade_50(); + + $number_of_rows = $wpdb->query( + $wpdb->prepare( 'SELECT * + FROM %i + WHERE %i = "_yst_content_links_processed"', + [ $wpdb->postmeta, 'meta_key' ] + ) + ); + + $this->assertEquals( 0, $number_of_rows ); + + } + + public function test_upgrade_90() { + global $wpdb; + $instance = $this->get_instance(); + + \add_option( 'wpseo_sitemap_test', 'test', '', 'yes' ); + \add_option( 'wpseo_sitemap_test_no_autoload', 'test no autoload', '', 'no' ); + + $instance->upgrade_90(); + $number_of_sitemap_options = $wpdb->query( + $wpdb->prepare( 'SELECT * + FROM %i + WHERE %i LIKE %s + AND %i = "yes"', + [ $wpdb->options, 'option_name', 'wpseo_sitemap_%', 'autoload' ] + ) + ); + + $this->assertEquals( 0, $number_of_sitemap_options ); + } + + public function test_upgrade_125() { + global $wpdb; + + $admin_id_1 = self::factory()->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $admin_id_1 ); + + $admin_role = get_role( 'administrator' ); + + $admin_role->add_cap( 'wpseo_manage_options', true ); + + $notification = new Yoast_Notification( + 'This is a test notification.', + [ + 'type' => Yoast_Notification::WARNING, + 'id' => 'wpseo-dismiss-wordpress-upgrade', + 'capabilities' => 'wpseo_manage_options', + 'priority' => 0.8, + ] + ); + + $center = Yoast_Notification_Center::get(); + $center->add_notification( $notification ); + + \add_user_meta( 1, 'wp_yoast_promo_hide_premium_upsell_admin_block', 'test', true ); + + $instance = $this->get_instance(); + $instance->upgrade_125(); + + $number_of_rows = $wpdb->query( + $wpdb->prepare( 'SELECT * + FROM %i + WHERE %i = %s', + [ $wpdb->usermeta, 'meta_key', 'wp_yoast_promo_hide_premium_upsell_admin_block' ] + ) + ); + + $this->assertEquals( 0, $number_of_rows ); + $this->assertNull( $center->get_notification_by_id( 'wpseo-dismiss-wordpress-upgrade' ) ); + } + + public function test_clean_up_private_taxonomies_for_141() { + global $wpdb; + + register_taxonomy( 'wpseo_tax', 'post' ); + $term_id = self::factory()->term->create( array( 'taxonomy' => 'wpseo_tax', 'public' => false ) ); + + $term_builder = new Indexable_Term_Builder( + \YoastSEO()->helpers->taxonomy, + \YoastSEO()->classes->get( Indexable_Builder_Versions::class ), + \YoastSEO()->helpers->post + ); + + $term_builder->set_social_image_helpers( + \YoastSEO()->helpers->image, + \YoastSEO()->helpers->open_graph->image, + \YoastSEO()->helpers->twitter->image + ); + + $indexable = new Indexable(); + $indexable->orm = ORM::for_table( 'wp_yoast_indexable' ); + + $result = $term_builder->build( $term_id, $indexable ); + + $wpdb->query( + $wpdb->prepare( + 'UPDATE %i SET %i = false WHERE %i = %d', + [ Model::get_table_name( 'Indexable' ), 'term_taxonomy_id', $result->object_id ] + ) + ); + + $instance = $this->get_instance(); + $instance->clean_up_private_taxonomies_for_141(); + + $term = get_term( $term_id, $taxonomy ); + + $this->assertNull( $term ); + } /** * Provides a filter return value. * From 2cfdcc5b789c9375d525967f9d53455683c77dca Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Tue, 30 Jan 2024 16:20:44 +0100 Subject: [PATCH 05/18] Fix test --- tests/WP/Inc/Upgrade_Test.php | 105 ++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 38 deletions(-) diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index 297952a04d1..1c9de11f32e 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -4,15 +4,15 @@ use stdClass; use WPSEO_Options; -use Yoast_Notification; -use Yoast_Notification_Center; use Yoast\WP\Lib\Model; use Yoast\WP\Lib\ORM; use Yoast\WP\SEO\Builders\Indexable_Term_Builder; use Yoast\WP\SEO\Models\Indexable; -use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions; use Yoast\WP\SEO\Tests\WP\Doubles\Inc\Upgrade_Double; use Yoast\WP\SEO\Tests\WP\TestCase; +use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions; +use Yoast_Notification; +use Yoast_Notification_Center; /** * Unit Test Class. @@ -208,24 +208,26 @@ public function test_finish_up() { public function test_upgrade_36() { global $wpdb; - + \add_option( 'wpseo_sitemap_test', 'test', '', 'yes' ); \add_option( 'wpseo_sitemap_test_no_autoload', 'test no autoload', '', 'no' ); - + $instance = $this->get_instance(); $instance->upgrade_36(); $number_of_sitemap_options = $wpdb->query( - $wpdb->prepare( "SELECT * + $wpdb->prepare( + "SELECT * FROM %i WHERE %i LIKE %s AND %i = 'yes'", - [ $wpdb->options, 'option_name', "wpseo_sitemap_%", 'autoload' ] + [ $wpdb->options, 'option_name', 'wpseo_sitemap_%', 'autoload' ] ) ); $number_of_sitemap_options_no_autoload = $wpdb->query( - $wpdb->prepare( "SELECT * + $wpdb->prepare( + "SELECT * FROM %i WHERE %i LIKE %s AND %i = 'no'", @@ -241,7 +243,10 @@ public function test_upgrade_49() { $notifications = [ [ - 'options' => [ 'id' => 'wpseo-dismiss-about', 'is_dismissed' => false ], + 'options' => [ + 'id' => 'wpseo-dismiss-about', + 'is_dismissed' => false, + ], 'message' => 'Notification 1', ], [ @@ -249,51 +254,58 @@ public function test_upgrade_49() { 'message' => 'Notification 2', ], [ - 'options' => [ 'id' => 'not-wpseo-dismiss-about', 'is_dismissed' => false ], + 'options' => [ + 'id' => 'not-wpseo-dismiss-about', + 'is_dismissed' => false, + ], 'message' => 'Notification 3', ], ]; - \update_user_option( 1, Yoast_Notification_Center::STORAGE_KEY, array_values( $notifications ) ); + \update_user_option( 1, Yoast_Notification_Center::STORAGE_KEY, \array_values( $notifications ) ); $notifications = [ [ - 'options' => [ 'id' => 'wpseo-dismiss-about', 'is_dismissed' => false ], + 'options' => [ + 'id' => 'wpseo-dismiss-about', + 'is_dismissed' => false, + ], 'message' => 'Notification 4', ], [ 'options' => [ 'is_dismissed' => false ], 'message' => 'Notification 5', - ] + ], ]; - - \update_user_option( 2, Yoast_Notification_Center::STORAGE_KEY, array_values( $notifications ) ); + + \update_user_option( 2, Yoast_Notification_Center::STORAGE_KEY, \array_values( $notifications ) ); $instance = $this->get_instance(); $instance->upgrade_49(); $user_id1_notifications = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, 1 ); - $user_id2_notification = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, 2 ); + $user_id2_notification = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, 2 ); - $this->assertEquals( 2, count( $user_id1_notifications ) ); - $this->assertEquals( 1, count( $user_id2_notification ) ); + $this->assertEquals( 2, \count( $user_id1_notifications ) ); + $this->assertEquals( 1, \count( $user_id2_notification ) ); } public function test_upgrade_50() { global $wpdb; $post_id1 = self::factory()->post->create(); - add_post_meta( $post_id1, '_yst_content_links_processed', true ); + \add_post_meta( $post_id1, '_yst_content_links_processed', true ); $post_id2 = self::factory()->post->create(); - add_post_meta( $post_id2, '_yst_content_links_processed', false ); + \add_post_meta( $post_id2, '_yst_content_links_processed', false ); $instance = $this->get_instance(); $instance->upgrade_50(); $number_of_rows = $wpdb->query( - $wpdb->prepare( 'SELECT * + $wpdb->prepare( + 'SELECT * FROM %i WHERE %i = "_yst_content_links_processed"', [ $wpdb->postmeta, 'meta_key' ] @@ -301,7 +313,6 @@ public function test_upgrade_50() { ); $this->assertEquals( 0, $number_of_rows ); - } public function test_upgrade_90() { @@ -312,8 +323,9 @@ public function test_upgrade_90() { \add_option( 'wpseo_sitemap_test_no_autoload', 'test no autoload', '', 'no' ); $instance->upgrade_90(); - $number_of_sitemap_options = $wpdb->query( - $wpdb->prepare( 'SELECT * + $number_of_sitemap_options = $wpdb->query( + $wpdb->prepare( + 'SELECT * FROM %i WHERE %i LIKE %s AND %i = "yes"', @@ -327,10 +339,10 @@ public function test_upgrade_90() { public function test_upgrade_125() { global $wpdb; - $admin_id_1 = self::factory()->user->create( array( 'role' => 'administrator' ) ); - wp_set_current_user( $admin_id_1 ); + $admin_id_1 = self::factory()->user->create( [ 'role' => 'administrator' ] ); + \wp_set_current_user( $admin_id_1 ); - $admin_role = get_role( 'administrator' ); + $admin_role = \get_role( 'administrator' ); $admin_role->add_cap( 'wpseo_manage_options', true ); @@ -346,14 +358,15 @@ public function test_upgrade_125() { $center = Yoast_Notification_Center::get(); $center->add_notification( $notification ); - + \add_user_meta( 1, 'wp_yoast_promo_hide_premium_upsell_admin_block', 'test', true ); - + $instance = $this->get_instance(); $instance->upgrade_125(); $number_of_rows = $wpdb->query( - $wpdb->prepare( 'SELECT * + $wpdb->prepare( + 'SELECT * FROM %i WHERE %i = %s', [ $wpdb->usermeta, 'meta_key', 'wp_yoast_promo_hide_premium_upsell_admin_block' ] @@ -367,9 +380,13 @@ public function test_upgrade_125() { public function test_clean_up_private_taxonomies_for_141() { global $wpdb; - register_taxonomy( 'wpseo_tax', 'post' ); - $term_id = self::factory()->term->create( array( 'taxonomy' => 'wpseo_tax', 'public' => false ) ); - + $indexables_table = Model::get_table_name( 'Indexable' ); + $taxonomy = 'wpseo_tax'; + + \register_taxonomy( $taxonomy, 'post' ); + + $term_id = self::factory()->term->create( [ 'taxonomy' => $taxonomy ] ); + $term_builder = new Indexable_Term_Builder( \YoastSEO()->helpers->taxonomy, \YoastSEO()->classes->get( Indexable_Builder_Versions::class ), @@ -386,21 +403,33 @@ public function test_clean_up_private_taxonomies_for_141() { $indexable->orm = ORM::for_table( 'wp_yoast_indexable' ); $result = $term_builder->build( $term_id, $indexable ); - + + // We need to change the taxonomy visibility after we have created the term indexable, as + // we don't create them for private taxonomies anymore. + \register_taxonomy( $taxonomy, 'post', [ 'public' => false ] ); + $wpdb->query( $wpdb->prepare( - 'UPDATE %i SET %i = false WHERE %i = %d', - [ Model::get_table_name( 'Indexable' ), 'term_taxonomy_id', $result->object_id ] + 'UPDATE %i SET is_public = false WHERE %i = %d', + [ $indexables_table, 'object_id', $result->object_id ] ) ); $instance = $this->get_instance(); $instance->clean_up_private_taxonomies_for_141(); - $term = get_term( $term_id, $taxonomy ); + $private_taxonomy_indexables = $wpdb->get_results( + $wpdb->prepare( + "SELECT id FROM %i + WHERE object_type = 'term' + AND object_sub_type = %s", + [ $indexables_table, $taxonomy ] + ) + ); - $this->assertNull( $term ); + $this->assertEmpty( $private_taxonomy_indexables ); } + /** * Provides a filter return value. * From 078f55dd128c50b6c13bf3704dee54d3611bf107 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Tue, 30 Jan 2024 17:08:43 +0100 Subject: [PATCH 06/18] Added test for upgrade_74 --- inc/class-upgrade.php | 2 +- tests/WP/Doubles/Inc/Upgrade_Double.php | 4 ++++ tests/WP/Inc/Upgrade_Test.php | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/inc/class-upgrade.php b/inc/class-upgrade.php index aad671e25cc..5303bb035bc 100644 --- a/inc/class-upgrade.php +++ b/inc/class-upgrade.php @@ -560,7 +560,7 @@ private function upgrade_73() { * * @return void */ - private function upgrade_74() { + protected function upgrade_74() { $this->remove_sitemap_validators(); } diff --git a/tests/WP/Doubles/Inc/Upgrade_Double.php b/tests/WP/Doubles/Inc/Upgrade_Double.php index 2e8f58f1c89..82a1687d407 100644 --- a/tests/WP/Doubles/Inc/Upgrade_Double.php +++ b/tests/WP/Doubles/Inc/Upgrade_Double.php @@ -74,6 +74,10 @@ public function upgrade_50() { parent::upgrade_50(); } + public function upgrade_74() { + parent::upgrade_74(); + } + public function upgrade_90() { parent::upgrade_90(); } diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index 1c9de11f32e..606f039aa7c 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -430,6 +430,26 @@ public function test_clean_up_private_taxonomies_for_141() { $this->assertEmpty( $private_taxonomy_indexables ); } + public function test_upgrade_74() { + global $wpdb; + + \add_option( 'wpseo_sitemap_video_cache_validator', 'test no autoload', '', 'no' ); + + $instance = $this->get_instance(); + $instance->upgrade_74(); + + $number_of_sitemap_validators = $wpdb->query( + $wpdb->prepare( + "SELECT * + FROM %i + WHERE option_name LIKE %s", + [ $wpdb->options, 'wpseo_sitemap%validator%' ] + ) + ); + + $this->assertEquals( 0, $number_of_sitemap_validators ); + } + /** * Provides a filter return value. * From 248089544265c98f4af100715534a4d9c83f6117 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Wed, 31 Jan 2024 17:04:20 +0100 Subject: [PATCH 07/18] Moar tests! --- tests/WP/Doubles/Inc/Upgrade_Double.php | 8 + tests/WP/Inc/Upgrade_Test.php | 219 ++++++++++++++++++++++++ 2 files changed, 227 insertions(+) diff --git a/tests/WP/Doubles/Inc/Upgrade_Double.php b/tests/WP/Doubles/Inc/Upgrade_Double.php index 82a1687d407..1c27515ff54 100644 --- a/tests/WP/Doubles/Inc/Upgrade_Double.php +++ b/tests/WP/Doubles/Inc/Upgrade_Double.php @@ -85,4 +85,12 @@ public function upgrade_90() { public function clean_up_private_taxonomies_for_141() { parent::clean_up_private_taxonomies_for_141(); } + + public function remove_indexable_rows_for_non_public_post_types() { + parent::remove_indexable_rows_for_non_public_post_types(); + } + + public function remove_indexable_rows_for_non_public_taxonomies() { + parent::remove_indexable_rows_for_non_public_taxonomies(); + } } diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index 606f039aa7c..b8095bb245e 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -6,6 +6,7 @@ use WPSEO_Options; use Yoast\WP\Lib\Model; use Yoast\WP\Lib\ORM; +use Yoast\WP\SEO\Builders\Indexable_Post_Builder; use Yoast\WP\SEO\Builders\Indexable_Term_Builder; use Yoast\WP\SEO\Models\Indexable; use Yoast\WP\SEO\Tests\WP\Doubles\Inc\Upgrade_Double; @@ -450,6 +451,224 @@ public function test_upgrade_74() { $this->assertEquals( 0, $number_of_sitemap_validators ); } + public function test_remove_indexable_rows_for_non_public_post_types() { + global $wpdb; + $indexables_table = Model::get_table_name( 'Indexable' ); + + $post_id = self::factory()->post->create(); + + $post_builder = new Indexable_Post_Builder( + \YoastSEO()->helpers->post, + \YoastSEO()->helpers->post_type, + \YoastSEO()->classes->get( Indexable_Builder_Versions::class ), + \YoastSEO()->helpers->meta + ); + + $post_builder->set_social_image_helpers( + \YoastSEO()->helpers->image, + \YoastSEO()->helpers->open_graph->image, + \YoastSEO()->helpers->twitter->image + ); + + $indexable = new Indexable(); + $indexable->orm = ORM::for_table( 'wp_yoast_indexable' ); + + $post_builder->build( $post_id, $indexable ); + + // We simulate an attachmentindexable (which is a non-indexable post type) by changing the object_sub_type to attachment. + $wpdb->query( + $wpdb->prepare( + "UPDATE %i + SET object_sub_type = 'attachment' + WHERE object_id = %s", + [ $indexables_table, $post_id ] + ) + ); + $indexables_for_non_public_posts = $wpdb->get_results( + $wpdb->prepare( + "SELECT * + FROM %i + WHERE object_id = %s", + [$indexables_table, $post_id ] + ) + ); + $instance = $this->get_instance(); + $instance->remove_indexable_rows_for_non_public_post_types(); + + $indexables_for_non_public_posts = $wpdb->get_results( + $wpdb->prepare( + "SELECT * + FROM %i + WHERE object_id = %s", + [$indexables_table, $post_id ] + ) + ); + + $this->assertEmpty( $indexables_for_non_public_posts ); + } + + public function test_remove_indexable_rows_for_non_public_post_types_when_no_public_post_types() { + global $wpdb; + + $post_id = self::factory()->post->create(); + + $post_builder = new Indexable_Post_Builder( + \YoastSEO()->helpers->post, + \YoastSEO()->helpers->post_type, + \YoastSEO()->classes->get( Indexable_Builder_Versions::class ), + \YoastSEO()->helpers->meta + ); + + $post_builder->set_social_image_helpers( + \YoastSEO()->helpers->image, + \YoastSEO()->helpers->open_graph->image, + \YoastSEO()->helpers->twitter->image + ); + + $indexable = new Indexable(); + $indexable->orm = ORM::for_table( 'wp_yoast_indexable' ); + + $post_builder->build( $post_id, $indexable ); + + \add_filter( 'wpseo_indexable_forced_included_post_types', [ $this, 'override_public_post_types' ]); + + $instance = $this->get_instance(); + $instance->remove_indexable_rows_for_non_public_post_types(); + + $indexables_for_non_public_posts = $wpdb->get_results( + $wpdb->prepare( + "SELECT * + FROM %i + WHERE object_id = %s", + [ Model::get_table_name( 'Indexable' ), $post_id ] + ) + ); + + $this->assertEmpty( $indexables_for_non_public_posts ); + } + + public function test_remove_indexable_rows_for_non_public_taxonomies() { + global $wpdb; + $indexables_table = Model::get_table_name( 'Indexable' ); + + $taxonomy = 'wpseo_tax'; + + \register_taxonomy( $taxonomy, 'post' ); + + $term_id = self::factory()->term->create( [ 'taxonomy' => $taxonomy ] ); + + $term_builder = new Indexable_Term_Builder( + \YoastSEO()->helpers->taxonomy, + \YoastSEO()->classes->get( Indexable_Builder_Versions::class ), + \YoastSEO()->helpers->post + ); + + $term_builder->set_social_image_helpers( + \YoastSEO()->helpers->image, + \YoastSEO()->helpers->open_graph->image, + \YoastSEO()->helpers->twitter->image + ); + + $indexable = new Indexable(); + $indexable->orm = ORM::for_table( 'wp_yoast_indexable' ); + + $term_builder->build( $term_id, $indexable ); + + // We need to change the taxonomy visibility after we have created the term indexable, as + // we don't create them for private taxonomies anymore. + \register_taxonomy( $taxonomy, 'post', [ 'public' => false ] ); + + $wpdb->query( + $wpdb->prepare( + "UPDATE %i + SET is_public = false + WHERE object_type = 'term' + AND object_sub_type = %s", + [ $indexables_table, $taxonomy ] + ) + ); + + $instance = $this->get_instance(); + $instance->remove_indexable_rows_for_non_public_taxonomies(); + + $private_taxonomy_indexables = $wpdb->get_results( + $wpdb->prepare( + "SELECT id FROM %i + WHERE object_type = 'term' + AND object_sub_type = %s", + [ $indexables_table, $taxonomy ] + ) + ); + + $this->assertEmpty( $private_taxonomy_indexables ); + } + + public function test_remove_indexable_rows_for_non_public_taxonomies_when_no_public_taxonomies() { + global $wpdb; + $indexables_table = Model::get_table_name( 'Indexable' ); + + $taxonomy = 'wpseo_tax'; + + \register_taxonomy( $taxonomy, 'post' ); + + $term_id = self::factory()->term->create( [ 'taxonomy' => $taxonomy ] ); + + $term_builder = new Indexable_Term_Builder( + \YoastSEO()->helpers->taxonomy, + \YoastSEO()->classes->get( Indexable_Builder_Versions::class ), + \YoastSEO()->helpers->post + ); + + $term_builder->set_social_image_helpers( + \YoastSEO()->helpers->image, + \YoastSEO()->helpers->open_graph->image, + \YoastSEO()->helpers->twitter->image + ); + + $indexable = new Indexable(); + $indexable->orm = ORM::for_table( 'wp_yoast_indexable' ); + + $term_builder->build( $term_id, $indexable ); + + \add_filter( 'wpseo_indexable_excluded_taxonomies', [ $this, 'override_excluded_taxonomies' ] ); + + // We need to change the taxonomy visibility after we have created the term indexable, as + // we don't create them for private taxonomies anymore. + \register_taxonomy( $taxonomy, 'post', [ 'public' => false ] ); + + $wpdb->query( + $wpdb->prepare( + "UPDATE %i + SET is_public = false + WHERE object_type = 'term' + AND object_sub_type = %s", + [ $indexables_table, $taxonomy ] + ) + ); + + $instance = $this->get_instance(); + $instance->remove_indexable_rows_for_non_public_taxonomies(); + + $private_taxonomy_indexables = $wpdb->get_results( + $wpdb->prepare( + "SELECT id FROM %i + WHERE object_type = 'term' + AND object_sub_type = %s", + [ $indexables_table, $taxonomy ] + ) + ); + + $this->assertEmpty( $private_taxonomy_indexables ); + } + + public function override_public_post_types( $public_post_types ) { + return []; + } + + public function override_excluded_taxonomies( $excluded_taxonomies ) { + return \array_merge( $excluded_taxonomies, [ 'wpseo_tax', 'category', 'post_tag', 'post_format' ] ); + } + /** * Provides a filter return value. * From aa177f83ab569ff4d0190cffba8f97408a61366b Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Wed, 31 Jan 2024 17:05:08 +0100 Subject: [PATCH 08/18] Fix (some) cs --- tests/WP/Inc/Upgrade_Test.php | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index b8095bb245e..06791357f17 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -441,9 +441,9 @@ public function test_upgrade_74() { $number_of_sitemap_validators = $wpdb->query( $wpdb->prepare( - "SELECT * + 'SELECT * FROM %i - WHERE option_name LIKE %s", + WHERE option_name LIKE %s', [ $wpdb->options, 'wpseo_sitemap%validator%' ] ) ); @@ -484,23 +484,23 @@ public function test_remove_indexable_rows_for_non_public_post_types() { [ $indexables_table, $post_id ] ) ); - $indexables_for_non_public_posts = $wpdb->get_results( + $indexables_for_non_public_posts = $wpdb->get_results( $wpdb->prepare( - "SELECT * + 'SELECT * FROM %i - WHERE object_id = %s", - [$indexables_table, $post_id ] + WHERE object_id = %s', + [ $indexables_table, $post_id ] ) ); - $instance = $this->get_instance(); + $instance = $this->get_instance(); $instance->remove_indexable_rows_for_non_public_post_types(); - $indexables_for_non_public_posts = $wpdb->get_results( + $indexables_for_non_public_posts = $wpdb->get_results( $wpdb->prepare( - "SELECT * + 'SELECT * FROM %i - WHERE object_id = %s", - [$indexables_table, $post_id ] + WHERE object_id = %s', + [ $indexables_table, $post_id ] ) ); @@ -509,37 +509,37 @@ public function test_remove_indexable_rows_for_non_public_post_types() { public function test_remove_indexable_rows_for_non_public_post_types_when_no_public_post_types() { global $wpdb; - + $post_id = self::factory()->post->create(); - + $post_builder = new Indexable_Post_Builder( \YoastSEO()->helpers->post, \YoastSEO()->helpers->post_type, \YoastSEO()->classes->get( Indexable_Builder_Versions::class ), \YoastSEO()->helpers->meta ); - + $post_builder->set_social_image_helpers( \YoastSEO()->helpers->image, \YoastSEO()->helpers->open_graph->image, \YoastSEO()->helpers->twitter->image ); - + $indexable = new Indexable(); $indexable->orm = ORM::for_table( 'wp_yoast_indexable' ); - + $post_builder->build( $post_id, $indexable ); - - \add_filter( 'wpseo_indexable_forced_included_post_types', [ $this, 'override_public_post_types' ]); + + \add_filter( 'wpseo_indexable_forced_included_post_types', [ $this, 'override_public_post_types' ] ); $instance = $this->get_instance(); $instance->remove_indexable_rows_for_non_public_post_types(); - $indexables_for_non_public_posts = $wpdb->get_results( + $indexables_for_non_public_posts = $wpdb->get_results( $wpdb->prepare( - "SELECT * + 'SELECT * FROM %i - WHERE object_id = %s", + WHERE object_id = %s', [ Model::get_table_name( 'Indexable' ), $post_id ] ) ); From 9f64f31178a9c52494e5d724dc997267d9ece2e6 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 2 Feb 2024 11:37:05 +0100 Subject: [PATCH 09/18] More tests and fix cs --- inc/class-upgrade.php | 2 +- tests/WP/Doubles/Inc/Upgrade_Double.php | 4 + tests/WP/Inc/Upgrade_Test.php | 158 ++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) diff --git a/inc/class-upgrade.php b/inc/class-upgrade.php index 5303bb035bc..cfde29ea9bb 100644 --- a/inc/class-upgrade.php +++ b/inc/class-upgrade.php @@ -1588,7 +1588,7 @@ public function remove_indexable_rows_for_non_public_taxonomies() { * * @return void */ - private function deduplicate_unindexed_indexable_rows() { + protected function deduplicate_unindexed_indexable_rows() { global $wpdb; // If migrations haven't been completed successfully the following may give false errors. So suppress them. diff --git a/tests/WP/Doubles/Inc/Upgrade_Double.php b/tests/WP/Doubles/Inc/Upgrade_Double.php index 1c27515ff54..f0cf0b4bf89 100644 --- a/tests/WP/Doubles/Inc/Upgrade_Double.php +++ b/tests/WP/Doubles/Inc/Upgrade_Double.php @@ -93,4 +93,8 @@ public function remove_indexable_rows_for_non_public_post_types() { public function remove_indexable_rows_for_non_public_taxonomies() { parent::remove_indexable_rows_for_non_public_taxonomies(); } + + public function deduplicate_unindexed_indexable_rows() { + parent::deduplicate_unindexed_indexable_rows(); + } } diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index 06791357f17..0e244945218 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -207,6 +207,13 @@ public function test_finish_up() { $this->assertNotEmpty( \get_option( 'wpseo_social' ) ); } + /** + * Tests the upgrade_36 upgrade routine. + * + * @covers WPSEO_Upgrade::upgrade_36 + * + * @return void + */ public function test_upgrade_36() { global $wpdb; @@ -239,6 +246,13 @@ public function test_upgrade_36() { $this->assertEquals( 1, $number_of_sitemap_options_no_autoload ); } + /** + * Tests the upgrade_49 upgrade routine. + * + * @covers WPSEO_Upgrade::upgrade_49 + * + * @return void + */ public function test_upgrade_49() { \wp_create_user( 'test_user', 'test password', 'test@test.org' ); @@ -292,6 +306,13 @@ public function test_upgrade_49() { $this->assertEquals( 1, \count( $user_id2_notification ) ); } + /** + * Tests the upgrade_50 upgrade routine. + * + * @covers WPSEO_Upgrade::upgrade_50 + * + * @return void + */ public function test_upgrade_50() { global $wpdb; @@ -316,6 +337,13 @@ public function test_upgrade_50() { $this->assertEquals( 0, $number_of_rows ); } + /** + * Tests the upgrade_90 upgrade routine. + * + * @covers WPSEO_Upgrade::upgrade_90 + * + * @return void + */ public function test_upgrade_90() { global $wpdb; $instance = $this->get_instance(); @@ -337,6 +365,13 @@ public function test_upgrade_90() { $this->assertEquals( 0, $number_of_sitemap_options ); } + /** + * Tests the upgrade_125 upgrade routine. + * + * @covers WPSEO_Upgrade::upgrade_125 + * + * @return void + */ public function test_upgrade_125() { global $wpdb; @@ -378,6 +413,13 @@ public function test_upgrade_125() { $this->assertNull( $center->get_notification_by_id( 'wpseo-dismiss-wordpress-upgrade' ) ); } + /** + * Tests the clean_up_private_taxonomies_for_141 method. + * + * @covers WPSEO_Upgrade::clean_up_private_taxonomies_for_141 + * + * @return void + */ public function test_clean_up_private_taxonomies_for_141() { global $wpdb; @@ -431,6 +473,13 @@ public function test_clean_up_private_taxonomies_for_141() { $this->assertEmpty( $private_taxonomy_indexables ); } + /** + * Tests the upgrade_74 routine. + * + * @covers WPSEO_Upgrade::upgrade_74 + * + * @return void + */ public function test_upgrade_74() { global $wpdb; @@ -451,6 +500,13 @@ public function test_upgrade_74() { $this->assertEquals( 0, $number_of_sitemap_validators ); } + /** + * Tests the remove_indexable_rows_for_non_public_post_types method. + * + * @covers WPSEO_Upgrade::remove_indexable_rows_for_non_public_post_types + * + * @return void + */ public function test_remove_indexable_rows_for_non_public_post_types() { global $wpdb; $indexables_table = Model::get_table_name( 'Indexable' ); @@ -507,6 +563,13 @@ public function test_remove_indexable_rows_for_non_public_post_types() { $this->assertEmpty( $indexables_for_non_public_posts ); } + /** + * Tests the remove_indexable_rows_for_non_public_post_types method in case no public post types are present. + * + * @covers WPSEO_Upgrade::remove_indexable_rows_for_non_public_post_types + * + * @return void + */ public function test_remove_indexable_rows_for_non_public_post_types_when_no_public_post_types() { global $wpdb; @@ -547,6 +610,13 @@ public function test_remove_indexable_rows_for_non_public_post_types_when_no_pub $this->assertEmpty( $indexables_for_non_public_posts ); } + /** + * Tests the remove_indexable_rows_for_non_public_taxonomies method. + * + * @covers WPSEO_Upgrade::remove_indexable_rows_for_non_public_post_taxonomies + * + * @return void + */ public function test_remove_indexable_rows_for_non_public_taxonomies() { global $wpdb; $indexables_table = Model::get_table_name( 'Indexable' ); @@ -603,6 +673,13 @@ public function test_remove_indexable_rows_for_non_public_taxonomies() { $this->assertEmpty( $private_taxonomy_indexables ); } + /** + * Tests the remove_indexable_rows_for_non_public_taxonomies method in case no public taxonomies are present. + * + * @covers WPSEO_Upgrade::remove_indexable_rows_for_non_public_post_taxonomies + * + * @return void + */ public function test_remove_indexable_rows_for_non_public_taxonomies_when_no_public_taxonomies() { global $wpdb; $indexables_table = Model::get_table_name( 'Indexable' ); @@ -661,10 +738,91 @@ public function test_remove_indexable_rows_for_non_public_taxonomies_when_no_pub $this->assertEmpty( $private_taxonomy_indexables ); } + /** + * Tests the deduplicate_unindexed_indexable_rows method. + * + * @covers WPSEO_Upgrade::deduplicate_unindexed_indexable_rows + * + * @return void + */ + public function test_deduplicate_unindexed_indexable_rows() { + global $wpdb; + + $indexables_table = Model::get_table_name( 'Indexable' ); + + $wpdb->query( + $wpdb->prepare( + "INSERT INTO %i (object_id, object_type, post_status) + VALUES + (1, 'post', 'unindexed'), + (1, 'post', 'unindexed'), + (1, 'post', 'unindexed'), + (1, 'term', 'unindexed'), + (1, 'term', 'unindexed'), + (1, 'term', 'unindexed'), + (1, 'user', 'unindexed'), + (1, 'user', 'unindexed'), + (1, 'user', 'unindexed')", + [ $indexables_table ] + ) + ); + + $instance = $this->get_instance(); + $instance->deduplicate_unindexed_indexable_rows(); + + $posts = $wpdb->query( + $wpdb->prepare( + "SELECT * + FROM %i + WHERE object_id = 1 + AND object_type = 'post' + AND post_status = 'unindexed'", + [ $indexables_table ] + ) + ); + $terms = $wpdb->query( + $wpdb->prepare( + "SELECT * + FROM %i + WHERE object_id = 1 + AND object_type = 'user' + AND post_status = 'unindexed'", + [ $indexables_table ] + ) + ); + $users = $wpdb->query( + $wpdb->prepare( + "SELECT * + FROM %i + WHERE object_id = 1 + AND object_type = 'user' + AND post_status = 'unindexed'", + [ $indexables_table ] + ) + ); + $this->assertEquals( 1, $posts ); + $this->assertEquals( 1, $terms ); + $this->assertEquals( 1, $users ); + } + + /** + * Filter used to override the public post types. + * + * @param array $public_post_types The public post types. + * + * @return array An empty array to override the public post types. + */ public function override_public_post_types( $public_post_types ) { return []; } + /** + * Filter used to override the excluded taxonomies. + * + * @param array $excluded_taxonomies The excluded taxonomies. + * + * @return array The excluded taxonomies with all the taxonomies. + */ public function override_excluded_taxonomies( $excluded_taxonomies ) { return \array_merge( $excluded_taxonomies, [ 'wpseo_tax', 'category', 'post_tag', 'post_format' ] ); } From 0e0e3a3915d87c42e9025dff0f309eb5f5c547bc Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 2 Feb 2024 14:46:48 +0100 Subject: [PATCH 10/18] Added final tests --- inc/class-upgrade.php | 6 +- tests/WP/Doubles/Inc/Upgrade_Double.php | 12 ++ tests/WP/Inc/Upgrade_Test.php | 217 +++++++++++++++++++++--- 3 files changed, 212 insertions(+), 23 deletions(-) diff --git a/inc/class-upgrade.php b/inc/class-upgrade.php index cfde29ea9bb..48072ddf49f 100644 --- a/inc/class-upgrade.php +++ b/inc/class-upgrade.php @@ -1650,7 +1650,7 @@ protected function deduplicate_unindexed_indexable_rows() { * * @return void */ - private function clean_unindexed_indexable_rows_with_no_object_id() { + protected function clean_unindexed_indexable_rows_with_no_object_id() { global $wpdb; // If migrations haven't been completed successfully the following may give false errors. So suppress them. @@ -1678,7 +1678,7 @@ private function clean_unindexed_indexable_rows_with_no_object_id() { * * @return void */ - private function remove_indexable_rows_for_disabled_authors_archive() { + protected function remove_indexable_rows_for_disabled_authors_archive() { global $wpdb; if ( ! YoastSEO()->helpers->author_archive->are_disabled() ) { @@ -1712,7 +1712,7 @@ private function remove_indexable_rows_for_disabled_authors_archive() { * * @return string The query that removes all but one duplicate for each object of the object type. */ - private function get_indexable_deduplication_query_for_type( $object_type, $duplicates, $wpdb ) { + protected function get_indexable_deduplication_query_for_type( $object_type, $duplicates, $wpdb ) { $indexable_table = Model::get_table_name( 'Indexable' ); $filtered_duplicates = array_filter( diff --git a/tests/WP/Doubles/Inc/Upgrade_Double.php b/tests/WP/Doubles/Inc/Upgrade_Double.php index f0cf0b4bf89..50a01405829 100644 --- a/tests/WP/Doubles/Inc/Upgrade_Double.php +++ b/tests/WP/Doubles/Inc/Upgrade_Double.php @@ -97,4 +97,16 @@ public function remove_indexable_rows_for_non_public_taxonomies() { public function deduplicate_unindexed_indexable_rows() { parent::deduplicate_unindexed_indexable_rows(); } + + public function clean_unindexed_indexable_rows_with_no_object_id() { + parent::clean_unindexed_indexable_rows_with_no_object_id(); + } + + public function remove_indexable_rows_for_disabled_authors_archive() { + parent::remove_indexable_rows_for_disabled_authors_archive(); + } + + public function get_indexable_deduplication_query_for_type( $object_type, $duplicates, $wpdb ) { + return parent::get_indexable_deduplication_query_for_type( $object_type, $duplicates, $wpdb ); + } } diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index 0e244945218..753bc53f1b3 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -8,6 +8,7 @@ use Yoast\WP\Lib\ORM; use Yoast\WP\SEO\Builders\Indexable_Post_Builder; use Yoast\WP\SEO\Builders\Indexable_Term_Builder; +use Yoast\WP\SEO\Builders\Indexable_Author_Builder; use Yoast\WP\SEO\Models\Indexable; use Yoast\WP\SEO\Tests\WP\Doubles\Inc\Upgrade_Double; use Yoast\WP\SEO\Tests\WP\TestCase; @@ -22,6 +23,18 @@ */ final class Upgrade_Test extends TestCase { + /** + * The indexables table name. + * + * @var string + */ + private $indexables_table; + + public function set_up() { + parent::set_up(); + + $this->indexables_table = Model::get_table_name( 'Indexable' ); + } /** * Retrieves the instance to test against. * @@ -423,7 +436,6 @@ public function test_upgrade_125() { public function test_clean_up_private_taxonomies_for_141() { global $wpdb; - $indexables_table = Model::get_table_name( 'Indexable' ); $taxonomy = 'wpseo_tax'; \register_taxonomy( $taxonomy, 'post' ); @@ -454,7 +466,7 @@ public function test_clean_up_private_taxonomies_for_141() { $wpdb->query( $wpdb->prepare( 'UPDATE %i SET is_public = false WHERE %i = %d', - [ $indexables_table, 'object_id', $result->object_id ] + [ $this->indexables_table, 'object_id', $result->object_id ] ) ); @@ -466,7 +478,7 @@ public function test_clean_up_private_taxonomies_for_141() { "SELECT id FROM %i WHERE object_type = 'term' AND object_sub_type = %s", - [ $indexables_table, $taxonomy ] + [ $this->indexables_table, $taxonomy ] ) ); @@ -509,7 +521,6 @@ public function test_upgrade_74() { */ public function test_remove_indexable_rows_for_non_public_post_types() { global $wpdb; - $indexables_table = Model::get_table_name( 'Indexable' ); $post_id = self::factory()->post->create(); @@ -537,7 +548,7 @@ public function test_remove_indexable_rows_for_non_public_post_types() { "UPDATE %i SET object_sub_type = 'attachment' WHERE object_id = %s", - [ $indexables_table, $post_id ] + [ $this->indexables_table, $post_id ] ) ); $indexables_for_non_public_posts = $wpdb->get_results( @@ -545,7 +556,7 @@ public function test_remove_indexable_rows_for_non_public_post_types() { 'SELECT * FROM %i WHERE object_id = %s', - [ $indexables_table, $post_id ] + [ $this->indexables_table, $post_id ] ) ); $instance = $this->get_instance(); @@ -556,7 +567,7 @@ public function test_remove_indexable_rows_for_non_public_post_types() { 'SELECT * FROM %i WHERE object_id = %s', - [ $indexables_table, $post_id ] + [ $this->indexables_table, $post_id ] ) ); @@ -603,7 +614,7 @@ public function test_remove_indexable_rows_for_non_public_post_types_when_no_pub 'SELECT * FROM %i WHERE object_id = %s', - [ Model::get_table_name( 'Indexable' ), $post_id ] + [$this->indexables_table, $post_id ] ) ); @@ -619,7 +630,6 @@ public function test_remove_indexable_rows_for_non_public_post_types_when_no_pub */ public function test_remove_indexable_rows_for_non_public_taxonomies() { global $wpdb; - $indexables_table = Model::get_table_name( 'Indexable' ); $taxonomy = 'wpseo_tax'; @@ -654,7 +664,7 @@ public function test_remove_indexable_rows_for_non_public_taxonomies() { SET is_public = false WHERE object_type = 'term' AND object_sub_type = %s", - [ $indexables_table, $taxonomy ] + [ $this->indexables_table, $taxonomy ] ) ); @@ -666,7 +676,7 @@ public function test_remove_indexable_rows_for_non_public_taxonomies() { "SELECT id FROM %i WHERE object_type = 'term' AND object_sub_type = %s", - [ $indexables_table, $taxonomy ] + [ $this->indexables_table, $taxonomy ] ) ); @@ -682,7 +692,6 @@ public function test_remove_indexable_rows_for_non_public_taxonomies() { */ public function test_remove_indexable_rows_for_non_public_taxonomies_when_no_public_taxonomies() { global $wpdb; - $indexables_table = Model::get_table_name( 'Indexable' ); $taxonomy = 'wpseo_tax'; @@ -719,7 +728,7 @@ public function test_remove_indexable_rows_for_non_public_taxonomies_when_no_pub SET is_public = false WHERE object_type = 'term' AND object_sub_type = %s", - [ $indexables_table, $taxonomy ] + [ $this->indexables_table, $taxonomy ] ) ); @@ -731,7 +740,7 @@ public function test_remove_indexable_rows_for_non_public_taxonomies_when_no_pub "SELECT id FROM %i WHERE object_type = 'term' AND object_sub_type = %s", - [ $indexables_table, $taxonomy ] + [ $this->indexables_table, $taxonomy ] ) ); @@ -748,8 +757,6 @@ public function test_remove_indexable_rows_for_non_public_taxonomies_when_no_pub public function test_deduplicate_unindexed_indexable_rows() { global $wpdb; - $indexables_table = Model::get_table_name( 'Indexable' ); - $wpdb->query( $wpdb->prepare( "INSERT INTO %i (object_id, object_type, post_status) @@ -763,7 +770,7 @@ public function test_deduplicate_unindexed_indexable_rows() { (1, 'user', 'unindexed'), (1, 'user', 'unindexed'), (1, 'user', 'unindexed')", - [ $indexables_table ] + [ $this->indexables_table ] ) ); @@ -777,7 +784,7 @@ public function test_deduplicate_unindexed_indexable_rows() { WHERE object_id = 1 AND object_type = 'post' AND post_status = 'unindexed'", - [ $indexables_table ] + [ $this->indexables_table ] ) ); $terms = $wpdb->query( @@ -787,7 +794,7 @@ public function test_deduplicate_unindexed_indexable_rows() { WHERE object_id = 1 AND object_type = 'user' AND post_status = 'unindexed'", - [ $indexables_table ] + [ $this->indexables_table ] ) ); $users = $wpdb->query( @@ -797,7 +804,7 @@ public function test_deduplicate_unindexed_indexable_rows() { WHERE object_id = 1 AND object_type = 'user' AND post_status = 'unindexed'", - [ $indexables_table ] + [ $this->indexables_table ] ) ); $this->assertEquals( 1, $posts ); @@ -805,6 +812,176 @@ public function test_deduplicate_unindexed_indexable_rows() { $this->assertEquals( 1, $users ); } + /** + * Tests the clean_unindexed_indexable_rows_with_no_object_id method. + * + * @covers WPSEO_Upgrade::clean_unindexed_indexable_rows_with_no_object_id + * + * @return void + */ + public function test_clean_unindexed_indexable_rows_with_no_object_id() { + global $wpdb; + + $wpdb->query( + $wpdb->prepare( + "INSERT INTO %i (object_id, object_type, post_status) + VALUES + (NULL, 'post', 'unindexed'), + (NULL, 'term', 'unindexed'), + (NULL, 'user', 'unindexed'), + (NULL, 'system-page', 'unindexed'), + (1, 'post', 'unindexed'), + (2, 'user', 'draft'), + (3, 'system-page', 'unindexed')", + [ $this->indexables_table ] + ) + ); + + $instance = $this->get_instance(); + $instance->clean_unindexed_indexable_rows_with_no_object_id(); + + $null_ids = $wpdb->query( + $wpdb->prepare( + "SELECT * + FROM %i + WHERE object_id IS NULL", + [ $this->indexables_table ] + ) + ); + + $indexables = $wpdb->query( + $wpdb->prepare( + "SELECT * + FROM %i", + [ $this->indexables_table ] + ) + ); + + $this->assertEquals( 1, $null_ids ); + $this->assertEquals( 4, $indexables ); + } + + /** + * Tests the test_remove_indexable_rows_for_disabled_authors_archive method. + * + * @covers WPSEO_Upgrade::test_remove_indexable_rows_for_disabled_authors_archive + * + * @return void + */ + public function test_remove_indexable_rows_for_disabled_authors_archive() { + global $wpdb; + $options_helper = \YoastSEO()->helpers->options; + $wpdb->query( + $wpdb->prepare( + "INSERT INTO %i (object_id, object_type, post_status) + VALUES + (1, 'user', 'publish'), + (2, 'user', 'publish'), + (3, 'user', 'publish')", + [ $this->indexables_table ] + ) + ); + + $options_helper->set( 'disable-author', true ); + + $instance = $this->get_instance(); + $instance->remove_indexable_rows_for_disabled_authors_archive(); + + $user_indexables = $wpdb->query( + $wpdb->prepare( + "SELECT * + FROM %i + WHERE object_type = 'user'", + [ $this->indexables_table ] + ) + ); + + $this->assertEquals( 0, $user_indexables ); + } + + + public function test_get_indexable_deduplication_query_for_type() { + global $wpdb; + + $wpdb->query( + $wpdb->prepare( + "INSERT INTO %i (object_id, object_type, post_status) + VALUES + (1, 'user', 'unindexed'), + (1, 'user', 'unindexed'), + (1, 'user', 'unindexed'), + (1, 'term', 'unindexed'), + (1, 'term', 'unindexed'), + (1, 'term', 'unindexed'), + (1, 'post', 'unindexed'), + (1, 'post', 'unindexed'), + (1, 'post', 'unindexed'), + (2, 'post', 'unindexed'), + (3, 'post', 'unindexed'), + (3, 'post', 'unindexed'), + (3, 'term', 'publish')", + [ $this->indexables_table ] + ) + ); + + $duplicates = $wpdb->get_results( + $wpdb->prepare( + " + SELECT + MAX(id) as newest_id, + object_id, + object_type + FROM + %i + WHERE + post_status = 'unindexed' + AND object_type IN ( 'term', 'post', 'user' ) + GROUP BY + object_id, + object_type + HAVING + count(*) > 1", + [ $this->indexables_table ] + ) + , ARRAY_A); + + $posts_ids = \array_column( + \array_filter( $duplicates, function ( $duplicate ) { + return $duplicate['object_type'] === 'post'; + } ), + 'newest_id' + ); + $terms_ids = \array_column( + \array_filter( $duplicates, function ( $duplicate ) { + return $duplicate['object_type'] === 'term'; + } ), + 'newest_id' + ); + + $users_ids = \array_column( + \array_filter( $duplicates, function ( $duplicate ) { + return $duplicate['object_type'] === 'user'; + } ), + 'newest_id' + ); + $instance = $this->get_instance(); + $posts_query = $instance->get_indexable_deduplication_query_for_type( 'post', $duplicates, $wpdb ); + $terms_query = $instance->get_indexable_deduplication_query_for_type( 'term', $duplicates, $wpdb ); + $users_query = $instance->get_indexable_deduplication_query_for_type( 'user', $duplicates, $wpdb ); + + $this->assertStringContainsString( 'object_type = \'post\'', $posts_query ); + $this->assertStringContainsString( 'object_id IN ( 1, 3 )', $posts_query ); + $this->assertStringContainsString( 'AND id NOT IN ( ' . \implode( ', ', $posts_ids ) . ' )', $posts_query ); + + $this->assertStringContainsString( 'object_type = \'term\'', $terms_query ); + $this->assertStringContainsString( 'object_id IN ( 1 )', $terms_query ); + $this->assertStringContainsString( 'AND id NOT IN ( ' . \implode( ', ', $terms_ids ) . ' )', $terms_query ); + + $this->assertStringContainsString( 'object_type = \'user\'', $users_query ); + $this->assertStringContainsString( 'object_id IN ( 1 )', $users_query ); + $this->assertStringContainsString( 'AND id NOT IN ( ' . \implode( ', ', $users_ids ) . ' )', $users_query ); + } + /** * Filter used to override the public post types. * From ffa3c865c48e16a4c43985748d75cae011eac93a Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 2 Feb 2024 14:51:46 +0100 Subject: [PATCH 11/18] Fix cs --- tests/WP/Inc/Upgrade_Test.php | 69 +++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index 753bc53f1b3..ad807555900 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -8,7 +8,6 @@ use Yoast\WP\Lib\ORM; use Yoast\WP\SEO\Builders\Indexable_Post_Builder; use Yoast\WP\SEO\Builders\Indexable_Term_Builder; -use Yoast\WP\SEO\Builders\Indexable_Author_Builder; use Yoast\WP\SEO\Models\Indexable; use Yoast\WP\SEO\Tests\WP\Doubles\Inc\Upgrade_Double; use Yoast\WP\SEO\Tests\WP\TestCase; @@ -30,11 +29,17 @@ final class Upgrade_Test extends TestCase { */ private $indexables_table; + /** + * Set up the class which will be tested. + * + * @return void + */ public function set_up() { parent::set_up(); $this->indexables_table = Model::get_table_name( 'Indexable' ); } + /** * Retrieves the instance to test against. * @@ -436,7 +441,7 @@ public function test_upgrade_125() { public function test_clean_up_private_taxonomies_for_141() { global $wpdb; - $taxonomy = 'wpseo_tax'; + $taxonomy = 'wpseo_tax'; \register_taxonomy( $taxonomy, 'post' ); @@ -614,7 +619,7 @@ public function test_remove_indexable_rows_for_non_public_post_types_when_no_pub 'SELECT * FROM %i WHERE object_id = %s', - [$this->indexables_table, $post_id ] + [ $this->indexables_table, $post_id ] ) ); @@ -842,17 +847,17 @@ public function test_clean_unindexed_indexable_rows_with_no_object_id() { $null_ids = $wpdb->query( $wpdb->prepare( - "SELECT * + 'SELECT * FROM %i - WHERE object_id IS NULL", + WHERE object_id IS NULL', [ $this->indexables_table ] ) ); $indexables = $wpdb->query( $wpdb->prepare( - "SELECT * - FROM %i", + 'SELECT * + FROM %i', [ $this->indexables_table ] ) ); @@ -899,7 +904,13 @@ public function test_remove_indexable_rows_for_disabled_authors_archive() { $this->assertEquals( 0, $user_indexables ); } - + /** + * Tests the get_indexable_deduplication_query_for_type method. + * + * @covers WPSEO_Upgrade::get_indexable_deduplication_query_for_type + * + * @return void + */ public function test_get_indexable_deduplication_query_for_type() { global $wpdb; @@ -942,32 +953,42 @@ public function test_get_indexable_deduplication_query_for_type() { HAVING count(*) > 1", [ $this->indexables_table ] - ) - , ARRAY_A); + ), + \ARRAY_A + ); $posts_ids = \array_column( - \array_filter( $duplicates, function ( $duplicate ) { - return $duplicate['object_type'] === 'post'; - } ), + \array_filter( + $duplicates, + static function ( $duplicate ) { + return $duplicate['object_type'] === 'post'; + } + ), 'newest_id' ); $terms_ids = \array_column( - \array_filter( $duplicates, function ( $duplicate ) { - return $duplicate['object_type'] === 'term'; - } ), + \array_filter( + $duplicates, + static function ( $duplicate ) { + return $duplicate['object_type'] === 'term'; + } + ), 'newest_id' ); - $users_ids = \array_column( - \array_filter( $duplicates, function ( $duplicate ) { - return $duplicate['object_type'] === 'user'; - } ), + $users_ids = \array_column( + \array_filter( + $duplicates, + static function ( $duplicate ) { + return $duplicate['object_type'] === 'user'; + } + ), 'newest_id' ); - $instance = $this->get_instance(); - $posts_query = $instance->get_indexable_deduplication_query_for_type( 'post', $duplicates, $wpdb ); - $terms_query = $instance->get_indexable_deduplication_query_for_type( 'term', $duplicates, $wpdb ); - $users_query = $instance->get_indexable_deduplication_query_for_type( 'user', $duplicates, $wpdb ); + $instance = $this->get_instance(); + $posts_query = $instance->get_indexable_deduplication_query_for_type( 'post', $duplicates, $wpdb ); + $terms_query = $instance->get_indexable_deduplication_query_for_type( 'term', $duplicates, $wpdb ); + $users_query = $instance->get_indexable_deduplication_query_for_type( 'user', $duplicates, $wpdb ); $this->assertStringContainsString( 'object_type = \'post\'', $posts_query ); $this->assertStringContainsString( 'object_id IN ( 1, 3 )', $posts_query ); From 003b919e21b13fdf1c97ba0ba108c9b8e950d718 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 2 Feb 2024 15:16:12 +0100 Subject: [PATCH 12/18] Fix cs --- tests/WP/Doubles/Inc/Upgrade_Double.php | 71 +++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/tests/WP/Doubles/Inc/Upgrade_Double.php b/tests/WP/Doubles/Inc/Upgrade_Double.php index 50a01405829..0a99aba88fa 100644 --- a/tests/WP/Doubles/Inc/Upgrade_Double.php +++ b/tests/WP/Doubles/Inc/Upgrade_Double.php @@ -32,7 +32,7 @@ public function cleanup_option_data( $option_name ) { * * @param string $option_name Option to retrieve. * - * @return array|mixed The content of the option if exists, otherwise an empty array. + * @return array The content of the option if exists, otherwise an empty array. */ public function get_option_from_database( $option_name ) { return parent::get_option_from_database( $option_name ); @@ -41,9 +41,9 @@ public function get_option_from_database( $option_name ) { /** * Test double. Saves an option setting to where it should be stored. * - * @param array $source_data The option containing the value to be migrated. - * @param string $source_setting Name of the key in the "from" option. - * @param string|null $target_setting Name of the key in the "to" option. + * @param array $source_data The option containing the value to be migrated. + * @param string $source_setting Name of the key in the "from" option. + * @param string|null $target_setting Name of the key in the "to" option. * * @return void */ @@ -62,50 +62,113 @@ public function finish_up( $previous_version = null ) { parent::finish_up( $previous_version ); } + /** + * Test double. Upgrades the plugin to version 3.6. + * + * @return void + */ public function upgrade_36() { parent::upgrade_36(); } + /** + * Test double. Upgrades the plugin to version 4.9. + * + * @return void + */ public function upgrade_49() { parent::upgrade_49(); } + /** + * Test double. Upgrades the plugin to version 5.0. + * + * @return void + */ public function upgrade_50() { parent::upgrade_50(); } + /** + * Test double. Upgrades the plugin to version 7.4. + * + * @return void + */ public function upgrade_74() { parent::upgrade_74(); } + /** + * Test double. Upgrades the plugin to version 9.0. + * + * @return void + */ public function upgrade_90() { parent::upgrade_90(); } + /** + * Test double. Upgrades the plugin to version 14.1. + * + * @return void + */ public function clean_up_private_taxonomies_for_141() { parent::clean_up_private_taxonomies_for_141(); } + /** + * Test double. Removes all the indexables for non-public post types. + * + * @return void + */ public function remove_indexable_rows_for_non_public_post_types() { parent::remove_indexable_rows_for_non_public_post_types(); } + /** + * Test double. Removes all the indexables for non-public taxonomies. + * + * @return void + */ public function remove_indexable_rows_for_non_public_taxonomies() { parent::remove_indexable_rows_for_non_public_taxonomies(); } + /** + * Test double. Removes all the indexables duplicates. + * + * @return void + */ public function deduplicate_unindexed_indexable_rows() { parent::deduplicate_unindexed_indexable_rows(); } + /** + * Test double. Removes all the indexables referring to unindexed elements with no id. + * + * @return void + */ public function clean_unindexed_indexable_rows_with_no_object_id() { parent::clean_unindexed_indexable_rows_with_no_object_id(); } + /** + * Test double. Removes all the user indexables when the author archive is disabled. + * + * @return void + */ public function remove_indexable_rows_for_disabled_authors_archive() { parent::remove_indexable_rows_for_disabled_authors_archive(); } + /** + * Test double. Gets the indexable deduplication query for a specific object type. + * + * @param string $object_type The object type to get the deduplication query for. + * @param array $duplicates The duplicates to deduplicate. + * @param object $wpdb The WordPress database object. + * @return string The deduplication query. + */ public function get_indexable_deduplication_query_for_type( $object_type, $duplicates, $wpdb ) { return parent::get_indexable_deduplication_query_for_type( $object_type, $duplicates, $wpdb ); } From a9c128a6d5fffe7f1776821f50b8cd0f6b8e4e78 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 2 Feb 2024 15:24:31 +0100 Subject: [PATCH 13/18] Fix cs --- inc/sitemaps/class-sitemaps-cache-validator.php | 3 +-- tests/WP/Inc/Upgrade_Test.php | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/inc/sitemaps/class-sitemaps-cache-validator.php b/inc/sitemaps/class-sitemaps-cache-validator.php index 271e94998f4..2c06fd82c52 100644 --- a/inc/sitemaps/class-sitemaps-cache-validator.php +++ b/inc/sitemaps/class-sitemaps-cache-validator.php @@ -187,8 +187,7 @@ public static function cleanup_database( $type = null, $validator = null ) { $where = []; $where[] = sprintf( "option_name LIKE '%s'", addcslashes( '_transient_' . $like, '_' ) ); $where[] = sprintf( "option_name LIKE '%s'", addcslashes( '_transient_timeout_' . $like, '_' ) ); - - $query = sprintf( 'DELETE FROM %1$s WHERE %2$s', $wpdb->options, implode( ' OR ', $where ) ); + // Delete transients. //phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- We need to use a direct query here. //phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches. diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index ad807555900..51191af77af 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -1006,11 +1006,9 @@ static function ( $duplicate ) { /** * Filter used to override the public post types. * - * @param array $public_post_types The public post types. - * * @return array An empty array to override the public post types. */ - public function override_public_post_types( $public_post_types ) { + public function override_public_post_types() { return []; } From d3037c3eca184b023df6c2181f210996088b7a16 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 2 Feb 2024 15:44:31 +0100 Subject: [PATCH 14/18] Fix test --- tests/WP/Inc/Upgrade_Test.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index 51191af77af..dcbb7153e35 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -847,23 +847,16 @@ public function test_clean_unindexed_indexable_rows_with_no_object_id() { $null_ids = $wpdb->query( $wpdb->prepare( - 'SELECT * + "SELECT * FROM %i - WHERE object_id IS NULL', - [ $this->indexables_table ] - ) - ); - - $indexables = $wpdb->query( - $wpdb->prepare( - 'SELECT * - FROM %i', + WHERE object_id IS NULL + AND object_type IN ( 'post', 'term', 'user' ) + AND post_status = 'unindexed'", [ $this->indexables_table ] ) ); - $this->assertEquals( 1, $null_ids ); - $this->assertEquals( 4, $indexables ); + $this->assertEquals( 0, $null_ids ); } /** From ad67845155c60fa6d73b32dc687619b42a43d6e8 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Fri, 2 Feb 2024 15:51:34 +0100 Subject: [PATCH 15/18] Fix test --- tests/WP/Inc/Upgrade_Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index dcbb7153e35..af28411c225 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -321,7 +321,7 @@ public function test_upgrade_49() { $user_id2_notification = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, 2 ); $this->assertEquals( 2, \count( $user_id1_notifications ) ); - $this->assertEquals( 1, \count( $user_id2_notification ) ); + $this->assertEquals( 1, $user_id2_notification ); } /** From afc15406a9bf6de70a0ae7d9fcd14851031757b0 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Tue, 13 Feb 2024 15:57:21 +0100 Subject: [PATCH 16/18] Fix tests --- tests/WP/Inc/Upgrade_Test.php | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index af28411c225..b75c0db2a72 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -40,6 +40,18 @@ public function set_up() { $this->indexables_table = Model::get_table_name( 'Indexable' ); } + /** + * Tear down the class which was tested. + * + * @return void + */ + public function tear_down() { + parent::tear_down(); + + $notification_center = Yoast_Notification_Center::get(); + $notification_center->deactivate_hook(); + } + /** * Retrieves the instance to test against. * @@ -272,7 +284,18 @@ public function test_upgrade_36() { * @return void */ public function test_upgrade_49() { - \wp_create_user( 'test_user', 'test password', 'test@test.org' ); + $user_id = $this->factory->user->create( + [ + 'user_login' => 'User_Login', + 'display_name' => 'User_Nicename', + ] + ); + $other_user_id = $this->factory->user->create( + [ + 'user_login' => 'Other_User_Login', + 'display_name' => 'Other_User_Nicename', + ] + ); $notifications = [ [ @@ -295,7 +318,7 @@ public function test_upgrade_49() { ], ]; - \update_user_option( 1, Yoast_Notification_Center::STORAGE_KEY, \array_values( $notifications ) ); + \update_user_option( $user_id, Yoast_Notification_Center::STORAGE_KEY, \array_values( $notifications ) ); $notifications = [ [ @@ -311,17 +334,17 @@ public function test_upgrade_49() { ], ]; - \update_user_option( 2, Yoast_Notification_Center::STORAGE_KEY, \array_values( $notifications ) ); + \update_user_option( $other_user_id, Yoast_Notification_Center::STORAGE_KEY, \array_values( $notifications ) ); $instance = $this->get_instance(); $instance->upgrade_49(); - $user_id1_notifications = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, 1 ); - $user_id2_notification = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, 2 ); + $user_id1_notifications = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, $user_id ); + $user_id2_notification = \get_user_option( Yoast_Notification_Center::STORAGE_KEY, $other_user_id ); $this->assertEquals( 2, \count( $user_id1_notifications ) ); - $this->assertEquals( 1, $user_id2_notification ); + $this->assertEquals( 1, \count( $user_id2_notification ) ); } /** From 87a9b9ed8f13a0e55b95448cc01d353e6dd6fc08 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Wed, 14 Feb 2024 10:00:30 +0100 Subject: [PATCH 17/18] Remove unnecessary placeholders --- tests/WP/Inc/Upgrade_Test.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index b75c0db2a72..0aa8918ebd5 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -257,9 +257,9 @@ public function test_upgrade_36() { $wpdb->prepare( "SELECT * FROM %i - WHERE %i LIKE %s - AND %i = 'yes'", - [ $wpdb->options, 'option_name', 'wpseo_sitemap_%', 'autoload' ] + WHERE option_name LIKE %s + AND autoload = 'yes'", + [ $wpdb->options, 'wpseo_sitemap_%' ] ) ); @@ -267,9 +267,9 @@ public function test_upgrade_36() { $wpdb->prepare( "SELECT * FROM %i - WHERE %i LIKE %s - AND %i = 'no'", - [ $wpdb->options, 'option_name', 'wpseo_sitemap_%', 'autoload' ] + WHERE option_name LIKE %s + AND autoload = 'no'", + [ $wpdb->options, 'wpseo_sitemap_%' ] ) ); $this->assertEquals( 0, $number_of_sitemap_options ); @@ -370,8 +370,8 @@ public function test_upgrade_50() { $wpdb->prepare( 'SELECT * FROM %i - WHERE %i = "_yst_content_links_processed"', - [ $wpdb->postmeta, 'meta_key' ] + WHERE meta_key = "_yst_content_links_processed"', + [ $wpdb->postmeta ] ) ); @@ -397,9 +397,9 @@ public function test_upgrade_90() { $wpdb->prepare( 'SELECT * FROM %i - WHERE %i LIKE %s - AND %i = "yes"', - [ $wpdb->options, 'option_name', 'wpseo_sitemap_%', 'autoload' ] + WHERE option_name LIKE %s + AND autoload = "yes"', + [ $wpdb->options, 'wpseo_sitemap_%' ] ) ); @@ -445,8 +445,8 @@ public function test_upgrade_125() { $wpdb->prepare( 'SELECT * FROM %i - WHERE %i = %s', - [ $wpdb->usermeta, 'meta_key', 'wp_yoast_promo_hide_premium_upsell_admin_block' ] + WHERE meta_key = %s', + [ $wpdb->usermeta, 'wp_yoast_promo_hide_premium_upsell_admin_block' ] ) ); @@ -493,8 +493,8 @@ public function test_clean_up_private_taxonomies_for_141() { $wpdb->query( $wpdb->prepare( - 'UPDATE %i SET is_public = false WHERE %i = %d', - [ $this->indexables_table, 'object_id', $result->object_id ] + 'UPDATE %i SET is_public = false WHERE object_id = %d', + [ $this->indexables_table, $result->object_id ] ) ); From 6116fd1a3784e1655cdf22ae267d45e2e073b8a9 Mon Sep 17 00:00:00 2001 From: Thijs van der heijden Date: Fri, 23 Feb 2024 14:52:19 +0100 Subject: [PATCH 18/18] Update tests to add check for placeholder. --- tests/WP/Inc/Upgrade_Test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/WP/Inc/Upgrade_Test.php b/tests/WP/Inc/Upgrade_Test.php index 0aa8918ebd5..c36d01267c9 100644 --- a/tests/WP/Inc/Upgrade_Test.php +++ b/tests/WP/Inc/Upgrade_Test.php @@ -1007,15 +1007,15 @@ static function ( $duplicate ) { $users_query = $instance->get_indexable_deduplication_query_for_type( 'user', $duplicates, $wpdb ); $this->assertStringContainsString( 'object_type = \'post\'', $posts_query ); - $this->assertStringContainsString( 'object_id IN ( 1, 3 )', $posts_query ); + $this->assertStringContainsString( '`object_id` IN ( 1, 3 )', $posts_query ); $this->assertStringContainsString( 'AND id NOT IN ( ' . \implode( ', ', $posts_ids ) . ' )', $posts_query ); $this->assertStringContainsString( 'object_type = \'term\'', $terms_query ); - $this->assertStringContainsString( 'object_id IN ( 1 )', $terms_query ); + $this->assertStringContainsString( '`object_id` IN ( 1 )', $terms_query ); $this->assertStringContainsString( 'AND id NOT IN ( ' . \implode( ', ', $terms_ids ) . ' )', $terms_query ); $this->assertStringContainsString( 'object_type = \'user\'', $users_query ); - $this->assertStringContainsString( 'object_id IN ( 1 )', $users_query ); + $this->assertStringContainsString( '`object_id` IN ( 1 )', $users_query ); $this->assertStringContainsString( 'AND id NOT IN ( ' . \implode( ', ', $users_ids ) . ' )', $users_query ); }