WebshopApi: Tests
authorWerner Hahn <wh@futureworldsearch.net>
Fri, 22 Sep 2017 00:21:44 +0000 (02:21 +0200)
committerWerner Hahn <wh@futureworldsearch.net>
Tue, 26 Sep 2017 10:25:02 +0000 (12:25 +0200)
t/shop/json_ok.json [new file with mode: 0644]
t/shop/shop_order.t [new file with mode: 0644]
t/shop/shopware.t [new file with mode: 0644]

diff --git a/t/shop/json_ok.json b/t/shop/json_ok.json
new file mode 100644 (file)
index 0000000..a66bcde
--- /dev/null
@@ -0,0 +1 @@
+{"data":{"id":2,"number":"20001","customerId":1,"paymentId":5,"dispatchId":9,"partnerId":"","shopId":1,"invoiceAmount":153.85,"invoiceAmountNet":129.29,"invoiceShipping":3.9,"invoiceShippingNet":3.28,"orderTime":"2017-09-07T14:15:44+0200","transactionId":"","comment":"","customerComment":"","internalComment":"","net":0,"taxFree":0,"temporaryId":"","referer":"","clearedDate":null,"trackingCode":"","languageIso":"1","currency":"EUR","currencyFactor":1,"remoteAddress":"91.36.133.94","deviceType":"desktop","details":[{"id":2,"orderId":2,"articleId":2,"taxId":1,"taxRate":19,"statusId":0,"number":"20001","articleNumber":"SW10002","price":149.95,"quantity":1,"articleName":"TITANIUM CARBON GS 120 cm","shipped":0,"shippedGroup":0,"releaseDate":"-0001-11-30T00:00:00+0100","mode":0,"esdArticle":0,"config":"","ean":"","unit":"","packUnit":"Paar","attribute":{"id":2,"orderDetailId":2,"attribute1":"","attribute2":null,"attribute3":null,"attribute4":null,"attribute5":null,"attribute6":null}}],"documents":[],"payment":{"id":5,"name":"prepayment","description":"Vorkasse","template":"prepayment.tpl","class":"prepayment.php","table":"","hide":false,"additionalDescription":"Sie zahlen einfach vorab und erhalten die Ware bequem und g\u00fcnstig bei Zahlungseingang nach Hause geliefert.","debitPercent":0,"surcharge":0,"surchargeString":"","position":1,"active":true,"esdActive":false,"mobileInactive":false,"embedIFrame":"","hideProspect":0,"action":null,"pluginId":null,"source":null,"attribute":null},"paymentStatus":{"id":17,"name":"open","description":"Offen","position":0,"group":"payment","sendMail":0},"orderStatus":{"id":0,"name":"open","description":"Offen","position":1,"group":"state","sendMail":1},"customer":{"number":"20003","id":1,"paymentId":5,"groupKey":"EK","shopId":1,"priceGroupId":null,"encoderName":"bcrypt","hashPassword":"$2y$10$.TqdM5h2qNqtJbzKF7GpP.kUXWL2UOylkgbqKBU6WuOpDladvkGva","active":true,"email":"werner.hahn@vulke.de","firstLogin":"2017-09-07T00:00:00+0200","lastLogin":"2017-09-07T14:15:44+0200","accountMode":1,"confirmationKey":"","sessionId":"10i196b4lm3r1gjdnurkq18j22","newsletter":0,"validation":"","affiliate":0,"paymentPreset":0,"languageId":"1","referer":"","internalComment":"","failedLogins":0,"lockedUntil":null,"salutation":"mr","title":null,"firstname":"Kalli","lastname":"Kr\u00fcger","birthday":null},"paymentInstances":[{"orderId":2,"firstName":"Kalli","lastName":"Kr\u00fcger","address":"Dorfstr. 6","zipCode":"37318","city":"Sch\u00f6nhagen","bankName":null,"bankCode":null,"accountNumber":null,"accountHolder":null,"bic":null,"iban":null,"amount":"153.8500","createdAt":"2017-09-07T00:00:00+0200","id":1}],"billing":{"title":null,"additionalAddressLine1":null,"additionalAddressLine2":null,"id":1,"orderId":2,"customerId":1,"countryId":2,"stateId":null,"company":"","department":"","salutation":"mr","number":"20003","firstName":"Kalli","lastName":"Kr\u00fcger","street":"Dorfstr. 6","zipCode":"37318","city":"Sch\u00f6nhagen","phone":"","vatId":null,"country":{"id":2,"name":"Deutschland","iso":"DE","isoName":"GERMANY","position":1,"description":"","taxFree":0,"taxFreeUstId":0,"taxFreeUstIdChecked":0,"active":true,"iso3":"DEU","displayStateInRegistration":false,"forceStateInRegistration":false,"areaId":1},"state":null,"attribute":{"id":1,"orderBillingId":1,"text1":null,"text2":null,"text3":null,"text4":null,"text5":null,"text6":null}},"shipping":{"title":"","additionalAddressLine1":"","additionalAddressLine2":"","id":1,"orderId":2,"countryId":2,"stateId":null,"customerId":1,"company":"","department":"","salutation":"mr","firstName":"Kalli","lastName":"Kr\u00fcger","street":"Dorfstr. 6","zipCode":"37318","city":"Sch\u00f6nhagen","attribute":{"id":1,"orderShippingId":1,"text1":null,"text2":null,"text3":null,"text4":null,"text5":null,"text6":null},"country":{"id":2,"name":"Deutschland","iso":"DE","isoName":"GERMANY","position":1,"description":"","taxFree":0,"taxFreeUstId":0,"taxFreeUstIdChecked":0,"active":true,"iso3":"DEU","displayStateInRegistration":false,"forceStateInRegistration":false,"areaId":1},"state":null},"shop":{"id":1,"mainId":null,"categoryId":3,"name":"Demoshop","title":null,"position":0,"host":"shopware.vulke.de","basePath":"","baseUrl":null,"hosts":"shopware.vulke.de","secure":false,"alwaysSecure":false,"secureHost":null,"secureBasePath":null,"templateId":23,"default":true,"active":true,"customerScope":false},"dispatch":{"id":9,"name":"Standard Versand","type":0,"description":"","comment":"","active":true,"position":0,"calculation":0,"surchargeCalculation":3,"taxCalculation":0,"shippingFree":null,"multiShopId":null,"customerGroupId":null,"bindShippingFree":0,"bindTimeFrom":null,"bindTimeTo":null,"bindInStock":null,"bindLastStock":0,"bindWeekdayFrom":null,"bindWeekdayTo":null,"bindWeightFrom":null,"bindWeightTo":null,"bindPriceFrom":null,"bindPriceTo":null,"bindSql":null,"statusLink":"","calculationSql":null,"attribute":null},"attribute":{"id":2,"orderId":2,"attribute1":null,"attribute2":null,"attribute3":null,"attribute4":null,"attribute5":null,"attribute6":null},"languageSubShop":{"id":1,"mainId":null,"categoryId":3,"name":"Demoshop","title":null,"position":0,"host":"shopware.vulke.de","basePath":"","baseUrl":null,"hosts":"shopware.vulke.de","secure":false,"alwaysSecure":false,"secureHost":null,"secureBasePath":null,"templateId":23,"default":true,"active":true,"customerScope":false,"locale":{"id":1,"locale":"de_DE","language":"Deutsch","territory":"Deutschland"}},"paymentStatusId":17,"orderStatusId":0},"success":true}
diff --git a/t/shop/shop_order.t b/t/shop/shop_order.t
new file mode 100644 (file)
index 0000000..3c70070
--- /dev/null
@@ -0,0 +1,141 @@
+use strict;
+use Test::More;
+
+use lib 't';
+use Support::TestSetup;
+use Carp;
+use Test::Exception;
+use SL::Dev::ALL;
+use SL::DB::Shop;
+use SL::DB::ShopOrder;
+use SL::DB::ShopOrderItem;
+use SL::Controller::ShopOrder;
+use Data::Dumper;
+
+my ($shop, $shop_order, $shop_part, $part, $customer, $employee);
+
+sub reset_state {
+  my %params = @_;
+
+  clear_up();
+
+  $shop = SL::Dev::Shop::new_shop->save;
+  $part = SL::Dev::Part::new_part->save;
+  $shop_part = SL::Dev::Shop::new_shop_part(part => $part, shop => $shop)->save;
+
+  $employee = SL::DB::Manager::Employee->current || croak "No employee";
+
+  $customer = SL::Dev::CustomerVendor::new_customer(
+    name    => 'Evil Inc',
+    street  => 'Evil Street',
+    zipcode => '66666',
+    email   => 'evil@evilinc.com'
+  )->save;
+}
+
+sub save_shorcontroller_to_string {
+
+  my $output;
+  open(my $outputFH, '<', \$output) or die "OUTPUT";
+  my $oldFH = select $outputFH;
+  my $shor_controller = SL::Controller::ShopOrder->new;
+  $shor_controller->action_transfer;
+
+  select $oldFH;
+  close $outputFH;
+  return $output;
+}
+sub test_transfer {
+  my ( %params ) = @_;
+  $::form = Support::TestSetup->create_new_form;
+  $::form->{import_id} = $params{import_id};
+  $::form->{customer} =  $params{customer};
+  my $test_name = 'Test Controller Action Transfer';
+  save_shorcontroller_to_string();
+  my @links_record = RecordLinks->get_links( 'from_table' => 'shop_orders',
+                                            'from_id'    => $params{import_id},
+                                            'to_table'   => 'oe',
+                                          );
+  is($links_record[0]->{from_id}    , $params{import_id}, "record from id check");
+  is($links_record[0]->{from_table} , 'shop_orders'     , "record from table <shop_orders> check");
+  is($links_record[0]->{to_table}   , 'oe'              , "record to table <oe> check");
+}
+
+Support::TestSetup::login();
+
+reset_state();
+
+my $shop_trans_id = 1;
+
+$shop_order = SL::Dev::Shop::new_shop_order(
+  shop              => $shop,
+  shop_trans_id     => $shop_trans_id,
+  amount            => 59.5,
+  billing_lastname  => 'Schmidt',
+  billing_firstname => 'Sven',
+  billing_company   => 'Evil Inc',
+  billing_street    => 'Evil Street',
+  billing_zipcode   => $customer->zipcode,
+  billing_email     => $customer->email,
+);
+
+my $shop_order_item = SL::DB::ShopOrderItem->new(
+  partnumber    => $part->partnumber,
+  position      => 1,
+  quantity      => 5,
+  price         => 10,
+  shop_trans_id => $shop_trans_id,
+);
+$shop_order->shop_order_items( [ $shop_order_item ] );
+$shop_order->save;
+
+note('testing check_for_existing_customers');
+my $fuzzy_customers = $shop_order->check_for_existing_customers;
+
+is(scalar @{ $fuzzy_customers }, 1, 'found 1 matching customer');
+is($fuzzy_customers->[0]->name, 'Evil Inc', 'matched customer Evil Inc');
+
+note('adding a not-so-similar customer');
+my $customer_different = SL::Dev::CustomerVendor::new_customer(
+  name    => "Different Name",
+  street  => 'Good Straet', # difference large enough from "Evil Street"
+  zipcode => $customer->zipcode,
+  email   => "foo",
+)->save;
+$fuzzy_customers = $shop_order->check_for_existing_customers;
+is(scalar @{ $fuzzy_customers }, 1, 'still only found 1 matching customer (zipcode equal + street dissimilar');
+
+note('adding a similar customer');
+my $customer_similar = SL::Dev::CustomerVendor::new_customer(
+  name    => "Different Name",
+  street  => 'Good Street', # difference not large enough from "Evil Street", street matches
+  zipcode => $customer->zipcode,
+  email   => "foo",
+)->save;
+$fuzzy_customers = $shop_order->check_for_existing_customers;
+is(scalar @{ $fuzzy_customers }, 2, 'found 2 matching customers (zipcode equal + street similar)');
+
+is($shop->description   , 'testshop' , 'shop description ok');
+is($shop_order->shop_id , $shop->id  , "shop_id ok");
+
+note('testing convert_to_sales_order');
+my $order = $shop_order->convert_to_sales_order(employee => $employee, customer => $customer);
+$order->calculate_prices_and_taxes;
+$order->save;
+
+is(ref($order), 'SL::DB::Order', 'order ok');
+is($order->amount,    59.5, 'order amount ok');
+is($order->netamount, 50,   'order netamount ok');
+
+test_transfer( import_id => $shop_order->id , customer => $customer->id );
+
+done_testing;
+
+clear_up();
+
+1;
+
+sub clear_up {
+  "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(OrderItem Order);
+  "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(ShopPart Part ShopOrderItem ShopOrder Shop Customer);
+}
diff --git a/t/shop/shopware.t b/t/shop/shopware.t
new file mode 100644 (file)
index 0000000..fc030a6
--- /dev/null
@@ -0,0 +1,84 @@
+use strict;
+use Test::More;
+
+use lib 't';
+use Support::TestSetup;
+use Carp;
+use Test::Exception;
+use SL::Dev::ALL;
+use SL::DB::Shop;
+use SL::DB::ShopOrder;
+use SL::DB::ShopOrderItem;
+use SL::Controller::ShopOrder;
+use SL::Shop;
+use Data::Dumper;
+use SL::JSON;
+use SL::ShopConnector::Shopware;
+my ($shop, $shopware, $shop_order, $shop_part, $part, $customer, $employee, $json_import);
+
+sub reset_state {
+  my %params = @_;
+
+  clear_up();
+
+  $shop = SL::Dev::Shop::new_shop( connector         => 'shopware',
+                                   last_order_number => 20000,
+                                   pricetype         => 'brutto',
+                                   price_source      => 'master_data',
+                                   taxzone_id        => 1,
+                                 );
+  $shopware = SL::Shop->new( config => $shop );
+  $part = SL::Dev::Part::new_part( partnumber   => 'SW10002',
+                                         description  => 'TITANIUM CARBON GS 12m cm',
+                                       );
+  $shop_part = SL::Dev::Shop::new_shop_part(part => $part, shop => $shop);
+
+  $employee = SL::DB::Manager::Employee->current || croak "No employee";
+
+  $customer = SL::Dev::CustomerVendor::new_customer(
+    name    => 'Evil Inc',
+    street  => 'Evil Street',
+    zipcode => '66666',
+    email   => 'evil@evilinc.com'
+  )->save;
+
+}
+
+sub get_json {
+  local $/;
+  my $file = "t/shop/json_ok.json";
+  my $json_text = do {
+    open(my $json_fh, "<:encoding(UTF-8)", $file)
+         or die("Can't open \"$file\": $!\n");
+    local $/;
+    <$json_fh>
+  };
+
+  return $json_text;
+}
+
+sub test_import {
+
+  my $json_import = get_json();
+  note('testing shoporder mapping json good');
+  my $import = SL::JSON::decode_json($json_import);
+  $shop_order = $shopware->connector->import_data_to_shop_order($import);
+  is($shop_order->shop_id , $shop->id  , "shop_id ok");
+}
+
+Support::TestSetup::login();
+
+reset_state();
+
+test_import();
+
+done_testing;
+
+clear_up();
+
+1;
+
+sub clear_up {
+  "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(OrderItem Order);
+  "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(ShopPart Part ShopOrderItem ShopOrder Shop Customer);
+}