Problem/Motivation

We need an upgrade path for the Drupal 7 Image module.

Remaining Tasks

Write a migration (with tests) for image styles. These are configuration entities in Drupal 8. Any variables maintained by the image module will also need to be moved into configuration.

Image files and fields do not need to be migrated in this issue -- they're being handled elsewhere: #2416765: Migrate Drupal 7 Field/Instance/View mode settings

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

svendecabooter’s picture

Assigned: Unassigned » svendecabooter
svendecabooter’s picture

Assigned: svendecabooter » Unassigned
Status: Active » Needs work
FileSize
2.47 KB

Attached is a patch to migrate the image styles from D7 to D8.
Still needs tests and unit tests.

lostkangaroo’s picture

+++ b/core/modules/image/src/Plugin/migrate/source/d7/ImageStyles.php
@@ -0,0 +1,74 @@
+ * @MigrateSource(
+ *   id = "d7_image_styles"
+ * )

It would be beneficial to add a source_provider annotation to make sure this module exists as a source.

source_provider = "image"
phenaproxima’s picture

Found a few thangs...

  1. +++ b/core/modules/image/migration_templates/d7_image_styles.yml
    @@ -0,0 +1,20 @@
    +  name:
    +    -
    +      plugin: machine_name
    +      source: name
    +    -
    +      plugin: dedupe_entity
    +      entity_type: image_style
    +      field: name
    +      length: 32
    

    Let's lose the machine_name and dedupe_entity plugins here -- we want to merge with existing styles by machine name, so this can just be a direct mapping (name: name).

  2. +++ b/core/modules/image/src/Plugin/migrate/source/d7/ImageStyles.php
    @@ -0,0 +1,74 @@
    + * @MigrateSource(
    + *   id = "d7_image_styles"
    + * )
    

    As @lostkangaroo said, please do add source_provider = "image".

  3. +++ b/core/modules/image/src/Plugin/migrate/source/d7/ImageStyles.php
    @@ -0,0 +1,74 @@
    +    return $this->select('image_styles', 'ims')
    +      ->fields('ims');
    

    'ims' is a strange alias for the table. Can it just be 'is'?

  4. +++ b/core/modules/image/src/Plugin/migrate/source/d7/ImageStyles.php
    @@ -0,0 +1,74 @@
    +    foreach($results as $key => $result) {
    

    Need a space between foreach and the opening parenthesis.

  5. +++ b/core/modules/image/src/Plugin/migrate/source/d7/ImageStyles.php
    @@ -0,0 +1,74 @@
    +        'id'     => $result['name'],
    

    'name' should not be changed to 'id'. The source plugins should stay as close as possible to the original data.

  6. +++ b/core/modules/image/src/Plugin/migrate/source/d7/ImageStyles.php
    @@ -0,0 +1,74 @@
    +        'data'   => $result['data'],
    +      ];
    +      $effects[$key]['data'] = unserialize($result['data']);
    

    Why not simply inline the call to unserialize()? 'data' => unserialize($result['data'])

phenaproxima’s picture

Issue tags: +Needs tests

Oh, and. :)

svendecabooter’s picture

Assigned: Unassigned » svendecabooter
svendecabooter’s picture

Assigned: svendecabooter » Unassigned
Status: Needs work » Needs review
FileSize
8.86 KB

Attached a new version of the patch, with tests added.

Some comments to the feedback of phenaproxima:
#3: I used alias "is" first, but that gave SQL errors when testing. After banging my head for a while, I realised "IS" is a reserved SQL keyword, making the queries crash. So I used alias "ims" instead.
#5: The plugin won't import the image style effects with key 'name' instead of 'id'. How would I go about fixing that then?

Status: Needs review » Needs work

The last submitted patch, 7: upgrade_path_for_image-2500483-7.patch, failed testing.

The last submitted patch, 7: upgrade_path_for_image-2500483-7.patch, failed testing.

phenaproxima’s picture

The plugin won't import the image style effects with key 'name' instead of 'id'. How would I go about fixing that then?

You will need to change it in the process pipeline, within the migration entity itself.

+process:
+  name: name
+  label: label
+  effects: effects

This should be:

+process:
+  id: name
+  label: label
+  effects: effects

