diff --git a/core/modules/image/image.install b/core/modules/image/image.install index 75ffd2a..5cdf997 100644 --- a/core/modules/image/image.install +++ b/core/modules/image/image.install @@ -103,3 +103,68 @@ function image_requirements($phase) { return $requirements; } + +/** + * Loads all effects for an image style. + * + * @param array $style + * The image style (array) to retrieve effects for. + * + * @see image_update_8000() + */ +function _image_update_get_style_with_effects(array $style) { + // Retrieve image effects. + // @todo Module updates are not able to invoke module hooks and API functions; + // additional information previously supplied via hook_image_effect_info() + // is not and cannot be taken into account. + $effects = array(); + $result = db_select('image_effects', NULL, array('fetch' => PDO::FETCH_ASSOC)) + ->fields('image_effects') + ->condition('isid', $style['isid']) + ->execute(); + foreach ($result as $effect) { + unset($effect['isid']); + $effect['data'] = unserialize($effect['data']); + + // Generate new key for each effect. + $effect['ieid'] = $effect['name']; + foreach ($effect['data'] as $key => $value) { + $effect['ieid'] .= '_' . $value; + } + $effect['ieid'] = preg_replace('@[^a-zA-Z0-9_-]@', '', $effect['ieid']); + + $effects[$effect['ieid']] = $effect; + } + return $effects; +} + +/** + * Convert existing image styles to the new config system. + */ +function image_update_8000() { + $styles = array(); + $result = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC)) + ->fields('image_styles') + ->execute() + ->fetchAllAssoc('name', PDO::FETCH_ASSOC); + foreach ($result as $style_name => $style) { + $style['effects'] = _image_update_get_style_with_effects($style); + $styles[$style_name] = $style; + } + + // Convert each style into a configuration object. + foreach ($styles as $name => $style) { + $config = config('image.style.' . $name); + $config->set('name', $name); + $config->set('effects', $style['effects']); + $config->save(); + } +} + +/** + * Remove the {image_styles} and {image_effects} tables. + */ +function image_update_8001() { + db_drop_table('image_styles'); + db_drop_table('image_effects'); +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php new file mode 100644 index 0000000..86c4360 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php @@ -0,0 +1,81 @@ + 'Image upgrade test', + 'description' => 'Upgrade tests for overridden and custom image styles.', + 'group' => 'Upgrade path', + ); + } + + public function setUp() { + $this->databaseDumpFiles = array( + drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.bare.standard_all.database.php.gz', + drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.image.database.php', + ); + parent::setUp(); + } + + /** + * Tests that custom and overridden image styles have been upgraded. + */ + public function testImageStyleUpgrade() { + $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); + + // Verify that image styles were properly upgraded. + $expected_styles['test-custom'] = array( + 'name' => 'test-custom', + 'effects' => array( + 'image_rotate_90_FFFFFF_1' => array( + 'name' => 'image_rotate', + 'data' => array( + 'degrees' => '90', + 'bgcolor' => '#FFFFFF', + 'random' => '1', + ), + 'ieid' => 'image_rotate_90_FFFFFF_1', + 'weight' => '1', + ), + 'image_desaturate' => array( + 'name' => 'image_desaturate', + 'data' => array(), + 'ieid' => 'image_desaturate', + 'weight' => '2', + ), + ), + ); + $expected_styles['thumbnail'] = array( + 'name' => 'thumbnail', + 'effects' => array ( + 'image_scale_177_177_0' => array( + 'name' => 'image_scale', + 'data' => array ( + 'width' => '177', + 'height' => '177', + 'upscale' => '0', + ), + 'ieid' => 'image_scale_177_177_0', + 'weight' => '0', + ), + ), + ); + foreach ($expected_styles as $name => $style) { + $config = config('image.style.' . $name)->get(); + $this->assertEqual($style, $config, format_string('@first is equal to @second.', array( + '@first' => var_export($style, TRUE), + '@second' => var_export($config, TRUE), + ))); + } + } +} diff --git a/core/modules/system/tests/upgrade/drupal-7.image.database.php b/core/modules/system/tests/upgrade/drupal-7.image.database.php new file mode 100644 index 0000000..6d7f456 --- /dev/null +++ b/core/modules/system/tests/upgrade/drupal-7.image.database.php @@ -0,0 +1,59 @@ +fields(array( + 'isid', + 'name', +)) +// Override thumbnail style. +->values(array( + 'isid' => '1', + 'name' => 'thumbnail', +)) +// Custom style. +->values(array( + 'isid' => '2', + 'name' => 'test-custom', +)) +->execute(); + +// Add image effects. +db_insert('image_effects')->fields(array( + 'ieid', + 'isid', + 'weight', + 'name', + 'data', +)) +->values(array( + 'ieid' => '1', + 'isid' => '1', + 'weight' => '0', + 'name' => 'image_scale', + 'data' => 'a:3:{s:5:"width";s:3:"177";s:6:"height";s:3:"177";s:7:"upscale";i:0;}', +)) +->values(array( + 'ieid' => '3', + 'isid' => '2', + 'weight' => '1', + 'name' => 'image_rotate', + 'data' => 'a:3:{s:7:"degrees";s:2:"90";s:7:"bgcolor";s:7:"#FFFFFF";s:6:"random";i:1;}', +)) +->values(array( + 'ieid' => '4', + 'isid' => '2', + 'weight' => '2', + 'name' => 'image_desaturate', + 'data' => 'a:0:{}', +)) +->execute();