From 1ad2c2c3ffe955c4deeebde27208d3454ada51f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 4 Jun 2024 09:27:07 -0400 Subject: [PATCH 1/7] add guard clauses in case the classes are not found --- inc/ThirdParty/Plugins/SEO/RankMathSEO.php | 36 +++++++++------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/inc/ThirdParty/Plugins/SEO/RankMathSEO.php b/inc/ThirdParty/Plugins/SEO/RankMathSEO.php index f9e9e40bc0..5abe406870 100644 --- a/inc/ThirdParty/Plugins/SEO/RankMathSEO.php +++ b/inc/ThirdParty/Plugins/SEO/RankMathSEO.php @@ -1,42 +1,30 @@ option = $option; - } + public static function get_subscribed_events(): array { + if ( ! class_exists( 'RankMath\Helper' ) ) { + return []; + } - /** - * Subscribed events. - */ - public static function get_subscribed_events() { // @phpstan-ignore-next-line if ( ! defined( 'RANK_MATH_FILE' ) || ! Helper::is_module_active( 'sitemap' ) ) { return []; } return [ - 'rocket_sitemap_preload_list' => [ 'rocket_sitemap', 15 ], + 'rocket_sitemap_preload_list' => [ 'add_sitemap', 15 ], ]; } @@ -44,9 +32,13 @@ public static function get_subscribed_events() { * Add SEO sitemap URL to the sitemaps to preload * * @param array $sitemaps Sitemaps to preload. - * @return array Updated Sitemaps to preload + * + * @return array */ - public function rocket_sitemap( $sitemaps ) { + public function add_sitemap( $sitemaps ) { + if ( ! class_exists( 'RankMath\Sitemap\Router' ) ) { + return $sitemaps; + } // @phpstan-ignore-next-line $sitemaps[] = Router::get_base_url( 'sitemap_index.xml' ); From 857f52ec225aadd3f0d76f5e68f62d3c93520763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 4 Jun 2024 09:27:15 -0400 Subject: [PATCH 2/7] update rankmathseo instantiation --- inc/ThirdParty/ServiceProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/ThirdParty/ServiceProvider.php b/inc/ThirdParty/ServiceProvider.php index 80e710b4a1..50fbc95569 100644 --- a/inc/ThirdParty/ServiceProvider.php +++ b/inc/ThirdParty/ServiceProvider.php @@ -238,7 +238,6 @@ public function register(): void { ->addTag( 'common_subscriber' ); $this->getContainer() ->addShared( 'rank_math_seo', RankMathSEO::class ) - ->addArgument( $options ) ->addTag( 'common_subscriber' ); $this->getContainer() ->addShared( 'all_in_one_seo_pack', AllInOneSEOPack::class ) From b5356649d3d1833b262e26c9092fcadb3145b070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 4 Jun 2024 09:27:27 -0400 Subject: [PATCH 3/7] rename fixture file --- .../SEO/RankMathSEO/{rocketSitemap.php => addSitemap.php} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename tests/Fixtures/inc/ThirdParty/Plugins/SEO/RankMathSEO/{rocketSitemap.php => addSitemap.php} (62%) diff --git a/tests/Fixtures/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php b/tests/Fixtures/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php similarity index 62% rename from tests/Fixtures/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php rename to tests/Fixtures/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php index 7d0cada3a8..2587a1e28c 100644 --- a/tests/Fixtures/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php +++ b/tests/Fixtures/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php @@ -2,11 +2,11 @@ return [ 'shouldAddSitemap' => [ 'config' => [ - 'sitemap' => 'sitemap', - 'sitemaps' => [] + 'sitemap' => 'sitemap', + 'sitemaps' => [], ], 'expected' => [ 'sitemap', - ] - ] + ], + ], ]; From 0ece90254998537f1814dd241dfb786c10b26e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 4 Jun 2024 09:27:35 -0400 Subject: [PATCH 4/7] update test config --- .../Plugins/SEO/RankMathSEO/addSitemap.php | 39 +++++++++++++++++++ .../Plugins/SEO/RankMathSEO/rocketSitemap.php | 39 ------------------- 2 files changed, 39 insertions(+), 39 deletions(-) create mode 100644 tests/Integration/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php delete mode 100644 tests/Integration/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php diff --git a/tests/Integration/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php b/tests/Integration/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php new file mode 100644 index 0000000000..6dc6ca2df4 --- /dev/null +++ b/tests/Integration/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php @@ -0,0 +1,39 @@ +unregisterAllCallbacksExcept( 'rocket_sitemap_preload_list', 'add_sitemap', 15 ); + } + + public function tear_down() { + $this->restoreWpHook( 'rocket_sitemap_preload_list' ); + + parent::tear_down(); + } + + /** + * @dataProvider configTestData + */ + public function testShouldReturnExpected( $config, $expected ) { + Router::$sitemap = $config['sitemap']; + + $this->assertSame( + $expected, + apply_filters( 'rocket_sitemap_preload_list', $config['sitemaps'] ) + ); + } +} diff --git a/tests/Integration/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php b/tests/Integration/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php deleted file mode 100644 index 68b116abad..0000000000 --- a/tests/Integration/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php +++ /dev/null @@ -1,39 +0,0 @@ -unregisterAllCallbacksExcept('rocket_sitemap_preload_list', 'rocket_sitemap', 15); - } - - public function tearDown(): void - { - $this->restoreWpHook('rocket_sitemap_preload_list'); - parent::tearDown(); - } - - /** - * @dataProvider configTestData - */ - public function testShouldReturnAsExpected($config, $expected) { - Router::$sitemap = $config['sitemap']; - $this->assertSame($expected, apply_filters('rocket_sitemap_preload_list', $config['sitemaps'])); - } - -} From 7a69e428a4d218c5729e1d3c58eb73f1cabf2919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 4 Jun 2024 09:27:40 -0400 Subject: [PATCH 5/7] update test config --- .../Plugins/SEO/RankMathSEO/addSitemap.php | 37 +++++++++++++++++++ .../Plugins/SEO/RankMathSEO/rocketSitemap.php | 36 ------------------ 2 files changed, 37 insertions(+), 36 deletions(-) create mode 100644 tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php delete mode 100644 tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php diff --git a/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php b/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php new file mode 100644 index 0000000000..23095ce030 --- /dev/null +++ b/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php @@ -0,0 +1,37 @@ +subscriber = new RankMathSEO(); + } + + /** + * @dataProvider configTestData + */ + public function testShouldReturnAsExpected( $config, $expected ) { + Router::$sitemap = $config['sitemap']; + + $this->assertSame( + $expected, + $this->subscriber->rocket_sitemap( $config['sitemaps'] ) + ); + } +} diff --git a/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php b/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php deleted file mode 100644 index a34d8fab8a..0000000000 --- a/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/rocketSitemap.php +++ /dev/null @@ -1,36 +0,0 @@ -option = Mockery::mock(Options_Data::class); - $this->subscriber = new RankMathSEO($this->option); - } - - /** - * @dataProvider configTestData - */ - public function testShouldReturnAsExpected($config, $expected) { - Router::$sitemap = $config['sitemap']; - $this->assertSame($expected, $this->subscriber->rocket_sitemap($config['sitemaps'])); - } -} From 755a1a9c8d6dc8dedeebf9e31523882dd40acfd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 4 Jun 2024 09:31:50 -0400 Subject: [PATCH 6/7] update method name --- .../Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php b/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php index 23095ce030..d05229d41a 100644 --- a/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php +++ b/tests/Unit/inc/ThirdParty/Plugins/SEO/RankMathSEO/addSitemap.php @@ -31,7 +31,7 @@ public function testShouldReturnAsExpected( $config, $expected ) { $this->assertSame( $expected, - $this->subscriber->rocket_sitemap( $config['sitemaps'] ) + $this->subscriber->add_sitemap( $config['sitemaps'] ) ); } } From 9a4f31a077dd39b0c4e7ef66de6e7edf4281f523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 4 Jun 2024 10:40:03 -0400 Subject: [PATCH 7/7] remove phpstan ignore --- inc/ThirdParty/Plugins/SEO/RankMathSEO.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/inc/ThirdParty/Plugins/SEO/RankMathSEO.php b/inc/ThirdParty/Plugins/SEO/RankMathSEO.php index 5abe406870..639fea8445 100644 --- a/inc/ThirdParty/Plugins/SEO/RankMathSEO.php +++ b/inc/ThirdParty/Plugins/SEO/RankMathSEO.php @@ -18,7 +18,6 @@ public static function get_subscribed_events(): array { return []; } - // @phpstan-ignore-next-line if ( ! defined( 'RANK_MATH_FILE' ) || ! Helper::is_module_active( 'sitemap' ) ) { return []; } @@ -40,7 +39,6 @@ public function add_sitemap( $sitemaps ) { return $sitemaps; } - // @phpstan-ignore-next-line $sitemaps[] = Router::get_base_url( 'sitemap_index.xml' ); return $sitemaps;