Other, minor things -- once these are fixed and DrupalCI approves, it's RTBC from me.

  1. +++ b/core/modules/image/src/Tests/Migrate/d7/MigrateImageStylesTest.php
    @@ -0,0 +1,76 @@
    +  /**
    +   * Modules to enable.
    +   *
    +   * @var array
    +   */
    

    Please change this to {@inheritdoc} -- we're going to do that in another issue for all existing tests, so best to make it consistent here :)

  2. +++ b/core/modules/image/src/Tests/Migrate/d7/MigrateImageStylesTest.php
    @@ -0,0 +1,76 @@
    +   * Asserts various aspects of a ImageStyle entity.
    

    The nit to end all nits: s/a/an

  3. +++ b/core/modules/image/src/Tests/Migrate/d7/MigrateImageStylesTest.php
    @@ -0,0 +1,76 @@
    +   * @param string $id
    +   *   The expected image style ID.
    +   * @param string $label
    +   *   The expected image style label.
    

    Missing documentation for the other parameters.

  4. +++ b/core/modules/image/src/Tests/Migrate/d7/MigrateImageStylesTest.php
    @@ -0,0 +1,76 @@
    +  protected function assertEntity($id, $label, $expected_size, $expected_effect_plugins, $expected_effect_config) {
    

    $expected_effect_config should have the array type hint.

phenaproxima’s picture

Issue tags: -Needs tests

Derp.

svendecabooter’s picture

With regards to the "id" vs "name" issue:
I can't change this in the process yml declaration, because that's for migrating the image styles, while the "id" issue is about the underlying image effect plugins (the "effects" process pipeline in d7_image_styles.yml).
So i'm not sure what the best way is to migrate to D8 plugins that within themselves reference other existing plugins.

svendecabooter’s picture

Also, I can't figure out why the PHPUnit test won't run. It looks exactly like other PHPUnit tests I wrote for migrate issues, so not sure what's going on. Probably something stupid I forgot, but it would be nice to have another pair of eyes on that...

svendecabooter’s picture

Attached a new version with the minor issues fixed.

phenaproxima’s picture

I can't change this in the process yml declaration, because that's for migrating the image styles, while the "id" issue is about the underlying image effect plugins (the "effects" process pipeline in d7_image_styles.yml).
So i'm not sure what the best way is to migrate to D8 plugins that within themselves reference other existing plugins.

The iterator process plugin might be the answer to this. https://www.drupal.org/node/2135345

MattA’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 14: upgrade_path_for_image-2500483-14.patch, failed testing.

The last submitted patch, 14: upgrade_path_for_image-2500483-14.patch, failed testing.

svendecabooter’s picture

Here's an updated version of the patch that uses the iterator process.
Only thing I can't figure out is why the PHPUnit test doesn't run properly. Checked with some guys here at Drupalcon, but no luck.

phenaproxima’s picture

  1. +++ b/core/modules/image/src/Plugin/migrate/source/d7/ImageStyles.php
    @@ -0,0 +1,74 @@
    +      $effects[$key] = [
    +        'name'     => $result['name'],
    +        'weight' => $result['weight'],
    +        'data'   => unserialize($result['data']),
    +      ];
    

    This can just be $effects[$key]['data'] = unserialize($result['data']. No need to copy the name and weight verbatim.

  2. +++ b/core/modules/image/src/Tests/Migrate/d7/MigrateImageStylesTest.php
    @@ -0,0 +1,78 @@
    +   * @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
    

    $expected_size is missing :) It might not even be necessary, since $expected_effect_plugins is being passed in anyway...?

Only thing I can't figure out is why the PHPUnit test doesn't run properly.

Can you elaborate on this?

svendecabooter’s picture

#1: Wouldn't it be more complete to migrate the weight of an image effect within an image style as well? I see no reason to leave that out, as it might changes the image style upon migration if left out I think.
#2: reworked this bit in the updated patch
#3: See the failed test results: https://qa.drupal.org/pifr/test/1159943 (PHPunit Test failed to complete, line 0) - it seems the (inherited) assertions in my Unit test don't get run or detected for some unknown reason.

svendecabooter’s picture

Or for #1, do you mean I just unserialize $results[$key]['data'], and return that variable, instead of $effects? In that case there would be more stuff in the resulting array, rather than just name, weight & data, which seem to be the only useful fields as far as I know. Hence my approach.

phenaproxima’s picture

For #1, I mean this:

foreach ($results as $key => $result) {
  $result['data'] = unserialize($result['data']);
  $effects[$key] = $result;
}

It's OK to include more stuff than just name, weight, and data. Generally I think source plugins should err on the side of returning as much data as is available and allow the migration to decide what, if anything, to do with it.

I'm not sure why the PHPUnit test is failing. Does it produce any warnings or fatals locally?

svendecabooter’s picture

OK I see now. Will update the code.

As for the PHPUnit test - I'm having the same issue with another patch as well.
I get the error "No test results to display." when I run the test via the Testing module UI.

If I set some breakpoints in the class, it seems to find the class, but not the setUp() or testXXX() methods.
Structure of the classes is the same as found in other migrate Unit tests in D8 core, so I'm kind of baffled why it doesn't work.

svendecabooter’s picture

Finally found out why I get "No test results to display." errors.
I was getting PHP errors in sites/default/files/simpletest/phpunit-xxx.xml. My debugger doesn't work on PHPUnit tests for some reason...
Would be cool if the testbot dumped out those PHP errors upon testing as well. Would have saved me tons of time :)

Anyway, I was missing some logic in the setUp function of the Unit test, which is now added.

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/image/src/Plugin/migrate/source/d7/ImageStyles.php
@@ -0,0 +1,71 @@
+  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;
+  }

We should probably mention effects here, but I think that can be fixed on commit. Many existing source plugins do not return accurate field info.

Fantastic job, @svendecabooter!

The last submitted patch, 21: upgrade_path_for_image-2500483-21.patch, failed testing.

The last submitted patch, 19: upgrade_path_for_image-2500483-19.patch, failed testing.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.0.x. Thanks!

  • webchick committed 191aec4 on 8.0.x
    Issue #2500483 by svendecabooter, phenaproxima, lostkangaroo: Upgrade...

The last submitted patch, 19: upgrade_path_for_image-2500483-19.patch, failed testing.

The last submitted patch, 21: upgrade_path_for_image-2500483-21.patch, failed testing.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.