PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.27
ووسلام – همگام سازی ووکامرس و باسلام v1.10.27
1.1.26 1.10.27 1.10.24 1.10.25 1.10.23 1.10.22 1.10.21 1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 All 60 releases
sync-basalam / tests / Services / OrderManagerAddBasalamItemTest.php

OrderManagerAddBasalamItemTest.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.27, at tests/Services/OrderManagerAddBasalamItemTest.php

479 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace {
4 if (!function_exists('is_wp_error')) {
5 function is_wp_error($value)
6 {
7 return false;
8 }
9 }
10
11 if (!function_exists('esc_url')) {
12 function esc_url($url)
13 {
14 return (string) $url;
15 }
16 }
17
18 if (!function_exists('esc_html')) {
19 function esc_html($value)
20 {
21 return (string) $value;
22 }
23 }
24
25 if (!function_exists('get_option')) {
26 function get_option($name, $default = false)
27 {
28 return $GLOBALS['sync_basalam_test_options'][$name] ?? $default;
29 }
30 }
31
32 if (!function_exists('update_option')) {
33 function update_option($name, $value, $autoload = null)
34 {
35 $GLOBALS['sync_basalam_test_options'][$name] = $value;
36 return true;
37 }
38 }
39
40 if (!function_exists('get_post_meta')) {
41 function get_post_meta($postId, $metaKey, $single = false)
42 {
43 return $GLOBALS['sync_basalam_test_meta'][$postId][$metaKey] ?? '';
44 }
45 }
46
47 if (!function_exists('update_post_meta')) {
48 function update_post_meta($postId, $metaKey, $value)
49 {
50 $GLOBALS['sync_basalam_test_meta'][$postId][$metaKey] = $value;
51 return true;
52 }
53 }
54
55 if (!function_exists('apply_filters')) {
56 function apply_filters($hookName, $value)
57 {
58 return $value;
59 }
60 }
61
62 if (!function_exists('do_action')) {
63 function do_action($hookName, ...$args)
64 {
65 }
66 }
67
68 if (!function_exists('syncBasalamSettings')) {
69 function syncBasalamSettings()
70 {
71 return new class {
72 public function getSettings($setting = null)
73 {
74 $settings = $GLOBALS['sync_basalam_test_options']['sync_basalam_settings'] ?? [];
75
76 return $setting === null ? $settings : ($settings[$setting] ?? null);
77 }
78 };
79 }
80 }
81
82 if (!class_exists('WC_Logger')) {
83 class WC_Logger
84 {
85 }
86 }
87
88 if (!function_exists('wc_get_logger')) {
89 function wc_get_logger()
90 {
91 return new class {
92 public function log($level, $message, $context = [])
93 {
94 }
95 };
96 }
97 }
98
99 if (!class_exists('WC_Order_Item')) {
100 class WC_Order_Item
101 {
102 }
103 }
104
105 if (!class_exists('WC_Order_Item_Product')) {
106 class WC_Order_Item_Product extends WC_Order_Item
107 {
108 private array $meta = [];
109
110 public function update_meta_data($key, $value, $save = true)
111 {
112 $this->meta[$key] = $value;
113 return true;
114 }
115
116 public function get_meta_data(): array
117 {
118 return $this->meta;
119 }
120
121 public function set_subtotal($value)
122 {
123 }
124
125 public function set_total($value)
126 {
127 }
128
129 public function save()
130 {
131 return true;
132 }
133 }
134 }
135
136 if (!class_exists('WC_Product')) {
137 class WC_Product
138 {
139 }
140 }
141
142 if (!class_exists('WC_Product_Simple')) {
143 class WC_Product_Simple extends WC_Product
144 {
145 private string $name = '';
146 private string $status = '';
147 private string $sku = '';
148
149 public function set_name($name)
150 {
151 $this->name = (string) $name;
152 }
153
154 public function set_status($status)
155 {
156 $this->status = (string) $status;
157 }
158
159 public function set_sku($sku)
160 {
161 $this->sku = (string) $sku;
162 }
163
164 public function save()
165 {
166 return $GLOBALS['sync_basalam_placeholder_id'] ?? 900;
167 }
168
169 public function get_id()
170 {
171 return $GLOBALS['sync_basalam_placeholder_id'] ?? 900;
172 }
173 }
174 }
175
176 if (!class_exists('WC_Order')) {
177 class WC_Order
178 {
179 /** @var array<int, WC_Order_Item_Product> */
180 private array $items = [];
181 private array $meta = [];
182 private int $nextItemId = 100;
183
184 public function add_product($product, $quantity = 1, $args = [])
185 {
186 $itemId = $this->nextItemId++;
187 $this->items[$itemId] = new WC_Order_Item_Product();
188
189 return $itemId;
190 }
191
192 public function get_item($itemId)
193 {
194 return $this->items[$itemId] ?? null;
195 }
196
197 public function update_meta_data($key, $value, $save = true)
198 {
199 $this->meta[$key] = $value;
200 return true;
201 }
202
203 public function get_order_item_meta(): array
204 {
205 return $this->meta;
206 }
207
208 /** @return array<int, WC_Order_Item_Product> */
209 public function addedItems(): array
210 {
211 return $this->items;
212 }
213 }
214 }
215
216 if (!function_exists('wc_get_product')) {
217 function wc_get_product($productId)
218 {
219 return $GLOBALS['sync_basalam_test_products'][$productId] ?? null;
220 }
221 }
222
223 if (!function_exists('get_posts')) {
224 function get_posts($args)
225 {
226 $key = ($args['meta_key'] ?? '') . '|' . ($args['meta_value'] ?? '');
227 $found = $GLOBALS['sync_basalam_test_posts_by_meta'][$key] ?? [];
228
229 if (($args['fields'] ?? '') === 'ids') {
230 return $found; // getWooProductVariableId �
231 ستقی�
232 اً خود شناسه را �
233 ی‌خواند
234 }
235
236 return array_map(static function ($id) {
237 return new class ($id) {
238 public $ID;
239
240 public function __construct($id)
241 {
242 $this->ID = $id;
243 }
244 };
245 }, $found);
246 }
247 }
248
249 if (!function_exists('get_woocommerce_currency')) {
250 function get_woocommerce_currency()
251 {
252 return 'IRT';
253 }
254 }
255
256 // productExistsByTitle در �
257 سیر placeholder از wpdb استفاده �
258 ی‌کند.
259 if (!isset($GLOBALS['wpdb'])) {
260 $GLOBALS['wpdb'] = new class {
261 public $prefix = 'wp_';
262 public $posts = 'wp_posts';
263
264 public function get_var($query = null, $x = 0, $y = 0)
265 {
266 return null; // �
267 حصول جایگزین هنوز ساخته نشده
268 }
269
270 public function prepare($query, ...$args)
271 {
272 return $query;
273 }
274 };
275 }
276 }
277
278 namespace SyncBasalam\Services\Orders {
279 if (!function_exists(__NAMESPACE__ . '\\syncBasalamContainer')) {
280 function syncBasalamContainer()
281 {
282 return $GLOBALS['sync_basalam_test_container'];
283 }
284 }
285 }
286
287 namespace SyncBasalam\Tests\Services {
288 use PHPUnit\Framework\TestCase;
289 use SyncBasalam\Services\Orders\OrderManager;
290
291 require_once dirname(__DIR__, 2) . '/includes/Admin/Settings/SettingsConfig.php';
292 require_once dirname(__DIR__, 2) . '/includes/Admin/Settings/SettingsManager.php';
293 require_once dirname(__DIR__, 2) . '/includes/Utilities/ProductMetaKey.php';
294 require_once dirname(__DIR__, 2) . '/includes/Logger/LoggerInterface.php';
295 require_once dirname(__DIR__, 2) . '/includes/Logger/WooLogger.php';
296 require_once dirname(__DIR__, 2) . '/includes/Logger/Logger.php';
297 require_once dirname(__DIR__, 2) . '/includes/Services/Orders/OrderManager.php';
298
299 /**
300 * تست یکپارچه addBasalamItemToOrder: هر دو �
301 سیر �
302 پ‌شده و �
303 حصول جایگزین.
304 */
305 class OrderManagerAddBasalamItemTest extends TestCase
306 {
307 private const MAPPED_WOO_PRODUCT_ID = 500;
308
309 protected function setUp(): void
310 {
311 $GLOBALS['sync_basalam_test_options'] = [
312 'sync_basalam_settings' => ['vendor_id' => '123'],
313 ];
314 $GLOBALS['sync_basalam_test_meta'] = [];
315 $GLOBALS['sync_basalam_test_products'] = [
316 self::MAPPED_WOO_PRODUCT_ID => new \WC_Product(),
317 ];
318 // �
319 حصول باسلا�
320 777 → �
321 حصول ووکا�
322 رس 500 (�
323 سیر �
324 پ ساده)
325 $GLOBALS['sync_basalam_test_posts_by_meta'] = [
326 'sync_basalam_product_id_123|777' => [self::MAPPED_WOO_PRODUCT_ID],
327 ];
328 }
329
330 protected function tearDown(): void
331 {
332 unset(
333 $GLOBALS['sync_basalam_placeholder_id'],
334 $GLOBALS['sync_basalam_test_posts_by_meta'],
335 $GLOBALS['sync_basalam_test_products'],
336 $GLOBALS['sync_basalam_test_meta'],
337 $GLOBALS['sync_basalam_test_options']
338 );
339 }
340
341 public function testMappedItemGetsBasalamNameMeta(): void
342 {
343 $order = new \WC_Order();
344
345 OrderManager::addBasalamItemToOrder($order, [
346 'id' => 1001,
347 'quantity' => 2,
348 'product' => ['id' => 777, 'title' => '�
349 اگ حرارتی �
350 دل آلفا'],
351 'financial_report' => [
352 'report_items' => [
353 ['title' => 'قی�
354 ت �
355 حصول', 'amount' => 200000],
356 ],
357 ],
358 ]);
359
360 $items = $order->addedItems();
361 self::assertCount(1, $items);
362 $item = reset($items);
363
364 self::assertSame(
365 '�
366 اگ حرارتی �
367 دل آلفا',
368 $item->get_meta_data()[OrderManager::BASALAM_ITEM_NAME_META_KEY] ?? null
369 );
370 self::assertSame(
371 '<a href="https://basalam.com/p/777" target="_blank" rel="noopener">�
372 شاهده</a>',
373 $item->get_meta_data()[OrderManager::BASALAM_ITEM_LINK_META_KEY] ?? null
374 );
375 }
376
377 public function testUnmappedItemFallsBackToPlaceholderAndKeepsBasalamName(): void
378 {
379 $GLOBALS['sync_basalam_test_posts_by_meta'] = [];
380 $GLOBALS['sync_basalam_test_products'] = [];
381
382 // �
383 حصول جایگزین در اولین فراخوانی ساخته و کش �
384 ی‌شود.
385 $GLOBALS['sync_basalam_placeholder_id'] = 900;
386 $GLOBALS['sync_basalam_test_products'][900] = new \WC_Product();
387
388 $order = new \WC_Order();
389
390 OrderManager::addBasalamItemToOrder($order, [
391 'id' => 1002,
392 'quantity' => 1,
393 'product' => ['id' => 888, 'name' => 'کیبورد �
394 کانیکی گی�
395 ینگ'],
396 'variation' => ['id' => 999, 'title' => 'سوئیچ آبی'],
397 'financial_report' => [
398 'report_items' => [
399 ['title' => 'قی�
400 ت �
401 حصول', 'amount' => 350000],
402 ],
403 ],
404 ]);
405
406 $items = $order->addedItems();
407 self::assertCount(1, $items, 'آیت�
408 تعریف‌نشده باید با �
409 حصول جایگزین به سفارش اضافه شود');
410 $item = reset($items);
411
412 self::assertSame(
413 'کیبورد �
414 کانیکی گی�
415 ینگ - سوئیچ آبی',
416 $item->get_meta_data()[OrderManager::BASALAM_ITEM_NAME_META_KEY] ?? null
417 );
418 self::assertSame(
419 '<a href="https://basalam.com/p/888" target="_blank" rel="noopener">�
420 شاهده</a>',
421 $item->get_meta_data()[OrderManager::BASALAM_ITEM_LINK_META_KEY] ?? null
422 );
423 }
424
425 public function testMappedVariationItemGetsBasalamNameMeta(): void
426 {
427 // �
428 سیر �
429 پ تنوع: variation id 999 → �
430 حصول ووکا�
431 رس 600
432 $GLOBALS['sync_basalam_test_products'][600] = new \WC_Product();
433 $GLOBALS['sync_basalam_test_posts_by_meta']['sync_basalam_variation_id|999'] = [600];
434
435 $order = new \WC_Order();
436
437 OrderManager::addBasalamItemToOrder($order, [
438 'id' => 1003,
439 'quantity' => 1,
440 'product' => ['id' => 777, 'title' => 'کفش ورزشی'],
441 'variation' => ['id' => 999, 'title' => 'سایز ۴۲'],
442 ]);
443
444 $items = $order->addedItems();
445 self::assertCount(1, $items);
446 $item = reset($items);
447
448 self::assertSame(
449 'کفش ورزشی - سایز ۴۲',
450 $item->get_meta_data()[OrderManager::BASALAM_ITEM_NAME_META_KEY] ?? null
451 );
452 }
453
454 public function testItemWithoutBasalamProductIdIsIgnored(): void
455 {
456 $order = new \WC_Order();
457
458 OrderManager::addBasalamItemToOrder($order, ['quantity' => 1]);
459
460 self::assertCount(0, $order->addedItems());
461 }
462
463 public function testBasalamItemMetaIsRecordedAlongsideName(): void
464 {
465 $order = new \WC_Order();
466
467 OrderManager::addBasalamItemToOrder($order, [
468 'id' => 1001,
469 'quantity' => 1,
470 'product' => ['id' => 777, 'title' => '�
471 اگ حرارتی �
472 دل آلفا'],
473 ]);
474
475 self::assertArrayHasKey('_sync_basalam_item_id_100', $order->get_order_item_meta());
476 }
477 }
478 }
479