Skip to content

Commit

Permalink
Merge pull request #17 from SU-SWS/release-8.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
imonroe authored Oct 19, 2020
2 parents 2e2bf44 + 78c837f commit 4b9ff87
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Stanford Migrate

8.x-1.5
--------------------------------------------------------------------------------
_Release Date: 2020-10-19_

- D8CORE-2470: Add process plugin to check image dimensions (#15)
- CS-000 Go to the next item in the id map when continueing (#16)

8.x-1.4
--------------------------------------------------------------------------------
_Release Date: 2020-10-05_
Expand Down
5 changes: 5 additions & 0 deletions src/EventSubscriber/EventsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,16 @@ public function postImport(MigrateImportEvent $event) {
}

if (!$destination_id) {
// Move on to the next existing item.
$id_map->next();
continue;
}

/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $entity_storage->load($destination_id);
if (!$entity) {
// Move on to the next existing item.
$id_map->next();
continue;
}

Expand Down
113 changes: 113 additions & 0 deletions src/Plugin/migrate/process/ImageDimensionSkip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Drupal\stanford_migrate\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
* Migrate process plugin that will skip if an image isn't big enough.
*
* @code
* process:
* field_image:
* plugin: image_dimension_skip
* method: row
* width: 100
* height: 100
* field_image_2:
* plugin: image_dimension_skip
* method: process
* width: 100
* @endcode
*
* @MigrateProcessPlugin(
* id = "image_dimension_skip"
* )
*/
class ImageDimensionSkip extends ProcessPluginBase {

/**
* Skips the current row if the given image url/path is not large enough.
*
* @param mixed $value
* The input value.
* @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
* The migration in which this process is being executed.
* @param \Drupal\migrate\Row $row
* The row from the source to process.
* @param string $destination_property
* The destination property currently worked on. This is only used together
* with the $row above.
*
* @return mixed
* The input value, $value, if it is not empty.
*
* @throws \Drupal\migrate\MigrateSkipRowException
* Thrown if the source property is not set and the row should be skipped,
* records with STATUS_IGNORED status in the map.
*/
public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!$this->isImageBigger($value)) {
$message = 'Image is not big enough as defined by the limits.';
throw new MigrateSkipRowException($message);
}
return $value;
}

/**
* Stops processing the current property when value is not set.
*
* @param mixed $value
* The input value.
* @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
* The migration in which this process is being executed.
* @param \Drupal\migrate\Row $row
* The row from the source to process.
* @param string $destination_property
* The destination property currently worked on. This is only used together
* with the $row above.
*
* @return mixed
* The input value, $value, if it is not empty.
*
* @throws \Drupal\migrate\MigrateSkipProcessException
* Thrown if the source property is not set and rest of the process should
* be skipped.
*/
public function process($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!$this->isImageBigger($value)) {
throw new MigrateSkipProcessException();
}
return $value;
}

/**
* Check if the give url is bigger than the restrictions.
*
* @param mixed $value
* The input value, $value, if it is not empty.
*
* @return bool
* True if the image is bigger than the restrictions.
*/
protected function isImageBigger($value) {
if (!is_string($value)) {
return FALSE;
}
try {
[$width, $height] = getimagesize($value);
}
catch (\Exception $e) {
return FALSE;
}

$valid_width = empty($this->configuration['width']) ? TRUE : $this->configuration['width'] <= $width;
$valid_height = empty($this->configuration['height']) ? TRUE : $this->configuration['height'] <= $height;
return $valid_height && $valid_width;
}

}
2 changes: 1 addition & 1 deletion stanford_migrate.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: 'Adds more functionality to migrate and migrate plus modules'
type: module
core_version_requirement: ^8.8 || ^9
package: 'Stanford'
version: 8.x-1.4
version: 8.x-1.5
dependencies:
- drupal:migrate
- migrate_plus:migrate_plus
Expand Down
87 changes: 87 additions & 0 deletions tests/src/Unit/Plugin/migrate/process/ImageDimensionSkipTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Drupal\Tests\stanford_migrate\Unit\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Row;
use Drupal\stanford_migrate\Plugin\migrate\process\ImageDimensionSkip;
use Drupal\Tests\UnitTestCase;

/**
* Class ImageDimensionSkipTest
*
* @group stanford_migrate
* @coversDefaultClass \Drupal\stanford_migrate\Plugin\migrate\process\ImageDimensionSkip
*/
class ImageDimensionSkipTest extends UnitTestCase {

/**
* If the passed value is not a valid string, it'll trigger a skip.
*/
public function testNonString(){
$plugin = new ImageDimensionSkip([
'width' => 100,
'method' => 'row',
], '', []);
$migrate = $this->createMock(MigrateExecutableInterface::class);
$row = $this->createMock(Row::class);

$value = ['http://placecorgi.com/260/180'];
$this->expectException(MigrateSkipRowException::class);
$this->assertEquals($value, $plugin->transform($value, $migrate, $row, ''));
}

public function testNonUrl(){
$plugin = new ImageDimensionSkip([
'width' => 100,
'method' => 'row',
], '', []);
$migrate = $this->createMock(MigrateExecutableInterface::class);
$row = $this->createMock(Row::class);

$this->expectException(MigrateSkipRowException::class);
$value = 'foo/bar/baz';
$this->assertEquals($value, $plugin->transform($value, $migrate, $row, ''));
}

/**
* If the image is not the correct dimensions the row should be skipped.
*/
public function testRowSkip() {
$plugin = new ImageDimensionSkip([
'width' => 100,
'method' => 'row',
], '', []);
$migrate = $this->createMock(MigrateExecutableInterface::class);
$row = $this->createMock(Row::class);

$value = 'http://placecorgi.com/260/180';
$this->assertEquals($value, $plugin->transform($value, $migrate, $row, ''));

$value = 'http://placecorgi.com/50/50';
$this->expectException(MigrateSkipRowException::class);
$plugin->transform($value, $migrate, $row, '');
}

/**
* If the image is not the correct dimensions process plugin should skip.
*/
public function testProcessSkip() {
$plugin = new ImageDimensionSkip([
'width' => 100,
'method' => 'process',
], '', []);
$migrate = $this->createMock(MigrateExecutableInterface::class);
$row = $this->createMock(Row::class);

$value = 'http://placecorgi.com/260/180';
$this->assertEquals($value, $plugin->transform($value, $migrate, $row, ''));

$value = 'http://placecorgi.com/50/50';
$this->expectException(MigrateSkipProcessException::class);
$plugin->transform($value, $migrate, $row, '');
}

}

0 comments on commit 4b9ff87

Please sign in to comment.