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 / OrderManagerBasalamItemNameTest.php

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

455 lines 12.8 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
257 namespace SyncBasalam\Services\Orders {
258 if (!function_exists(__NAMESPACE__ . '\\syncBasalamContainer')) {
259 function syncBasalamContainer()
260 {
261 return $GLOBALS['sync_basalam_test_container'];
262 }
263 }
264 }
265
266 namespace SyncBasalam\Tests\Services {
267 use PHPUnit\Framework\TestCase;
268 use SyncBasalam\Services\Orders\OrderManager;
269
270 require_once dirname(__DIR__, 2) . '/includes/Admin/Settings/SettingsConfig.php';
271 require_once dirname(__DIR__, 2) . '/includes/Admin/Settings/SettingsManager.php';
272 require_once dirname(__DIR__, 2) . '/includes/Utilities/ProductMetaKey.php';
273 require_once dirname(__DIR__, 2) . '/includes/Logger/LoggerInterface.php';
274 require_once dirname(__DIR__, 2) . '/includes/Logger/WooLogger.php';
275 require_once dirname(__DIR__, 2) . '/includes/Logger/Logger.php';
276 require_once dirname(__DIR__, 2) . '/includes/Services/Orders/OrderManager.php';
277
278 class OrderManagerBasalamItemNameTest extends TestCase
279 {
280 protected function setUp(): void
281 {
282 $GLOBALS['sync_basalam_test_options'] = [
283 'sync_basalam_settings' => ['vendor_id' => '123'],
284 ];
285 $GLOBALS['sync_basalam_test_meta'] = [];
286 $GLOBALS['sync_basalam_test_products'] = [
287 500 => new \WC_Product(),
288 ];
289 $GLOBALS['sync_basalam_test_posts_by_meta'] = [];
290 }
291
292 public function testExtractsProductTitle(): void
293 {
294 $item = [
295 'product' => ['id' => 10, 'title' => 'شلوار جین �
296 ردانه'],
297 'quantity' => 1,
298 ];
299
300 self::assertSame('شلوار جین �
301 ردانه', OrderManager::extractBasalamItemName($item));
302 }
303
304 public function testFallsBackToNameFieldWhenTitleIsMissing(): void
305 {
306 $item = [
307 'product' => ['id' => 10, 'name' => 'تی‌شرت نخی'],
308 ];
309
310 self::assertSame('تی‌شرت نخی', OrderManager::extractBasalamItemName($item));
311 }
312
313 public function testAppendsVariationTitleWhenNotAlreadyIncluded(): void
314 {
315 $item = [
316 'product' => ['id' => 10, 'title' => 'کفش ورزشی'],
317 'variation' => ['id' => 55, 'title' => 'سایز ۴۲'],
318 ];
319
320 self::assertSame('کفش ورزشی - سایز ۴۲', OrderManager::extractBasalamItemName($item));
321 }
322
323 public function testDoesNotDuplicateVariationTitleAlreadyPresentInProductTitle(): void
324 {
325 $item = [
326 'product' => ['id' => 10, 'title' => 'کفش ورزشی سایز ۴۲'],
327 'variation' => ['id' => 55, 'title' => 'سایز ۴۲'],
328 ];
329
330 self::assertSame('کفش ورزشی سایز ۴۲', OrderManager::extractBasalamItemName($item));
331 }
332
333 public function testReturnsEmptyStringWhenNoNameAvailable(): void
334 {
335 self::assertSame('', OrderManager::extractBasalamItemName(['product' => ['id' => 10]]));
336 self::assertSame('', OrderManager::extractBasalamItemName([]));
337 }
338
339 public function testMetaKeyIsVisibleForWooCommerceDisplay(): void
340 {
341 // ووکا�
342 رس �
343 تاهایی که با «_» شروع ن�
344 ی‌شوند را در صفحه سفارش نشان �
345 ی‌دهد.
346 self::assertStringStartsNotWith('_', OrderManager::BASALAM_ITEM_NAME_META_KEY);
347 }
348
349 public function testStoresVisibleMetaOnOrderItem(): void
350 {
351 $order = new \WC_Order();
352 $orderItemId = $order->add_product(new \WC_Product());
353
354 $result = OrderManager::setBasalamItemNameOnOrderItem(
355 $order,
356 $orderItemId,
357 ' �
358 اگ حرارتی �
359 دل آلفا '
360 );
361
362 self::assertTrue($result);
363 self::assertSame(
364 ['نا�
365 �
366 حصول در باسلا�
367 ' => '�
368 اگ حرارتی �
369 دل آلفا'],
370 $order->get_item($orderItemId)->get_meta_data()
371 );
372 }
373
374 public function testStoresClickableLinkMetaWhenBasalamProductIdIsGiven(): void
375 {
376 $order = new \WC_Order();
377 $orderItemId = $order->add_product(new \WC_Product());
378
379 OrderManager::setBasalamItemNameOnOrderItem($order, $orderItemId, '�
380 اگ حرارتی �
381 دل آلفا', 12345);
382
383 $meta = $order->get_item($orderItemId)->get_meta_data();
384
385 self::assertArrayHasKey(OrderManager::BASALAM_ITEM_LINK_META_KEY, $meta);
386 self::assertStringStartsNotWith('_', OrderManager::BASALAM_ITEM_LINK_META_KEY);
387 self::assertSame(
388 '<a href="https://basalam.com/p/12345" target="_blank" rel="noopener">�
389 شاهده</a>',
390 $meta[OrderManager::BASALAM_ITEM_LINK_META_KEY]
391 );
392 }
393
394 public function testSkipsLinkMetaWhenBasalamProductIdIsMissing(): void
395 {
396 $order = new \WC_Order();
397 $orderItemId = $order->add_product(new \WC_Product());
398
399 OrderManager::setBasalamItemNameOnOrderItem($order, $orderItemId, '�
400 اگ حرارتی �
401 دل آلفا', null);
402
403 $meta = $order->get_item($orderItemId)->get_meta_data();
404
405 self::assertArrayNotHasKey(OrderManager::BASALAM_ITEM_LINK_META_KEY, $meta);
406 self::assertArrayHasKey(OrderManager::BASALAM_ITEM_NAME_META_KEY, $meta);
407 }
408
409 public function testSkipsMetaWhenItemNameIsEmpty(): void
410 {
411 $order = new \WC_Order();
412 $orderItemId = $order->add_product(new \WC_Product());
413
414 $result = OrderManager::setBasalamItemNameOnOrderItem($order, $orderItemId, ' ');
415
416 self::assertFalse($result);
417 self::assertSame([], $order->get_item($orderItemId)->get_meta_data());
418 }
419
420 public function testSkipsMetaWhenOrderItemIsMissing(): void
421 {
422 $order = new \WC_Order();
423
424 $result = OrderManager::setBasalamItemNameOnOrderItem($order, 999, '�
425 اگ حرارتی');
426
427 self::assertFalse($result);
428 }
429 }
430
431 }
432
433 namespace {
434 // productExistsByTitle در �
435 سیر placeholder از wpdb استفاده �
436 ی‌کند.
437 if (!isset($GLOBALS['wpdb'])) {
438 $GLOBALS['wpdb'] = new class {
439 public $prefix = 'wp_';
440 public $posts = 'wp_posts';
441
442 public function get_var($query = null, $x = 0, $y = 0)
443 {
444 return null; // �
445 حصول جایگزین هنوز ساخته نشده
446 }
447
448 public function prepare($query, ...$args)
449 {
450 return $query;
451 }
452 };
453 }
454 }
455