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, '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', '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; } }