-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create commerce context #8
Open
adamsokolowski06
wants to merge
19
commits into
main
Choose a base branch
from
feat/behat-definitions-commerce
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
000252d
Create commerce context
adamsokolowski06 ba7729b
Codestyle fix
adamsokolowski06 cd8dd6a
Codestyle fix
adamsokolowski06 23bb00d
Codestyle fix
adamsokolowski06 9e66885
Codestyle fix
adamsokolowski06 39a4754
Codestyle fix
adamsokolowski06 1690b9d
Codestyle fix
adamsokolowski06 6dfb669
Codestyle fix
adamsokolowski06 293c1ad
Codestyle fix
adamsokolowski06 4c116ab
Merge branch 'main' into feat/behat-definitions-commerce
adamsokolowski06 3edede9
Add commerce as dev dependency
adamsokolowski06 1961d90
Remove trailing comma
adamsokolowski06 38a1e64
Typehint variation storage
adamsokolowski06 17a66e0
Use entity to generate form edit URL
adamsokolowski06 0788f41
Create abstract class for deleting entities
adamsokolowski06 42ae567
Codestyle fix
adamsokolowski06 f09d9db
Fix use statements sorting
adamsokolowski06 b6c3027
Merge branch 'main' into feat/behat-definitions-commerce
eiriksm e5fd4de
Merge branch 'main' into feat/behat-definitions-commerce
adamsokolowski06 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,8 @@ | |
}, | ||
"require": { | ||
"drupal/drupal-extension": "^4.0 || ^5.0" | ||
}, | ||
"require-dev": { | ||
"drupal/commerce": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
namespace Frontkom\DrupalBehatDefinitions; | ||
|
||
use Behat\Gherkin\Node\TableNode; | ||
use Drupal\DrupalExtension\Context\RawDrupalContext; | ||
use Drupal\commerce_product\ProductVariationStorageInterface; | ||
use Drupal\commerce_promotion\Entity\PromotionInterface; | ||
|
||
/** | ||
* Class CommerceContext. | ||
* | ||
* Provide Behat step-definitions for common Commerce functionalities. | ||
*/ | ||
class CommerceContext extends EntityContextBase { | ||
|
||
/** | ||
* Generate coupons. | ||
* | ||
* @Given coupons: | ||
*/ | ||
public function createCoupons(TableNode $nodesTable) { | ||
$storage = \Drupal::entityTypeManager()->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'); | ||
adamsokolowski06 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to either be wrapped in some conditional to check if the database table exists or wrapped in try/catch. This is not default commerce, and can be disabled on sites. |
||
$database->delete('commerce_exchanger_latest_rates') | ||
->condition('manual', '666666', '=') | ||
->execute(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Frontkom\DrupalBehatDefinitions; | ||
|
||
use Drupal\DrupalExtension\Context\RawDrupalContext; | ||
|
||
/** | ||
* Class EntityContextBase. | ||
* | ||
* It is supposed to be extended by classes which implement creating entities. It automatically cleans up | ||
* all the entities created during a single test. | ||
*/ | ||
abstract class EntityContextBase extends RawDrupalContext { | ||
|
||
/** | ||
* An array of entites created in the context. | ||
* | ||
* @var array | ||
*/ | ||
protected $entites = []; | ||
|
||
/** | ||
* Remove entities by names. | ||
* | ||
* @Then I remove entities of type :entity_type_id with names :names | ||
*/ | ||
public function iRemoveEntities($entity_type_id, $names) { | ||
$entities = \Drupal::entityTypeManager()->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(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this make them automatically deleted after the scenario?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'm pretty sure of that. I checked the db records for
commerce_promotion_coupon
get cleared after a scenario with creating couponsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am now 100% sure that is not the case.
The entities are probably randomly deleted only because they are tied to a promotion deleted in the same cleanup. But there is nothing in our test classes or drupal extension to clean it up. We should do that here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be as easy as append it to $this->entities though