View source
<?php
namespace Drupal\Tests\recipe\Kernel\Migrate\recipe61;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\recipe\Plugin\migrate\source\recipe61\Recipe61;
class MigrateRecipe61Test extends MigrateRecipe61TestBase {
protected static $modules = [
'content_translation',
'ingredient',
'language',
'menu_ui',
'node',
'rdf',
'recipe',
'text',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('ingredient');
$this
->installEntitySchema('node');
$this
->installConfig(static::$modules);
$this
->installSchema('node', [
'node_access',
]);
$this
->executeMigrations([
'language',
'd6_filter_format',
'd6_user_role',
'd6_user',
'd6_node_type',
'd6_node_settings',
'd6_node',
'd6_node_translation',
'recipe1x_ingredient',
'recipe61_recipe',
'recipe61_recipe_translation',
]);
}
protected function assertRecipeFields(NodeInterface $node, \stdClass $recipe) {
$this
->assertSame($recipe->instructions, $node->recipe_instructions->value);
$this
->assertSame($recipe->notes, $node->recipe_notes->value);
$this
->assertEquals($recipe->preptime, $node->recipe_prep_time->value);
$this
->assertSame($recipe->source, $node->recipe_source->value);
$this
->assertEquals($recipe->yield, $node->recipe_yield_amount->value);
$this
->assertSame($recipe->yield_unit, $node->recipe_yield_unit->value);
for ($i = 0; $i < count($recipe->ingredients); $i++) {
$this
->assertEquals($recipe->ingredients[$i]->quantity, $node->recipe_ingredient[$i]->quantity);
$this
->assertEquals($recipe->ingredients[$i]->unit_key, $node->recipe_ingredient[$i]->unit_key);
$this
->assertEquals($recipe->ingredients[$i]->ingredient_id, $node->recipe_ingredient[$i]->target_id);
$this
->assertEquals($recipe->ingredients[$i]->note, $node->recipe_ingredient[$i]->note);
}
}
public function testRecipeFields() {
$database_connection = Database::getConnection('default', 'migrate');
$recipes = $database_connection
->select('recipe', 'r')
->fields('r')
->execute()
->fetchAll();
foreach ($recipes as &$source) {
$query = $database_connection
->select('recipe_node_ingredient', 'i');
$query
->leftJoin('recipe_unit', 'ru', 'i.unit_id = ru.id');
$query
->fields('i', [
'quantity',
'ingredient_id',
'weight',
'note',
])
->fields('ru', [
'name',
])
->condition('i.nid', $source->nid);
$results = $query
->execute()
->fetchAll();
$source->ingredients = $this
->addUnitKeys($results);
}
$node = Node::load(1);
$this
->assertRecipeFields($node, array_shift($recipes));
$node = Node::load(2);
$this
->assertRecipeFields($node, array_shift($recipes));
$this
->assertTrue($node
->hasTranslation('is'), 'Node 2 has an Islandic translation');
$translation = $node
->getTranslation('is');
$this
->assertRecipeFields($translation, array_shift($recipes));
}
protected function addUnitKeys(array $ingredients) {
foreach ($ingredients as $key => $ingredient) {
$ingredient->name = strtolower($ingredient->name);
$ingredients[$key]->unit_key = isset(Recipe61::CHANGED_UNITS[$ingredient->name]) ? Recipe61::CHANGED_UNITS[$ingredient->name] : $ingredient->name;
}
return $ingredients;
}
}