diff --git a/payment.install b/payment.install index 9e6d0a0..07a382d 100644 --- a/payment.install +++ b/payment.install @@ -9,61 +9,6 @@ * Implements hook_schema(). */ function payment_schema() { - $schema['payment_line_item'] = array( - 'fields' => array( - 'amount' => array( - 'type' => 'float', - 'size' => 'big', - 'not null' => TRUE, - ), - 'amount_total' => array( - 'type' => 'float', - 'size' => 'big', - 'not null' => TRUE, - ), - 'currency_code' => array( - 'type' => 'varchar', - 'length' => 3, - 'not null' => TRUE, - ), - 'name' => array( - 'type' => 'varchar', - 'length' => 255, - ), - 'payment_id' => array( - 'description' => 'The {payment}.id this line item belongs to.', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - ), - 'plugin_configuration' => array( - 'type' => 'blob', - 'not null' => TRUE, - 'serialize' => TRUE, - ), - 'plugin_id' => array( - 'type' => 'varchar', - 'length' => 255, - ), - 'quantity' => array( - 'type' => 'int', - 'default' => 1, - 'not null' => TRUE, - ), - ), - 'primary key' => array('name', 'payment_id'), - 'foreign keys' => array( - 'payment_id' => array( - 'table' => 'payment', - 'columns' => array( - 'payment_id' => 'id', - ), - ), - ), - 'indexes' => array( - 'payment_id' => array('payment_id'), - ), - ); $schema['payment_queue'] = array( 'fields' => array( 'acquisition_code' => array( @@ -107,40 +52,6 @@ function payment_schema() { 'queue_id' => array('queue_id'), ), ); - $schema['payment_status'] = array( - 'fields' => array( - 'created' => array( - 'type' => 'int', - 'not null' => TRUE, - ), - 'payment_id' => array( - 'description' => 'The payment ID.', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - ), - 'plugin_id' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - ), - 'id' => array( - 'type' => 'serial', - ), - ), - 'foreign keys' => array( - 'payment_id' => array( - 'table' => 'payment', - 'columns' => array( - 'payment_id' => 'id', - ), - ), - ), - 'indexes' => array( - 'payment_id' => array('payment_id'), - ), - 'primary key' => array('id'), - ); return $schema; } diff --git a/src/DatabaseQueue.php b/src/DatabaseQueue.php index 3158710..95477e1 100644 --- a/src/DatabaseQueue.php +++ b/src/DatabaseQueue.php @@ -235,7 +235,7 @@ class DatabaseQueue implements QueueInterface { } $query = $this->database->select('payment_queue', 'pq'); $query->addJoin('INNER', 'payment', 'p', 'p.id = pq.payment_id'); - $query->addJoin('INNER', 'payment_status', 'ps', 'p.last_payment_status_id = ps.id'); + $query->addJoin('INNER', 'payment_status', 'ps', 'p.last_payment_status_delta = ps.id'); $query->fields('pq', array('payment_id')) ->condition('pq.category_id', $category_id) ->condition('ps.plugin_id', $allowed_payment_status_ids) diff --git a/src/Entity/Payment.php b/src/Entity/Payment.php index 9aaadd5..f4ef039 100644 --- a/src/Entity/Payment.php +++ b/src/Entity/Payment.php @@ -77,10 +77,10 @@ class Payment extends ContentEntityBase implements PaymentInterface { * {@inheritdoc} */ public function getPaymentType() { - /** @var \Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase $payment_type_item */ - $payment_type_item = $this->get('payment_type')[0]; + /** @var \Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase $field_item */ + $field_item = $this->get('payment_type')[0]; - return $payment_type_item->getContainedPluginInstance(); + return $field_item->getContainedPluginInstance(); } /** @@ -124,12 +124,13 @@ class Payment extends ContentEntityBase implements PaymentInterface { */ public function setLineItem(PaymentLineItemInterface $line_item) { $line_item->setPayment($this); - $this->lineItems[$line_item->getName()] = $line_item; - - $line_items = $this->getLineItems(); $line_items[] = $line_item; - $this->set('line_items', $line_items); + $this->get('line_items')->applyDefaultValue(); + foreach (array_values($line_items) as $delta => $line_item) { + $this->get('line_items')[$delta]->setValue($line_item); + } + return $this; } @@ -138,7 +139,13 @@ class Payment extends ContentEntityBase implements PaymentInterface { * {@inheritdoc} */ public function unsetLineItem($name) { - unset($this->lineItems[$name]); + $line_items = $this->getLineItems(); + foreach ($line_items as $delta => $line_item) { + if ($line_item->getName() == $name) { + unset($line_items[$delta]); + } + } + $this->get('line_items')->setValue($line_items); return $this; } @@ -147,14 +154,30 @@ class Payment extends ContentEntityBase implements PaymentInterface { * {@inheritdoc} */ public function getLineItems() { - return $this->lineItems; + $line_items = []; + /** @var \Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase $field_item */ + foreach ($this->get('line_items') as $field_item) { + /** @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface $line_item */ + $line_item = $field_item->getContainedPluginInstance(); + if ($line_item) { + $line_items[$line_item->getName()] = $line_item; + } + } + + return $line_items; } /** * {@inheritdoc} */ public function getLineItem($name) { - return isset($this->lineItems[$name]) ? $this->lineItems[$name] : NULL; + $line_items = $this->getLineItems(); + foreach ($line_items as $delta => $line_item) { + if ($line_item->getName() == $name) { + return $line_item; + } + } + return NULL; } /** @@ -162,9 +185,9 @@ class Payment extends ContentEntityBase implements PaymentInterface { */ public function getLineItemsByType($plugin_id) { $line_items = []; - foreach ($this->getLineItems() as $line_item) { + foreach ($this->getLineItems() as $name => $line_item) { if ($line_item->getPluginId() == $plugin_id) { - $line_items[$line_item->getName()] = $line_item; + $line_items[$name] = $line_item; } } @@ -191,8 +214,11 @@ class Payment extends ContentEntityBase implements PaymentInterface { $previous_status = $this->getPaymentStatus(); $status->setPayment($this); // Prevent duplicate statuses. + $payment_statuses = $this->getPaymentStatuses(); if (!$this->getPaymentStatus() || $this->getPaymentStatus()->getPluginId() != $status->getPluginId()) { - $this->statuses[] = $status; + $payment_statuses = $this->getPaymentStatuses(); + $payment_statuses[] = $status; + $this->get('payment_statuses')->setValue($payment_statuses); } if ($notify) { /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */ @@ -209,14 +235,27 @@ class Payment extends ContentEntityBase implements PaymentInterface { * {@inheritdoc} */ public function getPaymentStatuses() { - return $this->statuses; + $payment_statuses = []; + /** @var \Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase $field_item */ + foreach ($this->get('payment_statuses') as $field_item) { + $payment_statuses[] = $field_item->getContainedPluginInstance(); + } + + return $payment_statuses; } /** * {@inheritdoc} */ public function getPaymentStatus() { - return $this->statuses ? end($this->statuses) : NULL; + $deltas = []; + /** @var \Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase $field_item */ + foreach ($this->get('payment_statuses') as $delta => $field_item) { + $deltas[] = $delta; + } + $field_item = $this->get('payment_statuses')[max($deltas)]; + + return $field_item->getContainedPluginInstance(); } /** @@ -224,9 +263,9 @@ class Payment extends ContentEntityBase implements PaymentInterface { */ public function setPaymentMethod(PluginPaymentMethodInterface $payment_method) { $payment_method->setPayment($this); - /** @var \Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase $payment_type_item */ - $payment_type_item = $this->get('payment_method')[0]; - $payment_type_item->setContainedPluginInstance($payment_method); + /** @var \Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase $field_item */ + $field_item = $this->get('payment_method')[0]; + $field_item->setContainedPluginInstance($payment_method); return $this; } @@ -235,10 +274,10 @@ class Payment extends ContentEntityBase implements PaymentInterface { * {@inheritdoc} */ public function getPaymentMethod() { - /** @var \Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase $payment_type_item */ - $payment_type_item = $this->get('payment_method')[0]; + /** @var \Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase $field_item */ + $field_item = $this->get('payment_method')[0]; - return $payment_type_item->getContainedPluginInstance(); + return $field_item->getContainedPluginInstance(); } /** diff --git a/src/Entity/Payment/PaymentStorage.php b/src/Entity/Payment/PaymentStorage.php index 397c186..5ea6ca6 100644 --- a/src/Entity/Payment/PaymentStorage.php +++ b/src/Entity/Payment/PaymentStorage.php @@ -110,7 +110,6 @@ class PaymentStorage extends SqlContentEntityStorage { } $record->first_payment_status_delta = min($deltas); $record->last_payment_status_delta = max($deltas); - debug($record); return $record; } diff --git a/src/Plugin/Field/FieldType/PluginBagItemBase.php b/src/Plugin/Field/FieldType/PluginBagItemBase.php index 1e63a62..627c608 100644 --- a/src/Plugin/Field/FieldType/PluginBagItemBase.php +++ b/src/Plugin/Field/FieldType/PluginBagItemBase.php @@ -30,12 +30,12 @@ abstract class PluginBagItemBase extends FieldItemBase { /** * The previously contained plugin instance. * - * @var \Drupal\Component\Plugin\PluginInspectionInterface|\Drupal\payment\Plugin\Payment\FieldTypePluginInterface + * @var \Drupal\payment\Plugin\Payment\FieldTypePluginInterface */ protected $previouslyContainedPluginInstance; /** - * Returns the plugin manager. + * Returns the manager for plugins of the type contained by this item. * * @return \Drupal\Component\Plugin\PluginManagerInterface */ @@ -44,17 +44,14 @@ abstract class PluginBagItemBase extends FieldItemBase { /** * Validates a plugin instance. * - * @param \Drupal\Component\Plugin\PluginInspectionInterface $plugin_instance + * @param \Drupal\payment\Plugin\Payment\FieldTypePluginInterface $plugin_instance * * @throws \Exception */ - protected function validatePluginInstance(PluginInspectionInterface $plugin_instance) { + protected function validatePluginInstance(FieldTypePluginInterface $plugin_instance) { if (!$this->getPluginManager()->hasDefinition($plugin_instance->getPluginId())) { throw new \Exception(sprintf('Plugin manager %s does not have a definition for plugin %s.', get_class($this->getPluginManager()), $plugin_instance->getPluginId())); } - if (!$plugin_instance instanceof FieldTypePluginInterface) { - throw new \Exception(sprintf('%s must implement \Drupal\payment\Plugin\Payment\FieldTypePluginInterface if it is to be contained by a field item.', get_class($plugin_instance))); - } } /** @@ -69,7 +66,7 @@ abstract class PluginBagItemBase extends FieldItemBase { /** @var \Drupal\payment\Plugin\Payment\FieldTypePluginInterface $plugin_instance */ $plugin_instance = $this->getPluginManager()->createInstance($plugin_id, $plugin_configuration); $this->validatePluginInstance($plugin_instance); - $plugin_instance->setFieldItem($this); + $plugin_instance->setContainingFieldItem($this); return $plugin_instance; } @@ -77,8 +74,8 @@ abstract class PluginBagItemBase extends FieldItemBase { /** * Gets the instantiated plugin. * - * @return \Drupal\Component\Plugin\PluginInspectionInterface|null - * The plugin or NULL of no plugin ID was configured yet. + * @return \Drupal\payment\Plugin\Payment\FieldTypePluginInterface|null + * The plugin or NULL if no plugin was set yet. */ public function getContainedPluginInstance() { return $this->get('plugin_instance')->getValue(); @@ -189,7 +186,7 @@ abstract class PluginBagItemBase extends FieldItemBase { if ($this->get($property_name)->getValue()) { $plugin_instance = $this->createContainedPluginInstance($this->getContainedPluginId()); if ($this->previouslyContainedPluginInstance) { - $this->previouslyContainedPluginInstance->unsetFieldItem(); + $this->previouslyContainedPluginInstance->unsetContainingFieldItem(); } $this->previouslyContainedPluginInstance = $plugin_instance; $this->get('plugin_instance')->setValue($plugin_instance, FALSE); @@ -219,12 +216,12 @@ abstract class PluginBagItemBase extends FieldItemBase { if ($property_name == 'plugin_instance') { if ($this->previouslyContainedPluginInstance) { - $this->previouslyContainedPluginInstance->unsetFieldItem(); + $this->previouslyContainedPluginInstance->unsetContainingFieldItem(); } $plugin_instance = $this->getContainedPluginInstance(); $this->previouslyContainedPluginInstance = $plugin_instance; if ($plugin_instance) { - $plugin_instance->setFieldItem($this); + $plugin_instance->setContainingFieldItem($this); $this->validatePluginInstance($plugin_instance); $this->get('plugin_id') ->setValue($plugin_instance->getPluginId(), FALSE); @@ -282,18 +279,33 @@ abstract class PluginBagItemBase extends FieldItemBase { * {@inheritdoc} */ public function isEmpty() { - return !$this->getContainedPluginInstance(); + return !$this->getContainedPluginId() || !$this->getContainedPluginConfiguration() || !$this->getContainedPluginInstance(); } /** * {@inheritdoc} */ public function setValue($values, $notify = TRUE) { - if (!is_array($values)) { - $this->get('plugin_instance')->setValue($values); - return; + if ($values instanceof FieldTypePluginInterface) { + $values = [ + 'plugin_instance' => $values, + ]; + } + + foreach ($this->properties as $name => $property) { + if (!isset($values[$name])) { + continue; + } + + // Always notify properties, because this field items depends on + // notifications for property synchronization. + $property->setValue($values[$name], TRUE); + } + + // Notify the parent of any changes. + if ($notify && isset($this->parent)) { + $this->parent->onChange($this->name); } - parent::setValue($values, $notify); } /** diff --git a/src/Plugin/Payment/FieldTypePluginInterface.php b/src/Plugin/Payment/FieldTypePluginInterface.php index 795087c..70ca63e 100644 --- a/src/Plugin/Payment/FieldTypePluginInterface.php +++ b/src/Plugin/Payment/FieldTypePluginInterface.php @@ -6,12 +6,13 @@ namespace Drupal\payment\Plugin\Payment; +use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\payment\Plugin\Field\FieldType\PluginBagItemBase; /** * Defines a plugin that can be contained in a field. */ -interface FieldTypePluginInterface { +interface FieldTypePluginInterface extends PluginInspectionInterface { /** * Sets the containing field item. @@ -20,13 +21,13 @@ interface FieldTypePluginInterface { * * @return $this */ - public function setFieldItem(PluginBagItemBase $field_item); + public function setContainingFieldItem(PluginBagItemBase $field_item); /** * Unsets the containing field item. * * @return $this */ - public function unsetFieldItem(); + public function unsetContainingFieldItem(); } diff --git a/src/Plugin/Payment/FieldTypePluginTrait.php b/src/Plugin/Payment/FieldTypePluginTrait.php index bcd7cf9..6d55f83 100644 --- a/src/Plugin/Payment/FieldTypePluginTrait.php +++ b/src/Plugin/Payment/FieldTypePluginTrait.php @@ -26,7 +26,7 @@ trait FieldTypePluginTrait { /** * {@inheritdoc} */ - public function setFieldItem(PluginBagItemBase $field_item) { + public function setContainingFieldItem(PluginBagItemBase $field_item) { $this->fieldItem = $field_item; return $this; @@ -35,14 +35,14 @@ trait FieldTypePluginTrait { /** * {@inheritdoc} */ - public function unsetFieldItem() { + public function unsetContainingFieldItem() { $this->fieldItem = NULL; return $this; } /** - * {@inheritdoc} + * Sets the plugin configuration on the containing field item. */ public function setContainingFieldItemConfiguration() { if ($this->fieldItem) { diff --git a/src/Plugin/Payment/LineItem/PaymentLineItemBase.php b/src/Plugin/Payment/LineItem/PaymentLineItemBase.php index f61d9b5..750d8d5 100644 --- a/src/Plugin/Payment/LineItem/PaymentLineItemBase.php +++ b/src/Plugin/Payment/LineItem/PaymentLineItemBase.php @@ -11,6 +11,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginBase; use Drupal\currency\Math\MathInterface; use Drupal\payment\Entity\PaymentInterface; +use Drupal\payment\Plugin\Payment\FieldTypePluginTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -21,6 +22,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ abstract class PaymentLineItemBase extends PluginBase implements PaymentLineItemInterface, ContainerFactoryPluginInterface { + use FieldTypePluginTrait; + /** * The math service. * diff --git a/src/Plugin/Payment/LineItem/PaymentLineItemInterface.php b/src/Plugin/Payment/LineItem/PaymentLineItemInterface.php index 6feebbf..78034bd 100644 --- a/src/Plugin/Payment/LineItem/PaymentLineItemInterface.php +++ b/src/Plugin/Payment/LineItem/PaymentLineItemInterface.php @@ -10,11 +10,12 @@ use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Plugin\PluginFormInterface; use Drupal\payment\Entity\PaymentInterface; +use Drupal\payment\Plugin\Payment\FieldTypePluginInterface; /** * A payment line item. */ -interface PaymentLineItemInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface { +interface PaymentLineItemInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface, FieldTypePluginInterface { /** * Sets the payment the line item belongs to. diff --git a/src/Plugin/Payment/Method/PaymentMethodBase.php b/src/Plugin/Payment/Method/PaymentMethodBase.php index b2da542..3821d51 100644 --- a/src/Plugin/Payment/Method/PaymentMethodBase.php +++ b/src/Plugin/Payment/Method/PaymentMethodBase.php @@ -18,7 +18,6 @@ use Drupal\payment\Event\PaymentExecuteAccess; use Drupal\payment\Event\PaymentPreCapture; use Drupal\payment\Event\PaymentPreExecute; use Drupal\payment\Event\PaymentPreRefund; -use Drupal\payment\Plugin\Payment\FieldTypePluginInterface; use Drupal\payment\Plugin\Payment\FieldTypePluginTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -32,7 +31,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; * form. * - message_text_format: The ID of the text format to format message_text with. */ -abstract class PaymentMethodBase extends PluginBase implements ContainerFactoryPluginInterface, FieldTypePluginInterface, PaymentMethodInterface, PaymentMethodCapturePaymentInterface, PaymentMethodRefundPaymentInterface { +abstract class PaymentMethodBase extends PluginBase implements ContainerFactoryPluginInterface, PaymentMethodInterface, PaymentMethodCapturePaymentInterface, PaymentMethodRefundPaymentInterface { use FieldTypePluginTrait; diff --git a/src/Plugin/Payment/Method/PaymentMethodInterface.php b/src/Plugin/Payment/Method/PaymentMethodInterface.php index 3e45743..e6329f0 100644 --- a/src/Plugin/Payment/Method/PaymentMethodInterface.php +++ b/src/Plugin/Payment/Method/PaymentMethodInterface.php @@ -11,6 +11,7 @@ use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Core\Session\AccountInterface; use Drupal\payment\Entity\PaymentInterface; +use Drupal\payment\Plugin\Payment\FieldTypePluginInterface; /** * Defines a payment method. @@ -26,7 +27,7 @@ use Drupal\payment\Entity\PaymentInterface; * - \Drupal\payment\Plugin\Payment\Method\PaymentMethodCapturePaymentInterface: * This interface lets payment methods capture already authorized payments. */ -interface PaymentMethodInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface { +interface PaymentMethodInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface, FieldTypePluginInterface { /** * Checks if the payment can be executed. diff --git a/src/Plugin/Payment/Method/Unavailable.php b/src/Plugin/Payment/Method/Unavailable.php index 65caf78..b70a23b 100644 --- a/src/Plugin/Payment/Method/Unavailable.php +++ b/src/Plugin/Payment/Method/Unavailable.php @@ -10,7 +10,6 @@ use Drupal\Component\Plugin\PluginBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\payment\Entity\PaymentInterface; -use Drupal\payment\Plugin\Payment\FieldTypePluginInterface; use Drupal\payment\Plugin\Payment\FieldTypePluginTrait; /** @@ -27,7 +26,7 @@ use Drupal\payment\Plugin\Payment\FieldTypePluginTrait; * module = "payment" * ) */ -class Unavailable extends PluginBase implements PaymentMethodInterface, FieldTypePluginInterface { +class Unavailable extends PluginBase implements PaymentMethodInterface { use FieldTypePluginTrait; diff --git a/src/Plugin/Payment/Status/PaymentStatusBase.php b/src/Plugin/Payment/Status/PaymentStatusBase.php index 9404d97..9588860 100644 --- a/src/Plugin/Payment/Status/PaymentStatusBase.php +++ b/src/Plugin/Payment/Status/PaymentStatusBase.php @@ -9,7 +9,6 @@ namespace Drupal\payment\Plugin\Payment\Status; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginBase; use Drupal\payment\Entity\PaymentInterface; -use Drupal\payment\Plugin\Payment\FieldTypePluginInterface; use Drupal\payment\Plugin\Payment\FieldTypePluginTrait; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -19,7 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * Plugins extending this class should provide a configuration schema that * extends payment.plugin_configuration.payment_status.payment_base. */ -abstract class PaymentStatusBase extends PluginBase implements ContainerFactoryPluginInterface, FieldTypePluginInterface, PaymentStatusInterface { +abstract class PaymentStatusBase extends PluginBase implements ContainerFactoryPluginInterface, PaymentStatusInterface { use FieldTypePluginTrait; @@ -127,22 +126,6 @@ abstract class PaymentStatusBase extends PluginBase implements ContainerFactoryP /** * {@inheritdoc} */ - public function setId($id) { - $this->configuration['id'] = $id; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getId() { - return $this->configuration['id']; - } - - /** - * {@inheritdoc} - */ function getAncestors(){ return $this->paymentStatusManager->getAncestors($this->getPluginId()); } diff --git a/src/Plugin/Payment/Status/PaymentStatusInterface.php b/src/Plugin/Payment/Status/PaymentStatusInterface.php index 2ba2f9a..296d9ae 100644 --- a/src/Plugin/Payment/Status/PaymentStatusInterface.php +++ b/src/Plugin/Payment/Status/PaymentStatusInterface.php @@ -9,11 +9,12 @@ namespace Drupal\payment\plugin\payment\status; use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\payment\Entity\PaymentInterface; +use Drupal\payment\Plugin\Payment\FieldTypePluginInterface; /** * A payment status plugin. */ -interface PaymentStatusInterface extends PluginInspectionInterface, ConfigurablePluginInterface { +interface PaymentStatusInterface extends PluginInspectionInterface, ConfigurablePluginInterface, FieldTypePluginInterface { /** * Sets the created date and time. @@ -50,22 +51,6 @@ interface PaymentStatusInterface extends PluginInspectionInterface, Configurable public function getPayment(); /** - * Sets the ID. - * - * @param int $id - * * - * @return static - */ - public function setId($id); - - /** - * Gets the ID. - * - * @return int - */ - public function getId(); - - /** * Gets this payment status's ancestors. * * @return array diff --git a/src/Plugin/Payment/Type/PaymentTypeBase.php b/src/Plugin/Payment/Type/PaymentTypeBase.php index 1702a57..4bc0239 100644 --- a/src/Plugin/Payment/Type/PaymentTypeBase.php +++ b/src/Plugin/Payment/Type/PaymentTypeBase.php @@ -11,7 +11,6 @@ use Drupal\Core\Plugin\PluginBase; use Drupal\payment\Entity\PaymentInterface; use Drupal\payment\Event\PaymentEvents; use Drupal\payment\Event\PaymentTypePreResumeContext; -use Drupal\payment\Plugin\Payment\FieldTypePluginInterface; use Drupal\payment\Plugin\Payment\FieldTypePluginTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -19,7 +18,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Defines base payment type. */ -abstract class PaymentTypeBase extends PluginBase implements ContainerFactoryPluginInterface, FieldTypePluginInterface, PaymentTypeInterface { +abstract class PaymentTypeBase extends PluginBase implements ContainerFactoryPluginInterface, PaymentTypeInterface { use FieldTypePluginTrait; diff --git a/src/Plugin/Payment/Type/PaymentTypeInterface.php b/src/Plugin/Payment/Type/PaymentTypeInterface.php index 0aff7d7..9f95a77 100644 --- a/src/Plugin/Payment/Type/PaymentTypeInterface.php +++ b/src/Plugin/Payment/Type/PaymentTypeInterface.php @@ -10,11 +10,12 @@ use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Session\AccountInterface; use Drupal\payment\Entity\PaymentInterface; +use Drupal\payment\Plugin\Payment\FieldTypePluginInterface; /** * A payment type plugin. */ -interface PaymentTypeInterface extends PluginInspectionInterface, ConfigurablePluginInterface { +interface PaymentTypeInterface extends PluginInspectionInterface, ConfigurablePluginInterface, FieldTypePluginInterface { /** * Sets the ID of the payment this plugin is of. diff --git a/src/Tests/Entity/Payment/PaymentStorageWebTest.php b/src/Tests/Entity/Payment/PaymentStorageWebTest.php index 39fdc64..11e160d 100644 --- a/src/Tests/Entity/Payment/PaymentStorageWebTest.php +++ b/src/Tests/Entity/Payment/PaymentStorageWebTest.php @@ -60,18 +60,18 @@ class PaymentStorageWebTest extends WebTestBase { $this->assertTrue(is_numeric($payment->getOwnerId())); // Check references to other tables. $payment_data = $database->select('payment', 'p') - ->fields('p', array('first_payment_status_id', 'last_payment_status_id')) + ->fields('p', array('first_payment_status_delta', 'last_payment_status_delta')) ->condition('id', $payment->id()) ->execute() ->fetchAssoc(); - $this->assertEqual($payment_data['first_payment_status_id'], 1); - $this->assertEqual($payment_data['last_payment_status_id'], 2); + $this->assertEqual($payment_data['first_payment_status_delta'], 0); + $this->assertEqual($payment_data['last_payment_status_delta'], 1); /** @var \Drupal\payment\Entity\PaymentInterface $payment_loaded */ $payment_loaded = entity_load_unchanged('payment', $payment->id()); $this->assertEqual(count($payment_loaded->getLineItems()), count($payment->getLineItems())); $this->assertEqual(count($payment_loaded->getPaymentStatuses()), count($payment->getPaymentStatuses())); - $this->assertEqual($payment_loaded->getPaymentMethod()->getConfiguration(), $payment_method->getConfiguration()); - $this->assertEqual($payment_loaded->getPaymentType()->getConfiguration(), $payment_type_configuration); + $this->assertEqual($payment_loaded->getPaymentMethod()->getConfiguration(), $payment->getPaymentMethod()->getConfiguration()); + $this->assertEqual($payment_loaded->getPaymentType()->getConfiguration(), $payment->getPaymentType()->getConfiguration()); // Test deleting a payment. $payment->delete(); diff --git a/src/Tests/Entity/PaymentUnitTest.php b/src/Tests/Entity/PaymentUnitTest.php index b6dda0b..386ac70 100644 --- a/src/Tests/Entity/PaymentUnitTest.php +++ b/src/Tests/Entity/PaymentUnitTest.php @@ -136,7 +136,7 @@ class PaymentUnitTest extends KernelTestBase { $line_item_1->setName($this->randomMachineName()); $line_item_2 = $this->lineItemManager->createInstance('payment_basic'); $line_item_2->setName($this->randomMachineName()); - $this->assertTrue(spl_object_hash($this->payment->setLineItems(array($line_item_1, $line_item_2))), spl_object_hash($this->payment)); + $this->assertTrue(spl_object_hash($this->payment->setLineItems([$line_item_1, $line_item_2])), spl_object_hash($this->payment)); $line_items = $this->payment->getLineItems(); if ($this->assertTrue(is_array($line_items))) { $this->assertEqual(spl_object_hash($line_items[$line_item_1->getName()]), spl_object_hash($line_item_1)); @@ -156,18 +156,18 @@ class PaymentUnitTest extends KernelTestBase { } /** - * Tests setStatus() and getStatus(). + * Tests setPaymentStatus() and getPaymentStatus(). */ - protected function testGetStatus() { + protected function testGetPaymentStatus() { $status = $this->statusManager->createInstance('payment_pending'); $this->assertEqual(spl_object_hash($this->payment->setPaymentStatus($status, FALSE)), spl_object_hash($this->payment)); $this->assertEqual(spl_object_hash($this->payment->getPaymentStatus()), spl_object_hash($status)); } /** - * Tests setStatuses() and getStatuses(). + * Tests setPaymentStatuses() and getPaymentStatuses(). */ - protected function testGetStatuses() { + protected function testGetPaymentStatuses() { $statuses = array($this->statusManager->createInstance('payment_pending'), $this->statusManager->createInstance('payment_failed')); $this->assertEqual(spl_object_hash($this->payment->setPaymentStatuses($statuses)), spl_object_hash($this->payment)); $retrieved_statuses = $this->payment->getPaymentStatuses(); diff --git a/tests/src/Unit/Entity/Payment/PaymentStorageUnitTest.php b/tests/src/Unit/Entity/Payment/PaymentStorageUnitTest.php index 6b293f3..9e89706 100644 --- a/tests/src/Unit/Entity/Payment/PaymentStorageUnitTest.php +++ b/tests/src/Unit/Entity/Payment/PaymentStorageUnitTest.php @@ -47,20 +47,6 @@ class PaymentStorageUnitTest extends UnitTestCase { protected $entityType; /** - * The payment line item manager. - * - * @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemManagerInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $paymentLineItemManager; - - /** - * The payment method manager. - * - * @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodManagerInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $paymentMethodManager; - - /** * The payment status manager. * * @var \Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface|\PHPUnit_Framework_MockObject_MockObject @@ -97,15 +83,11 @@ class PaymentStorageUnitTest extends UnitTestCase { $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); - $this->paymentLineItemManager = $this->getMock('\Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemManagerInterface'); - - $this->paymentMethodManager = $this->getMock('\Drupal\payment\Plugin\Payment\Method\PaymentMethodManagerInterface'); - $this->paymentStatusManager= $this->getMock('\Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface'); $this->paymentTypeManager = $this->getMock('\Drupal\payment\Plugin\Payment\Type\PaymentTypeManagerInterface'); - $this->storage = new PaymentStorage($this->entityType, $this->database, $this->entityManager, $this->cacheBackend, $this->paymentLineItemManager, $this->paymentMethodManager, $this->paymentStatusManager, $this->paymentTypeManager); + $this->storage = new PaymentStorage($this->entityType, $this->database, $this->entityManager, $this->cacheBackend, $this->paymentStatusManager, $this->paymentTypeManager); } /** @@ -117,8 +99,6 @@ class PaymentStorageUnitTest extends UnitTestCase { array('cache.entity', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->cacheBackend), array('database', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->database), array('entity.manager', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->entityManager), - array('plugin.manager.payment.line_item', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->paymentLineItemManager), - array('plugin.manager.payment.method', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->paymentMethodManager), array('plugin.manager.payment.status', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->paymentStatusManager), array('plugin.manager.payment.type', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->paymentTypeManager), ); @@ -137,8 +117,8 @@ class PaymentStorageUnitTest extends UnitTestCase { $bundle = $this->randomMachineName(); $currency_code = $this->randomMachineName(); $id = $this->randomMachineName(); - $first_payment_status_id = mt_rand(); - $last_payment_status_id = mt_rand(); + $first_payment_status_delta = mt_rand(); + $last_payment_status_delta = mt_rand(); $owner_id = mt_rand(); $uuid = $this->randomMachineName(); $payment_method_id = $this->randomMachineName(); @@ -153,11 +133,11 @@ class PaymentStorageUnitTest extends UnitTestCase { $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_id)); + ->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_id)); + ->will($this->returnValue($last_payment_status_delta)); $payment_method = $this->getMock('\Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface'); $payment_method->expects($this->atLeastOnce()) @@ -175,9 +155,7 @@ class PaymentStorageUnitTest extends UnitTestCase { ->method('getConfiguration') ->will($this->returnValue($payment_type_configuration)); - $payment= $this->getMockBuilder('\Drupal\payment\Entity\Payment') - ->disableOriginalConstructor() - ->getMock(); + $payment= $this->getMock('\Drupal\payment\Entity\PaymentInterface'); $payment->expects($this->atLeastOnce()) ->method('bundle') ->will($this->returnValue($bundle)); @@ -216,8 +194,8 @@ class PaymentStorageUnitTest extends UnitTestCase { $this->assertSame($currency_code, $record->currency); $this->assertSame($bundle, $record->bundle); $this->assertSame($owner_id, $record->owner); - $this->assertSame($first_payment_status_id, $record->first_payment_status_id); - $this->assertSame($last_payment_status_id, $record->last_payment_status_id); + $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); diff --git a/tests/src/Unit/Plugin/Payment/Status/PaymentStatusBaseUnitTest.php b/tests/src/Unit/Plugin/Payment/Status/PaymentStatusBaseUnitTest.php index 0fcdb31..ff36893 100644 --- a/tests/src/Unit/Plugin/Payment/Status/PaymentStatusBaseUnitTest.php +++ b/tests/src/Unit/Plugin/Payment/Status/PaymentStatusBaseUnitTest.php @@ -135,16 +135,6 @@ class PaymentStatusBaseUnitTest extends UnitTestCase { } /** - * @covers ::setId - * @covers ::getId - */ - public function testGetId() { - $created = mt_rand(); - $this->assertSame($this->status, $this->status->setId($created)); - $this->assertSame($created, $this->status->getId()); - } - - /** * @covers ::getChildren */ public function testGetChildren() {