diff --git a/plugins/woocommerce/src/Admin/Marketing/MarketingCampaign.php b/plugins/woocommerce/src/Admin/Marketing/MarketingCampaign.php index 7b3f99a4b3a..45037fea367 100644 --- a/plugins/woocommerce/src/Admin/Marketing/MarketingCampaign.php +++ b/plugins/woocommerce/src/Admin/Marketing/MarketingCampaign.php @@ -22,6 +22,13 @@ class MarketingCampaign implements JsonSerializable { */ protected $id; + /** + * The marketing channel that this campaign belongs to. + * + * @var MarketingChannelInterface + */ + protected $channel; + /** * Title of the marketing campaign. * @@ -46,13 +53,15 @@ class MarketingCampaign implements JsonSerializable { /** * MarketingCampaign constructor. * - * @param string $id The marketing campaign's unique identifier. - * @param string $title The title of the marketing campaign. - * @param string $manage_url The URL to the channel's campaign management page. - * @param Price|null $cost The cost of the marketing campaign with the currency. + * @param string $id The marketing campaign's unique identifier. + * @param MarketingChannelInterface $channel The marketing channel that this campaign belongs to. + * @param string $title The title of the marketing campaign. + * @param string $manage_url The URL to the channel's campaign management page. + * @param Price|null $cost The cost of the marketing campaign with the currency. */ - public function __construct( string $id, string $title, string $manage_url, Price $cost = null ) { + public function __construct( string $id, MarketingChannelInterface $channel, string $title, string $manage_url, Price $cost = null ) { $this->id = $id; + $this->channel = $channel; $this->title = $title; $this->manage_url = $manage_url; $this->cost = $cost; @@ -67,6 +76,15 @@ class MarketingCampaign implements JsonSerializable { return $this->id; } + /** + * Returns the marketing channel that this campaign belongs to. + * + * @return MarketingChannelInterface + */ + public function get_channel(): MarketingChannelInterface { + return $this->channel; + } + /** * Returns the title of the marketing campaign. * @@ -102,6 +120,7 @@ class MarketingCampaign implements JsonSerializable { public function jsonSerialize() { return [ 'id' => $this->get_id(), + 'channel' => $this->get_channel()->get_slug(), 'title' => $this->get_title(), 'manage_url' => $this->get_manage_url(), 'cost' => $this->get_cost(), diff --git a/plugins/woocommerce/tests/php/src/Admin/Marketing/MarketingCampaignTest.php b/plugins/woocommerce/tests/php/src/Admin/Marketing/MarketingCampaignTest.php index 401e1630294..30d8c243ba5 100644 --- a/plugins/woocommerce/tests/php/src/Admin/Marketing/MarketingCampaignTest.php +++ b/plugins/woocommerce/tests/php/src/Admin/Marketing/MarketingCampaignTest.php @@ -3,6 +3,7 @@ namespace Automattic\WooCommerce\Tests\Admin\Marketing; use Automattic\WooCommerce\Admin\Marketing\MarketingCampaign; +use Automattic\WooCommerce\Admin\Marketing\MarketingChannelInterface; use Automattic\WooCommerce\Admin\Marketing\Price; use WC_Unit_Test_Case; @@ -15,7 +16,10 @@ class MarketingCampaignTest extends WC_Unit_Test_Case { * @testdox `get_id`, `get_title`, `get_manage_url`, and `get_cost` return the class properties set by the constructor. */ public function test_get_methods_return_properties() { - $marketing_campaign = new MarketingCampaign( '1234', 'Ad #1234', 'https://example.com/manage-campaigns', new Price( '1000', 'USD' ) ); + $test_channel_1 = $this->createMock( MarketingChannelInterface::class ); + $test_channel_1->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); + + $marketing_campaign = new MarketingCampaign( '1234', $test_channel_1, 'Ad #1234', 'https://example.com/manage-campaigns', new Price( '1000', 'USD' ) ); $this->assertEquals( '1234', $marketing_campaign->get_id() ); $this->assertEquals( 'Ad #1234', $marketing_campaign->get_title() ); @@ -29,7 +33,9 @@ class MarketingCampaignTest extends WC_Unit_Test_Case { * @testdox `cost` property can be null. */ public function test_cost_can_be_null() { - $marketing_campaign = new MarketingCampaign( '1234', 'Ad #1234', 'https://example.com/manage-campaigns' ); + $test_channel_1 = $this->createMock( MarketingChannelInterface::class ); + + $marketing_campaign = new MarketingCampaign( '1234', $test_channel_1, 'Ad #1234', 'https://example.com/manage-campaigns' ); $this->assertNull( $marketing_campaign->get_cost() ); } @@ -38,13 +44,17 @@ class MarketingCampaignTest extends WC_Unit_Test_Case { * @testdox It can be serialized to JSON including all its properties. */ public function test_can_be_serialized_to_json() { - $marketing_campaign = new MarketingCampaign( '1234', 'Ad #1234', 'https://example.com/manage-campaigns', new Price( '1000', 'USD' ) ); + $test_channel_1 = $this->createMock( MarketingChannelInterface::class ); + $test_channel_1->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' ); + + $marketing_campaign = new MarketingCampaign( '1234', $test_channel_1, 'Ad #1234', 'https://example.com/manage-campaigns', new Price( '1000', 'USD' ) ); $json = wp_json_encode( $marketing_campaign ); $this->assertNotEmpty( $json ); $this->assertEqualSets( [ 'id' => $marketing_campaign->get_id(), + 'channel' => 'test-channel-1', 'title' => $marketing_campaign->get_title(), 'manage_url' => $marketing_campaign->get_manage_url(), 'cost' => [