-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21620 from Yoast/21619-refactor-is-news-seo-active
21619 refactor is news seo active
- Loading branch information
Showing
4 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
95
tests/Unit/Editors/Framework/Integrations/News_SEO_Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
]; | ||
} | ||
} |