View source
<?php
namespace Drupal\Tests\recipe\Kernel\Migrate\recipe71;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
class MigrateRecipe71Test extends MigrateRecipe71TestBase {
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',
'd7_user_role',
'd7_user',
'd7_node_type',
'd7_node',
'd7_node_translation',
'recipe1x_ingredient',
'recipe71_recipe',
'recipe71_recipe_translation',
]);
}
protected function assertRecipeFields(NodeInterface $node, \stdClass $recipe) {
$this
->assertEquals($recipe->cooktime, $node->recipe_cook_time->value);
$this
->assertSame($recipe->description, $node->recipe_description->value);
$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);
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) {
$source->ingredients = $database_connection
->select('recipe_node_ingredient', 'rni')
->fields('rni')
->condition('rni.nid', $source->nid)
->execute()
->fetchAll();
}
$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));
}
}