MigrateAsteriaCustomersCommandTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Unit\Migration;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Hash;
  6. use Illuminate\Support\Facades\Schema;
  7. use Webkul\BagistoApi\Tests\BagistoApiTestCase;
  8. use Webkul\Core\Models\SubscribersList;
  9. use Webkul\Customer\Models\Customer;
  10. use Webkul\Customer\Models\CustomerAddress;
  11. use Webkul\Customer\Models\CustomerGroup;
  12. class MigrateAsteriaCustomersCommandTest extends BagistoApiTestCase
  13. {
  14. private string $sqlitePath;
  15. private string $connection = 'asteria_test';
  16. public function setUp(): void
  17. {
  18. parent::setUp();
  19. if (! Schema::hasColumn('customers', 'migrated_from_asteria_id')
  20. || ! Schema::hasColumn('customers', 'legacy_password')
  21. || ! Schema::hasColumn('customers', 'customer_source')) {
  22. $this->markTestSkipped('Run php artisan migrate to add Asteria customer columns.');
  23. }
  24. $this->seedRequiredData();
  25. Cache::forget('migrate_asteria_customers_last_id');
  26. $this->sqlitePath = sys_get_temp_dir().'/asteria_cmd_'.uniqid('', true).'.sqlite';
  27. touch($this->sqlitePath);
  28. config()->set('database.connections.'.$this->connection, [
  29. 'driver' => 'sqlite',
  30. 'database' => $this->sqlitePath,
  31. 'prefix' => '',
  32. 'foreign_key_constraints' => false,
  33. ]);
  34. DB::purge($this->connection);
  35. MagentoSchema::create($this->connection);
  36. }
  37. public function tearDown(): void
  38. {
  39. DB::purge($this->connection);
  40. if (isset($this->sqlitePath) && is_file($this->sqlitePath)) {
  41. @unlink($this->sqlitePath);
  42. }
  43. parent::tearDown();
  44. }
  45. public function test_it_migrates_a_customer_and_address_into_the_general_group(): void
  46. {
  47. MagentoSchema::seedCustomer($this->connection, [
  48. 'entity_id' => 10,
  49. 'email' => 'jane@example.com',
  50. 'firstname' => 'Jane',
  51. 'lastname' => 'Doe',
  52. 'telephone' => '5551112222',
  53. 'gender' => 1,
  54. 'dob' => '1990-05-01 00:00:00',
  55. 'password_hash' => md5('abcsecret12').':abc',
  56. 'group_id' => 99,
  57. 'default_billing' => 21,
  58. 'default_shipping' => 21,
  59. 'customer_source' => 20,
  60. ]);
  61. MagentoSchema::seedAddress($this->connection, [
  62. 'entity_id' => 21,
  63. 'parent_id' => 10,
  64. 'firstname' => 'Jane',
  65. 'lastname' => 'Doe',
  66. 'street' => "123 Main St\nApt 4",
  67. 'city' => 'Austin',
  68. 'region' => 'TX',
  69. 'postcode' => '78701',
  70. 'country_id' => 'US',
  71. 'telephone' => '5551112222',
  72. 'company' => 'Acme',
  73. ]);
  74. $this->artisan('customers:migrate-asteria', [
  75. '--connection' => $this->connection,
  76. '--reset-progress' => true,
  77. ])->assertSuccessful();
  78. $customer = Customer::query()->where('email', 'jane@example.com')->first();
  79. $this->assertNotNull($customer);
  80. $this->assertSame(10, (int) $customer->migrated_from_asteria_id);
  81. $this->assertSame('Jane', $customer->first_name);
  82. $this->assertSame('Doe', $customer->last_name);
  83. $this->assertSame('Male', $customer->gender);
  84. $this->assertSame('1990-05-01', $customer->date_of_birth);
  85. $this->assertSame('5551112222', $customer->phone);
  86. $this->assertSame(md5('abcsecret12').':abc', $customer->legacy_password);
  87. $this->assertSame(1, (int) $customer->is_verified);
  88. $this->assertSame(0, (int) $customer->subscribed_to_news_letter);
  89. $this->assertSame(20, (int) $customer->customer_source);
  90. $generalId = CustomerGroup::query()->where('code', 'general')->value('id');
  91. $this->assertSame((int) $generalId, (int) $customer->customer_group_id);
  92. $address = CustomerAddress::query()->where('customer_id', $customer->id)->first();
  93. $this->assertNotNull($address);
  94. $this->assertSame('123 Main St, Apt 4', $address->address);
  95. $this->assertSame('Austin', $address->city);
  96. $this->assertSame('US', $address->country);
  97. $this->assertTrue((bool) $address->default_address);
  98. $this->assertTrue((bool) $address->use_for_shipping);
  99. $this->assertSame(21, $this->asteriaAddressId($address));
  100. }
  101. public function test_dry_run_does_not_write_customers(): void
  102. {
  103. MagentoSchema::seedCustomer($this->connection, [
  104. 'entity_id' => 10,
  105. 'email' => 'dry-run@example.com',
  106. 'firstname' => 'Dry',
  107. 'lastname' => 'Run',
  108. ]);
  109. $before = Customer::query()->count();
  110. $this->artisan('customers:migrate-asteria', [
  111. '--connection' => $this->connection,
  112. '--dry-run' => true,
  113. '--reset-progress' => true,
  114. ])->assertSuccessful();
  115. $this->assertSame($before, Customer::query()->count());
  116. $this->assertNull(Customer::query()->where('email', 'dry-run@example.com')->first());
  117. }
  118. public function test_it_links_an_existing_email_without_overwriting_the_password(): void
  119. {
  120. $existing = $this->createCustomer([
  121. 'email' => 'existing@example.com',
  122. 'password' => Hash::make('bagisto-secret'),
  123. 'first_name'=> 'Keep',
  124. ]);
  125. MagentoSchema::seedCustomer($this->connection, [
  126. 'entity_id' => 44,
  127. 'email' => 'existing@example.com',
  128. 'firstname' => 'Magento',
  129. 'lastname' => 'Name',
  130. 'password_hash' => md5('abcsecret12').':abc',
  131. ]);
  132. MagentoSchema::seedAddress($this->connection, [
  133. 'entity_id' => 45,
  134. 'parent_id' => 44,
  135. 'firstname' => 'Magento',
  136. 'lastname' => 'Name',
  137. 'street' => '9 Oak Rd',
  138. 'city' => 'Dallas',
  139. 'country_id' => 'US',
  140. ]);
  141. $this->artisan('customers:migrate-asteria', [
  142. '--connection' => $this->connection,
  143. '--reset-progress' => true,
  144. ])->assertSuccessful();
  145. $existing->refresh();
  146. $this->assertSame(44, (int) $existing->migrated_from_asteria_id);
  147. $this->assertSame('Keep', $existing->first_name);
  148. $this->assertTrue(Hash::check('bagisto-secret', $existing->password));
  149. $this->assertNull($existing->legacy_password);
  150. $this->assertSame(1, Customer::query()->where('email', 'existing@example.com')->count());
  151. $this->assertSame(1, CustomerAddress::query()->where('customer_id', $existing->id)->count());
  152. }
  153. public function test_it_nulls_a_conflicting_phone_number(): void
  154. {
  155. $this->createCustomer([
  156. 'email' => 'owner@example.com',
  157. 'phone' => '5550001111',
  158. ]);
  159. MagentoSchema::seedCustomer($this->connection, [
  160. 'entity_id' => 70,
  161. 'email' => 'other@example.com',
  162. 'firstname' => 'Other',
  163. 'lastname' => 'Person',
  164. 'telephone' => '5550001111',
  165. ]);
  166. $this->artisan('customers:migrate-asteria', [
  167. '--connection' => $this->connection,
  168. '--reset-progress' => true,
  169. ])->assertSuccessful();
  170. $migrated = Customer::query()->where('email', 'other@example.com')->first();
  171. $this->assertNotNull($migrated);
  172. $this->assertNull($migrated->phone);
  173. }
  174. public function test_it_is_idempotent_on_a_second_run(): void
  175. {
  176. MagentoSchema::seedCustomer($this->connection, [
  177. 'entity_id' => 80,
  178. 'email' => 'once@example.com',
  179. 'firstname' => 'Once',
  180. 'lastname' => 'Only',
  181. ]);
  182. MagentoSchema::seedAddress($this->connection, [
  183. 'entity_id' => 81,
  184. 'parent_id' => 80,
  185. 'firstname' => 'Once',
  186. 'lastname' => 'Only',
  187. 'street' => '1 Repeat Ln',
  188. 'city' => 'Miami',
  189. 'country_id' => 'US',
  190. ]);
  191. $options = [
  192. '--connection' => $this->connection,
  193. '--reset-progress' => true,
  194. ];
  195. $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
  196. $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
  197. $this->assertSame(1, Customer::query()->where('email', 'once@example.com')->count());
  198. $customer = Customer::query()->where('email', 'once@example.com')->first();
  199. $this->assertSame(1, CustomerAddress::query()->where('customer_id', $customer->id)->count());
  200. }
  201. public function test_it_migrates_a_subscribed_customer_into_subscribers_list(): void
  202. {
  203. MagentoSchema::seedCustomer($this->connection, [
  204. 'entity_id' => 880000090,
  205. 'email' => 'asteria-nl-sub@nshop.test',
  206. 'firstname' => 'Jane',
  207. 'lastname' => 'Sub',
  208. ]);
  209. MagentoSchema::seedSubscriber($this->connection, [
  210. 'subscriber_id' => 1,
  211. 'customer_id' => 880000090,
  212. 'subscriber_email' => 'asteria-nl-sub@nshop.test',
  213. 'subscriber_status' => 1,
  214. 'subscriber_confirm_code' => 'magento-token',
  215. ]);
  216. $this->artisan('customers:migrate-asteria', [
  217. '--connection' => $this->connection,
  218. '--reset-progress' => true,
  219. ])->assertSuccessful();
  220. $customer = Customer::query()->where('email', 'asteria-nl-sub@nshop.test')->first();
  221. $this->assertNotNull($customer);
  222. $this->assertSame(1, (int) $customer->subscribed_to_news_letter);
  223. $subscription = SubscribersList::query()->where('email', 'asteria-nl-sub@nshop.test')->first();
  224. $this->assertNotNull($subscription);
  225. $this->assertSame(1, (int) $subscription->is_subscribed);
  226. $this->assertSame($customer->id, (int) $subscription->customer_id);
  227. $this->assertSame('magento-token', $subscription->token);
  228. $this->assertSame(1, SubscribersList::query()->where('email', 'asteria-nl-sub@nshop.test')->count());
  229. }
  230. public function test_it_records_an_unsubscribed_magento_customer(): void
  231. {
  232. MagentoSchema::seedCustomer($this->connection, [
  233. 'entity_id' => 880000091,
  234. 'email' => 'asteria-nl-unsub@nshop.test',
  235. 'firstname' => 'Un',
  236. 'lastname' => 'Sub',
  237. ]);
  238. MagentoSchema::seedSubscriber($this->connection, [
  239. 'subscriber_id' => 2,
  240. 'customer_id' => 880000091,
  241. 'subscriber_email' => 'asteria-nl-unsub@nshop.test',
  242. 'subscriber_status' => 3,
  243. ]);
  244. $this->artisan('customers:migrate-asteria', [
  245. '--connection' => $this->connection,
  246. '--reset-progress' => true,
  247. ])->assertSuccessful();
  248. $customer = Customer::query()->where('email', 'asteria-nl-unsub@nshop.test')->first();
  249. $this->assertNotNull($customer);
  250. $this->assertSame(0, (int) $customer->subscribed_to_news_letter);
  251. $subscription = SubscribersList::query()->where('email', 'asteria-nl-unsub@nshop.test')->first();
  252. $this->assertNotNull($subscription);
  253. $this->assertSame(0, (int) $subscription->is_subscribed);
  254. $this->assertSame($customer->id, (int) $subscription->customer_id);
  255. }
  256. public function test_it_syncs_subscription_when_linking_an_existing_customer(): void
  257. {
  258. $existing = $this->createCustomer([
  259. 'email' => 'asteria-nl-keep@nshop.test',
  260. 'first_name' => 'Keep',
  261. 'subscribed_to_news_letter' => 0,
  262. ]);
  263. MagentoSchema::seedCustomer($this->connection, [
  264. 'entity_id' => 880000092,
  265. 'email' => 'asteria-nl-keep@nshop.test',
  266. 'firstname' => 'Magento',
  267. 'lastname' => 'Name',
  268. ]);
  269. MagentoSchema::seedSubscriber($this->connection, [
  270. 'subscriber_id' => 3,
  271. 'customer_id' => 880000092,
  272. 'subscriber_email' => 'asteria-nl-keep@nshop.test',
  273. 'subscriber_status' => 1,
  274. ]);
  275. $this->artisan('customers:migrate-asteria', [
  276. '--connection' => $this->connection,
  277. '--reset-progress' => true,
  278. ])->assertSuccessful();
  279. $existing->refresh();
  280. $this->assertSame('Keep', $existing->first_name);
  281. $this->assertSame(1, (int) $existing->subscribed_to_news_letter);
  282. $this->assertSame(1, SubscribersList::query()->where('email', 'asteria-nl-keep@nshop.test')->where('is_subscribed', 1)->count());
  283. }
  284. public function test_it_backfills_subscriptions_without_rescanning_customers(): void
  285. {
  286. MagentoSchema::seedCustomer($this->connection, [
  287. 'entity_id' => 880000093,
  288. 'email' => 'asteria-nl-later@nshop.test',
  289. 'firstname' => 'Later',
  290. 'lastname' => 'Sub',
  291. ]);
  292. $this->artisan('customers:migrate-asteria', [
  293. '--connection' => $this->connection,
  294. '--reset-progress' => true,
  295. ])->assertSuccessful();
  296. $customer = Customer::query()->where('email', 'asteria-nl-later@nshop.test')->first();
  297. $this->assertNotNull($customer);
  298. $this->assertSame(0, (int) $customer->subscribed_to_news_letter);
  299. MagentoSchema::seedSubscriber($this->connection, [
  300. 'subscriber_id' => 4,
  301. 'customer_id' => 880000093,
  302. 'subscriber_email' => 'asteria-nl-later@nshop.test',
  303. 'subscriber_status' => 1,
  304. ]);
  305. $this->artisan('customers:migrate-asteria', [
  306. '--connection' => $this->connection,
  307. ])->assertSuccessful();
  308. $customer->refresh();
  309. $this->assertSame(1, (int) $customer->subscribed_to_news_letter);
  310. $this->assertSame(1, SubscribersList::query()->where('email', 'asteria-nl-later@nshop.test')->where('customer_id', $customer->id)->count());
  311. }
  312. public function test_it_imports_guest_newsletter_subscribers(): void
  313. {
  314. MagentoSchema::seedSubscriber($this->connection, [
  315. 'subscriber_id' => 5,
  316. 'customer_id' => 0,
  317. 'subscriber_email' => 'asteria-nl-guest@nshop.test',
  318. 'subscriber_status' => 1,
  319. ]);
  320. MagentoSchema::seedSubscriber($this->connection, [
  321. 'subscriber_id' => 6,
  322. 'customer_id' => 0,
  323. 'subscriber_email' => 'asteria-nl-guest-unsub@nshop.test',
  324. 'subscriber_status' => 3,
  325. ]);
  326. $this->artisan('customers:migrate-asteria', [
  327. '--connection' => $this->connection,
  328. '--reset-progress' => true,
  329. ])->assertSuccessful();
  330. $guest = SubscribersList::query()->where('email', 'asteria-nl-guest@nshop.test')->first();
  331. $this->assertNotNull($guest);
  332. $this->assertSame(1, (int) $guest->is_subscribed);
  333. $this->assertNull($guest->customer_id);
  334. $this->assertNull(SubscribersList::query()->where('email', 'asteria-nl-guest-unsub@nshop.test')->first());
  335. }
  336. public function test_it_does_not_duplicate_subscribers_on_a_second_run(): void
  337. {
  338. MagentoSchema::seedCustomer($this->connection, [
  339. 'entity_id' => 880000094,
  340. 'email' => 'asteria-nl-twice@nshop.test',
  341. 'firstname' => 'Twice',
  342. 'lastname' => 'Sub',
  343. ]);
  344. MagentoSchema::seedSubscriber($this->connection, [
  345. 'subscriber_id' => 7,
  346. 'customer_id' => 880000094,
  347. 'subscriber_email' => 'asteria-nl-twice@nshop.test',
  348. 'subscriber_status' => 1,
  349. ]);
  350. $options = [
  351. '--connection' => $this->connection,
  352. '--reset-progress' => true,
  353. ];
  354. $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
  355. $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
  356. $this->assertSame(1, SubscribersList::query()->where('email', 'asteria-nl-twice@nshop.test')->count());
  357. }
  358. public function test_it_skips_newsletter_when_the_magento_table_is_missing(): void
  359. {
  360. Schema::connection($this->connection)->drop('newsletter_subscriber');
  361. MagentoSchema::seedCustomer($this->connection, [
  362. 'entity_id' => 880000095,
  363. 'email' => 'asteria-nl-notable@nshop.test',
  364. 'firstname' => 'No',
  365. 'lastname' => 'Table',
  366. ]);
  367. $this->artisan('customers:migrate-asteria', [
  368. '--connection' => $this->connection,
  369. '--reset-progress' => true,
  370. ])->assertSuccessful();
  371. $customer = Customer::query()->where('email', 'asteria-nl-notable@nshop.test')->first();
  372. $this->assertNotNull($customer);
  373. $this->assertSame(0, (int) $customer->subscribed_to_news_letter);
  374. $this->assertSame(0, SubscribersList::query()->where('email', 'asteria-nl-notable@nshop.test')->count());
  375. }
  376. public function test_it_migrates_customer_source(): void
  377. {
  378. MagentoSchema::seedCustomer($this->connection, [
  379. 'entity_id' => 880000096,
  380. 'email' => 'asteria-src@nshop.test',
  381. 'firstname' => 'Src',
  382. 'lastname' => 'User',
  383. 'customer_source' => 22,
  384. ]);
  385. $this->artisan('customers:migrate-asteria', [
  386. '--connection' => $this->connection,
  387. '--reset-progress' => true,
  388. ])->assertSuccessful();
  389. $customer = Customer::query()->where('email', 'asteria-src@nshop.test')->first();
  390. $this->assertNotNull($customer);
  391. $this->assertSame(22, (int) $customer->customer_source);
  392. }
  393. public function test_it_backfills_customer_source_on_an_already_migrated_customer(): void
  394. {
  395. MagentoSchema::seedCustomer($this->connection, [
  396. 'entity_id' => 880000097,
  397. 'email' => 'asteria-src-later@nshop.test',
  398. 'firstname' => 'Later',
  399. 'lastname' => 'Src',
  400. ]);
  401. $this->artisan('customers:migrate-asteria', [
  402. '--connection' => $this->connection,
  403. '--reset-progress' => true,
  404. ])->assertSuccessful();
  405. $customer = Customer::query()->where('email', 'asteria-src-later@nshop.test')->first();
  406. $this->assertNotNull($customer);
  407. $this->assertNull($customer->customer_source);
  408. DB::connection($this->connection)->table('customer_entity_int')->insert([
  409. 'entity_id' => 880000097,
  410. 'attribute_id' => 89,
  411. 'value' => 15,
  412. ]);
  413. $this->artisan('customers:migrate-asteria', [
  414. '--connection' => $this->connection,
  415. '--reset-progress' => true,
  416. ])->assertSuccessful();
  417. $customer->refresh();
  418. $this->assertSame(15, (int) $customer->customer_source);
  419. }
  420. private function asteriaAddressId(CustomerAddress $address): ?int
  421. {
  422. $additional = $address->additional;
  423. if (is_string($additional) && $additional !== '') {
  424. $additional = json_decode($additional, true);
  425. }
  426. return is_array($additional) ? (int) ($additional['asteria_address_id'] ?? 0) : null;
  427. }
  428. }