@@ -51,7 +51,7 @@ public function test_orders_are_not_created_in_woocommerce_if_they_contain_out_o
5151 $ sf_order = new ShoppingFeed \ShoppingFeedWC \Orders \Order ( $ order_resource );
5252 $ sf_order ->add ();
5353
54- $ results = wc_get_orders ( [ Query::WC_META_SF_REFERENCE => $ order_resource ->getReference () ] );
54+ $ results = wc_get_orders ( [ Query::WC_META_SF_REFERENCE => $ order_resource ->getReference () ] );
5555 $ this ->assertEmpty ( $ results );
5656 }
5757
@@ -84,15 +84,15 @@ public function test_order_exist() {
8484 $ this ->assertFalse ( ShoppingFeed \ShoppingFeedWC \Orders \Order::exists ( $ order_resource_bis ) );
8585 }
8686
87- public function test_code_nif_is_imported () : void {
87+ public function test_code_nif_is_imported () : void {
8888 $ order_resource = $ this ->get_order_resource ( 'order-sf-nif ' );
8989 $ sf_order = new ShoppingFeed \ShoppingFeedWC \Orders \Order ( $ order_resource );
9090 $ sf_order ->add ();
9191
9292 $ results = wc_get_orders ( [ Query::WC_META_SF_REFERENCE => $ order_resource ->getReference () ] );
9393 $ wc_order = reset ( $ results );
9494
95- $ this ->assertEquals ('210474114 ' , $ wc_order ->get_meta ( 'sf_nif ' ));
95+ $ this ->assertEquals ( '210474114 ' , $ wc_order ->get_meta ( 'sf_nif ' ) );
9696 }
9797
9898 public function test_code_sn_nif_meta_exists (): void {
@@ -106,6 +106,102 @@ public function test_code_sn_nif_meta_exists(): void {
106106 $ this ->assertEmpty ( $ wc_order ->get_meta ( 'sf_nif ' ) );
107107 }
108108
109+ /**
110+ * Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
111+ *
112+ * This test case is for when the feed is configured to use the ID as the product identifier.
113+ *
114+ * @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
115+ * @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
116+ */
117+ public function test_order_with_id_reference_alias () {
118+ $ order_resource = $ this ->get_order_resource ( 'simple-order-id-reference-alias ' );
119+ $ sf_order = new ShoppingFeed \ShoppingFeedWC \Orders \Order ( $ order_resource );
120+ $ sf_order ->add ();
121+
122+ $ results = wc_get_orders ( [ Query::WC_META_SF_REFERENCE => $ order_resource ->getReference () ] );
123+ $ wc_order = reset ( $ results );
124+ $ items = $ wc_order ->get_items ();
125+
126+ $ this ->assertEquals ( 1 , $ wc_order ->get_item_count (), 'Assert the order contain the same number of product from ShoppingFeed ' );
127+ $ this ->assertEquals ( 'Hoodie with Logo ' , reset ( $ items )->get_name (), 'Assert the order contain the same name of product from ShoppingFeed ' );
128+ }
129+
130+ /**
131+ * Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
132+ *
133+ * This test case is for when the feed is configured to use the ID as the product identifier.
134+ *
135+ * @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
136+ * @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
137+ */
138+ public function test_order_with_unknown_id_reference_alias () {
139+ $ order_resource = $ this ->get_order_resource ( 'simple-order-id-reference-alias-invalid ' );
140+ $ sf_order = new ShoppingFeed \ShoppingFeedWC \Orders \Order ( $ order_resource );
141+ $ sf_order ->add ();
142+
143+ $ results = wc_get_orders ( [ Query::WC_META_SF_REFERENCE => $ order_resource ->getReference () ] );
144+
145+ $ this ->assertEmpty ( $ results );
146+ }
147+
148+ /**
149+ * Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
150+ *
151+ * This test case is for when the feed is configured to use the SKU as the product identifier.
152+ *
153+ * @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
154+ * @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
155+ */
156+ public function test_order_with_sku_reference_alias () {
157+ add_filter (
158+ 'pre_option_sf_feed_options ' ,
159+ function ( $ value ) {
160+ return [
161+ 'product_identifier ' => 'sku ' ,
162+ ];
163+ }
164+ );
165+
166+ $ order_resource = $ this ->get_order_resource ( 'simple-order-sku-reference-alias ' );
167+ $ sf_order = new ShoppingFeed \ShoppingFeedWC \Orders \Order ( $ order_resource );
168+ $ sf_order ->add ();
169+
170+ $ results = wc_get_orders ( [ Query::WC_META_SF_REFERENCE => $ order_resource ->getReference () ] );
171+ $ wc_order = reset ( $ results );
172+ $ items = $ wc_order ->get_items ();
173+
174+ $ this ->assertEquals ( 1 , $ wc_order ->get_item_count (), 'Assert the order contain the same number of product from ShoppingFeed ' );
175+ $ this ->assertEquals ( 'Hoodie with Logo ' , reset ( $ items )->get_name (), 'Assert the order contain the same name of product from ShoppingFeed ' );
176+ }
177+
178+ /**
179+ * Test that products references are correctly mapped using itemsReferencesAliases when importing an order.
180+ *
181+ * This test case is for when the feed is configured to use the SKU as the product identifier.
182+ *
183+ * @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::set_products
184+ * @covers ShoppingFeed\ShoppingFeedWC\Orders\Order\Products::mapping_product
185+ */
186+ public function test_order_with_unknown_sku_reference_alias () {
187+ add_filter (
188+ 'pre_option_sf_feed_options ' ,
189+ function ( $ value ) {
190+ return [
191+ 'product_identifier ' => 'sku ' ,
192+ ];
193+ }
194+ );
195+
196+ $ order_resource = $ this ->get_order_resource ( 'simple-order-sku-reference-alias-invalid ' );
197+ $ sf_order = new ShoppingFeed \ShoppingFeedWC \Orders \Order ( $ order_resource );
198+ $ sf_order ->add ();
199+
200+ $ results = wc_get_orders ( [ Query::WC_META_SF_REFERENCE => $ order_resource ->getReference () ] );
201+
202+ $ this ->assertEmpty ( $ results );
203+ }
204+
109205 public function custom_orders_table ( $ value ) {
110206 return 'no ' ;
111207 }
0 commit comments