View source
<?php
namespace Drupal\Tests\commerce_migrate_commerce\Kernel\Migrate\commerce1;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Tests\commerce_cart\Traits\CartManagerTestTrait;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
abstract class Commerce1TestBase extends MigrateDrupal7TestBase {
use CartManagerTestTrait;
protected static $modules = [
'action',
'address',
'commerce',
'entity',
'entity_reference_revisions',
'inline_entity_form',
'profile',
'state_machine',
'text',
'views',
'commerce_migrate',
'commerce_migrate_commerce',
];
protected function getFixtureFilePath() {
return __DIR__ . '/../../../../fixtures/ck2.php';
}
protected function migrateFields() {
$this
->installEntitySchema('commerce_store');
$this
->migrateContentTypes();
$this
->migrateCommentTypes();
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('commerce_product_variation');
$this
->installEntitySchema('profile');
$this
->executeMigration('d7_field');
$this
->executeMigrations([
'd7_taxonomy_vocabulary',
'd7_field_instance',
]);
}
protected function migrateContentTypes() {
parent::migrateContentTypes();
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('commerce_product_variation');
$this
->executeMigrations([
'commerce1_product_variation_type',
'commerce1_product_type',
]);
}
protected function migrateOrders() {
$this
->migrateOrderItems();
$this
->migrateStore();
$this
->migrateProfiles();
$this
->executeMigrations([
'commerce1_product_variation_type',
'commerce1_product_variation',
'commerce1_order_item_type',
'commerce1_order_item',
'commerce1_order',
]);
}
protected function migrateOrdersWithCart() {
$this
->migrateOrderItems();
$this
->migrateStore();
$store = \Drupal::entityTypeManager()
->getStorage('commerce_store')
->load(1);
$address = $store
->getAddress();
$address->country_code = 'NZ';
$address->address_line1 = '123 Nowhere St';
$address->locality = 'Wellington';
$store
->setAddress($address);
$store
->save();
$this
->installCommerceCart();
$this
->migrateProfiles();
$this
->executeMigrations([
'commerce1_product_variation_type',
'commerce1_product_variation',
'commerce1_order_item_type',
'commerce1_order_item',
'commerce1_order',
]);
}
protected function migrateOrderItems() {
$this
->installEntitySchema('commerce_store');
$this
->installEntitySchema('commerce_order');
$this
->installEntitySchema('commerce_order_item');
$this
->installEntitySchema('commerce_product_variation');
$this
->installEntitySchema('profile');
$this
->installConfig([
'commerce_order',
]);
$this
->migrateProducts();
$this
->installSchema('commerce_number_pattern', [
'commerce_number_pattern_sequence',
]);
$this
->installConfig([
'commerce_order',
'commerce_product',
]);
$this
->executeMigrations([
'commerce1_order_item_type',
'commerce1_order_item',
]);
}
protected function migrateProducts() {
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('path_alias');
$this
->migrateStore();
$this
->migrateProductVariations();
$this
->executeMigrations([
'commerce1_product_type',
'commerce1_product',
]);
}
protected function migrateProductVariations() {
$this
->installEntitySchema('view');
$this
->installEntitySchema('commerce_product_variation');
$this
->executeMigrations([
'commerce1_product_variation_type',
'commerce1_product_variation',
]);
}
protected function migrateProfiles() {
$this
->installEntitySchema('commerce_number_pattern');
$this
->installEntitySchema('commerce_store');
$this
->installEntitySchema('commerce_order');
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('commerce_product_variation');
$this
->installEntitySchema('profile');
$this
->installEntitySchema('view');
$this
->installConfig('commerce_order');
$this
->installConfig('commerce_product');
$this
->installConfig('profile');
$this
->migrateUsers(FALSE);
$this
->executeMigrations([
'commerce1_profile_type',
'commerce1_profile',
'commerce1_profile_revision',
]);
}
protected function migrateStore() {
$this
->installEntitySchema('commerce_store');
$this
->migrateUsers(FALSE);
$this
->executeMigrations([
'commerce1_currency',
'commerce1_store',
]);
}
protected function migrateFiles() {
$this
->installSchema('file', [
'file_usage',
]);
$this
->installEntitySchema('file');
$this->container
->get('stream_wrapper_manager')
->registerWrapper('public', PublicStream::class, StreamWrapperInterface::NORMAL);
$fs = \Drupal::service('file_system');
$fs
->mkdir('public://sites/default/files/', NULL, TRUE);
$files = $this->sourceDatabase
->select('file_managed', 'f')
->fields('f')
->execute()
->fetchAll();
foreach ($files as $file) {
$sub_dir = str_replace([
'public://',
$file->filename,
], '', $file->uri);
if ($sub_dir) {
@$fs
->mkdir('public://sites/default/files/' . $sub_dir, NULL, TRUE);
}
$filepath = str_replace('public://', 'public://sites/default/files/', $file->uri);
file_put_contents($filepath, str_repeat('*', 8));
}
$migration = $this
->getMigration('d7_file');
$source = $migration
->getSourceConfiguration();
$source['constants']['source_base_path'] = $fs
->realpath('public://');
$migration
->set('source', $source);
$this
->executeMigration($migration);
}
}