View source
<?php
namespace Drupal\Tests\commerce_migrate\Kernel;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\migrate\Kernel\MigrateTestBase;
use Drupal\commerce_product\Entity\ProductAttribute;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\migrate\MigrateException;
use Drupal\taxonomy\Entity\Vocabulary;
abstract class CsvTestBase extends MigrateTestBase {
public static $modules = [
'system',
'migrate_source_csv',
];
protected $csvPath = 'public://import';
protected $fixtures;
protected $fs;
protected function setUp() {
KernelTestBase::setUp();
$this->fs = $this->container
->get('file_system');
$this
->config('system.file')
->set('default_scheme', 'public')
->save();
$this
->loadFixture($this
->getFixtureFilePath());
}
protected function getFixtureFilePath() {
return $this->fixtures;
}
protected function loadFixture($fixtures) {
if (is_string($fixtures)) {
$fixtures = [
$fixtures,
];
}
if (!file_exists($this->csvPath)) {
$this->fs
->mkdir($this->csvPath, NULL, TRUE);
}
foreach ($fixtures as $fixture) {
$filename = basename($fixture);
$destination_uri = $this->csvPath . '/' . $filename;
$file_system = \Drupal::service('file_system');
if (!$file_system
->copy($fixture, $destination_uri)) {
throw new MigrateException("Migration setup failed to copy source CSV file '{$fixture}' to '{$destination_uri}'.");
}
}
}
protected function createAttribute(array $attributes) {
if (is_array($attributes)) {
foreach ($attributes as $attribute) {
$id = strtolower($attribute);
$id = preg_replace('/[^a-z0-9_]+/', '_', $id);
preg_replace('/_+/', '_', $id);
$field_name = 'attribute_' . $id;
$field_storage_definition = [
'field_name' => $field_name,
'entity_type' => 'commerce_product_variation',
'type' => 'entity_reference',
'cardinality' => 1,
'settings' => [
'target_type' => 'commerce_product_attribute_value',
],
];
$storage = FieldStorageConfig::create($field_storage_definition);
$storage
->save();
$field_instance = [
'field_name' => $field_name,
'entity_type' => 'commerce_product_variation',
'bundle' => 'default',
'label' => $attribute,
'settings' => [
'handler' => 'default:commerce_product_attribute_value',
'handler_settings' => [
'target_bundles' => [
$id,
],
],
],
];
$field = FieldConfig::create($field_instance);
$field
->save();
$ret = ProductAttribute::create([
'id' => strtolower($id),
'label' => $attribute,
]);
$ret
->save();
}
}
}
protected function createVocabularies(array $vids) {
if (is_array($vids)) {
foreach ($vids as $vid) {
$vocabulary = Vocabulary::create([
'name' => $vid,
'description' => $this
->randomMachineName(),
'vid' => mb_strtolower($vid),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'weight' => mt_rand(0, 10),
]);
$vocabulary
->save();
}
}
}
protected function fileMigrationSetup($source_directory) {
$this
->installSchema('file', [
'file_usage',
]);
$this
->installEntitySchema('file');
$this->container
->get('stream_wrapper_manager')
->registerWrapper('public', PublicStream::class, StreamWrapperInterface::NORMAL);
$destination = $this->csvPath . '/images';
$this
->recurseCopy($source_directory, $destination);
}
private function recurseCopy($src, $dst) {
$dir = opendir($src);
if (!file_exists($dst)) {
$this->fs
->mkdir($dst, NULL, TRUE);
}
$file_system = \Drupal::service('file_system');
while (FALSE !== ($file = readdir($dir))) {
if ($file != '.' && $file != '..') {
if (is_dir($src . '/' . $file)) {
$this
->recurseCopy($src . '/' . $file, $dst . '/' . $file);
}
else {
if (!$file_system
->copy($src . '/' . $file, $dst . '/' . $file)) {
throw new MigrateException("Migration setup failed to copy source CSV file '{$fixture}' to '{$destination_uri}'.");
}
}
}
}
closedir($dir);
}
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);
}
}