Commit 191aec42 authored by webchick's avatar webchick
Browse files

Issue #2500483 by svendecabooter, phenaproxima, lostkangaroo: Upgrade path for Image 7.x

parent bf74e9d3
id: d7_image_styles
label: Image styles
migration_tags:
- Drupal 7
source:
plugin: d7_image_styles
process:
name: name
label: label
effects:
plugin: iterator
source: effects
process:
id: name
weight: weight
data: data
destination:
plugin: entity:image_style
<?php
/**
* @file
* Contains \Drupal\image\Plugin\migrate\source\d7\ImageStyles.
*/
namespace Drupal\image\Plugin\migrate\source\d7;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\migrate\Row;
/**
* Drupal image styles source from database.
*
* @MigrateSource(
* id = "d7_image_styles",
* source_provider = "image"
* )
*/
class ImageStyles extends DrupalSqlBase {
/**
* {@inheritdoc}
*/
public function query() {
return $this->select('image_styles', 'ims')
->fields('ims');
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'isid' => $this->t('The primary identifier for an image style.'),
'name' => $this->t('The style machine name.'),
'label' => $this->t('The style administrative name.'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['isid']['type'] = 'integer';
return $ids;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$effects = array();
$results = $this->select('image_effects', 'ie')
->fields('ie')
->condition('isid', $row->getSourceProperty('isid'))
->execute();
foreach ($results as $key => $result) {
$result['data'] = unserialize($result['data']);
$effects[$key] = $result;
}
$row->setSourceProperty('effects', $effects);
return parent::prepareRow($row);
}
}
<?php
/**
* @file
* Contains \Drupal\image\Tests\Migrate\d7\MigrateImageStylesTest.
*/
namespace Drupal\image\Tests\Migrate\d7;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\ImageStyleInterface;
use Drupal\image\ImageEffectBase;
use Drupal\migrate_drupal\Tests\d7\MigrateDrupal7TestBase;
/**
* Test image styles migration to config entities.
*
* @group image
*/
class MigrateImageStylesTest extends MigrateDrupal7TestBase {
/**
* {@inheritdoc}
*/
public static $modules = array('image');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(static::$modules);
$this->executeMigration('d7_image_styles');
}
/**
* Test the image styles migration.
*/
public function testImageStylesMigration() {
$this->assertEntity('custom_image_style_1', "Custom image style 1", ['image_scale_and_crop', 'image_desaturate'], [['width' => 55, 'height' => 55], []]);
$this->assertEntity('custom_image_style_2', "Custom image style 2", ['image_resize', 'image_rotate'], [['width' => 55, 'height' => 100], ['degrees' => 45, 'bgcolor' => '#FFFFFF', 'random' => false]]);
$this->assertEntity('custom_image_style_3', "Custom image style 3", ['image_scale', 'image_crop'], [['width' => 150, 'height' => NULL, 'upscale' => false], ['width' => 50, 'height' => 50, 'anchor' => 'left-top']]);
}
/**
* Asserts various aspects of an ImageStyle entity.
*
* @param string $id
* The expected image style ID.
* @param string $label
* The expected image style label.
* @param array $expected_effect_plugins
* An array of expected plugins attached to the image style entity
* @param array $expected_effect_config
* An array of expected configuration for each effect in the image style
*/
protected function assertEntity($id, $label, array $expected_effect_plugins, array $expected_effect_config) {
$style = ImageStyle::load($id);
$this->assertTrue($style instanceof ImageStyleInterface);
/** @var \Drupal\image\ImageStyleInterface $style */
$this->assertIdentical($id, $style->id());
$this->assertIdentical($label, $style->label());
// Check the number of effects associated with the style.
$effects = $style->getEffects();
$this->assertIdentical(count($effects), count($expected_effect_plugins));
$index = 0;
foreach ($effects as $effect) {
$this->assertTrue($effect instanceof ImageEffectBase);
$this->assertIdentical($expected_effect_plugins[$index], $effect->getPluginId());
$config = $effect->getConfiguration();
$this->assertIdentical($expected_effect_config[$index], $config['data']);
$index++;
}
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\image\Unit\Plugin\migrate\source\d7\MigrateImageStylesTest.
*/
namespace Drupal\Tests\image\Unit\Plugin\migrate\source\d7;
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
/**
* Tests D7 ImageStyles source plugin.
*
* @group image
*/
class MigrateImageStylesTest extends MigrateSqlSourceTestCase {
const PLUGIN_CLASS = 'Drupal\image\Plugin\migrate\source\d7\ImageStyles';
protected $migrationConfiguration = [
'id' => 'test',
'source' => [
'plugin' => 'd7_image_styles',
],
];
protected $expectedResults = [
[
'isid' => 1,
'name' => 'custom_image_style_1',
'label' => 'Custom image style 1',
'effects' => [
[
'ieid' => 1,
'isid' => 1,
'weight' => 1,
'name' => 'image_desaturate',
'data' => [],
]
]
],
];
/**
* {@inheritdoc}
*/
public function setUp() {
foreach ($this->expectedResults as $k => $row) {
foreach (array('isid', 'name', 'label') as $field) {
$this->databaseContents['image_styles'][$k][$field] = $row[$field];
}
foreach ($row['effects'] as $id => $effect) {
$row['effects'][$id]['data'] = serialize($row['effects'][$id]['data']);
}
$this->databaseContents['image_effects'] = $row['effects'];
}
parent::setUp();
}
}
......@@ -64,8 +64,44 @@ public function load() {
'name',
'data',
))
->execute();
->values(array(
'ieid' => '3',
'isid' => '1',
'weight' => '1',
'name' => 'image_scale_and_crop',
'data' => 'a:2:{s:5:"width";s:2:"55";s:6:"height";s:2:"55";}',
))->values(array(
'ieid' => '4',
'isid' => '1',
'weight' => '2',
'name' => 'image_desaturate',
'data' => 'a:0:{}',
))->values(array(
'ieid' => '5',
'isid' => '2',
'weight' => '1',
'name' => 'image_resize',
'data' => 'a:2:{s:5:"width";s:2:"55";s:6:"height";s:3:"100";}',
))->values(array(
'ieid' => '6',
'isid' => '2',
'weight' => '2',
'name' => 'image_rotate',
'data' => 'a:3:{s:7:"degrees";s:2:"45";s:7:"bgcolor";s:7:"#FFFFFF";s:6:"random";i:0;}',
))->values(array(
'ieid' => '7',
'isid' => '3',
'weight' => '1',
'name' => 'image_scale',
'data' => 'a:3:{s:5:"width";s:3:"150";s:6:"height";s:0:"";s:7:"upscale";i:0;}',
))->values(array(
'ieid' => '8',
'isid' => '3',
'weight' => '2',
'name' => 'image_crop',
'data' => 'a:3:{s:5:"width";s:2:"50";s:6:"height";s:2:"50";s:6:"anchor";s:8:"left-top";}',
))->execute();
}
}
#02b95b18fd065377d10d2eb5db756894
#5e2aa799db43de19b84994816b4a426b
......@@ -50,8 +50,20 @@ public function load() {
'name',
'label',
))
->execute();
->values(array(
'isid' => '1',
'name' => 'custom_image_style_1',
'label' => 'Custom image style 1',
))->values(array(
'isid' => '2',
'name' => 'custom_image_style_2',
'label' => 'Custom image style 2',
))->values(array(
'isid' => '3',
'name' => 'custom_image_style_3',
'label' => 'Custom image style 3',
))->execute();
}
}
#64077dfb27e8f49e53f6d40a40b6d36d
#ace52192be7a9ef33a60e4202b6f33f4
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment