| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- <?php
- namespace Webkul\BagistoApi\Tests\Unit\Migration;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Schema;
- use Webkul\BagistoApi\Tests\BagistoApiTestCase;
- use Webkul\Core\Models\SubscribersList;
- use Webkul\Customer\Models\Customer;
- use Webkul\Customer\Models\CustomerAddress;
- use Webkul\Customer\Models\CustomerGroup;
- class MigrateAsteriaCustomersCommandTest extends BagistoApiTestCase
- {
- private string $sqlitePath;
- private string $connection = 'asteria_test';
- public function setUp(): void
- {
- parent::setUp();
- if (! Schema::hasColumn('customers', 'migrated_from_asteria_id')
- || ! Schema::hasColumn('customers', 'legacy_password')
- || ! Schema::hasColumn('customers', 'customer_source')) {
- $this->markTestSkipped('Run php artisan migrate to add Asteria customer columns.');
- }
- $this->seedRequiredData();
- Cache::forget('migrate_asteria_customers_last_id');
- $this->sqlitePath = sys_get_temp_dir().'/asteria_cmd_'.uniqid('', true).'.sqlite';
- touch($this->sqlitePath);
- config()->set('database.connections.'.$this->connection, [
- 'driver' => 'sqlite',
- 'database' => $this->sqlitePath,
- 'prefix' => '',
- 'foreign_key_constraints' => false,
- ]);
- DB::purge($this->connection);
- MagentoSchema::create($this->connection);
- }
- public function tearDown(): void
- {
- DB::purge($this->connection);
- if (isset($this->sqlitePath) && is_file($this->sqlitePath)) {
- @unlink($this->sqlitePath);
- }
- parent::tearDown();
- }
- public function test_it_migrates_a_customer_and_address_into_the_general_group(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 10,
- 'email' => 'jane@example.com',
- 'firstname' => 'Jane',
- 'lastname' => 'Doe',
- 'telephone' => '5551112222',
- 'gender' => 1,
- 'dob' => '1990-05-01 00:00:00',
- 'password_hash' => md5('abcsecret12').':abc',
- 'group_id' => 99,
- 'default_billing' => 21,
- 'default_shipping' => 21,
- 'customer_source' => 20,
- ]);
- MagentoSchema::seedAddress($this->connection, [
- 'entity_id' => 21,
- 'parent_id' => 10,
- 'firstname' => 'Jane',
- 'lastname' => 'Doe',
- 'street' => "123 Main St\nApt 4",
- 'city' => 'Austin',
- 'region' => 'TX',
- 'postcode' => '78701',
- 'country_id' => 'US',
- 'telephone' => '5551112222',
- 'company' => 'Acme',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $customer = Customer::query()->where('email', 'jane@example.com')->first();
- $this->assertNotNull($customer);
- $this->assertSame(10, (int) $customer->migrated_from_asteria_id);
- $this->assertSame('Jane', $customer->first_name);
- $this->assertSame('Doe', $customer->last_name);
- $this->assertSame('Male', $customer->gender);
- $this->assertSame('1990-05-01', $customer->date_of_birth);
- $this->assertSame('5551112222', $customer->phone);
- $this->assertSame(md5('abcsecret12').':abc', $customer->legacy_password);
- $this->assertSame(1, (int) $customer->is_verified);
- $this->assertSame(0, (int) $customer->subscribed_to_news_letter);
- $this->assertSame(20, (int) $customer->customer_source);
- $generalId = CustomerGroup::query()->where('code', 'general')->value('id');
- $this->assertSame((int) $generalId, (int) $customer->customer_group_id);
- $address = CustomerAddress::query()->where('customer_id', $customer->id)->first();
- $this->assertNotNull($address);
- $this->assertSame('123 Main St, Apt 4', $address->address);
- $this->assertSame('Austin', $address->city);
- $this->assertSame('US', $address->country);
- $this->assertTrue((bool) $address->default_address);
- $this->assertTrue((bool) $address->use_for_shipping);
- $this->assertSame(21, $this->asteriaAddressId($address));
- }
- public function test_dry_run_does_not_write_customers(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 10,
- 'email' => 'dry-run@example.com',
- 'firstname' => 'Dry',
- 'lastname' => 'Run',
- ]);
- $before = Customer::query()->count();
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--dry-run' => true,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $this->assertSame($before, Customer::query()->count());
- $this->assertNull(Customer::query()->where('email', 'dry-run@example.com')->first());
- }
- public function test_it_links_an_existing_email_without_overwriting_the_password(): void
- {
- $existing = $this->createCustomer([
- 'email' => 'existing@example.com',
- 'password' => Hash::make('bagisto-secret'),
- 'first_name'=> 'Keep',
- ]);
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 44,
- 'email' => 'existing@example.com',
- 'firstname' => 'Magento',
- 'lastname' => 'Name',
- 'password_hash' => md5('abcsecret12').':abc',
- ]);
- MagentoSchema::seedAddress($this->connection, [
- 'entity_id' => 45,
- 'parent_id' => 44,
- 'firstname' => 'Magento',
- 'lastname' => 'Name',
- 'street' => '9 Oak Rd',
- 'city' => 'Dallas',
- 'country_id' => 'US',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $existing->refresh();
- $this->assertSame(44, (int) $existing->migrated_from_asteria_id);
- $this->assertSame('Keep', $existing->first_name);
- $this->assertTrue(Hash::check('bagisto-secret', $existing->password));
- $this->assertNull($existing->legacy_password);
- $this->assertSame(1, Customer::query()->where('email', 'existing@example.com')->count());
- $this->assertSame(1, CustomerAddress::query()->where('customer_id', $existing->id)->count());
- }
- public function test_it_nulls_a_conflicting_phone_number(): void
- {
- $this->createCustomer([
- 'email' => 'owner@example.com',
- 'phone' => '5550001111',
- ]);
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 70,
- 'email' => 'other@example.com',
- 'firstname' => 'Other',
- 'lastname' => 'Person',
- 'telephone' => '5550001111',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $migrated = Customer::query()->where('email', 'other@example.com')->first();
- $this->assertNotNull($migrated);
- $this->assertNull($migrated->phone);
- }
- public function test_it_is_idempotent_on_a_second_run(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 80,
- 'email' => 'once@example.com',
- 'firstname' => 'Once',
- 'lastname' => 'Only',
- ]);
- MagentoSchema::seedAddress($this->connection, [
- 'entity_id' => 81,
- 'parent_id' => 80,
- 'firstname' => 'Once',
- 'lastname' => 'Only',
- 'street' => '1 Repeat Ln',
- 'city' => 'Miami',
- 'country_id' => 'US',
- ]);
- $options = [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ];
- $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
- $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
- $this->assertSame(1, Customer::query()->where('email', 'once@example.com')->count());
- $customer = Customer::query()->where('email', 'once@example.com')->first();
- $this->assertSame(1, CustomerAddress::query()->where('customer_id', $customer->id)->count());
- }
- public function test_it_migrates_a_subscribed_customer_into_subscribers_list(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 880000090,
- 'email' => 'asteria-nl-sub@nshop.test',
- 'firstname' => 'Jane',
- 'lastname' => 'Sub',
- ]);
- MagentoSchema::seedSubscriber($this->connection, [
- 'subscriber_id' => 1,
- 'customer_id' => 880000090,
- 'subscriber_email' => 'asteria-nl-sub@nshop.test',
- 'subscriber_status' => 1,
- 'subscriber_confirm_code' => 'magento-token',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $customer = Customer::query()->where('email', 'asteria-nl-sub@nshop.test')->first();
- $this->assertNotNull($customer);
- $this->assertSame(1, (int) $customer->subscribed_to_news_letter);
- $subscription = SubscribersList::query()->where('email', 'asteria-nl-sub@nshop.test')->first();
- $this->assertNotNull($subscription);
- $this->assertSame(1, (int) $subscription->is_subscribed);
- $this->assertSame($customer->id, (int) $subscription->customer_id);
- $this->assertSame('magento-token', $subscription->token);
- $this->assertSame(1, SubscribersList::query()->where('email', 'asteria-nl-sub@nshop.test')->count());
- }
- public function test_it_records_an_unsubscribed_magento_customer(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 880000091,
- 'email' => 'asteria-nl-unsub@nshop.test',
- 'firstname' => 'Un',
- 'lastname' => 'Sub',
- ]);
- MagentoSchema::seedSubscriber($this->connection, [
- 'subscriber_id' => 2,
- 'customer_id' => 880000091,
- 'subscriber_email' => 'asteria-nl-unsub@nshop.test',
- 'subscriber_status' => 3,
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $customer = Customer::query()->where('email', 'asteria-nl-unsub@nshop.test')->first();
- $this->assertNotNull($customer);
- $this->assertSame(0, (int) $customer->subscribed_to_news_letter);
- $subscription = SubscribersList::query()->where('email', 'asteria-nl-unsub@nshop.test')->first();
- $this->assertNotNull($subscription);
- $this->assertSame(0, (int) $subscription->is_subscribed);
- $this->assertSame($customer->id, (int) $subscription->customer_id);
- }
- public function test_it_syncs_subscription_when_linking_an_existing_customer(): void
- {
- $existing = $this->createCustomer([
- 'email' => 'asteria-nl-keep@nshop.test',
- 'first_name' => 'Keep',
- 'subscribed_to_news_letter' => 0,
- ]);
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 880000092,
- 'email' => 'asteria-nl-keep@nshop.test',
- 'firstname' => 'Magento',
- 'lastname' => 'Name',
- ]);
- MagentoSchema::seedSubscriber($this->connection, [
- 'subscriber_id' => 3,
- 'customer_id' => 880000092,
- 'subscriber_email' => 'asteria-nl-keep@nshop.test',
- 'subscriber_status' => 1,
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $existing->refresh();
- $this->assertSame('Keep', $existing->first_name);
- $this->assertSame(1, (int) $existing->subscribed_to_news_letter);
- $this->assertSame(1, SubscribersList::query()->where('email', 'asteria-nl-keep@nshop.test')->where('is_subscribed', 1)->count());
- }
- public function test_it_backfills_subscriptions_without_rescanning_customers(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 880000093,
- 'email' => 'asteria-nl-later@nshop.test',
- 'firstname' => 'Later',
- 'lastname' => 'Sub',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $customer = Customer::query()->where('email', 'asteria-nl-later@nshop.test')->first();
- $this->assertNotNull($customer);
- $this->assertSame(0, (int) $customer->subscribed_to_news_letter);
- MagentoSchema::seedSubscriber($this->connection, [
- 'subscriber_id' => 4,
- 'customer_id' => 880000093,
- 'subscriber_email' => 'asteria-nl-later@nshop.test',
- 'subscriber_status' => 1,
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- ])->assertSuccessful();
- $customer->refresh();
- $this->assertSame(1, (int) $customer->subscribed_to_news_letter);
- $this->assertSame(1, SubscribersList::query()->where('email', 'asteria-nl-later@nshop.test')->where('customer_id', $customer->id)->count());
- }
- public function test_it_imports_guest_newsletter_subscribers(): void
- {
- MagentoSchema::seedSubscriber($this->connection, [
- 'subscriber_id' => 5,
- 'customer_id' => 0,
- 'subscriber_email' => 'asteria-nl-guest@nshop.test',
- 'subscriber_status' => 1,
- ]);
- MagentoSchema::seedSubscriber($this->connection, [
- 'subscriber_id' => 6,
- 'customer_id' => 0,
- 'subscriber_email' => 'asteria-nl-guest-unsub@nshop.test',
- 'subscriber_status' => 3,
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $guest = SubscribersList::query()->where('email', 'asteria-nl-guest@nshop.test')->first();
- $this->assertNotNull($guest);
- $this->assertSame(1, (int) $guest->is_subscribed);
- $this->assertNull($guest->customer_id);
- $this->assertNull(SubscribersList::query()->where('email', 'asteria-nl-guest-unsub@nshop.test')->first());
- }
- public function test_it_does_not_duplicate_subscribers_on_a_second_run(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 880000094,
- 'email' => 'asteria-nl-twice@nshop.test',
- 'firstname' => 'Twice',
- 'lastname' => 'Sub',
- ]);
- MagentoSchema::seedSubscriber($this->connection, [
- 'subscriber_id' => 7,
- 'customer_id' => 880000094,
- 'subscriber_email' => 'asteria-nl-twice@nshop.test',
- 'subscriber_status' => 1,
- ]);
- $options = [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ];
- $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
- $this->artisan('customers:migrate-asteria', $options)->assertSuccessful();
- $this->assertSame(1, SubscribersList::query()->where('email', 'asteria-nl-twice@nshop.test')->count());
- }
- public function test_it_skips_newsletter_when_the_magento_table_is_missing(): void
- {
- Schema::connection($this->connection)->drop('newsletter_subscriber');
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 880000095,
- 'email' => 'asteria-nl-notable@nshop.test',
- 'firstname' => 'No',
- 'lastname' => 'Table',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $customer = Customer::query()->where('email', 'asteria-nl-notable@nshop.test')->first();
- $this->assertNotNull($customer);
- $this->assertSame(0, (int) $customer->subscribed_to_news_letter);
- $this->assertSame(0, SubscribersList::query()->where('email', 'asteria-nl-notable@nshop.test')->count());
- }
- public function test_it_migrates_customer_source(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 880000096,
- 'email' => 'asteria-src@nshop.test',
- 'firstname' => 'Src',
- 'lastname' => 'User',
- 'customer_source' => 22,
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $customer = Customer::query()->where('email', 'asteria-src@nshop.test')->first();
- $this->assertNotNull($customer);
- $this->assertSame(22, (int) $customer->customer_source);
- }
- public function test_it_backfills_customer_source_on_an_already_migrated_customer(): void
- {
- MagentoSchema::seedCustomer($this->connection, [
- 'entity_id' => 880000097,
- 'email' => 'asteria-src-later@nshop.test',
- 'firstname' => 'Later',
- 'lastname' => 'Src',
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $customer = Customer::query()->where('email', 'asteria-src-later@nshop.test')->first();
- $this->assertNotNull($customer);
- $this->assertNull($customer->customer_source);
- DB::connection($this->connection)->table('customer_entity_int')->insert([
- 'entity_id' => 880000097,
- 'attribute_id' => 89,
- 'value' => 15,
- ]);
- $this->artisan('customers:migrate-asteria', [
- '--connection' => $this->connection,
- '--reset-progress' => true,
- ])->assertSuccessful();
- $customer->refresh();
- $this->assertSame(15, (int) $customer->customer_source);
- }
- private function asteriaAddressId(CustomerAddress $address): ?int
- {
- $additional = $address->additional;
- if (is_string($additional) && $additional !== '') {
- $additional = json_decode($additional, true);
- }
- return is_array($additional) ? (int) ($additional['asteria_address_id'] ?? 0) : null;
- }
- }
|