-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from SU-SWS/release-8.1.5
- Loading branch information
Showing
5 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
tests/src/Unit/Plugin/migrate/process/ImageDimensionSkipTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ''); | ||
} | ||
|
||
} |