View source
<?php
namespace Drupal\Tests\panopoly_core\Unit\Plugin\migrate\process;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Image\ImageInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileStorageInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\panopoly_core\Plugin\migrate\process\DemoImage;
use Drupal\Tests\UnitTestCase;
class DemoImageTest extends UnitTestCase {
protected $migration;
protected $fileStorage;
protected $moduleHandler;
protected $filesystem;
protected $imageFactory;
public function setUp() {
parent::setUp();
require_once './includes/file.inc';
$this->migration = $this
->prophesize(MigrationInterface::class);
$this->fileStorage = $this
->prophesize(FileStorageInterface::class);
$this->moduleHandler = $this
->prophesize(ModuleHandlerInterface::class);
$this->filesystem = $this
->prophesize(FileSystemInterface::class);
$this->imageFactory = $this
->prophesize(ImageFactory::class);
}
protected function createPlugin(array $configuration, array $methods = NULL) {
return $this
->getMockBuilder(DemoImage::class)
->setConstructorArgs([
$configuration,
'demo_image',
[],
$this->migration
->reveal(),
$this->fileStorage
->reveal(),
$this->moduleHandler
->reveal(),
$this->filesystem
->reveal(),
$this->imageFactory
->reveal(),
])
->onlyMethods($methods)
->getMock();
}
protected function invokeMethod($object, $method, array $arguments = []) {
$method = new \ReflectionMethod(get_class($object), $method);
$method
->setAccessible(TRUE);
return $method
->invokeArgs($object, $arguments);
}
public function testTransformSuccess() {
$image = $this
->prophesize(ImageInterface::class);
$image
->getWidth()
->willReturn(300);
$image
->getHeight()
->willReturn(200);
$this->imageFactory
->get('public://demo_images/image.jpg')
->willReturn($image);
$executable = $this
->prophesize(MigrateExecutableInterface::class);
$row = $this
->prophesize(Row::class);
$row
->getSourceProperty('image_filename')
->willReturn('image.jpg');
$plugin = $this
->createPlugin([
'filename_property' => 'image_filename',
], [
'getSourcePath',
'getDestinationPath',
'checkFile',
'findOrCreateFile',
'getOptionalProperty',
]);
$plugin
->method('getSourcePath')
->willReturn('modules/demo_module/images');
$plugin
->method('getDestinationPath')
->willReturn('public://demo_images');
$plugin
->method('checkFile')
->willReturn(TRUE);
$file = $this
->prophesize(File::class);
$file
->id()
->willReturn(32);
$plugin
->method('findOrCreateFile')
->with('modules/demo_module/images/image.jpg', 'public://demo_images/image.jpg')
->willReturn($file
->reveal());
$plugin
->expects($this
->exactly(3))
->method('getOptionalProperty')
->willReturnMap([
[
'description_property',
$row
->reveal(),
NULL,
],
[
'alt_property',
$row
->reveal(),
'This is the alternative text',
],
[
'title',
$row
->reveal(),
'',
],
]);
$result = $plugin
->transform(NULL, $executable
->reveal(), $row
->reveal(), 'field_image');
$this
->assertEquals([
'target_id' => 32,
'display' => 1,
'description' => '',
'alt' => 'This is the alternative text',
'title' => '',
'width' => 300,
'height' => 200,
], $result);
}
public function testTransformBadConfiguration() {
$plugin = $this
->createPlugin([]);
$executable = $this
->prophesize(MigrateExecutableInterface::class);
$row = $this
->prophesize(Row::class);
$this
->assertEquals([], $plugin
->transform(NULL, $executable
->reveal(), $row
->reveal(), 'field_image'));
}
public function testTransformBadSourceFile() {
$executable = $this
->prophesize(MigrateExecutableInterface::class);
$row = $this
->prophesize(Row::class);
$row
->getSourceProperty('image_filename')
->willReturn('image.jpg');
$plugin = $this
->createPlugin([
'filename_property' => 'image_filename',
], [
'getSourcePath',
'getDestinationPath',
'checkFile',
]);
$plugin
->method('getSourcePath')
->willReturn('modules/demo_module/images');
$plugin
->method('getDestinationPath')
->willReturn('public://demo_images');
$plugin
->method('checkFile')
->willReturn(FALSE);
$plugin
->transform(NULL, $executable
->reveal(), $row
->reveal(), 'field_image');
}
public function testGetOptionalProperty() {
$plugin = $this
->createPlugin([
'test_property' => 'source_property_name',
]);
$row = $this
->prophesize(Row::class);
$row
->getSourceProperty('source_property_name')
->willReturn('test_value');
$this
->assertEquals('test_value', $this
->invokeMethod($plugin, 'getOptionalProperty', [
'test_property',
$row
->reveal(),
]));
}
}