View source
<?php
namespace Drupal\Tests\commerce_migrate\Kernel;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\address\AddressInterface;
use Drupal\address\Plugin\Field\FieldType\AddressItem;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_order\Entity\OrderItemType;
use Drupal\commerce_price\Entity\Currency;
use Drupal\commerce_price\Entity\CurrencyInterface;
use Drupal\commerce_price\Price;
use Drupal\commerce_payment\Entity\Payment;
use Drupal\commerce_payment\Entity\PaymentGateway;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductType;
use Drupal\commerce_product\Entity\ProductAttribute;
use Drupal\commerce_product\Entity\ProductAttributeValue;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\commerce_shipping\Entity\ShippingMethod;
use Drupal\commerce_store\Entity\Store;
use Drupal\commerce_tax\Entity\TaxType;
use Drupal\profile\Entity\Profile;
use Drupal\profile\Entity\ProfileType;
use Drupal\migrate\MigrateExecutable;
trait CommerceMigrateTestTrait {
public function assertAddressField(array $address, $country_code, $administrative_area, $locality, $dependent_locality, $postal_code, $sorting_code, $address_line_1, $address_line_2, $given_name, $additional_name, $family_name, $organization) {
$this
->assertSame($country_code, $address['country_code']);
$this
->assertSame($administrative_area, $address['administrative_area']);
$this
->assertSame($locality, $address['locality']);
$this
->assertSame($dependent_locality, $address['dependent_locality']);
$this
->assertSame($postal_code, $address['postal_code']);
$this
->assertSame($sorting_code, $address['sorting_code']);
$this
->assertSame($address_line_1, $address['address_line1']);
$this
->assertSame($address_line_2, $address['address_line2']);
$this
->assertSame($given_name, $address['given_name']);
$this
->assertSame($additional_name, $address['additional_name']);
$this
->assertSame($family_name, $address['family_name']);
$this
->assertSame($organization, $address['organization']);
}
public function assertAddressItem(AddressInterface $address, $country, $administrative_area, $locality, $dependent_locality, $postal_code, $sorting_code, $address_line_1, $address_line_2, $given_name, $additional_name, $family_name, $organization) {
$this
->assertInstanceOf(AddressItem::class, $address);
$this
->assertSame($country, $address
->getCountryCode());
$this
->assertSame($administrative_area, $address
->getAdministrativeArea());
$this
->assertSame($locality, $address
->getLocality());
$this
->assertSame($dependent_locality, $address
->getDependentLocality());
$this
->assertSame($postal_code, $address
->getPostalCode());
$this
->assertSame($sorting_code, $address
->getSortingCode());
$this
->assertSame($address_line_1, $address
->getAddressLine1());
$this
->assertSame($address_line_2, $address
->getAddressLine2());
$this
->assertSame($given_name, $address
->getGivenName());
$this
->assertSame($additional_name, $address
->getAdditionalName());
$this
->assertSame($family_name, $address
->getFamilyName());
$this
->assertSame($organization, $address
->getOrganization());
}
public function assertAdjustment(Adjustment $expected, Adjustment $actual) {
$this
->assertSame($expected
->getLabel(), $actual
->getLabel());
$this
->assertSame($expected
->getPercentage(), $actual
->getPercentage());
$this
->assertSame($expected
->getSourceId(), $actual
->getSourceId());
$this
->assertSame($expected
->getType(), $actual
->getType());
$this
->assertPrice($expected
->getAmount(), $actual
->getAmount());
}
public function assertAdjustments(array $expected_adjustments, array $actual_adjustments) {
$this
->assertSame(count($expected_adjustments), count($actual_adjustments));
$i = 0;
foreach ($expected_adjustments as $expected) {
foreach ($actual_adjustments as $actual) {
if ($expected
->getLabel() === $actual
->getLabel() && $expected
->getType() === $actual
->getType()) {
$this
->assertAdjustment($expected, $actual);
$i++;
break;
}
}
}
$this
->assertSame(count($actual_adjustments), $i);
}
public function assertCurrencyEntity($id, $currency_code, $name, $numeric_code, $fraction_digits, $symbol) {
$currency = Currency::load($id);
$this
->assertInstanceOf(CurrencyInterface::class, $currency);
$this
->assertSame($currency_code, $currency
->getCurrencyCode());
$this
->assertSame($name, $currency
->getName());
$this
->assertSame($fraction_digits, $currency
->getFractionDigits());
$this
->assertSame($numeric_code, $currency
->getNumericCode());
$this
->assertSame($symbol, $currency
->getSymbol());
}
public function assertDefaultStore() {
$defaultStore = $this->container
->get('commerce_store.default_store_resolver')
->resolve();
$this
->assertInstanceOf(Store::class, $defaultStore);
}
public function assertOrder(array $order) {
$order_instance = Order::load($order['id']);
$this
->assertInstanceOf(Order::class, $order_instance);
$this
->assertSame($order['type'], $order_instance
->bundle());
$this
->assertSame($order['number'], $order_instance
->getOrderNumber());
$this
->assertSame($order['store_id'], $order_instance
->getStoreId());
$this
->assertSame($order['created_time'], $order_instance
->getCreatedTime());
$this
->assertSame($order['changed_time'], $order_instance
->getChangedTime());
$this
->assertSame($order['completed_time'], $order_instance
->getCompletedTime());
$this
->assertSame($order['email'], $order_instance
->getEmail());
$this
->assertInstanceOf(Profile::class, $order_instance
->getBillingProfile());
$this
->assertSame($order['customer_id'], $order_instance
->getCustomerId());
$this
->assertSame($order['ip_address'], $order_instance
->getIpAddress());
$this
->assertSame($order['placed_time'], $order_instance
->getPlacedTime());
$actual_total_price = $order_instance
->getTotalPrice();
if ($actual_total_price != NULL) {
$this
->assertEquals($order['total_price_currency'], $order_instance
->getTotalPrice()
->getCurrencyCode());
$formatted_number = $this
->formatNumber($order['total_price'], $order_instance
->getTotalPrice()
->getNumber());
$this
->assertSame($formatted_number['expected'], $formatted_number['actual']);
}
$this
->assertAdjustments($order['adjustments'], $order_instance
->getAdjustments());
$this
->assertSame($order['label_value'], $order_instance
->getState()->value);
$data = $order_instance
->get('data')
->getValue();
$this
->assertSame($order['data'], $data);
$state_label = $order_instance
->getState()
->getLabel();
$label = NULL;
if (is_string($state_label)) {
$label = $state_label;
}
elseif ($state_label instanceof TranslatableMarkup) {
$arguments = $state_label
->getArguments();
$label = isset($arguments['@label']) ? $arguments['@label'] : $state_label
->render();
}
$this
->assertSame($order['label_rendered'], $label);
if (isset($order['cart'])) {
$this
->assertSame($order['cart'], $order_instance
->get('cart')->value);
}
$billing_profile = [
'target_id' => $order['billing_profile'][0],
'target_revision_id' => $order['billing_profile'][1],
];
$this
->assertSame([
$billing_profile,
], $order_instance
->get('billing_profile')
->getValue());
$actual_order_items = $order_instance
->get('order_items')
->getValue();
$actual_order_item_ids = [];
foreach ($actual_order_items as $actual_order_item) {
$actual_order_item_ids[] = $actual_order_item['target_id'];
}
sort($actual_order_item_ids);
sort($order['order_items_ids']);
$this
->assertSame($order['order_items_ids'], $actual_order_item_ids);
}
public function assertOrderItem(array $order_item) {
$actual = OrderItem::load($order_item['id']);
$this
->assertInstanceOf(OrderItem::class, $actual);
$this
->assertSame($order_item['created'], $actual
->getCreatedTime());
$this
->assertSame($order_item['changed'], $actual
->getChangedTime());
$formatted_number = $this
->formatNumber($order_item['quantity'], $actual
->getQuantity(), '%01.2f');
$this
->assertSame($formatted_number['expected'], $formatted_number['actual']);
$this
->assertEquals($order_item['title'], $actual
->getTitle());
$formatted_number = $this
->formatNumber($order_item['unit_price'], $actual
->getUnitPrice()
->getNumber());
$this
->assertSame($formatted_number['expected'], $formatted_number['actual']);
$this
->assertEquals($order_item['unit_price_currency_code'], $actual
->getUnitPrice()
->getCurrencyCode());
$formatted_number = $this
->formatNumber($order_item['total_price'], $actual
->getTotalPrice()
->getNumber());
$this
->assertSame($formatted_number['expected'], $formatted_number['actual']);
$this
->assertEquals($order_item['total_price_currency_code'], $actual
->getTotalPrice()
->getCurrencyCode());
$this
->assertEquals($order_item['purchased_entity_id'], $actual
->getPurchasedEntityId());
$this
->assertEquals($order_item['order_id'], $actual
->getOrderId());
$this
->assertSame($order_item['uses_legacy_adjustments'], $actual
->usesLegacyAdjustments());
$this
->assertAdjustments($order_item['adjustments'], $actual
->getAdjustments());
}
public function assertOrderItemType(array $expected) {
$order_item_type = OrderItemType::load($expected['id']);
$this
->assertInstanceOf(OrderItemType::class, $order_item_type);
$this
->assertSame($expected['label'], $order_item_type
->label());
$this
->assertSame($expected['purchasableEntityType'], $order_item_type
->getPurchasableEntityTypeId());
$this
->assertSame($expected['orderType'], $order_item_type
->getOrderTypeId());
}
private function assertPaymentEntity(array $payment) {
$payment_instance = Payment::load($payment['id']);
$this
->assertInstanceOf(Payment::class, $payment_instance);
$this
->assertSame($payment['order_id'], $payment_instance
->getOrderId());
$this
->assertSame($payment['type'], $payment_instance
->getType()
->getPluginId());
$this
->assertSame($payment['payment_gateway'], $payment_instance
->getPaymentGatewayId());
$this
->assertSame($payment['payment_method'], $payment_instance
->getPaymentMethodId());
$formatted_number = $this
->formatNumber($payment['amount_number'], $payment_instance
->getAmount()
->getNumber());
$this
->assertSame($formatted_number['expected'], $formatted_number['actual']);
$this
->assertSame($payment['amount_currency_code'], $payment_instance
->getAmount()
->getCurrencyCode());
$formatted_number = $this
->formatNumber($payment['balance_number'], $payment_instance
->getBalance()
->getNumber(), '%01.2f');
$this
->assertSame($formatted_number['expected'], $formatted_number['actual']);
$this
->assertSame($payment['balance_currency_code'], $payment_instance
->getBalance()
->getCurrencyCode());
$formatted_number = $this
->formatNumber($payment['refunded_amount_number'], $payment_instance
->getRefundedAmount()
->getNumber());
$this
->assertSame($formatted_number['expected'], $formatted_number['actual']);
$this
->assertSame($payment['refunded_amount_currency_code'], $payment_instance
->getRefundedAmount()
->getCurrencyCode());
$this
->assertSame($payment['label_value'], $payment_instance
->getState()->value);
$state_label = $payment_instance
->getState()
->getLabel();
$label = NULL;
if (is_string($state_label)) {
$label = $state_label;
}
elseif ($state_label instanceof TranslatableMarkup) {
$arguments = $state_label
->getArguments();
$label = isset($arguments['@label']) ? $arguments['@label'] : $state_label
->render();
}
$this
->assertSame($payment['label_rendered'], $label);
}
private function assertPaymentGatewayEntity($id, $label, $weight) {
$gateway = PaymentGateway::load($id);
$this
->assertInstanceOf(PaymentGateway::class, $gateway);
$this
->assertSame($label, $gateway
->label());
$this
->assertSame($weight, $gateway
->getWeight());
}
public function assertPrice(Price $expected, Price $actual) {
$formatted_number = $this
->formatNumber($expected
->getNumber(), $actual
->getNumber());
$this
->assertSame($formatted_number['expected'], $formatted_number['actual']);
$this
->assertSame($expected
->getCurrencyCode(), $actual
->getCurrencyCode());
}
protected function assertProductAttributeEntity($id, $label, $element_type) {
$attribute = ProductAttribute::load($id);
$this
->assertInstanceOf(ProductAttribute::class, $attribute);
$this
->assertSame($label, $attribute
->label());
$this
->assertSame($element_type, $attribute
->getElementType());
}
protected function assertProductAttributeValueEntity($id, $attribute_id, $name, $label, $weight) {
$attribute_value = ProductAttributeValue::load($id);
$this
->assertInstanceOf(ProductAttributeValue::class, $attribute_value);
$this
->assertSame($attribute_id, $attribute_value
->getAttributeId());
$this
->assertSame($name, $attribute_value
->getName());
$this
->assertSame($label, $attribute_value
->label());
$this
->assertSame($weight, $attribute_value
->getWeight());
}
public function assertProductEntity($id, $type, $owner_id, $title, $is_published, array $store_ids, array $variations) {
$product = Product::load($id);
$this
->assertInstanceOf(Product::class, $product);
$this
->assertSame($type, $product
->bundle());
$this
->assertSame($owner_id, $product
->getOwnerId());
$this
->assertSame($title, $product
->getTitle());
$this
->assertSame($is_published, $product
->isPublished());
$this
->assertSame($store_ids, $product
->getStoreIds());
$actual_variations = $product
->getVariationIds();
$this
->assertSame(asort($variations), asort($actual_variations));
}
public function assertProductTypeEntity($id, $label, $description, $variation_type_id) {
$product_type = ProductType::load($id);
$this
->assertInstanceOf(ProductType::class, $product_type);
$entity_field_manager = \Drupal::service('entity_field.manager');
$field_definitions = $entity_field_manager
->getFieldDefinitions('commerce_product', $id);
$this
->assertArrayHasKey('stores', $field_definitions);
$this
->assertArrayHasKey('body', $field_definitions);
$this
->assertArrayHasKey('variations', $field_definitions);
$this
->assertSame($label, $product_type
->label());
$this
->assertSame($description, $product_type
->getDescription());
$this
->assertSame($variation_type_id, $product_type
->getVariationTypeId());
}
public function assertProductVariationEntity(array $product_variation) {
$variation = ProductVariation::load($product_variation['id']);
$this
->assertInstanceOf(ProductVariation::class, $variation);
$this
->assertSame($product_variation['type'], $variation
->bundle());
$this
->assertSame($product_variation['uid'], $variation
->getOwnerId());
$this
->assertSame($product_variation['sku'], $variation
->getSku());
$formatted_number = $this
->formatNumber($product_variation['price'], $variation
->getPrice()
->getNumber());
$this
->assertSame($formatted_number['expected'], $formatted_number['actual']);
$this
->assertSame($product_variation['currency'], $variation
->getPrice()
->getCurrencyCode());
$this
->assertSame($product_variation['product_id'], $variation
->getProductId());
$this
->assertSame($product_variation['title'], $variation
->getOrderItemTitle());
$this
->assertSame($product_variation['order_item_type_id'], $variation
->getOrderItemTypeId());
if ($product_variation['created_time'] != NULL) {
$this
->assertSame($product_variation['created_time'], $variation
->getCreatedTime());
}
if ($product_variation['changed_time'] != NULL) {
$this
->assertSame($product_variation['changed_time'], $variation
->getChangedTime());
}
}
public function assertProductVariationTypeEntity($id, $label, $order_item_type_id, $is_title_generated, array $traits) {
$variation_type = ProductVariationType::load($id);
$this
->assertInstanceOf(ProductVariationType::class, $variation_type);
$this
->assertSame($label, $variation_type
->label());
$this
->assertSame($order_item_type_id, $variation_type
->getOrderItemTypeId());
$this
->assertSame($is_title_generated, $variation_type
->shouldGenerateTitle());
$this
->assertSame($traits, $variation_type
->getTraits());
}
public function assertProfile($id, $type, $owner_id, $langcode, $is_active, $is_default, $created_time, $changed_time) {
$profile = Profile::load($id);
$this
->assertProfileEntity($profile, $type, $owner_id, $langcode, $is_active, $is_default, $created_time, $changed_time);
}
public function assertProfileEntity($profile, $type, $owner_id, $langcode, $is_active, $is_default, $created_time, $changed_time) {
$this
->assertInstanceOf(Profile::class, $profile);
$this
->assertSame($type, $profile
->bundle());
$this
->assertSame($owner_id, $profile
->getOwnerId());
$this
->assertSame($langcode, $profile
->language()
->getId());
$this
->assertSame($is_active, $profile
->isPublished());
$this
->assertSame($is_default, $profile
->isDefault());
if ($created_time != NULL) {
$this
->assertSame($created_time, $profile
->getCreatedTime());
}
if ($changed_time != NULL) {
$this
->assertSame($changed_time, $profile
->getChangedTime());
}
}
public function assertProfileRevision($id, $type, $owner_id, $langcode, $is_active, $is_default, $created_time, $changed_time) {
$revision = \Drupal::entityTypeManager()
->getStorage('profile')
->loadRevision($id);
$this
->assertProfileEntity($revision, $type, $owner_id, $langcode, $is_active, $is_default, $created_time, $changed_time);
}
public function assertProfileType($id, $label, $multiple, $revisions) {
$profile_type = ProfileType::load($id);
$this
->assertInstanceOf(ProfileType::class, $profile_type);
$this
->assertSame($label, $profile_type
->label());
$this
->assertSame($multiple, $profile_type
->allowsMultiple());
$this
->assertSame($revisions, $profile_type
->shouldCreateNewRevision());
}
public function assertShippingMethod(array $shipping_method) {
$shipping_method_instance = ShippingMethod::load($shipping_method['id']);
$this
->assertInstanceOf(ShippingMethod::class, $shipping_method_instance);
$plugin = $shipping_method_instance
->getPlugin();
$this
->assertSame($shipping_method['label'], $shipping_method_instance
->label());
$this
->assertSame($shipping_method['stores'], $shipping_method_instance
->getStoreIds());
$rate_amount = [
'number' => $shipping_method['rate_amount']['number'],
'currency_code' => $shipping_method['rate_amount']['currency_code'],
];
$this
->assertEquals($rate_amount, $plugin
->getConfiguration()['rate_amount']);
}
public function assertStoreEntity($id, $name, $email, $default_currency_code, $bundle, $owner_id, $default = NULL) {
$store = Store::load($id);
$this
->assertInstanceOf(Store::class, $store);
$this
->assertSame($name, $store
->getName());
$this
->assertSame($email, $store
->getEmail());
$this
->assertSame($default_currency_code, $store
->getDefaultCurrencyCode());
$this
->assertSame($bundle, $store
->bundle());
$this
->assertSame($owner_id, $store
->getOwnerId());
if ($default) {
$this
->assertSame($default, $store
->isDefault());
}
}
public function assertTaxType($id, $label, $plugin, $rate, array $territories) {
$tax_type = TaxType::load($id);
$this
->assertInstanceOf(TaxType::class, $tax_type);
$this
->assertSame($label, $tax_type
->label());
$this
->assertSame($plugin, $tax_type
->getPluginId());
$tax_type_config = $tax_type
->getPluginConfiguration();
$this
->assertSame($id, $tax_type_config['rates'][0]['id']);
$this
->assertSame($label, $tax_type_config['rates'][0]['label']);
$this
->assertSame($rate, $tax_type_config['rates'][0]['percentage']);
$this
->assertSame($territories, $tax_type_config['territories']);
}
public function assertUbercartOrder(array $order) {
$this
->assertOrder($order);
$order_instance = Order::load($order['id']);
if (isset($order['order_admin_comments'])) {
$this
->assertSame($order['order_admin_comments'], $order_instance
->get('field_order_admin_comments')
->getValue());
}
if (isset($order['order_comments'])) {
$this
->assertSame($order['order_comments'], $order_instance
->get('field_order_comments')
->getValue());
}
if (isset($order['order_logs'])) {
$this
->assertSame($order['order_logs'], $order_instance
->get('field_order_logs')
->getValue());
}
}
protected function createDefaultStore() {
$currency_importer = \Drupal::service('commerce_price.currency_importer');
$store_storage = \Drupal::service('entity_type.manager')
->getStorage('commerce_store');
$currency_importer
->import('USD');
$store_values = [
'type' => 'default',
'uid' => 1,
'name' => 'Demo store',
'mail' => 'admin@example.com',
'address' => [
'country_code' => 'US',
],
'default_currency' => 'USD',
];
$store = $store_storage
->create($store_values);
$store
->save();
$store_storage
->markAsDefault($store);
}
public function productTest(array $product) {
$variation_ids = [];
foreach ($product['variations'] as $variation) {
$variation_ids[] = $variation['variation_id'];
}
$this
->assertProductEntity($product['product_id'], $product['type'], $product['uid'], $product['title'], $product['published'], $product['store_ids'], $variation_ids);
$this
->productVariationTest($product);
}
public function productVariationTest(array $product) {
$productInstance = Product::load($product['product_id']);
foreach ($product['variations'] as $variation) {
$found = FALSE;
foreach ($productInstance->variations as $variationInstance) {
if ($variation['variation_id'] == $variationInstance->target_id) {
$found = TRUE;
}
}
$this
->assertTrue($found, "No variation exists for variation_id: {$variation['variation_id']}");
$this
->assertProductVariationEntity($variation['variation_id'], $variation['uid'], $variation['sku'], $variation['price'], $variation['currency'], $product['product_id'], $variation['title'], $variation['order_item_type'], $variation['created_time'], $variation['changed_time']);
}
}
public function formatNumber($expected, $actual, $format_string = '%01.6f') {
$ret['expected'] = $expected;
$ret['actual'] = $actual;
if ($this->container
->get('database')
->driver() === 'sqlite') {
$ret['expected'] = sprintf($format_string, $expected);
$ret['actual'] = sprintf($format_string, $actual);
}
return $ret;
}
protected function executeRollback($migration) {
if (is_string($migration)) {
$this->migration = $this
->getMigration($migration);
}
else {
$this->migration = $migration;
}
(new MigrateExecutable($this->migration, $this))
->rollback();
}
protected function executeRollbacks(array $ids) {
$manager = $this->container
->get('plugin.manager.migration');
array_walk($ids, function ($id) use ($manager) {
$instances = $manager
->createInstances($id);
$this
->assertNotEmpty($instances, sprintf("No migrations created for id '%s'.", $id));
array_walk($instances, [
$this,
'executeRollback',
]);
});
}
}