-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathServiceBasedMerchantHooksTest.php
More file actions
180 lines (131 loc) · 6.26 KB
/
ServiceBasedMerchantHooksTest.php
File metadata and controls
180 lines (131 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Options;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\ServiceBasedMerchantHooks;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\ServiceBasedMerchantState;
use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\UnitTest;
use PHPUnit\Framework\MockObject\MockObject;
use WC_Helper_Product;
/**
* Class ServiceBasedMerchantHooksTest
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Options
*/
class ServiceBasedMerchantHooksTest extends UnitTest {
/** @var MockObject|ServiceBasedMerchantState */
protected $state;
/** @var ServiceBasedMerchantHooks */
protected $hooks;
public function setUp(): void {
parent::setUp();
$this->state = $this->createMock( ServiceBasedMerchantState::class );
$this->hooks = new ServiceBasedMerchantHooks( $this->state );
}
public function test_register_adds_expected_hooks() {
$this->hooks->register();
$this->assertGreaterThan( 0, has_action( 'woocommerce_new_product', [ $this->hooks, 'handle_product_change' ] ) );
$this->assertGreaterThan( 0, has_action( 'woocommerce_update_product', [ $this->hooks, 'handle_product_change' ] ) );
$this->assertGreaterThan( 0, has_action( 'untrashed_post', [ $this->hooks, 'handle_product_restore' ] ) );
$this->assertGreaterThan( 0, has_action( 'trashed_post', [ $this->hooks, 'handle_product_removal' ] ) );
$this->assertGreaterThan( 0, has_action( 'deleted_post', [ $this->hooks, 'handle_product_removal' ] ) );
}
public function test_handle_product_change_resets_flag_when_physical_product_added_to_service_based_store() {
$product = WC_Helper_Product::create_simple_product();
$product->set_virtual( false );
$product->save();
$this->state->method( 'is_service_based_merchant' )->willReturn( true );
$this->state->expects( $this->once() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_change( $product->get_id(), $product );
$product->delete( true );
}
public function test_handle_product_change_does_not_reset_when_virtual_product_added_to_service_based_store() {
$product = WC_Helper_Product::create_simple_product();
$product->set_virtual( true );
$product->save();
$this->state->method( 'is_service_based_merchant' )->willReturn( true );
$this->state->expects( $this->never() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_change( $product->get_id(), $product );
$product->delete( true );
}
public function test_handle_product_change_does_not_reset_when_store_is_already_product_based() {
$product = WC_Helper_Product::create_simple_product();
$product->set_virtual( false );
$product->save();
$this->state->method( 'is_service_based_merchant' )->willReturn( false );
$this->state->expects( $this->never() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_change( $product->get_id(), $product );
$product->delete( true );
}
public function test_handle_product_change_loads_product_when_not_passed() {
$product = WC_Helper_Product::create_simple_product();
$product->set_virtual( false );
$product->save();
$this->state->method( 'is_service_based_merchant' )->willReturn( true );
$this->state->expects( $this->once() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_change( $product->get_id() );
$product->delete( true );
}
public function test_handle_product_removal_resets_flag_when_product_trashed_in_product_based_store() {
$product = WC_Helper_Product::create_simple_product();
$product->set_virtual( false );
$product->save();
$product_id = $product->get_id();
$this->state->method( 'is_service_based_merchant' )->willReturn( false );
$this->state->expects( $this->once() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_removal( $product_id );
$product->delete( true );
}
public function test_handle_product_removal_does_not_reset_when_store_is_already_service_based() {
$product = WC_Helper_Product::create_simple_product();
$product->save();
$product_id = $product->get_id();
$this->state->method( 'is_service_based_merchant' )->willReturn( true );
$this->state->expects( $this->never() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_removal( $product_id );
$product->delete( true );
}
public function test_handle_product_removal_ignores_non_product_post_types() {
$post_id = wp_insert_post(
[
'post_type' => 'post',
'post_title' => 'Test Post',
'post_status' => 'publish',
]
);
$this->state->expects( $this->never() )->method( 'is_service_based_merchant' );
$this->state->expects( $this->never() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_removal( $post_id );
wp_delete_post( $post_id, true );
}
public function test_handle_product_restore_resets_flag_when_physical_product_restored_to_service_based_store() {
$product = WC_Helper_Product::create_simple_product();
$product->set_virtual( false );
$product->save();
$this->state->method( 'is_service_based_merchant' )->willReturn( true );
$this->state->expects( $this->once() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_restore( $product->get_id() );
$product->delete( true );
}
public function test_handle_product_restore_does_not_reset_when_virtual_product_restored() {
$product = WC_Helper_Product::create_simple_product();
$product->set_virtual( true );
$product->save();
$this->state->method( 'is_service_based_merchant' )->willReturn( true );
$this->state->expects( $this->never() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_restore( $product->get_id() );
$product->delete( true );
}
public function test_handle_product_restore_ignores_non_product_post_types() {
$post_id = wp_insert_post(
[
'post_type' => 'post',
'post_title' => 'Test Post',
'post_status' => 'publish',
]
);
$this->state->expects( $this->never() )->method( 'is_service_based_merchant' );
$this->state->expects( $this->never() )->method( 'reset_service_based_merchant_status' );
$this->hooks->handle_product_restore( $post_id );
wp_delete_post( $post_id, true );
}
}