Skip to content

Commit

Permalink
Merge pull request #21620 from Yoast/21619-refactor-is-news-seo-active
Browse files Browse the repository at this point in the history
21619 refactor is news seo active
  • Loading branch information
igorschoester authored Sep 12, 2024
2 parents 25aab53 + b811e22 commit 708a25c
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 2 deletions.
1 change: 0 additions & 1 deletion inc/class-wpseo-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,6 @@ public static function get_admin_l10n() {
'isBreadcrumbsDisabled' => WPSEO_Options::get( 'breadcrumbs-enable', false ) !== true && ! current_theme_supports( 'yoast-seo-breadcrumbs' ),
// phpcs:ignore Generic.ControlStructures.DisallowYodaConditions -- Bug: squizlabs/PHP_CodeSniffer#2962.
'isPrivateBlog' => ( (string) get_option( 'blog_public' ) ) === '0',
'news_seo_is_active' => ( defined( 'WPSEO_NEWS_FILE' ) ),
'isAiFeatureActive' => (bool) WPSEO_Options::get( 'enable_ai_generator' ),
];

Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/redux/reducers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function getDefaultState() {
useTwitterData: window.wpseoScriptData.metabox.showSocial.twitter,
isWincherIntegrationActive: isWincherIntegrationActive(),
isInsightsEnabled: get( window, "wpseoScriptData.metabox.isInsightsEnabled", false ),
isNewsEnabled: ! ! window.wpseoAdminL10n.news_seo_is_active,
isNewsEnabled: get( window, "wpseoScriptData.metabox.isNewsSeoActive", false ),
isAiFeatureActive: Boolean( window.wpseoAdminL10n.isAiFeatureActive ),
};
}
Expand Down
55 changes: 55 additions & 0 deletions src/editors/framework/integrations/news-seo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
// @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class.
namespace Yoast\WP\SEO\Editors\Framework\Integrations;

use WPSEO_Addon_Manager;
use Yoast\WP\SEO\Editors\Domain\Integrations\Integration_Data_Provider_Interface;

/**
* Describes if the News SEO plugin is enabled.
*/
class News_SEO implements Integration_Data_Provider_Interface {

/**
* The addon manager.
*
* @var WPSEO_Addon_Manager
*/
private $addon_manager;

/**
* The constructor.
*
* @param WPSEO_Addon_Manager $addon_manager The addon manager.
*/
public function __construct( WPSEO_Addon_Manager $addon_manager ) {
$this->addon_manager = $addon_manager;
}

/**
* If the plugin is activated.
*
* @return bool If the plugin is activated.
*/
public function is_enabled(): bool {
return \is_plugin_active( $this->addon_manager->get_plugin_file( WPSEO_Addon_Manager::NEWS_SLUG ) );
}

/**
* Return this object represented by a key value array.
*
* @return array<string,bool> Returns the name and if the feature is enabled.
*/
public function to_array(): array {
return [ 'isNewsSeoActive' => $this->is_enabled() ];
}

/**
* Returns this object represented by a key value structure that is compliant with the script data array.
*
* @return array<string,bool> Returns the legacy key and if the feature is enabled.
*/
public function to_legacy_array(): array {
return [ 'isNewsSeoActive' => $this->is_enabled() ];
}
}
95 changes: 95 additions & 0 deletions tests/Unit/Editors/Framework/Integrations/News_SEO_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
// @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class.
namespace Yoast\WP\SEO\Tests\Unit\Editors\Framework\Integrations;

use Brain\Monkey;
use Mockery;
use WPSEO_Addon_Manager;
use Yoast\WP\SEO\Editors\Framework\Integrations\News_SEO;
use Yoast\WP\SEO\Tests\Unit\TestCase;

/**
* Class News_SEO_Test
*
* @group editors
*
* @covers \Yoast\WP\SEO\Editors\Framework\Integrations\News_SEO
*/
final class News_SEO_Test extends TestCase {

/**
* Holds the WPSEO_Addon_Manager mock.
*
* @var Mockery\MockInterface|WPSEO_Addon_Manager
*/
protected $addon_manager;

/**
* The News_SEO feature.
*
* @var News_SEO
*/
private $instance;

/**
* Set up the test.
*
* @return void
*/
protected function set_up() {
parent::set_up();
$this->addon_manager = Mockery::mock( WPSEO_Addon_Manager::class );
$this->instance = new News_SEO( $this->addon_manager );
}

/**
* Tests the is_enabled method.
*
* @dataProvider data_provider_is_enabled
*
* @param bool $news_seo_enabled If the news plugin is enabled.
* @param bool $expected The expected outcome.
*
* @return void
*/
public function test_is_enabled(
bool $news_seo_enabled,
bool $expected
) {

$this->addon_manager
->expects( 'get_plugin_file' )
->times( 3 )
->with( 'yoast-seo-news' )
->andReturn( 'wpseo-news/wpseo-news.php' );

Monkey\Functions\expect( 'is_plugin_active' )
->times( 3 )
->with( 'wpseo-news/wpseo-news.php' )
->andReturn( $news_seo_enabled );

$is_woo_seo_active = $this->instance->is_enabled();

$this->assertSame( $expected, $is_woo_seo_active );
$this->assertSame( [ 'isNewsSeoActive' => $is_woo_seo_active ], $this->instance->to_legacy_array() );
$this->assertSame( [ 'isNewsSeoActive' => $is_woo_seo_active ], $this->instance->to_array() );
}

/**
* Data provider for test_is_enabled.
*
* @return array<array<string,bool>>
*/
public static function data_provider_is_enabled() {
return [
'Enabled' => [
'news_seo_enabled' => true,
'expected' => true,
],
'Disabled' => [
'news_seo_enabled' => false,
'expected' => false,
],
];
}
}

0 comments on commit 708a25c

Please sign in to comment.