diff --git a/src/Entity/Payment.php b/src/Entity/Payment.php index 3618bf9..9c06970 100644 --- a/src/Entity/Payment.php +++ b/src/Entity/Payment.php @@ -62,6 +62,18 @@ class Payment extends ContentEntityBase implements PaymentInterface { /** * {@inheritdoc} */ + public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) { + // Unset the values for fields that are stored in the base table. These + // values are not needed here, but because the entity storage skips + // unserialization, they cause errors. + unset($values['payment_method']); + unset($values['payment_type']); + parent::__construct($values, $entity_type, $bundle, $translations); + } + + /** + * {@inheritdoc} + */ public function getChangedTime() { return $this->getPaymentStatus()->getCreated(); } @@ -316,7 +328,6 @@ class Payment extends ContentEntityBase implements PaymentInterface { $math = \Drupal::service('currency.math'); $total = 0; foreach ($this->getLineItems() as $line_item) { - debug(spl_object_hash($line_item)); $total = $math->add($total, $line_item->getTotalAmount()); } diff --git a/src/Plugin/DataType/PluginConfiguration.php b/src/Plugin/DataType/PluginConfiguration.php index a1df2c8..a919869 100644 --- a/src/Plugin/DataType/PluginConfiguration.php +++ b/src/Plugin/DataType/PluginConfiguration.php @@ -9,6 +9,7 @@ namespace Drupal\payment\Plugin\DataType; use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Core\TypedData\TypedData; +use Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface; /** * Provides a plugin configuration data type. diff --git a/src/Plugin/DataType/PluginInstance.php b/src/Plugin/DataType/PluginInstance.php index d303982..182f3f8 100644 --- a/src/Plugin/DataType/PluginInstance.php +++ b/src/Plugin/DataType/PluginInstance.php @@ -9,6 +9,7 @@ namespace Drupal\payment\Plugin\DataType; use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\TypedData\TypedData; +use Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface; /** * Provides a plugin instance data type. diff --git a/src/Plugin/Field/FieldType/PluginBagItemBase.php b/src/Plugin/Field/FieldType/PluginBagItemBase.php index 7444984..e3040e5 100644 --- a/src/Plugin/Field/FieldType/PluginBagItemBase.php +++ b/src/Plugin/Field/FieldType/PluginBagItemBase.php @@ -138,10 +138,6 @@ abstract class PluginBagItemBase extends FieldItemBase implements PluginBagItemI * {@inheritdoc} */ public function isEmpty() { - if (!$this->getContainedPluginId() || !$this->getContainedPluginConfiguration() || !$this->getContainedPluginInstance()) { - debug($this->getContainedPluginId()); - debug($this->getContainedPluginInstance()->getPluginId()); - } return !$this->getContainedPluginInstance(); } @@ -150,11 +146,22 @@ abstract class PluginBagItemBase extends FieldItemBase implements PluginBagItemI */ public function setValue($values, $notify = TRUE) { if ($values instanceof PluginInspectionInterface) { - $values = [ - 'plugin_instance' => $values, - ]; + $this->setContainedPluginInstance($values); + } + elseif (is_array($values)) { + if (isset($values['plugin_id'])) { + $this->setContainedPluginId($values['plugin_id']); + } + if (isset($values['plugin_configuration'])) { + $this->setContainedPluginConfiguration($values['plugin_configuration']); + } + } + // Field API has this weird habit of setting NULL instead of calling + // applyDefaultValue(), so we can't throw an exception on that. + elseif (!is_null($values)) { + $type = is_object($values) ? get_class($values) : gettype($values); + throw new \InvalidArgumentException(sprintf('The value must implement \Drupal\Component\Plugin\PluginInspectionInterface or be an associative array, but %s was given', $type)); } - parent::setValue($values, $notify); } /** diff --git a/src/Tests/Entity/Payment/PaymentStorageWebTest.php b/src/Tests/Entity/Payment/PaymentStorageWebTest.php index a845a57..3419e43 100644 --- a/src/Tests/Entity/Payment/PaymentStorageWebTest.php +++ b/src/Tests/Entity/Payment/PaymentStorageWebTest.php @@ -49,9 +49,7 @@ class PaymentStorageWebTest extends WebTestBase { // Test saving a payment. $this->assertFalse($payment->id()); // Set an extra status, so we can test for status IDs later. - debug(count($payment->getPaymentStatuses())); $payment->setPaymentStatus(Payment::statusManager()->createInstance('payment_success')); - debug(count($payment->getPaymentStatuses())); $payment->save(); // @todo The ID should be an integer, but for some reason the entity field // API returns a string. diff --git a/src/Tests/Entity/PaymentUnitTest.php b/src/Tests/Entity/PaymentUnitTest.php index cddc162..bf29d83 100644 --- a/src/Tests/Entity/PaymentUnitTest.php +++ b/src/Tests/Entity/PaymentUnitTest.php @@ -212,9 +212,6 @@ class PaymentUnitTest extends KernelTestBase { $line_item->setName($name); $line_item->setAmount($amount); $line_item->setQuantity($quantity); - debug(spl_object_hash($line_item)); - debug(get_class($line_item)); - debug($line_item->getName()); $this->payment->setLineItem($line_item); } $this->assertEqual($this->payment->getAmount(), 12); diff --git a/tests/src/Unit/Entity/Payment/PaymentStorageUnitTest.php b/tests/src/Unit/Entity/Payment/PaymentStorageUnitTest.php index 1304ab7..adaf93b 100644 --- a/tests/src/Unit/Entity/Payment/PaymentStorageUnitTest.php +++ b/tests/src/Unit/Entity/Payment/PaymentStorageUnitTest.php @@ -120,96 +120,4 @@ class PaymentStorageUnitTest extends UnitTestCase { $this->assertInstanceOf('\Drupal\payment\Entity\Payment\PaymentStorage', $storage); } - /** - * @covers ::mapToStorageRecord - */ - public function testMapToStorageRecord() { - $bundle = $this->randomMachineName(); - $currency_code = $this->randomMachineName(); - $id = $this->randomMachineName(); - $first_payment_status_delta = mt_rand(); - $last_payment_status_delta = mt_rand(); - $owner_id = mt_rand(); - $uuid = $this->randomMachineName(); - $payment_method_id = $this->randomMachineName(); - $payment_method_configuration = array( - 'foo' => $this->randomMachineName(), - ); - $payment_type_id = $this->randomMachineName(); - $payment_type_configuration = array( - 'foo' => $this->randomMachineName(), - ); - - $first_payment_status = $this->getMock('\Drupal\payment\Plugin\Payment\Status\PaymentStatusInterface'); - $first_payment_status->expects($this->any()) - ->method('getId') - ->will($this->returnValue($first_payment_status_delta)); - $last_payment_status = $this->getMock('\Drupal\payment\Plugin\Payment\Status\PaymentStatusInterface'); - $last_payment_status->expects($this->any()) - ->method('getId') - ->will($this->returnValue($last_payment_status_delta)); - - $payment_method = $this->getMock('\Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface'); - $payment_method->expects($this->atLeastOnce()) - ->method('getPluginId') - ->will($this->returnValue($payment_method_id)); - $payment_method->expects($this->atLeastOnce()) - ->method('getConfiguration') - ->will($this->returnValue($payment_method_configuration)); - - $payment_type = $this->getMock('\Drupal\payment\Plugin\Payment\Type\PaymentTypeInterface'); - $payment_type->expects($this->atLeastOnce()) - ->method('getPluginId') - ->will($this->returnValue($payment_type_id)); - $payment_type->expects($this->atLeastOnce()) - ->method('getConfiguration') - ->will($this->returnValue($payment_type_configuration)); - - $payment= $this->getMock('\Drupal\payment\Entity\PaymentInterface'); - $payment->expects($this->atLeastOnce()) - ->method('bundle') - ->will($this->returnValue($bundle)); - $payment->expects($this->atLeastOnce()) - ->method('getCurrencyCode') - ->will($this->returnValue($currency_code)); - $payment->expects($this->atLeastOnce()) - ->method('getOwnerId') - ->will($this->returnValue($owner_id)); - $payment->expects($this->atLeastOnce()) - ->method('getPaymentMethod') - ->will($this->returnValue($payment_method)); - $payment->expects($this->atLeastOnce()) - ->method('getPaymentType') - ->will($this->returnValue($payment_type)); - $payment->expects($this->once()) - ->method('getPaymentStatus') - ->will($this->returnValue($last_payment_status)); - $payment->expects($this->once()) - ->method('getPaymentStatuses') - ->will($this->returnValue(array($first_payment_status, $last_payment_status))); - $payment->expects($this->atLeastOnce()) - ->method('id') - ->will($this->returnValue($id)); - $payment->expects($this->atLeastOnce()) - ->method('uuid') - ->will($this->returnValue($uuid)); - - $method = new \ReflectionMethod($this->storage, 'mapToStorageRecord'); - $method->setAccessible(TRUE); - - $record = $method->invoke($this->storage, $payment); - $this->assertInstanceOf('\stdClass', $record); - $this->assertSame($uuid, $record->uuid); - $this->assertSame($id, $record->id); - $this->assertSame($currency_code, $record->currency); - $this->assertSame($bundle, $record->bundle); - $this->assertSame($owner_id, $record->owner); - $this->assertSame($first_payment_status_delta, $record->first_payment_status_delta); - $this->assertSame($last_payment_status_delta, $record->last_payment_status_delta); - $this->assertSame($payment_method_id, $record->payment_method_id); - $this->assertSame(serialize($payment_method_configuration), $record->payment_method_configuration); - $this->assertSame($payment_type_id, $record->payment_type_id); - $this->assertSame(serialize($payment_type_configuration), $record->payment_type_configuration); - } - } diff --git a/tests/src/Unit/Plugin/Payment/LineItem/PaymentLineItemBaseUnitTest.php b/tests/src/Unit/Plugin/Payment/LineItem/PaymentLineItemBaseUnitTest.php index ef3918c..1931a72 100644 --- a/tests/src/Unit/Plugin/Payment/LineItem/PaymentLineItemBaseUnitTest.php +++ b/tests/src/Unit/Plugin/Payment/LineItem/PaymentLineItemBaseUnitTest.php @@ -73,7 +73,7 @@ class PaymentLineItemBaseUnitTest extends UnitTestCase { public function testGetConfiguration() { $configuration = array( $this->randomMachineName() => mt_rand(), - ); + ) + $this->lineItem->defaultConfiguration(); $return = $this->lineItem->setConfiguration($configuration); $this->assertNull($return); $this->assertSame($configuration, $this->lineItem->getConfiguration()); diff --git a/tests/src/Unit/Plugin/Payment/MethodConfiguration/PaymentMethodConfigurationBaseUnitTest.php b/tests/src/Unit/Plugin/Payment/MethodConfiguration/PaymentMethodConfigurationBaseUnitTest.php index e7c3a36..c0a9441 100644 --- a/tests/src/Unit/Plugin/Payment/MethodConfiguration/PaymentMethodConfigurationBaseUnitTest.php +++ b/tests/src/Unit/Plugin/Payment/MethodConfiguration/PaymentMethodConfigurationBaseUnitTest.php @@ -193,7 +193,7 @@ class PaymentMethodConfigurationBaseUnitTest extends UnitTestCase { public function testGetConfiguration() { $configuration = array( $this->randomMachineName() => $this->randomMachineName(), - ); + ) + $this->paymentMethodConfiguration->defaultConfiguration(); $return = $this->paymentMethodConfiguration->setConfiguration($configuration); $this->assertSame(NULL, $return); $this->assertSame($configuration, $this->paymentMethodConfiguration->getConfiguration()); diff --git a/tests/src/Unit/Plugin/Payment/Status/PaymentStatusBaseUnitTest.php b/tests/src/Unit/Plugin/Payment/Status/PaymentStatusBaseUnitTest.php index ff36893..41152b5 100644 --- a/tests/src/Unit/Plugin/Payment/Status/PaymentStatusBaseUnitTest.php +++ b/tests/src/Unit/Plugin/Payment/Status/PaymentStatusBaseUnitTest.php @@ -107,7 +107,7 @@ class PaymentStatusBaseUnitTest extends UnitTestCase { public function testGetConfiguration() { $configuration = array( $this->randomMachineName() => mt_rand(), - ); + ) + $this->status->defaultConfiguration(); $this->assertNull($this->status->setConfiguration($configuration)); $this->assertSame($configuration, $this->status->getConfiguration()); }