Commit bd3893b3 authored by alexpott's avatar alexpott

Issue #2936463 by jigarius, ragasirtahk, Jo Fitzgerald, zuhair_ak, heddn,...

Issue #2936463 by jigarius, ragasirtahk, Jo Fitzgerald, zuhair_ak, heddn, larowlan, quietone: Throw exception when "callable" plugin is called with undefined on invalid callback
parent bbf77c25
......@@ -49,11 +49,21 @@ class Callback extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (is_callable($this->configuration['callable'])) {
$value = call_user_func($this->configuration['callable'], $value);
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
if (!isset($configuration['callable'])) {
throw new \InvalidArgumentException('The "callable" must be set.');
}
elseif (!is_callable($configuration['callable'])) {
throw new \InvalidArgumentException('The "callable" must be a valid function or method.');
}
return $value;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
return call_user_func($this->configuration['callable'], $value);
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\CallbackTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\Callback;
......@@ -17,39 +12,51 @@
class CallbackTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->plugin = new TestCallback();
parent::setUp();
}
/**
* Test callback with a function as callable.
* Test callback with valid "callable".
*
* @dataProvider providerCallback
*/
public function testCallbackWithFunction() {
$this->plugin->setCallable('strtolower');
public function testCallback($callable) {
$configuration = ['callable' => $callable];
$this->plugin = new Callback($configuration, 'map', []);
$value = $this->plugin->transform('FooBar', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame('foobar', $value);
}
/**
* Test callback with a class method as callable.
* Data provider for ::testCallback().
*/
public function testCallbackWithClassMethod() {
$this->plugin->setCallable(['\Drupal\Component\Utility\Unicode', 'strtolower']);
$value = $this->plugin->transform('FooBar', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame('foobar', $value);
public function providerCallback() {
return [
'function' => ['strtolower'],
'class method' => [['\Drupal\Component\Utility\Unicode', 'strtolower']],
];
}
}
class TestCallback extends Callback {
public function __construct() {
/**
* Test callback excpetions.
*
* @dataProvider providerCallbackExceptions
*/
public function testCallbackExceptions($message, $configuration) {
$this->setExpectedException(\InvalidArgumentException::class, $message);
$this->plugin = new Callback($configuration, 'map', []);
}
public function setCallable($callable) {
$this->configuration['callable'] = $callable;
/**
* Data provider for ::testCallbackExceptions().
*/
public function providerCallbackExceptions() {
return [
'not set' => [
'message' => 'The "callable" must be set.',
'configuration' => []
],
'invalid method' => [
'message' => 'The "callable" must be a valid function or method.',
'configuration' => ['callable' => 'nonexistent_callable']
],
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment