diff --git a/composer.json b/composer.json index 67df96b..43c1301 100644 --- a/composer.json +++ b/composer.json @@ -9,5 +9,8 @@ }, "require": { "drupal/drupal-extension": "^4.0 || ^5.0" + }, + "require-dev": { + "drupal/commerce": "*" } } diff --git a/src/CommerceContext.php b/src/CommerceContext.php new file mode 100644 index 0000000..55c5c17 --- /dev/null +++ b/src/CommerceContext.php @@ -0,0 +1,131 @@ +getStorage('commerce_promotion_coupon'); + foreach ($nodesTable->getHash() as $nodeHash) { + $coupon = (object) $nodeHash; + $promotion = $this->getPromotionByName($coupon->promotion); + + $coupon_saved = $this->couponCreate($coupon); + $coupon_loaded = $storage->load($coupon_saved->id); + $promotion->get('coupons')->appendItem($coupon_loaded); + $promotion->save(); + } + } + + /** + * Load promotion by name. + */ + public function getPromotionByName($name) { + $storage = \Drupal::entityTypeManager()->getStorage('commerce_promotion'); + $promotions = $storage->loadByProperties(['name' => $name]); + + if (count($promotions) !== 1) { + throw new \Exception('Expected 1 promotion with title ' . $name . ' but found ' . count($promotions)); + } + + return reset($promotions); + } + + /** + * Create coupon. + */ + public function couponCreate($coupon) { + $saved = $this->getDriver()->createEntity('commerce_promotion_coupon', $coupon); + return $saved; + } + + /** + * Visit promotion edit page. + * + * @Then I visit promotion :name edit page + */ + public function iVisitPromotionEditPage($name) { + $promotion = $this->getPromotionByName($name); + $this->getSession()->visit($this->locatePath($promotion->toUrl('edit-form')->toString())); + } + + /** + * Check if product variation exist. + * + * @Then /^a product with SKU "([^"]*)" should exist$/ + */ + public function aProductWithSkuShouldExist($sku) { + /** @var \Drupal\commerce_product\ProductVariationStorageInterface $variation_storage */ + $variation_storage = \Drupal::entityTypeManager()->getStorage('commerce_product_variation'); + $product_variations = $variation_storage->loadBySku($sku); + if (empty($product_variations)) { + throw new \Exception('No variation found with SKU ' . $sku); + } + } + + /** + * Set records DB for exchange rates. + * + * Valid for commerce_exchanger^2, because in version 1 the exchange rates are in config. + * + * @Given I set :value exchange rates + */ + public function setManualExchangeRates($value) { + $currency_storage = \Drupal::service('entity_type.manager')->getStorage('commerce_currency'); + $currencies = $currency_storage->loadMultiple(); + $currencies = array_keys($currencies); + $database = \Drupal::database(); + $time = time(); + + foreach ($currencies as $currency) { + $query = $database->insert('commerce_exchanger_latest_rates') + ->fields([ + 'exchanger', + 'source', + 'target', + 'value', + 'timestamp', + 'manual', + ]); + + $query->values([ + 'exchanger' => 'manual', + 'source' => $currency, + 'target' => 'NOK', + 'value' => $value, + 'timestamp' => $time, + // A weird value just to identify records to remove in AfterScenario. + 'manual' => '666666', + ]); + $query->execute(); + } + } + + /** + * Remove exchange rates. + * + * @AfterScenario + */ + public function cleanExchangeRates() { + $database = \Drupal::database(); + $database->delete('commerce_exchanger_latest_rates') + ->condition('manual', '666666', '=') + ->execute(); + } + +} diff --git a/src/EntityContextBase.php b/src/EntityContextBase.php new file mode 100644 index 0000000..fb2a132 --- /dev/null +++ b/src/EntityContextBase.php @@ -0,0 +1,48 @@ +getStorage($entity_type_id) + ->loadByProperties([ + 'name' => explode(', ', $names), + ]); + foreach ($entities as $entity) { + $entity->delete(); + } + } + + /** + * Clean up created entities. + * + * @AfterScenario + */ + public function cleanUpEntities() { + foreach ($this->entites as $entity) { + $entity->delete(); + } + } + +}